From c48c592883fc463d6eb5a40c8dda3b697ed67179 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 28 May 2024 10:14:48 +0100 Subject: [PATCH] Fixed broken modal on reposition page (#483) --- amd/build/rearrange-area.min.js | 2 +- amd/build/rearrange-area.min.js.map | 2 +- amd/src/rearrange-area.js | 385 +++++++++++++++------------- 3 files changed, 202 insertions(+), 187 deletions(-) diff --git a/amd/build/rearrange-area.min.js b/amd/build/rearrange-area.min.js index d2cd38ba..fd10ef29 100644 --- a/amd/build/rearrange-area.min.js +++ b/amd/build/rearrange-area.min.js @@ -5,6 +5,6 @@ * @copyright 2016 Mark Nelson * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define("mod_customcert/rearrange-area",["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax"],(function($,Y,fragment,Dialogue,notification,str,template,ajax){var RearrangeArea=function(selector){this._node=$(selector),this._setEvents()};return RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,RearrangeArea.prototype.PIXELSINMM=3.779527559055,RearrangeArea.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},RearrangeArea.prototype._editElement=function(event){var elementid=event.currentTarget.id.substr(8),contextid=this._node.attr("data-contextid"),params={elementid:elementid};fragment.loadFragment("mod_customcert","editelement",contextid,params).done(function(html,js){str.get_string("editelement","mod_customcert").done(function(title){Y.use("moodle-core-formchangechecker",function(){new Dialogue(title,"
",this._editElementDialogueConfig.bind(this,elementid,html,js),void 0,!0)}.bind(this))}.bind(this))}.bind(this)).fail(notification.exception)},RearrangeArea.prototype._editElementDialogueConfig=function(elementid,html,js,popup){template.replaceNode("#elementcontent",html,js),this._setPositionInForm(elementid);var body=$(popup.getContent());body.on("click","#id_submitbutton",function(e){M.core_formchangechecker.reset_form_dirty_state(),this._saveElement(elementid).then(function(){this._getElementHTML(elementid).done(function(html){var elementNode=this._node.find("#element-"+elementid),refpoint=parseInt($("#id_refpoint").val()),refpointClass="";refpoint==this.CUSTOMCERT_REF_POINT_TOPLEFT?refpointClass="refpoint-left":refpoint==this.CUSTOMCERT_REF_POINT_TOPCENTER?refpointClass="refpoint-center":refpoint==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(refpointClass="refpoint-right"),elementNode.empty().append(html),elementNode.removeClass(),elementNode.addClass("element "+refpointClass),elementNode.attr("data-refpoint",refpoint);var posx=$("#editelementform #id_posx").val(),posy=$("#editelementform #id_posy").val();this._setPosition(elementid,refpoint,posx,posy),popup.close()}.bind(this))}.bind(this)).fail(notification.exception),e.preventDefault()}.bind(this)),body.on("click","#id_cancel",(function(e){popup.close(),e.preventDefault()}))},RearrangeArea.prototype._setPosition=function(elementid,refpoint,posx,posy){var element=Y.one("#element-"+elementid);posx=Y.one("#pdf").getX()+posx*this.PIXELSINMM,posy=Y.one("#pdf").getY()+posy*this.PIXELSINMM;var nodewidth=parseFloat(element.getComputedStyle("width")),maxwidth=element.width*this.PIXELSINMM;switch(maxwidth&&nodewidth>maxwidth&&(nodewidth=maxwidth),refpoint){case this.CUSTOMCERT_REF_POINT_TOPCENTER:posx-=nodewidth/2;break;case this.CUSTOMCERT_REF_POINT_TOPRIGHT:posx=posx-nodewidth+2}element.setX(posx),element.setY(posy)},RearrangeArea.prototype._setPositionInForm=function(elementid){var posxelement=$("#editelementform #id_posx"),posyelement=$("#editelementform #id_posy");if(posxelement.length&&posyelement.length){var element=Y.one("#element-"+elementid),posx=element.getX()-Y.one("#pdf").getX(),posy=element.getY()-Y.one("#pdf").getY(),refpoint=parseInt(element.getData("refpoint")),nodewidth=parseFloat(element.getComputedStyle("width"));switch(refpoint){case this.CUSTOMCERT_REF_POINT_TOPCENTER:posx+=nodewidth/2;break;case this.CUSTOMCERT_REF_POINT_TOPRIGHT:posx+=nodewidth}posx=Math.round(parseFloat(posx/this.PIXELSINMM)),posy=Math.round(parseFloat(posy/this.PIXELSINMM)),posxelement.val(posx),posyelement.val(posy)}},RearrangeArea.prototype._getElementHTML=function(elementid){var templateid=this._node.attr("data-templateid");return ajax.call([{methodname:"mod_customcert_get_element_html",args:{templateid:templateid,elementid:elementid}}])[0]},RearrangeArea.prototype._saveElement=function(elementid){var templateid=this._node.attr("data-templateid"),inputs=$("#editelementform").serializeArray();return ajax.call([{methodname:"mod_customcert_save_element",args:{templateid:templateid,elementid:elementid,values:inputs}}])[0]},{init:function(selector){new RearrangeArea(selector)}}})); +define("mod_customcert/rearrange-area",["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax","core_form/changechecker"],(function($,Y,fragment,Dialogue,notification,str,template,ajax,FormChangeChecker){var RearrangeArea=function(selector){this._node=$(selector),this._setEvents()};return RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,RearrangeArea.prototype.PIXELSINMM=3.779527559055,RearrangeArea.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},RearrangeArea.prototype._editElement=function(event){var elementid=event.currentTarget.id.substr(8),contextid=this._node.attr("data-contextid"),params={elementid:elementid};fragment.loadFragment("mod_customcert","editelement",contextid,params).done(function(html,js){str.get_string("editelement","mod_customcert").done(function(title){Y.use("moodle-core-formchangechecker",function(){new Dialogue(title,"
",this._editElementDialogueConfig.bind(this,elementid,html,js),void 0,!0)}.bind(this))}.bind(this))}.bind(this)).fail(notification.exception)},RearrangeArea.prototype._editElementDialogueConfig=function(elementid,html,js,popup){template.replaceNode("#elementcontent",html,js),this._setPositionInForm(elementid);var body=$(popup.getContent());body.on("click","#id_savechanges",function(e){FormChangeChecker.resetAllFormDirtyStates(),this._saveElement(elementid).then(function(){this._getElementHTML(elementid).done(function(html){var elementNode=this._node.find("#element-"+elementid),refpoint=parseInt($("#id_refpoint").val()),refpointClass="";refpoint==this.CUSTOMCERT_REF_POINT_TOPLEFT?refpointClass="refpoint-left":refpoint==this.CUSTOMCERT_REF_POINT_TOPCENTER?refpointClass="refpoint-center":refpoint==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(refpointClass="refpoint-right"),elementNode.empty().append(html),elementNode.removeClass(),elementNode.addClass("element "+refpointClass),elementNode.attr("data-refpoint",refpoint);var posx=$("#editelementform #id_posx").val(),posy=$("#editelementform #id_posy").val();this._setPosition(elementid,refpoint,posx,posy),popup.close()}.bind(this))}.bind(this)).fail(notification.exception),e.preventDefault()}.bind(this)),body.on("click","#id_cancel",(function(e){popup.close(),e.preventDefault()}))},RearrangeArea.prototype._setPosition=function(elementid,refpoint,posx,posy){var element=Y.one("#element-"+elementid);posx=Y.one("#pdf").getX()+posx*this.PIXELSINMM,posy=Y.one("#pdf").getY()+posy*this.PIXELSINMM;var nodewidth=parseFloat(element.getComputedStyle("width")),maxwidth=element.width*this.PIXELSINMM;switch(maxwidth&&nodewidth>maxwidth&&(nodewidth=maxwidth),refpoint){case this.CUSTOMCERT_REF_POINT_TOPCENTER:posx-=nodewidth/2;break;case this.CUSTOMCERT_REF_POINT_TOPRIGHT:posx=posx-nodewidth+2}element.setX(posx),element.setY(posy)},RearrangeArea.prototype._setPositionInForm=function(elementid){var posxelement=$("#editelementform #id_posx"),posyelement=$("#editelementform #id_posy");if(posxelement.length&&posyelement.length){var element=Y.one("#element-"+elementid),posx=element.getX()-Y.one("#pdf").getX(),posy=element.getY()-Y.one("#pdf").getY(),refpoint=parseInt(element.getData("refpoint")),nodewidth=parseFloat(element.getComputedStyle("width"));switch(refpoint){case this.CUSTOMCERT_REF_POINT_TOPCENTER:posx+=nodewidth/2;break;case this.CUSTOMCERT_REF_POINT_TOPRIGHT:posx+=nodewidth}posx=Math.round(parseFloat(posx/this.PIXELSINMM)),posy=Math.round(parseFloat(posy/this.PIXELSINMM)),posxelement.val(posx),posyelement.val(posy)}},RearrangeArea.prototype._getElementHTML=function(elementid){var templateid=this._node.attr("data-templateid");return ajax.call([{methodname:"mod_customcert_get_element_html",args:{templateid:templateid,elementid:elementid}}])[0]},RearrangeArea.prototype._saveElement=function(elementid){var templateid=this._node.attr("data-templateid"),inputs=$("#editelementform").serializeArray();return ajax.call([{methodname:"mod_customcert_save_element",args:{templateid:templateid,elementid:elementid,values:inputs}}])[0]},{init:function(selector){new RearrangeArea(selector)}}})); //# sourceMappingURL=rearrange-area.min.js.map \ No newline at end of file diff --git a/amd/build/rearrange-area.min.js.map b/amd/build/rearrange-area.min.js.map index 45a61925..8e72ac16 100644 --- a/amd/build/rearrange-area.min.js.map +++ b/amd/build/rearrange-area.min.js.map @@ -1 +1 @@ -{"version":3,"file":"rearrange-area.min.js","sources":["../src/rearrange-area.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 * AMD module used when rearranging a custom certificate.\n *\n * @module mod_customcert/rearrange-area\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"],"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","this","_editElement","bind","event","elementid","currentTarget","id","substr","contextid","attr","params","loadFragment","done","html","js","get_string","title","use","_editElementDialogueConfig","undefined","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","call","methodname","args","inputs","serializeArray","values","init"],"mappings":";;;;;;;AAsBAA,uCAAO,CAAC,SAAU,WAAY,gBAAiB,0BAA2B,oBAClE,WAAY,iBAAkB,cAC9B,SAASC,EAAGC,EAAGC,SAAUC,SAAUC,aAAcC,IAAKC,SAAUC,UAOxDC,cAAgB,SAASC,eACpBC,MAAQV,EAAES,eACVE,qBAGTH,cAAcI,UAAUC,6BAA+B,EACvDL,cAAcI,UAAUE,+BAA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,aACzC+C,KAAOmB,KAAKC,MAAMX,WAAWT,KAAO7C,KAAKF,aAEzC8D,YAAYtB,IAAIM,MAChBiB,YAAYvB,IAAIO,QAIxBvD,cAAcI,UAAUuC,gBAAkB,SAAS7B,eAE3C8D,WAAalE,KAAKR,MAAMiB,KAAK,0BAGlBpB,KAAK8E,KAAK,CAAC,CACtBC,WAAY,kCACZC,KAAM,CACFH,WAAYA,WACZ9D,UAAWA,cAKH,IAGpBd,cAAcI,UAAUqC,aAAe,SAAS3B,eAExC8D,WAAalE,KAAKR,MAAMiB,KAAK,mBAC7B6D,OAASxF,EAAE,oBAAoByF,wBAGpBlF,KAAK8E,KAAK,CAAC,CACtBC,WAAY,8BACZC,KAAM,CACFH,WAAYA,WACZ9D,UAAWA,UACXoE,OAAQF,WAKA,IAGb,CACHG,KAAM,SAASlF,cACPD,cAAcC"} \ No newline at end of file +{"version":3,"file":"rearrange-area.min.js","sources":["../src/rearrange-area.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 * AMD module used when rearranging a custom certificate.\n *\n * @module mod_customcert/rearrange-area\n * @copyright 2016 Mark Nelson \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery',\n 'core/yui',\n 'core/fragment',\n 'mod_customcert/dialogue',\n 'core/notification',\n 'core/str',\n 'core/templates',\n 'core/ajax',\n 'core_form/changechecker',\n], function(\n $,\n Y,\n fragment,\n Dialogue,\n notification,\n str,\n template,\n ajax,\n FormChangeChecker\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_savechanges', function(e) {\n // Do not want to ask the user if they wish to stay on page after saving.\n FormChangeChecker.resetAllFormDirtyStates();\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"],"names":["define","$","Y","fragment","Dialogue","notification","str","template","ajax","FormChangeChecker","RearrangeArea","selector","_node","_setEvents","prototype","CUSTOMCERT_REF_POINT_TOPLEFT","CUSTOMCERT_REF_POINT_TOPCENTER","CUSTOMCERT_REF_POINT_TOPRIGHT","PIXELSINMM","on","this","_editElement","bind","event","elementid","currentTarget","id","substr","contextid","attr","params","loadFragment","done","html","js","get_string","title","use","_editElementDialogueConfig","undefined","fail","exception","popup","replaceNode","_setPositionInForm","body","getContent","e","resetAllFormDirtyStates","_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","call","methodname","args","inputs","serializeArray","values","init"],"mappings":";;;;;;;AAsBAA,uCAAO,CAAC,SACA,WACA,gBACA,0BACA,oBACA,WACA,iBACA,YACA,4BACL,SACCC,EACAC,EACAC,SACAC,SACAC,aACAC,IACAC,SACAC,KAC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,MAAQS,UAAY,aAEnBnD,KAAKH,8BACN6C,MAAQS,UAIhBT,KAAOoB,KAAKC,MAAMX,WAAWV,KAAO1C,KAAKF,aACzC6C,KAAOmB,KAAKC,MAAMX,WAAWT,KAAO3C,KAAKF,aAEzC4D,YAAYtB,IAAIM,MAChBiB,YAAYvB,IAAIO,QAIxBrD,cAAcI,UAAUqC,gBAAkB,SAAS3B,eAE3C4D,WAAahE,KAAKR,MAAMiB,KAAK,0BAGlBrB,KAAK6E,KAAK,CAAC,CACtBC,WAAY,kCACZC,KAAM,CACFH,WAAYA,WACZ5D,UAAWA,cAKH,IAGpBd,cAAcI,UAAUmC,aAAe,SAASzB,eAExC4D,WAAahE,KAAKR,MAAMiB,KAAK,mBAC7B2D,OAASvF,EAAE,oBAAoBwF,wBAGpBjF,KAAK6E,KAAK,CAAC,CACtBC,WAAY,8BACZC,KAAM,CACFH,WAAYA,WACZ5D,UAAWA,UACXkE,OAAQF,WAKA,IAGb,CACHG,KAAM,SAAShF,cACPD,cAAcC"} \ No newline at end of file diff --git a/amd/src/rearrange-area.js b/amd/src/rearrange-area.js index 1099d253..329b8110 100644 --- a/amd/src/rearrange-area.js +++ b/amd/src/rearrange-area.js @@ -20,193 +20,208 @@ * @copyright 2016 Mark Nelson * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/notification', - 'core/str', 'core/templates', 'core/ajax'], - function($, Y, fragment, Dialogue, notification, str, template, ajax) { - - /** - * RearrangeArea class. - * - * @param {String} selector The rearrange PDF selector - */ - var RearrangeArea = function(selector) { - this._node = $(selector); - this._setEvents(); - }; - - RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT = 0; - RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER = 1; - RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT = 2; - RearrangeArea.prototype.PIXELSINMM = 3.779527559055; - - RearrangeArea.prototype._setEvents = function() { - this._node.on('click', '.element', this._editElement.bind(this)); - }; - - RearrangeArea.prototype._editElement = function(event) { - var elementid = event.currentTarget.id.substr(8); - var contextid = this._node.attr('data-contextid'); - var params = { - 'elementid': elementid - }; - - fragment.loadFragment('mod_customcert', 'editelement', contextid, params).done(function(html, js) { - str.get_string('editelement', 'mod_customcert').done(function(title) { - Y.use('moodle-core-formchangechecker', function() { - new Dialogue( - title, - '
', - this._editElementDialogueConfig.bind(this, elementid, html, js), - undefined, - true - ); - }.bind(this)); - }.bind(this)); - }.bind(this)).fail(notification.exception); - }; - - RearrangeArea.prototype._editElementDialogueConfig = function(elementid, html, js, popup) { - // Place the content in the dialogue. - template.replaceNode('#elementcontent', html, js); - - // We may have dragged the element changing it's position. - // Ensure the form has the current up-to-date location. - this._setPositionInForm(elementid); - - // Add events for when we save, close and cancel the page. - var body = $(popup.getContent()); - body.on('click', '#id_submitbutton', function(e) { - // Do not want to ask the user if they wish to stay on page after saving. - M.core_formchangechecker.reset_form_dirty_state(); - // Save the data. - this._saveElement(elementid).then(function() { - // Update the DOM to reflect the adjusted value. - this._getElementHTML(elementid).done(function(html) { - var elementNode = this._node.find('#element-' + elementid); - var refpoint = parseInt($('#id_refpoint').val()); - var refpointClass = ''; - if (refpoint == this.CUSTOMCERT_REF_POINT_TOPLEFT) { - refpointClass = 'refpoint-left'; - } else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPCENTER) { - refpointClass = 'refpoint-center'; - } else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPRIGHT) { - refpointClass = 'refpoint-right'; - } - elementNode.empty().append(html); - // Update the ref point. - elementNode.removeClass(); - elementNode.addClass('element ' + refpointClass); - elementNode.attr('data-refpoint', refpoint); - // Move the element. - var posx = $('#editelementform #id_posx').val(); - var posy = $('#editelementform #id_posy').val(); - this._setPosition(elementid, refpoint, posx, posy); - // All done. - popup.close(); - }.bind(this)); - }.bind(this)).fail(notification.exception); - e.preventDefault(); +define(['jquery', + 'core/yui', + 'core/fragment', + 'mod_customcert/dialogue', + 'core/notification', + 'core/str', + 'core/templates', + 'core/ajax', + 'core_form/changechecker', +], function( + $, + Y, + fragment, + Dialogue, + notification, + str, + template, + ajax, + FormChangeChecker +){ + /** + * RearrangeArea class. + * + * @param {String} selector The rearrange PDF selector + */ + var RearrangeArea = function(selector) { + this._node = $(selector); + this._setEvents(); + }; + + RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT = 0; + RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER = 1; + RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT = 2; + RearrangeArea.prototype.PIXELSINMM = 3.779527559055; + + RearrangeArea.prototype._setEvents = function() { + this._node.on('click', '.element', this._editElement.bind(this)); + }; + + RearrangeArea.prototype._editElement = function(event) { + var elementid = event.currentTarget.id.substr(8); + var contextid = this._node.attr('data-contextid'); + var params = { + 'elementid': elementid + }; + + fragment.loadFragment('mod_customcert', 'editelement', contextid, params).done(function(html, js) { + str.get_string('editelement', 'mod_customcert').done(function(title) { + Y.use('moodle-core-formchangechecker', function() { + new Dialogue( + title, + '
', + this._editElementDialogueConfig.bind(this, elementid, html, js), + undefined, + true + ); }.bind(this)); - - body.on('click', '#id_cancel', function(e) { - popup.close(); - e.preventDefault(); - }); - }; - - RearrangeArea.prototype._setPosition = function(elementid, refpoint, posx, posy) { - var element = Y.one('#element-' + elementid); - - posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM; - posy = Y.one('#pdf').getY() + posy * this.PIXELSINMM; - var nodewidth = parseFloat(element.getComputedStyle('width')); - var maxwidth = element.width * this.PIXELSINMM; - - if (maxwidth && (nodewidth > maxwidth)) { - nodewidth = maxwidth; - } - - switch (refpoint) { - case this.CUSTOMCERT_REF_POINT_TOPCENTER: - posx -= nodewidth / 2; - break; - case this.CUSTOMCERT_REF_POINT_TOPRIGHT: - posx = posx - nodewidth + 2; - break; - } - - element.setX(posx); - element.setY(posy); - }; - - RearrangeArea.prototype._setPositionInForm = function(elementid) { - var posxelement = $('#editelementform #id_posx'); - var posyelement = $('#editelementform #id_posy'); - - if (posxelement.length && posyelement.length) { - var element = Y.one('#element-' + elementid); - var posx = element.getX() - Y.one('#pdf').getX(); - var posy = element.getY() - Y.one('#pdf').getY(); - var refpoint = parseInt(element.getData('refpoint')); - var nodewidth = parseFloat(element.getComputedStyle('width')); - - switch (refpoint) { - case this.CUSTOMCERT_REF_POINT_TOPCENTER: - posx += nodewidth / 2; - break; - case this.CUSTOMCERT_REF_POINT_TOPRIGHT: - posx += nodewidth; - break; - } - - posx = Math.round(parseFloat(posx / this.PIXELSINMM)); - posy = Math.round(parseFloat(posy / this.PIXELSINMM)); - - posxelement.val(posx); - posyelement.val(posy); - } - }; - - RearrangeArea.prototype._getElementHTML = function(elementid) { - // Get the variables we need. - var templateid = this._node.attr('data-templateid'); - - // Call the web service to get the updated element. - var promises = ajax.call([{ - methodname: 'mod_customcert_get_element_html', - args: { - templateid: templateid, - elementid: elementid + }.bind(this)); + }.bind(this)).fail(notification.exception); + }; + + RearrangeArea.prototype._editElementDialogueConfig = function(elementid, html, js, popup) { + // Place the content in the dialogue. + template.replaceNode('#elementcontent', html, js); + + // We may have dragged the element changing it's position. + // Ensure the form has the current up-to-date location. + this._setPositionInForm(elementid); + + // Add events for when we save, close and cancel the page. + var body = $(popup.getContent()); + body.on('click', '#id_savechanges', function(e) { + // Do not want to ask the user if they wish to stay on page after saving. + FormChangeChecker.resetAllFormDirtyStates(); + // Save the data. + this._saveElement(elementid).then(function() { + // Update the DOM to reflect the adjusted value. + this._getElementHTML(elementid).done(function(html) { + var elementNode = this._node.find('#element-' + elementid); + var refpoint = parseInt($('#id_refpoint').val()); + var refpointClass = ''; + if (refpoint == this.CUSTOMCERT_REF_POINT_TOPLEFT) { + refpointClass = 'refpoint-left'; + } else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPCENTER) { + refpointClass = 'refpoint-center'; + } else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPRIGHT) { + refpointClass = 'refpoint-right'; } - }]); - - // Return the promise. - return promises[0]; - }; - - RearrangeArea.prototype._saveElement = function(elementid) { - // Get the variables we need. - var templateid = this._node.attr('data-templateid'); - var inputs = $('#editelementform').serializeArray(); - - // Call the web service to save the element. - var promises = ajax.call([{ - methodname: 'mod_customcert_save_element', - args: { - templateid: templateid, - elementid: elementid, - values: inputs - } - }]); + elementNode.empty().append(html); + // Update the ref point. + elementNode.removeClass(); + elementNode.addClass('element ' + refpointClass); + elementNode.attr('data-refpoint', refpoint); + // Move the element. + var posx = $('#editelementform #id_posx').val(); + var posy = $('#editelementform #id_posy').val(); + this._setPosition(elementid, refpoint, posx, posy); + // All done. + popup.close(); + }.bind(this)); + }.bind(this)).fail(notification.exception); + e.preventDefault(); + }.bind(this)); + + body.on('click', '#id_cancel', function(e) { + popup.close(); + e.preventDefault(); + }); + }; + + RearrangeArea.prototype._setPosition = function(elementid, refpoint, posx, posy) { + var element = Y.one('#element-' + elementid); + + posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM; + posy = Y.one('#pdf').getY() + posy * this.PIXELSINMM; + var nodewidth = parseFloat(element.getComputedStyle('width')); + var maxwidth = element.width * this.PIXELSINMM; + + if (maxwidth && (nodewidth > maxwidth)) { + nodewidth = maxwidth; + } - // Return the promise. - return promises[0]; - }; + switch (refpoint) { + case this.CUSTOMCERT_REF_POINT_TOPCENTER: + posx -= nodewidth / 2; + break; + case this.CUSTOMCERT_REF_POINT_TOPRIGHT: + posx = posx - nodewidth + 2; + break; + } - return { - init: function(selector) { - new RearrangeArea(selector); - } - }; + element.setX(posx); + element.setY(posy); + }; + + RearrangeArea.prototype._setPositionInForm = function(elementid) { + var posxelement = $('#editelementform #id_posx'); + var posyelement = $('#editelementform #id_posy'); + + if (posxelement.length && posyelement.length) { + var element = Y.one('#element-' + elementid); + var posx = element.getX() - Y.one('#pdf').getX(); + var posy = element.getY() - Y.one('#pdf').getY(); + var refpoint = parseInt(element.getData('refpoint')); + var nodewidth = parseFloat(element.getComputedStyle('width')); + + switch (refpoint) { + case this.CUSTOMCERT_REF_POINT_TOPCENTER: + posx += nodewidth / 2; + break; + case this.CUSTOMCERT_REF_POINT_TOPRIGHT: + posx += nodewidth; + break; + } + + posx = Math.round(parseFloat(posx / this.PIXELSINMM)); + posy = Math.round(parseFloat(posy / this.PIXELSINMM)); + + posxelement.val(posx); + posyelement.val(posy); + } + }; + + RearrangeArea.prototype._getElementHTML = function(elementid) { + // Get the variables we need. + var templateid = this._node.attr('data-templateid'); + + // Call the web service to get the updated element. + var promises = ajax.call([{ + methodname: 'mod_customcert_get_element_html', + args: { + templateid: templateid, + elementid: elementid + } + }]); + + // Return the promise. + return promises[0]; + }; + + RearrangeArea.prototype._saveElement = function(elementid) { + // Get the variables we need. + var templateid = this._node.attr('data-templateid'); + var inputs = $('#editelementform').serializeArray(); + + // Call the web service to save the element. + var promises = ajax.call([{ + methodname: 'mod_customcert_save_element', + args: { + templateid: templateid, + elementid: elementid, + values: inputs + } + }]); + + // Return the promise. + return promises[0]; + }; + + return { + init: function(selector) { + new RearrangeArea(selector); } - ); + }; +});