From c6f1ee297743a0ea22b3890eba31492bc9b77fcc Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 5 Feb 2021 09:44:06 +0800 Subject: [PATCH] MDL-70780 customfield: Address random behat fails A number of behat fails have been creeping in with the customfield code as behat is moving onto the next step before the current step finishes. This change introduces some additional pending JS tracking to prevent this. --- customfield/amd/build/form.min.js | 2 +- customfield/amd/build/form.min.js.map | 2 +- customfield/amd/src/form.js | 95 +++++++++++++++++++-------- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/customfield/amd/build/form.min.js b/customfield/amd/build/form.min.js index 3e8fc457c40..121e7b041bc 100644 --- a/customfield/amd/build/form.min.js +++ b/customfield/amd/build/form.min.js @@ -1,2 +1,2 @@ -define ("core_customfield/form",["jquery","core/str","core/notification","core/ajax","core/templates","core/sortable_list","core_form/modalform","core/inplace_editable"],function(a,b,c,d,e,f,g){var h=function(f,g,h,i,j){b.get_strings([{key:"confirm"},{key:"confirmdelete"+g,component:"core_customfield"},{key:"yes"},{key:"no"}]).done(function(b){c.confirm(b[0],b[1],b[2],b[3],function(){var b="field"===g?"core_customfield_delete_field":"core_customfield_delete_category";d.call([{methodname:b,args:{id:f}},{methodname:"core_customfield_reload_template",args:{component:h,area:i,itemid:j}}])[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);return null}).fail(c.exception)})}).fail(c.exception)},i=function(b,f,g){var h=d.call([{methodname:"core_customfield_create_category",args:{component:b,area:f,itemid:g}},{methodname:"core_customfield_reload_template",args:{component:b,area:f,itemid:g}}]),i;h[0].then(function(a){i=a;return null}).fail(c.exception);h[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);window.location.href="#category-"+i;return null}).fail(c.exception)},j=function(a){var c=a.closest(".action-menu").querySelector(".dropdown-toggle"),d=new g({formClass:"core_customfield\\field_config_form",args:{categoryid:a.getAttribute("data-categoryid"),type:a.getAttribute("data-type")},modalConfig:{title:b.get_string("addingnewcustomfield","core_customfield",a.getAttribute("data-typename"))},returnFocus:c});d.addEventListener(d.events.FORM_SUBMITTED,function(){return window.location.reload()});d.show()},k=function(a){var c=new g({formClass:"core_customfield\\field_config_form",args:{id:a.getAttribute("data-id")},modalConfig:{title:b.get_string("editingfield","core_customfield",a.getAttribute("data-name"))},returnFocus:a});c.addEventListener(c.events.FORM_SUBMITTED,function(){return window.location.reload()});c.show()};return{init:function init(){var e=a("#customfield_catlist"),g=e.attr("data-component"),l=e.attr("data-area"),m=e.attr("data-itemid");a("[data-role=deletefield]").on("click",function(b){h(a(this).attr("data-id"),"field",g,l,m);b.preventDefault()});a("[data-role=deletecategory]").on("click",function(b){h(a(this).attr("data-id"),"category",g,l,m);b.preventDefault()});a("[data-role=addnewcategory]").on("click",function(){i(g,l,m)});a("[data-role=addfield]").on("click",function(a){j(a.currentTarget);a.preventDefault()});a("[data-role=editfield]").on("click",function(a){k(a.currentTarget);a.preventDefault()});var n=function(a){return a.closest("[data-category-id]").find("[data-inplaceeditable][data-itemtype=category][data-component=core_customfield]").attr("data-value")},o=new f(a("#customfield_catlist .categorieslist"),{moveHandlerSelector:".movecategory [data-drag-type=move]"});o.getElementName=function(b){return a.Deferred().resolve(n(b))};a("[data-category-id]").on("sortablelist-drop",function(a,b){if(b.positionChanged){var e=d.call([{methodname:"core_customfield_move_category",args:{id:b.element.data("category-id"),beforeid:b.targetNextElement.data("category-id")}}]);e[0].fail(c.exception)}a.stopPropagation()});var p=new f(a("#customfield_catlist .fieldslist tbody"),{moveHandlerSelector:".movefield [data-drag-type=move]"});p.getDestinationName=function(c,d){if(!d.length){return b.get_string("totopofcategory","customfield",n(c))}else if(d.attr("data-field-name")){return b.get_string("afterfield","customfield",d.attr("data-field-name"))}else{return a.Deferred().resolve("")}};a("[data-field-name]").on("sortablelist-drop",function(a,b){a.stopPropagation();if(b.positionChanged){var e=d.call([{methodname:"core_customfield_move_field",args:{id:b.element.data("field-id"),beforeid:b.targetNextElement.data("field-id"),categoryid:+b.targetList.closest("[data-category-id]").attr("data-category-id")}}]);e[0].fail(c.exception)}}).on("sortablelist-drag",function(d){d.stopPropagation();b.get_string("therearenofields","core_customfield").then(function(b){a("#customfield_catlist .categorieslist").children().each(function(){var c=a(this).find(a(".field")),d=a(this).find(a(".nofields"));if(!c.length&&!d.length){a(this).find("tbody").append(""+b+"")}if(c.length&&d.length){d.remove()}});return null}).fail(c.exception)});a("[data-category-id], [data-field-name]").on("sortablelist-dragstart",function(b,c){setTimeout(function(){a(".sortable-list-is-dragged").width(c.element.width())},501)})}}}); +define ("core_customfield/form",["jquery","core/str","core/notification","core/ajax","core/templates","core/sortable_list","core/pending","core_form/modalform","core/inplace_editable"],function(a,b,c,d,e,f,g,h){var i=function(f,h,i,j,k){var l=new g("core_customfield/form:confirmDelete");b.get_strings([{key:"confirm"},{key:"confirmdelete"+h,component:"core_customfield"},{key:"yes"},{key:"no"}]).then(function(b){c.confirm(b[0],b[1],b[2],b[3],function(){var b=new g("core_customfield/form:confirmDelete"),l="field"===h?"core_customfield_delete_field":"core_customfield_delete_category";d.call([{methodname:l,args:{id:f}},{methodname:"core_customfield_reload_template",args:{component:i,area:j,itemid:k}}])[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);return null}).then(b.resolve).catch(c.exception)})}).then(l.resolve).catch(c.exception)},j=function(b,f,h){var i=new g("core_customfield/form:confirmDelete"),j=d.call([{methodname:"core_customfield_create_category",args:{component:b,area:f,itemid:h}},{methodname:"core_customfield_reload_template",args:{component:b,area:f,itemid:h}}]),k;j[0].then(function(a){k=a;return null}).catch(c.exception);j[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);window.location.href="#category-"+k;return null}).catch(c.exception);Promise.all(j).then(i.resolve).catch()},k=function(a){var c=a.closest(".action-menu").querySelector(".dropdown-toggle"),d=new h({formClass:"core_customfield\\field_config_form",args:{categoryid:a.getAttribute("data-categoryid"),type:a.getAttribute("data-type")},modalConfig:{title:b.get_string("addingnewcustomfield","core_customfield",a.getAttribute("data-typename"))},returnFocus:c});d.addEventListener(d.events.FORM_SUBMITTED,function(){return window.location.reload()});d.show()},l=function(a){var c=new h({formClass:"core_customfield\\field_config_form",args:{id:a.getAttribute("data-id")},modalConfig:{title:b.get_string("editingfield","core_customfield",a.getAttribute("data-name"))},returnFocus:a});c.addEventListener(c.events.FORM_SUBMITTED,function(){return window.location.reload()});c.show()};return{init:function init(){var e=a("#customfield_catlist"),h=e.attr("data-component"),m=e.attr("data-area"),n=e.attr("data-itemid");a("[data-role=deletefield]").on("click",function(b){i(a(this).attr("data-id"),"field",h,m,n);b.preventDefault()});a("[data-role=deletecategory]").on("click",function(b){i(a(this).attr("data-id"),"category",h,m,n);b.preventDefault()});a("[data-role=addnewcategory]").on("click",function(){j(h,m,n)});a("[data-role=addfield]").on("click",function(a){k(a.currentTarget);a.preventDefault()});a("[data-role=editfield]").on("click",function(a){l(a.currentTarget);a.preventDefault()});var o=function(a){return a.closest("[data-category-id]").find("[data-inplaceeditable][data-itemtype=category][data-component=core_customfield]").attr("data-value")},p=new f(a("#customfield_catlist .categorieslist"),{moveHandlerSelector:".movecategory [data-drag-type=move]"});p.getElementName=function(b){return a.Deferred().resolve(o(b))};a("[data-category-id]").on("sortablelist-drop",function(a,b){if(b.positionChanged){var e=new g("core_customfield/form:categoryid:on:sortablelist-drop");d.call([{methodname:"core_customfield_move_category",args:{id:b.element.data("category-id"),beforeid:b.targetNextElement.data("category-id")}}])[0].then(e.resolve).catch(c.exception)}a.stopPropagation()});var q=new f(a("#customfield_catlist .fieldslist tbody"),{moveHandlerSelector:".movefield [data-drag-type=move]"});q.getDestinationName=function(c,d){if(!d.length){return b.get_string("totopofcategory","customfield",o(c))}else if(d.attr("data-field-name")){return b.get_string("afterfield","customfield",d.attr("data-field-name"))}else{return a.Deferred().resolve("")}};a("[data-field-name]").on("sortablelist-drop",function(a,b){a.stopPropagation();if(b.positionChanged){var e=new g("core_customfield/form:fieldname:on:sortablelist-drop");d.call([{methodname:"core_customfield_move_field",args:{id:b.element.data("field-id"),beforeid:b.targetNextElement.data("field-id"),categoryid:+b.targetList.closest("[data-category-id]").attr("data-category-id")}}])[0].then(e.resolve).catch(c.exception)}});a("[data-field-name]").on("sortablelist-drag",function(d){var e=new g("core_customfield/form:fieldname:on:sortablelist-drag");d.stopPropagation();b.get_string("therearenofields","core_customfield").then(function(b){a("#customfield_catlist .categorieslist").children().each(function(){var c=a(this).find(a(".field")),d=a(this).find(a(".nofields"));if(!c.length&&!d.length){a(this).find("tbody").append(""+b+"")}if(c.length&&d.length){d.remove()}});return null}).then(e.resolve).catch(c.exception)});a("[data-category-id], [data-field-name]").on("sortablelist-dragstart",function(b,c){setTimeout(function(){a(".sortable-list-is-dragged").width(c.element.width())},501)})}}}); //# sourceMappingURL=form.min.js.map diff --git a/customfield/amd/build/form.min.js.map b/customfield/amd/build/form.min.js.map index c952fd3441e..5e6d473fb0b 100644 --- a/customfield/amd/build/form.min.js.map +++ b/customfield/amd/build/form.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/form.js"],"names":["define","$","Str","Notification","Ajax","Templates","SortableList","ModalForm","confirmDelete","id","type","component","area","itemid","get_strings","done","s","confirm","func","call","methodname","args","then","response","render","html","js","replaceNode","fail","exception","createNewCategory","promises","categoryid","window","location","href","createNewField","element","returnFocus","closest","querySelector","form","formClass","getAttribute","modalConfig","title","get_string","addEventListener","events","FORM_SUBMITTED","reload","show","editField","init","mainlist","attr","on","e","preventDefault","currentTarget","categoryName","find","sortCat","moveHandlerSelector","getElementName","el","Deferred","resolve","evt","info","positionChanged","data","beforeid","targetNextElement","stopPropagation","sort","getDestinationName","parentElement","afterElement","length","targetList","children","each","fields","nofields","append","remove","setTimeout","width"],"mappings":"AAuBAA,OAAM,yBAAC,CAAC,QAAD,CAAW,UAAX,CAA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H,CA1HE,CA4HV,CAtOK,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 * This module depends on the real jquery - and returns the non-global version of it.\n *\n * @module core_customfield/form\n * @package core_customfield\n * @copyright 2018 Toni Barbera\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates', 'core/sortable_list',\n 'core_form/modalform', 'core/inplace_editable'],\n function(\n $, Str, Notification, Ajax, Templates, SortableList, ModalForm) {\n\n /**\n * Display confirmation dialogue\n *\n * @param {Number} id\n * @param {String} type\n * @param {String} component\n * @param {String} area\n * @param {Number} itemid\n */\n var confirmDelete = function(id, type, component, area, itemid) {\n Str.get_strings([\n {'key': 'confirm'},\n {'key': 'confirmdelete' + type, component: 'core_customfield'},\n {'key': 'yes'},\n {'key': 'no'},\n ]).done(function(s) {\n Notification.confirm(s[0], s[1], s[2], s[3], function() {\n var func = (type === 'field') ? 'core_customfield_delete_field' : 'core_customfield_delete_category';\n Ajax.call([\n {methodname: func, args: {id: id}},\n {methodname: 'core_customfield_reload_template', args: {component: component, area: area, itemid: itemid}}\n ])[1].then(function(response) {\n return Templates.render('core_customfield/list', response);\n }).then(function(html, js) {\n Templates.replaceNode($('[data-region=\"list-page\"]'), html, js);\n return null;\n }).fail(Notification.exception);\n });\n }).fail(Notification.exception);\n };\n\n /**\n * Creates a new custom fields category with default name and updates the list\n *\n * @param {String} component\n * @param {String} area\n * @param {Number} itemid\n */\n var createNewCategory = function(component, area, itemid) {\n var promises = Ajax.call([\n {methodname: 'core_customfield_create_category', args: {component: component, area: area, itemid: itemid}},\n {methodname: 'core_customfield_reload_template', args: {component: component, area: area, itemid: itemid}}\n ]),\n categoryid;\n\n promises[0].then(function(response) {\n categoryid = response;\n return null;\n }).fail(Notification.exception);\n\n promises[1].then(function(response) {\n return Templates.render('core_customfield/list', response);\n }).then(function(html, js) {\n Templates.replaceNode($('[data-region=\"list-page\"]'), html, js);\n window.location.href = '#category-' + categoryid;\n return null;\n }).fail(Notification.exception);\n };\n\n /**\n * Create new custom field\n *\n * @param {HTMLElement} element\n */\n var createNewField = function(element) {\n const returnFocus = element.closest(\".action-menu\").querySelector(\".dropdown-toggle\");\n const form = new ModalForm({\n formClass: \"core_customfield\\\\field_config_form\",\n args: {\n categoryid: element.getAttribute('data-categoryid'),\n type: element.getAttribute('data-type'),\n },\n modalConfig: {\n title: Str.get_string('addingnewcustomfield', 'core_customfield', element.getAttribute('data-typename')),\n },\n returnFocus,\n });\n form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());\n form.show();\n };\n\n /**\n * Edit custom field\n *\n * @param {HTMLElement} element\n */\n var editField = function(element) {\n const form = new ModalForm({\n formClass: \"core_customfield\\\\field_config_form\",\n args: {\n id: element.getAttribute('data-id'),\n },\n modalConfig: {\n title: Str.get_string('editingfield', 'core_customfield', element.getAttribute('data-name')),\n },\n returnFocus: element,\n });\n form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());\n form.show();\n };\n\n return {\n /**\n * Initialise the custom fields manager\n */\n init: function() {\n var mainlist = $('#customfield_catlist'),\n component = mainlist.attr('data-component'),\n area = mainlist.attr('data-area'),\n itemid = mainlist.attr('data-itemid');\n $(\"[data-role=deletefield]\").on('click', function(e) {\n confirmDelete($(this).attr('data-id'), 'field', component, area, itemid);\n e.preventDefault();\n });\n $(\"[data-role=deletecategory]\").on('click', function(e) {\n confirmDelete($(this).attr('data-id'), 'category', component, area, itemid);\n e.preventDefault();\n });\n $('[data-role=addnewcategory]').on('click', function() {\n createNewCategory(component, area, itemid);\n });\n $('[data-role=addfield]').on('click', function(e) {\n createNewField(e.currentTarget);\n e.preventDefault();\n });\n $('[data-role=editfield]').on('click', function(e) {\n editField(e.currentTarget);\n e.preventDefault();\n });\n\n var categoryName = function(element) {\n return element\n .closest('[data-category-id]')\n .find('[data-inplaceeditable][data-itemtype=category][data-component=core_customfield]')\n .attr('data-value');\n };\n\n // Sort category.\n var sortCat = new SortableList(\n $('#customfield_catlist .categorieslist'),\n {moveHandlerSelector: '.movecategory [data-drag-type=move]'}\n );\n\n sortCat.getElementName = function(el) {\n return $.Deferred().resolve(categoryName(el));\n };\n\n $('[data-category-id]').on('sortablelist-drop', function(evt, info) {\n if (info.positionChanged) {\n var promises = Ajax.call([\n {\n methodname: 'core_customfield_move_category',\n args: {\n id: info.element.data('category-id'),\n beforeid: info.targetNextElement.data('category-id')\n }\n\n },\n ]);\n promises[0].fail(Notification.exception);\n }\n evt.stopPropagation(); // Important for nested lists to prevent multiple targets.\n });\n\n // Sort fields.\n var sort = new SortableList(\n $('#customfield_catlist .fieldslist tbody'),\n {moveHandlerSelector: '.movefield [data-drag-type=move]'}\n );\n\n sort.getDestinationName = function(parentElement, afterElement) {\n if (!afterElement.length) {\n return Str.get_string('totopofcategory', 'customfield', categoryName(parentElement));\n } else if (afterElement.attr('data-field-name')) {\n return Str.get_string('afterfield', 'customfield', afterElement.attr('data-field-name'));\n } else {\n return $.Deferred().resolve('');\n }\n };\n\n $('[data-field-name]').on('sortablelist-drop', function(evt, info) {\n evt.stopPropagation(); // Important for nested lists to prevent multiple targets.\n if (info.positionChanged) {\n var promises = Ajax.call([\n {\n methodname: 'core_customfield_move_field',\n args: {\n id: info.element.data('field-id'),\n beforeid: info.targetNextElement.data('field-id'),\n categoryid: Number(info.targetList.closest('[data-category-id]').attr('data-category-id'))\n },\n },\n ]);\n promises[0].fail(Notification.exception);\n }\n }).on('sortablelist-drag', function(evt) {\n evt.stopPropagation(); // Important for nested lists to prevent multiple targets.\n // Refreshing fields tables.\n Str.get_string('therearenofields', 'core_customfield').then(function(s) {\n $('#customfield_catlist .categorieslist').children().each(function() {\n var fields = $(this).find($('.field')),\n nofields = $(this).find($('.nofields'));\n if (!fields.length && !nofields.length) {\n $(this).find('tbody').append(\n '' + s + ''\n );\n }\n if (fields.length && nofields.length) {\n nofields.remove();\n }\n });\n return null;\n }).fail(Notification.exception);\n });\n\n $('[data-category-id], [data-field-name]').on('sortablelist-dragstart',\n function(evt, info) {\n setTimeout(function() {\n $('.sortable-list-is-dragged').width(info.element.width());\n }, 501);\n }\n );\n\n }\n };\n});\n"],"file":"form.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/form.js"],"names":["define","$","Str","Notification","Ajax","Templates","SortableList","Pending","ModalForm","confirmDelete","id","type","component","area","itemid","pendingPromise","get_strings","then","s","confirm","pendingDeletePromise","func","call","methodname","args","response","render","html","js","replaceNode","resolve","catch","exception","createNewCategory","promises","categoryid","window","location","href","Promise","all","createNewField","element","returnFocus","closest","querySelector","form","formClass","getAttribute","modalConfig","title","get_string","addEventListener","events","FORM_SUBMITTED","reload","show","editField","init","mainlist","attr","on","e","preventDefault","currentTarget","categoryName","find","sortCat","moveHandlerSelector","getElementName","el","Deferred","evt","info","positionChanged","data","beforeid","targetNextElement","stopPropagation","sort","getDestinationName","parentElement","afterElement","length","targetList","children","each","fields","nofields","append","remove","setTimeout","width"],"mappings":"AAuBAA,OAAM,yBAAC,CACH,QADG,CAEH,UAFG,CAGH,mBAHG,CAIH,WAJG,CAKH,gBALG,CAMH,oBANG,CAOH,cAPG,CAQH,qBARG,CASH,uBATG,CAAD,CAUH,SAASC,CAAT,CAAYC,CAAZ,CAAiBC,CAAjB,CAA+BC,CAA/B,CAAqCC,CAArC,CAAgDC,CAAhD,CAA8DC,CAA9D,CAAuEC,CAAvE,CAAkF,IAW7EC,CAAAA,CAAa,CAAG,SAASC,CAAT,CAAaC,CAAb,CAAmBC,CAAnB,CAA8BC,CAA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AeMF,CAAc,CAACe,OAfrB,EAgBCC,KAhBD,CAgBO5B,CAAY,CAAC6B,SAhBpB,CAiBH,CAvBD,EAyBA/B,CAAC,CAAC,uCAAD,CAAD,CAA2C4D,EAA3C,CAA8C,wBAA9C,CACI,SAASW,CAAT,CAAcC,CAAd,CAAoB,CAChBkB,UAAU,CAAC,UAAW,CAClB1F,CAAC,CAAC,2BAAD,CAAD,CAA+B2F,KAA/B,CAAqCnB,CAAI,CAAC/B,OAAL,CAAakD,KAAb,EAArC,CACH,CAFS,CAEP,GAFO,CAGb,CALL,CAQH,CAxIE,CA0IV,CA7QK,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 * This module depends on the real jquery - and returns the non-global version of it.\n *\n * @module core_customfield/form\n * @package core_customfield\n * @copyright 2018 Toni Barbera\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine([\n 'jquery',\n 'core/str',\n 'core/notification',\n 'core/ajax',\n 'core/templates',\n 'core/sortable_list',\n 'core/pending',\n 'core_form/modalform',\n 'core/inplace_editable',\n], function($, Str, Notification, Ajax, Templates, SortableList, Pending, ModalForm) {\n\n /**\n * Display confirmation dialogue\n *\n * @param {Number} id\n * @param {String} type\n * @param {String} component\n * @param {String} area\n * @param {Number} itemid\n */\n var confirmDelete = function(id, type, component, area, itemid) {\n var pendingPromise = new Pending('core_customfield/form:confirmDelete');\n Str.get_strings([\n {'key': 'confirm'},\n {'key': 'confirmdelete' + type, component: 'core_customfield'},\n {'key': 'yes'},\n {'key': 'no'},\n ])\n .then(function(s) {\n Notification.confirm(s[0], s[1], s[2], s[3], function() {\n var pendingDeletePromise = new Pending('core_customfield/form:confirmDelete');\n var func = (type === 'field') ? 'core_customfield_delete_field' : 'core_customfield_delete_category';\n Ajax.call([\n {methodname: func, args: {id: id}},\n {methodname: 'core_customfield_reload_template', args: {component: component, area: area, itemid: itemid}}\n ])[1]\n .then(function(response) {\n return Templates.render('core_customfield/list', response);\n })\n .then(function(html, js) {\n Templates.replaceNode($('[data-region=\"list-page\"]'), html, js);\n return null;\n })\n .then(pendingDeletePromise.resolve)\n .catch(Notification.exception);\n });\n\n return;\n })\n .then(pendingPromise.resolve)\n .catch(Notification.exception);\n };\n\n /**\n * Creates a new custom fields category with default name and updates the list\n *\n * @param {String} component\n * @param {String} area\n * @param {Number} itemid\n */\n var createNewCategory = function(component, area, itemid) {\n var pendingPromise = new Pending('core_customfield/form:confirmDelete');\n var promises = Ajax.call([\n {methodname: 'core_customfield_create_category', args: {component: component, area: area, itemid: itemid}},\n {methodname: 'core_customfield_reload_template', args: {component: component, area: area, itemid: itemid}}\n ]);\n var categoryid;\n\n promises[0].then(function(response) {\n categoryid = response;\n return null;\n }).catch(Notification.exception);\n\n promises[1].then(function(response) {\n return Templates.render('core_customfield/list', response);\n })\n .then(function(html, js) {\n Templates.replaceNode($('[data-region=\"list-page\"]'), html, js);\n window.location.href = '#category-' + categoryid;\n return null;\n })\n .catch(Notification.exception);\n\n Promise.all(promises)\n .then(pendingPromise.resolve)\n .catch();\n };\n\n /**\n * Create new custom field\n *\n * @param {HTMLElement} element\n */\n var createNewField = function(element) {\n const returnFocus = element.closest(\".action-menu\").querySelector(\".dropdown-toggle\");\n const form = new ModalForm({\n formClass: \"core_customfield\\\\field_config_form\",\n args: {\n categoryid: element.getAttribute('data-categoryid'),\n type: element.getAttribute('data-type'),\n },\n modalConfig: {\n title: Str.get_string('addingnewcustomfield', 'core_customfield', element.getAttribute('data-typename')),\n },\n returnFocus,\n });\n form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());\n form.show();\n };\n\n /**\n * Edit custom field\n *\n * @param {HTMLElement} element\n */\n var editField = function(element) {\n const form = new ModalForm({\n formClass: \"core_customfield\\\\field_config_form\",\n args: {\n id: element.getAttribute('data-id'),\n },\n modalConfig: {\n title: Str.get_string('editingfield', 'core_customfield', element.getAttribute('data-name')),\n },\n returnFocus: element,\n });\n form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());\n form.show();\n };\n\n return {\n /**\n * Initialise the custom fields manager\n */\n init: function() {\n var mainlist = $('#customfield_catlist');\n var component = mainlist.attr('data-component');\n var area = mainlist.attr('data-area');\n var itemid = mainlist.attr('data-itemid');\n\n $(\"[data-role=deletefield]\").on('click', function(e) {\n confirmDelete($(this).attr('data-id'), 'field', component, area, itemid);\n e.preventDefault();\n });\n\n $(\"[data-role=deletecategory]\").on('click', function(e) {\n confirmDelete($(this).attr('data-id'), 'category', component, area, itemid);\n e.preventDefault();\n });\n\n $('[data-role=addnewcategory]').on('click', function() {\n createNewCategory(component, area, itemid);\n });\n $('[data-role=addfield]').on('click', function(e) {\n createNewField(e.currentTarget);\n e.preventDefault();\n });\n $('[data-role=editfield]').on('click', function(e) {\n editField(e.currentTarget);\n e.preventDefault();\n });\n\n var categoryName = function(element) {\n return element\n .closest('[data-category-id]')\n .find('[data-inplaceeditable][data-itemtype=category][data-component=core_customfield]')\n .attr('data-value');\n };\n\n // Sort category.\n var sortCat = new SortableList(\n $('#customfield_catlist .categorieslist'),\n {moveHandlerSelector: '.movecategory [data-drag-type=move]'}\n );\n\n sortCat.getElementName = function(el) {\n return $.Deferred().resolve(categoryName(el));\n };\n\n $('[data-category-id]').on('sortablelist-drop', function(evt, info) {\n if (info.positionChanged) {\n var pendingPromise = new Pending('core_customfield/form:categoryid:on:sortablelist-drop');\n Ajax.call([\n {\n methodname: 'core_customfield_move_category',\n args: {\n id: info.element.data('category-id'),\n beforeid: info.targetNextElement.data('category-id')\n }\n\n },\n ])[0]\n .then(pendingPromise.resolve)\n .catch(Notification.exception);\n }\n evt.stopPropagation(); // Important for nested lists to prevent multiple targets.\n });\n\n // Sort fields.\n var sort = new SortableList(\n $('#customfield_catlist .fieldslist tbody'),\n {moveHandlerSelector: '.movefield [data-drag-type=move]'}\n );\n\n sort.getDestinationName = function(parentElement, afterElement) {\n if (!afterElement.length) {\n return Str.get_string('totopofcategory', 'customfield', categoryName(parentElement));\n } else if (afterElement.attr('data-field-name')) {\n return Str.get_string('afterfield', 'customfield', afterElement.attr('data-field-name'));\n } else {\n return $.Deferred().resolve('');\n }\n };\n\n $('[data-field-name]').on('sortablelist-drop', function(evt, info) {\n evt.stopPropagation(); // Important for nested lists to prevent multiple targets.\n if (info.positionChanged) {\n var pendingPromise = new Pending('core_customfield/form:fieldname:on:sortablelist-drop');\n Ajax.call([\n {\n methodname: 'core_customfield_move_field',\n args: {\n id: info.element.data('field-id'),\n beforeid: info.targetNextElement.data('field-id'),\n categoryid: Number(info.targetList.closest('[data-category-id]').attr('data-category-id'))\n },\n },\n ])[0]\n .then(pendingPromise.resolve)\n .catch(Notification.exception);\n }\n });\n\n $('[data-field-name]').on('sortablelist-drag', function(evt) {\n var pendingPromise = new Pending('core_customfield/form:fieldname:on:sortablelist-drag');\n\n evt.stopPropagation(); // Important for nested lists to prevent multiple targets.\n\n // Refreshing fields tables.\n Str.get_string('therearenofields', 'core_customfield').then(function(s) {\n $('#customfield_catlist .categorieslist').children().each(function() {\n var fields = $(this).find($('.field')),\n nofields = $(this).find($('.nofields'));\n if (!fields.length && !nofields.length) {\n $(this).find('tbody').append(\n '' + s + ''\n );\n }\n if (fields.length && nofields.length) {\n nofields.remove();\n }\n });\n return null;\n })\n .then(pendingPromise.resolve)\n .catch(Notification.exception);\n });\n\n $('[data-category-id], [data-field-name]').on('sortablelist-dragstart',\n function(evt, info) {\n setTimeout(function() {\n $('.sortable-list-is-dragged').width(info.element.width());\n }, 501);\n }\n );\n\n }\n };\n});\n"],"file":"form.min.js"} \ No newline at end of file diff --git a/customfield/amd/src/form.js b/customfield/amd/src/form.js index 10df7e1a698..eea2d4f0509 100644 --- a/customfield/amd/src/form.js +++ b/customfield/amd/src/form.js @@ -21,10 +21,17 @@ * @copyright 2018 Toni Barbera * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates', 'core/sortable_list', - 'core_form/modalform', 'core/inplace_editable'], - function( - $, Str, Notification, Ajax, Templates, SortableList, ModalForm) { +define([ + 'jquery', + 'core/str', + 'core/notification', + 'core/ajax', + 'core/templates', + 'core/sortable_list', + 'core/pending', + 'core_form/modalform', + 'core/inplace_editable', +], function($, Str, Notification, Ajax, Templates, SortableList, Pending, ModalForm) { /** * Display confirmation dialogue @@ -36,25 +43,36 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' * @param {Number} itemid */ var confirmDelete = function(id, type, component, area, itemid) { + var pendingPromise = new Pending('core_customfield/form:confirmDelete'); Str.get_strings([ {'key': 'confirm'}, {'key': 'confirmdelete' + type, component: 'core_customfield'}, {'key': 'yes'}, {'key': 'no'}, - ]).done(function(s) { + ]) + .then(function(s) { Notification.confirm(s[0], s[1], s[2], s[3], function() { + var pendingDeletePromise = new Pending('core_customfield/form:confirmDelete'); var func = (type === 'field') ? 'core_customfield_delete_field' : 'core_customfield_delete_category'; Ajax.call([ {methodname: func, args: {id: id}}, {methodname: 'core_customfield_reload_template', args: {component: component, area: area, itemid: itemid}} - ])[1].then(function(response) { + ])[1] + .then(function(response) { return Templates.render('core_customfield/list', response); - }).then(function(html, js) { + }) + .then(function(html, js) { Templates.replaceNode($('[data-region="list-page"]'), html, js); return null; - }).fail(Notification.exception); + }) + .then(pendingDeletePromise.resolve) + .catch(Notification.exception); }); - }).fail(Notification.exception); + + return; + }) + .then(pendingPromise.resolve) + .catch(Notification.exception); }; /** @@ -65,24 +83,31 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' * @param {Number} itemid */ var createNewCategory = function(component, area, itemid) { + var pendingPromise = new Pending('core_customfield/form:confirmDelete'); var promises = Ajax.call([ - {methodname: 'core_customfield_create_category', args: {component: component, area: area, itemid: itemid}}, - {methodname: 'core_customfield_reload_template', args: {component: component, area: area, itemid: itemid}} - ]), - categoryid; + {methodname: 'core_customfield_create_category', args: {component: component, area: area, itemid: itemid}}, + {methodname: 'core_customfield_reload_template', args: {component: component, area: area, itemid: itemid}} + ]); + var categoryid; promises[0].then(function(response) { categoryid = response; return null; - }).fail(Notification.exception); + }).catch(Notification.exception); promises[1].then(function(response) { return Templates.render('core_customfield/list', response); - }).then(function(html, js) { + }) + .then(function(html, js) { Templates.replaceNode($('[data-region="list-page"]'), html, js); window.location.href = '#category-' + categoryid; return null; - }).fail(Notification.exception); + }) + .catch(Notification.exception); + + Promise.all(promises) + .then(pendingPromise.resolve) + .catch(); }; /** @@ -132,18 +157,21 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' * Initialise the custom fields manager */ init: function() { - var mainlist = $('#customfield_catlist'), - component = mainlist.attr('data-component'), - area = mainlist.attr('data-area'), - itemid = mainlist.attr('data-itemid'); + var mainlist = $('#customfield_catlist'); + var component = mainlist.attr('data-component'); + var area = mainlist.attr('data-area'); + var itemid = mainlist.attr('data-itemid'); + $("[data-role=deletefield]").on('click', function(e) { confirmDelete($(this).attr('data-id'), 'field', component, area, itemid); e.preventDefault(); }); + $("[data-role=deletecategory]").on('click', function(e) { confirmDelete($(this).attr('data-id'), 'category', component, area, itemid); e.preventDefault(); }); + $('[data-role=addnewcategory]').on('click', function() { createNewCategory(component, area, itemid); }); @@ -175,7 +203,8 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' $('[data-category-id]').on('sortablelist-drop', function(evt, info) { if (info.positionChanged) { - var promises = Ajax.call([ + var pendingPromise = new Pending('core_customfield/form:categoryid:on:sortablelist-drop'); + Ajax.call([ { methodname: 'core_customfield_move_category', args: { @@ -184,8 +213,9 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' } }, - ]); - promises[0].fail(Notification.exception); + ])[0] + .then(pendingPromise.resolve) + .catch(Notification.exception); } evt.stopPropagation(); // Important for nested lists to prevent multiple targets. }); @@ -209,7 +239,8 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' $('[data-field-name]').on('sortablelist-drop', function(evt, info) { evt.stopPropagation(); // Important for nested lists to prevent multiple targets. if (info.positionChanged) { - var promises = Ajax.call([ + var pendingPromise = new Pending('core_customfield/form:fieldname:on:sortablelist-drop'); + Ajax.call([ { methodname: 'core_customfield_move_field', args: { @@ -218,11 +249,17 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' categoryid: Number(info.targetList.closest('[data-category-id]').attr('data-category-id')) }, }, - ]); - promises[0].fail(Notification.exception); + ])[0] + .then(pendingPromise.resolve) + .catch(Notification.exception); } - }).on('sortablelist-drag', function(evt) { + }); + + $('[data-field-name]').on('sortablelist-drag', function(evt) { + var pendingPromise = new Pending('core_customfield/form:fieldname:on:sortablelist-drag'); + evt.stopPropagation(); // Important for nested lists to prevent multiple targets. + // Refreshing fields tables. Str.get_string('therearenofields', 'core_customfield').then(function(s) { $('#customfield_catlist .categorieslist').children().each(function() { @@ -238,7 +275,9 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates' } }); return null; - }).fail(Notification.exception); + }) + .then(pendingPromise.resolve) + .catch(Notification.exception); }); $('[data-category-id], [data-field-name]').on('sortablelist-dragstart',