{"version":3,"sources":["../src/rearrange-area.js"],"names":["define","$","Y","fragment","Dialogue","notification","str","template","ajax","RearrangeArea","selector","_node","_setEvents","prototype","CUSTOMCERT_REF_POINT_TOPLEFT","CUSTOMCERT_REF_POINT_TOPCENTER","CUSTOMCERT_REF_POINT_TOPRIGHT","PIXELSINMM","on","_editElement","bind","event","elementid","currentTarget","id","substr","contextid","attr","loadFragment","done","html","js","get_string","title","use","_editElementDialogueConfig","fail","exception","popup","replaceNode","_setPositionInForm","body","getContent","e","M","core_formchangechecker","reset_form_dirty_state","_saveElement","then","_getElementHTML","elementNode","find","refpoint","parseInt","val","refpointClass","empty","append","removeClass","addClass","posx","posy","_setPosition","close","preventDefault","element","one","getX","getY","nodewidth","parseFloat","getComputedStyle","maxwidth","width","setX","setY","posxelement","posyelement","length","getData","Math","round","templateid","promises","call","methodname","args","inputs","serializeArray","values","init"],"mappings":"AAuBAA,OAAM,iCAAC,CAAC,QAAD,CAAW,UAAX,CAAuB,eAAvB,CAAwC,yBAAxC,CAAmE,mBAAnE,CACC,UADD,CACa,gBADb,CAC+B,WAD/B,CAAD,CAEE,SAASC,CAAT,CAAYC,CAAZ,CAAeC,CAAf,CAAyBC,CAAzB,CAAmCC,CAAnC,CAAiDC,CAAjD,CAAsDC,CAAtD,CAAgEC,CAAhE,CAAsE,CAOlE,GAAIC,CAAAA,CAAa,CAAG,SAASC,CAAT,CAAmB,CACnC,KAAKC,KAAL,CAAaV,CAAC,CAACS,CAAD,CAAd,CACA,KAAKE,UAAL,EACH,CAHD,CAKAH,CAAa,CAACI,SAAd,CAAwBC,4BAAxB,CAAuD,CAAvD,CACAL,CAAa,CAACI,SAAd,CAAwBE,8BAAxB,CAAyD,CAAzD,CACAN,CAAa,CAACI,SAAd,CAAwBG,6BAAxB,CAAwD,CAAxD,CACAP,CAAa,CAACI,SAAd,CAAwBI,UAAxB,CAAqC,cAArC,CAEAR,CAAa,CAACI,SAAd,CAAwBD,UAAxB,CAAqC,UAAW,CAC5C,K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,CAAgBM,CAAhB,EACAiB,CAAW,CAACvB,GAAZ,CAAgBO,CAAhB,CACH,CACJ,CA1BD,CA4BApD,CAAa,CAACI,SAAd,CAAwBoC,eAAxB,CAA0C,SAAS3B,CAAT,CAAoB,IAEtD4D,CAAAA,CAAU,CAAG,KAAKvE,KAAL,CAAWgB,IAAX,CAAgB,iBAAhB,CAFyC,CAKtDwD,CAAQ,CAAG3E,CAAI,CAAC4E,IAAL,CAAU,CAAC,CACtBC,UAAU,CAAE,iCADU,CAEtBC,IAAI,CAAE,CACFJ,UAAU,CAAEA,CADV,CAEF5D,SAAS,CAAEA,CAFT,CAFgB,CAAD,CAAV,CAL2C,CAc1D,MAAO6D,CAAAA,CAAQ,CAAC,CAAD,CAClB,CAfD,CAiBA1E,CAAa,CAACI,SAAd,CAAwBkC,YAAxB,CAAuC,SAASzB,CAAT,CAAoB,IAEnD4D,CAAAA,CAAU,CAAG,KAAKvE,KAAL,CAAWgB,IAAX,CAAgB,iBAAhB,CAFsC,CAGnD4D,CAAM,CAAGtF,CAAC,CAAC,kBAAD,CAAD,CAAsBuF,cAAtB,EAH0C,CAMnDL,CAAQ,CAAG3E,CAAI,CAAC4E,IAAL,CAAU,CAAC,CACtBC,UAAU,CAAE,6BADU,CAEtBC,IAAI,CAAE,CACFJ,UAAU,CAAEA,CADV,CAEF5D,SAAS,CAAEA,CAFT,CAGFmE,MAAM,CAAEF,CAHN,CAFgB,CAAD,CAAV,CANwC,CAgBvD,MAAOJ,CAAAA,CAAQ,CAAC,CAAD,CAClB,CAjBD,CAmBA,MAAO,CACHO,IAAI,CAAE,cAAShF,CAAT,CAAmB,CACrB,GAAID,CAAAA,CAAJ,CAAkBC,CAAlB,CACH,CAHE,CAKV,CA5LH,CAAN","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 * AMD module used when rearranging a custom certificate.\n *\n * @module mod_customcert/rearrange-area\n * @package mod_customcert\n * @copyright 2016 Mark Nelson \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/notification',\n 'core/str', 'core/templates', 'core/ajax'],\n function($, Y, fragment, Dialogue, notification, str, template, ajax) {\n\n /**\n * RearrangeArea class.\n *\n * @param {String} selector The rearrange PDF selector\n */\n var RearrangeArea = function(selector) {\n this._node = $(selector);\n this._setEvents();\n };\n\n RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT = 0;\n RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER = 1;\n RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT = 2;\n RearrangeArea.prototype.PIXELSINMM = 3.779527559055;\n\n RearrangeArea.prototype._setEvents = function() {\n this._node.on('click', '.element', this._editElement.bind(this));\n };\n\n RearrangeArea.prototype._editElement = function(event) {\n var elementid = event.currentTarget.id.substr(8);\n var contextid = this._node.attr('data-contextid');\n var params = {\n 'elementid': elementid\n };\n\n fragment.loadFragment('mod_customcert', 'editelement', contextid, params).done(function(html, js) {\n str.get_string('editelement', 'mod_customcert').done(function(title) {\n Y.use('moodle-core-formchangechecker', function() {\n new Dialogue(\n title,\n '',\n this._editElementDialogueConfig.bind(this, elementid, html, js),\n undefined,\n true\n );\n }.bind(this));\n }.bind(this));\n }.bind(this)).fail(notification.exception);\n };\n\n RearrangeArea.prototype._editElementDialogueConfig = function(elementid, html, js, popup) {\n // Place the content in the dialogue.\n template.replaceNode('#elementcontent', html, js);\n\n // We may have dragged the element changing it's position.\n // Ensure the form has the current up-to-date location.\n this._setPositionInForm(elementid);\n\n // Add events for when we save, close and cancel the page.\n var body = $(popup.getContent());\n body.on('click', '#id_submitbutton', function(e) {\n // Do not want to ask the user if they wish to stay on page after saving.\n M.core_formchangechecker.reset_form_dirty_state();\n // Save the data.\n this._saveElement(elementid).then(function() {\n // Update the DOM to reflect the adjusted value.\n this._getElementHTML(elementid).done(function(html) {\n var elementNode = this._node.find('#element-' + elementid);\n var refpoint = parseInt($('#id_refpoint').val());\n var refpointClass = '';\n if (refpoint == this.CUSTOMCERT_REF_POINT_TOPLEFT) {\n refpointClass = 'refpoint-left';\n } else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPCENTER) {\n refpointClass = 'refpoint-center';\n } else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPRIGHT) {\n refpointClass = 'refpoint-right';\n }\n elementNode.empty().append(html);\n // Update the ref point.\n elementNode.removeClass();\n elementNode.addClass('element ' + refpointClass);\n elementNode.attr('data-refpoint', refpoint);\n // Move the element.\n var posx = $('#editelementform #id_posx').val();\n var posy = $('#editelementform #id_posy').val();\n this._setPosition(elementid, refpoint, posx, posy);\n // All done.\n popup.close();\n }.bind(this));\n }.bind(this)).fail(notification.exception);\n e.preventDefault();\n }.bind(this));\n\n body.on('click', '#id_cancel', function(e) {\n popup.close();\n e.preventDefault();\n });\n };\n\n RearrangeArea.prototype._setPosition = function(elementid, refpoint, posx, posy) {\n var element = Y.one('#element-' + elementid);\n\n posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM;\n posy = Y.one('#pdf').getY() + posy * this.PIXELSINMM;\n var nodewidth = parseFloat(element.getComputedStyle('width'));\n var maxwidth = element.width * this.PIXELSINMM;\n\n if (maxwidth && (nodewidth > maxwidth)) {\n nodewidth = maxwidth;\n }\n\n switch (refpoint) {\n case this.CUSTOMCERT_REF_POINT_TOPCENTER:\n posx -= nodewidth / 2;\n break;\n case this.CUSTOMCERT_REF_POINT_TOPRIGHT:\n posx = posx - nodewidth + 2;\n break;\n }\n\n element.setX(posx);\n element.setY(posy);\n };\n\n RearrangeArea.prototype._setPositionInForm = function(elementid) {\n var posxelement = $('#editelementform #id_posx');\n var posyelement = $('#editelementform #id_posy');\n\n if (posxelement.length && posyelement.length) {\n var element = Y.one('#element-' + elementid);\n var posx = element.getX() - Y.one('#pdf').getX();\n var posy = element.getY() - Y.one('#pdf').getY();\n var refpoint = parseInt(element.getData('refpoint'));\n var nodewidth = parseFloat(element.getComputedStyle('width'));\n\n switch (refpoint) {\n case this.CUSTOMCERT_REF_POINT_TOPCENTER:\n posx += nodewidth / 2;\n break;\n case this.CUSTOMCERT_REF_POINT_TOPRIGHT:\n posx += nodewidth;\n break;\n }\n\n posx = Math.round(parseFloat(posx / this.PIXELSINMM));\n posy = Math.round(parseFloat(posy / this.PIXELSINMM));\n\n posxelement.val(posx);\n posyelement.val(posy);\n }\n };\n\n RearrangeArea.prototype._getElementHTML = function(elementid) {\n // Get the variables we need.\n var templateid = this._node.attr('data-templateid');\n\n // Call the web service to get the updated element.\n var promises = ajax.call([{\n methodname: 'mod_customcert_get_element_html',\n args: {\n templateid: templateid,\n elementid: elementid\n }\n }]);\n\n // Return the promise.\n return promises[0];\n };\n\n RearrangeArea.prototype._saveElement = function(elementid) {\n // Get the variables we need.\n var templateid = this._node.attr('data-templateid');\n var inputs = $('#editelementform').serializeArray();\n\n // Call the web service to save the element.\n var promises = ajax.call([{\n methodname: 'mod_customcert_save_element',\n args: {\n templateid: templateid,\n elementid: elementid,\n values: inputs\n }\n }]);\n\n // Return the promise.\n return promises[0];\n };\n\n return {\n init: function(selector) {\n new RearrangeArea(selector);\n }\n };\n }\n );\n"],"file":"rearrange-area.min.js"}