MDL-75513 message: Enable optional text display in the contact toggle

This commit is contained in:
Mihail Geshoski 2022-10-08 00:50:50 +08:00
parent df5e5c0f88
commit ee2d1f806d
6 changed files with 32 additions and 11 deletions

View File

@ -5,6 +5,6 @@
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("core_message/toggle_contact_button",["jquery","core/ajax","core/templates","core/notification","core/custom_interaction_events"],(function($,Ajax,Templates,Notification,CustomEvents){var getUserId=function(element){return element.attr("data-userid")},getCurrentUserId=function(element){return element.attr("data-currentuserid")},isLoading=function(element){return element.hasClass("loading")||element.attr("disabled")},sendRequest=function(element,request){return isLoading(element)?$.Deferred():(element.addClass("loading"),element.attr("disabled","disabled"),Ajax.call([request])[0].fail(Notification.exception).always((function(){element.removeClass("loading"),element.removeAttr("disabled")})))};return{enhance:function(element){(element=$(element)).children(".loading-icon").length||Templates.render("core/loading",{}).done((function(html,js){element.append(html,js)})),CustomEvents.define(element,[CustomEvents.events.activate]),element.on(CustomEvents.events.activate,(function(e,data){!function(element){return"1"==element.attr("data-is-contact")}(element)?function(element){if(!isLoading(element)){var request={methodname:"core_message_create_contact_request",args:{userid:getCurrentUserId(element),requesteduserid:getUserId(element)}};sendRequest(element,request).done((function(){!function(element){element.attr("data-is-contact","1")}(element),Templates.render("message/remove_contact_button",{}).done((function(html,js){Templates.replaceNodeContents(element,html,js)}))}))}}(element):function(element){if(!isLoading(element)){var request={methodname:"core_message_delete_contacts",args:{userids:[getUserId(element)]}};sendRequest(element,request).done((function(){!function(element){element.attr("data-is-contact","0")}(element),Templates.render("message/add_contact_button",{}).done((function(html,js){Templates.replaceNodeContents(element,html,js)}))}))}}(element),e.preventDefault(),data.originalEvent.preventDefault()}))}}}));
define("core_message/toggle_contact_button",["jquery","core/ajax","core/templates","core/notification","core/custom_interaction_events"],(function($,Ajax,Templates,Notification,CustomEvents){var getUserId=function(element){return element.attr("data-userid")},getCurrentUserId=function(element){return element.attr("data-currentuserid")},displayTextLabel=function(element){return"1"==element.attr("data-display-text-label")},isLoading=function(element){return element.hasClass("loading")||element.attr("disabled")},sendRequest=function(element,request){return isLoading(element)?$.Deferred():(element.addClass("loading"),element.attr("disabled","disabled"),Ajax.call([request])[0].fail(Notification.exception).always((function(){element.removeClass("loading"),element.removeAttr("disabled")})))};return{enhance:function(element){(element=$(element)).children(".loading-icon").length||Templates.render("core/loading",{}).done((function(html,js){element.append(html,js)})),CustomEvents.define(element,[CustomEvents.events.activate]),element.on(CustomEvents.events.activate,(function(e,data){!function(element){return"1"==element.attr("data-is-contact")}(element)?function(element){if(!isLoading(element)){var request={methodname:"core_message_create_contact_request",args:{userid:getCurrentUserId(element),requesteduserid:getUserId(element)}};sendRequest(element,request).done((function(){!function(element){element.attr("data-is-contact","1")}(element);const templateContext={displaytextlabel:displayTextLabel(element)};Templates.render("message/remove_contact_button",templateContext).done((function(html,js){Templates.replaceNodeContents(element,html,js)}))}))}}(element):function(element){if(!isLoading(element)){var request={methodname:"core_message_delete_contacts",args:{userids:[getUserId(element)]}};sendRequest(element,request).done((function(){!function(element){element.attr("data-is-contact","0")}(element);const templateContext={displaytextlabel:displayTextLabel(element)};Templates.render("message/add_contact_button",templateContext).done((function(html,js){Templates.replaceNodeContents(element,html,js)}))}))}}(element),e.preventDefault(),data.originalEvent.preventDefault()}))}}}));
//# sourceMappingURL=toggle_contact_button.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -76,6 +76,17 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
return element.attr('data-currentuserid');
};
/**
* Check whether a text label should be displayed or not.
*
* @method getUserId
* @param {object} element jQuery object for the button
* @return {int}
*/
var displayTextLabel = function(element) {
return element.attr('data-display-text-label') == '1';
};
/**
* Check if this element is currently loading.
*
@ -134,7 +145,10 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
};
sendRequest(element, request).done(function() {
setContact(element);
Templates.render('message/remove_contact_button', {}).done(function(html, js) {
const templateContext = {
'displaytextlabel': displayTextLabel(element)
};
Templates.render('message/remove_contact_button', templateContext).done(function(html, js) {
Templates.replaceNodeContents(element, html, js);
});
});
@ -162,7 +176,10 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
sendRequest(element, request).done(function() {
setNotContact(element);
Templates.render('message/add_contact_button', {}).done(function(html, js) {
const templateContext = {
'displaytextlabel': displayTextLabel(element)
};
Templates.render('message/add_contact_button', templateContext).done(function(html, js) {
Templates.replaceNodeContents(element, html, js);
});
});

View File

@ -308,14 +308,16 @@ class helper {
*
* @param object $user User object.
* @param bool $iscontact
* @param bool $displaytextlabel Instructs whether to display a text label.
* @return array
*/
public static function togglecontact_link_params($user, $iscontact = false) {
public static function togglecontact_link_params($user, $iscontact = false, bool $displaytextlabel = true) {
global $USER;
$params = array(
'data-currentuserid' => $USER->id,
'data-userid' => $user->id,
'data-is-contact' => $iscontact,
'data-display-text-label' => $displaytextlabel,
'id' => 'toggle-contact-button',
'role' => 'button',
'class' => 'ajax-contact-button',

View File

@ -20,14 +20,15 @@
Template for the contents of the add contact button on the user's profile page.
Context variables required for this template:
* none
* displaytextlabel - Whether to display text next to the action icon.
Example context (json):
{
"displaytextlabel": true
}
}}
<span>
{{#pix}} t/addcontact, core {{/pix}}
<span class="header-button-title">{{#str}} addtoyourcontacts, message {{/str}}</span>
{{#pix}} t/addcontact, core, {{#str}} addtoyourcontacts, message {{/str}} {{/pix}}
{{#displaytextlabel}}<span class="header-button-title">{{#str}} addtoyourcontacts, message {{/str}}</span>{{/displaytextlabel}}
</span>
{{> core/loading }}

View File

@ -20,14 +20,15 @@
Template for the contents of the add contact button on the user's profile page.
Context variables required for this template:
* none
* displaytextlabel - Whether to display text next to the action icon.
Example context (json):
{
"displaytextlabel": true
}
}}
<span>
{{#pix}} t/removecontact, core {{/pix}}
<span class="header-button-title">{{#str}} removefromyourcontacts, message {{/str}}</span>
{{#pix}} t/removecontact, core, {{#str}} removefromyourcontacts, message {{/str}} {{/pix}}
{{#displaytextlabel}}<span class="header-button-title">{{#str}} removefromyourcontacts, message {{/str}}</span>{{/displaytextlabel}}
</span>
{{> core/loading }}