{"version":3,"file":"toggle_contact_button.min.js","sources":["../src/toggle_contact_button.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Module to add/remove contact using ajax.\n *\n * @module core_message/toggle_contact_button\n * @copyright 2016 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/custom_interaction_events'],\n function($, Ajax, Templates, Notification, CustomEvents) {\n\n /**\n * Check the state of the element, if the current user is a contact or not.\n *\n * @method isContact\n * @param {object} element jQuery object for the button\n * @return {bool}\n */\n var isContact = function(element) {\n return element.attr('data-is-contact') == '1';\n };\n\n /**\n * Check the state of the element, if the current user has sent a contact request or not.\n *\n * @method isRequested\n * @param {object} element jQuery object for the button\n * @return {bool}\n */\n let isRequested = (element) => element.attr('data-is-requested') == '1';\n\n /**\n * Record that the user has sent a contact request.\n *\n * @method setContactRequested\n * @param {object} element jQuery object for the button\n */\n var setContactRequested = function(element) {\n element.attr('data-is-requested', '1');\n };\n\n /**\n * Record that the user is not a contact.\n *\n * @method setNotContact\n * @param {object} element jQuery object for the button\n */\n var setNotContact = function(element) {\n element.attr('data-is-contact', '0');\n };\n\n /**\n * Get the id for the user being viewed.\n *\n * @method getUserId\n * @param {object} element jQuery object for the button\n * @return {int}\n */\n var getUserId = function(element) {\n return element.attr('data-userid');\n };\n\n /**\n * Get the id for the logged in user.\n *\n * @method getUserId\n * @param {object} element jQuery object for the button\n * @return {int}\n */\n var getCurrentUserId = function(element) {\n return element.attr('data-currentuserid');\n };\n\n /**\n * Check whether a text label should be displayed or not.\n *\n * @method getUserId\n * @param {object} element jQuery object for the button\n * @return {int}\n */\n var displayTextLabel = function(element) {\n return element.attr('data-display-text-label') == '1';\n };\n\n /**\n * Check if this element is currently loading.\n *\n * @method isLoading\n * @param {object} element jQuery object for the button\n * @return {bool}\n */\n var isLoading = function(element) {\n return element.hasClass('loading') || element.attr('disabled');\n };\n\n /**\n * Sends an ajax request to the server and handles the element state\n * while the request is being performed.\n *\n * @method sendRequest\n * @param {object} element jQuery object for the button\n * @param {object} request Request hash to send\n * @return {object} jQuery promise\n */\n var sendRequest = function(element, request) {\n if (isLoading(element)) {\n return $.Deferred();\n }\n\n element.addClass('loading');\n element.attr('disabled', 'disabled');\n\n return Ajax.call([request])[0]\n .fail(Notification.exception)\n .always(function() {\n element.removeClass('loading');\n element.removeAttr('disabled');\n });\n };\n\n /**\n * Send a request to the server to add the current user as\n * a contact. The contents of the button are changed to the\n * remove contact button upon success.\n *\n * @method addContact\n * @param {object} element jQuery object for the button\n */\n var addContact = function(element) {\n if (isLoading(element)) {\n return;\n }\n\n var request = {\n methodname: 'core_message_create_contact_request',\n args: {\n userid: getCurrentUserId(element),\n requesteduserid: getUserId(element),\n }\n };\n sendRequest(element, request).done(function() {\n setContactRequested(element);\n element.addClass('disabled');\n const templateContext = {\n 'displaytextlabel': displayTextLabel(element)\n };\n Templates.render('message/contact_request_sent', templateContext).done(function(html, js) {\n Templates.replaceNodeContents(element, html, js);\n });\n });\n };\n\n /**\n * Send a request to the server to remove the current user as\n * a contact. The contents of the button are changed to the\n * add contact button upon success.\n *\n * @method removeContact\n * @param {object} element jQuery object for the button\n */\n var removeContact = function(element) {\n if (isLoading(element)) {\n return;\n }\n\n var request = {\n methodname: 'core_message_delete_contacts',\n args: {\n userids: [getUserId(element)],\n }\n };\n\n sendRequest(element, request).done(function() {\n setNotContact(element);\n const templateContext = {\n 'displaytextlabel': displayTextLabel(element)\n };\n Templates.render('message/add_contact_button', templateContext).done(function(html, js) {\n Templates.replaceNodeContents(element, html, js);\n });\n });\n };\n\n /**\n * Enhances the given element with a loading gif and event handles to make\n * ajax requests to add or remove a contact where appropriate.\n *\n * @public\n * @method enhance\n * @param {object} element jQuery object for the button\n */\n var enhance = function(element) {\n element = $(element);\n\n if (!element.children('.loading-icon').length && !isRequested(element)) {\n // Add the loading gif if it isn't already there.\n Templates.render('core/loading', {}).done(function(html, js) {\n element.append(html, js);\n });\n }\n\n CustomEvents.define(element, [CustomEvents.events.activate]);\n\n element.on(CustomEvents.events.activate, function(e, data) {\n if (isContact(element)) {\n removeContact(element);\n } else if (!isRequested(element)) {\n addContact(element);\n }\n e.preventDefault();\n data.originalEvent.preventDefault();\n });\n };\n\n return {\n enhance: enhance\n };\n});\n"],"names":["define","$","Ajax","Templates","Notification","CustomEvents","isRequested","element","attr","getUserId","getCurrentUserId","displayTextLabel","isLoading","hasClass","sendRequest","request","Deferred","addClass","call","fail","exception","always","removeClass","removeAttr","enhance","children","length","render","done","html","js","append","events","activate","on","e","data","isContact","methodname","args","userid","requesteduserid","setContactRequested","templateContext","replaceNodeContents","addContact","userids","setNotContact","removeContact","preventDefault","originalEvent"],"mappings":";;;;;;;AAsBAA,4CAAO,CAAC,SAAU,YAAa,iBAAkB,oBAAqB,mCAC9D,SAASC,EAAGC,KAAMC,UAAWC,aAAcC,kBAoB3CC,YAAeC,SAAiD,KAArCA,QAAQC,KAAK,yBA6BxCC,UAAY,SAASF,gBACdA,QAAQC,KAAK,gBAUpBE,iBAAmB,SAASH,gBACrBA,QAAQC,KAAK,uBAUpBG,iBAAmB,SAASJ,eACsB,KAA3CA,QAAQC,KAAK,4BAUpBI,UAAY,SAASL,gBACdA,QAAQM,SAAS,YAAcN,QAAQC,KAAK,aAYnDM,YAAc,SAASP,QAASQ,gBAC5BH,UAAUL,SACHN,EAAEe,YAGbT,QAAQU,SAAS,WACjBV,QAAQC,KAAK,WAAY,YAElBN,KAAKgB,KAAK,CAACH,UAAU,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