1 |
efrain |
1 |
{"version":3,"file":"scaleconfig.min.js","sources":["../src/scaleconfig.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 <http://www.gnu.org/licenses/>.\n\n/**\n * Handle opening a dialogue to configure scale data.\n *\n * @module tool_lp/scaleconfig\n * @copyright 2015 Adrian Greeve <adrian@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/notification', 'core/templates', 'core/ajax', 'tool_lp/dialogue', 'tool_lp/scalevalues'],\n function($, notification, templates, ajax, Dialogue, ModScaleValues) {\n\n /**\n * Scale config object.\n * @param {String} selectSelector The select box selector.\n * @param {String} inputSelector The hidden input field selector.\n * @param {String} triggerSelector The trigger selector.\n */\n var ScaleConfig = function(selectSelector, inputSelector, triggerSelector) {\n this.selectSelector = selectSelector;\n this.inputSelector = inputSelector;\n this.triggerSelector = triggerSelector;\n\n // Get the current scale ID.\n this.originalscaleid = $(selectSelector).val();\n $(selectSelector).on('change', this.scaleChangeHandler.bind(this)).change();\n $(triggerSelector).click(this.showConfig.bind(this));\n };\n\n /** @var {String} The select box selector. */\n ScaleConfig.prototype.selectSelector = null;\n /** @var {String} The hidden field selector. */\n ScaleConfig.prototype.inputSelector = null;\n /** @var {String} The trigger selector. */\n ScaleConfig.prototype.triggerSelector = null;\n /** @var {Array} scalevalues ID and name of the scales. */\n ScaleConfig.prototype.scalevalues = null;\n /** @var {Number) originalscaleid Original scale ID when the page loads. */\n ScaleConfig.prototype.originalscaleid = 0;\n /** @var {Number} scaleid Current scale ID. */\n ScaleConfig.prototype.scaleid = 0;\n /** @var {Dialogue} Reference to the popup. */\n ScaleConfig.prototype.popup = null;\n\n /**\n * Displays the scale configuration dialogue.\n *\n * @method showConfig\n */\n ScaleConfig.prototype.showConfig = function() {\n var self = this;\n\n this.scaleid = $(this.selectSelector).val();\n if (this.scaleid <= 0) {\n // This should not happen.\n return;\n }\n\n var scalename = $(this.selectSelector).find(\"option:selected\").text();\n this.getScaleValues(this.scaleid).done(function() {\n\n var context = {\n scalename: scalename,\n scales: self.scalevalues\n };\n\n // Dish up the form.\n templates.render('tool_lp/scale_configuration_page', context)\n .done(function(html) {\n new Dialogue(\n scalename,\n html,\n self.initScaleConfig.bind(self)\n );\n }).fail(notification.exception);\n }).fail(notification.exception);\n };\n\n /**\n * Gets the original scale configuration if it was set.\n *\n * @method retrieveOriginalScaleConfig\n * @return {Object|String} scale configuration or empty string.\n */\n ScaleConfig.prototype.retrieveOriginalScaleConfig = function() {\n var jsonstring = $(this.inputSelector).val();\n if (jsonstring !== '') {\n var scaleconfiguration = $.parseJSON(jsonstring);\n // The first object should contain the scale ID for the configuration.\n var scaledetail = scaleconfiguration.shift();\n // Check that this scale id matches the one from the page before returning the configuration.\n if (scaledetail.scaleid === this.originalscaleid) {\n return scaleconfiguration;\n }\n }\n return '';\n };\n\n /**\n * Initialises the scale configuration dialogue.\n *\n * @method initScaleConfig\n * @param {Dialogue} popup Dialogue object to initialise.\n */\n ScaleConfig.prototype.initScaleConfig = function(popup) {\n this.popup = popup;\n var body = $(popup.getContent());\n if (this.originalscaleid === this.scaleid) {\n // Set up the popup to show the current configuration.\n var currentconfig = this.retrieveOriginalScaleConfig();\n // Set up the form only if there is configuration settings to set.\n if (currentconfig !== '') {\n currentconfig.forEach(function(value) {\n if (value.scaledefault === 1) {\n body.find('[data-field=\"tool_lp_scale_default_' + value.id + '\"]').attr('checked', true);\n }\n if (value.proficient === 1) {\n body.find('[data-field=\"tool_lp_scale_proficient_' + value.id + '\"]').attr('checked', true);\n }\n });\n }\n }\n body.on('click', '[data-action=\"close\"]', function() {\n this.setScaleConfig();\n popup.close();\n }.bind(this));\n body.on('click', '[data-action=\"cancel\"]', function() {\n popup.close();\n });\n };\n\n /**\n * Set the scale configuration back into a JSON string in the hidden element.\n *\n * @method setScaleConfig\n */\n ScaleConfig.prototype.setScaleConfig = function() {\n var body = $(this.popup.getContent());\n // Get the data.\n var data = [{scaleid: this.scaleid}];\n this.scalevalues.forEach(function(value) {\n var scaledefault = 0;\n var proficient = 0;\n if (body.find('[data-field=\"tool_lp_scale_default_' + value.id + '\"]').is(':checked')) {\n scaledefault = 1;\n }\n if (body.find('[data-field=\"tool_lp_scale_proficient_' + value.id + '\"]').is(':checked')) {\n proficient = 1;\n }\n\n if (!scaledefault && !proficient) {\n return;\n }\n\n data.push({\n id: value.id,\n scaledefault: scaledefault,\n proficient: proficient\n });\n });\n var datastring = JSON.stringify(data);\n // Send to the hidden field on the form.\n $(this.inputSelector).val(datastring);\n // Once the configuration has been saved then the original scale ID is set to the current scale ID.\n this.originalscaleid = this.scaleid;\n };\n\n /**\n * Get the scale values for the selected scale.\n *\n * @method getScaleValues\n * @param {Number} scaleid The scale ID of the selected scale.\n * @return {Promise} A deffered object with the scale values.\n */\n ScaleConfig.prototype.getScaleValues = function(scaleid) {\n return ModScaleValues.get_values(scaleid).then(function(values) {\n this.scalevalues = values;\n return values;\n }.bind(this));\n };\n\n /**\n * Triggered when a scale is selected.\n *\n * @name scaleChangeHandler\n * @param {Event} e\n * @function\n */\n ScaleConfig.prototype.scaleChangeHandler = function(e) {\n if ($(e.target).val() <= 0) {\n $(this.triggerSelector).prop('disabled', true);\n } else {\n $(this.triggerSelector).prop('disabled', false);\n }\n\n };\n\n return {\n\n /**\n * Main initialisation.\n *\n * @param {String} selectSelector The select box selector.\n * @param {String} inputSelector The hidden input field selector.\n * @param {String} triggerSelector The trigger selector.\n * @return {ScaleConfig} A new instance of ScaleConfig.\n * @method init\n */\n init: function(selectSelector, inputSelector, triggerSelector) {\n return new ScaleConfig(selectSelector, inputSelector, triggerSelector);\n }\n };\n});\n"],"names":["define","$","notification","templates","ajax","Dialogue","ModScaleValues","ScaleConfig","selectSelector","inputSelector","triggerSelector","originalscaleid","val","on","this","scaleChangeHandler","bind","change","click","showConfig","prototype","scalevalues","scaleid","popup","self","scalename","find","text","getScaleValues","done","context","scales","render","html","initScaleConfig","fail","exception","retrieveOriginalScaleConfig","jsonstring","scaleconfiguration","parseJSON","shift","body","getContent","currentconfig","forEach","value","scaledefault","id","attr","proficient","setScaleConfig","close","data","is","push","datastring","JSON","stringify","get_values","then","values","e","target","prop","init"],"mappings":";;;;;;;AAsBAA,6BAAO,CAAC,SAAU,oBAAqB,iBAAkB,YAAa,mBAAoB,wBACtF,SAASC,EAAGC,aAAcC,UAAWC,KAAMC,SAAUC,oBAQjDC,YAAc,SAASC,eAAgBC,cAAeC,sBACjDF,eAAiBA,oBACjBC,cAAgBA,mBAChBC,gBAAkBA,qBAGlBC,gBAAkBV,EAAEO,gBAAgBI,MACzCX,EAAEO,gBAAgBK,GAAG,SAAUC,KAAKC,mBAAmBC,KAAKF,OAAOG,SACnEhB,EAAES,iBAAiBQ,MAAMJ,KAAKK,WAAWH,KAAKF,eAIlDP,YAAYa,UAAUZ,eAAiB,KAEvCD,YAAYa,UAAUX,cAAgB,KAEtCF,YAAYa,UAAUV,gBAAkB,KAExCH,YAAYa,UAAUC,YAAc,KAEpCd,YAAYa,UAAUT,gBAAkB,EAExCJ,YAAYa,UAAUE,QAAU,EAEhCf,YAAYa,UAAUG,MAAQ,KAO9BhB,YAAYa,UAAUD,WAAa,eAC3BK,KAAOV,aAENQ,QAAUrB,EAAEa,KAAKN,gBAAgBI,QAClCE,KAAKQ,SAAW,QAKhBG,UAAYxB,EAAEa,KAAKN,gBAAgBkB,KAAK,mBAAmBC,YAC1DC,eAAed,KAAKQ,SAASO,MAAK,eAE/BC,QAAU,CACVL,UAAWA,UACXM,OAAQP,KAAKH,aAIjBlB,UA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