moodle/lib/amd/build/inplace_editable.min.js

3 lines
4.7 KiB
JavaScript

define ("core/inplace_editable",["jquery","core/ajax","core/templates","core/notification","core/str","core/config","core/url","core/form-autocomplete","core/pending"],function(a,b,c,d,e,f,g,h,i){a("body").on("click keypress","[data-inplaceeditable] [data-inplaceeditablelink]",function(j){if("keypress"===j.type&&13!==j.keyCode){return}var k=new i("autocomplete-start-editing");j.stopImmediatePropagation();j.preventDefault();var l=a(this),m=l.closest("[data-inplaceeditable]"),n=function(b){b.addClass("updating");var c=b.find("img.spinner");if(c.length){c.show()}else{c=a("<img/>").attr("src",g.imageUrl("i/loading_small")).addClass("spinner").addClass("smallicon");b.append(c)}},o=function(a){a.removeClass("updating");a.find("img.spinner").hide()},p=function(f,g){var h=[f.attr("data-itemid"),f.attr("data-component"),f.attr("data-itemtype")].join("-"),j=new i(h);n(f);b.call([{methodname:"core_update_inplace_editable",args:{itemid:f.attr("data-itemid"),component:f.attr("data-component"),itemtype:f.attr("data-itemtype"),value:g}}])[0].then(function(b){return c.render("core/inplace_editable",b).then(function(d,e){var g=f.attr("data-value"),h=a(d);c.replaceNode(f,h,e);h.find("[data-inplaceeditablelink]").focus();h.trigger({type:"updated",ajaxreturn:b,oldvalue:g})})}).then(function(){return j.resolve()}).fail(function(b){var c=a.Event("updatefailed",{exception:b,newvalue:g});o(f);M.util.js_complete(h);f.trigger(c);if(!c.isDefaultPrevented()){d.exception(b)}})},q=function(a){a.find("input").off();a.find("select").off();a.html(a.attr("data-oldcontent"));a.removeAttr("data-oldcontent");a.removeClass("inplaceeditingon");a.find("[data-inplaceeditablelink]").focus()},r=function(b,c){var d=b,e;for(e=0;e<c;e++){d+=Math.floor(10*Math.random())+""}if(0===a("#"+d).length){return d}return r(b,c)},s=function(b){e.get_string("edittitleinstructions").done(function(c){var d=a("<span class=\"editinstructions\">"+c+"</span>").attr("id",r("id_editinstructions_",20)),e=a("<input type=\"text\"/>").attr("id",r("id_inplacevalue_",20)).attr("value",b.attr("data-value")).attr("aria-describedby",d.attr("id")).addClass("ignoredirty").addClass("form-control"),g=a("<label class=\"accesshide\">"+m.attr("data-editlabel")+"</label>").attr("for",e.attr("id"));b.html("").append(d).append(g).append(e);e.focus();e.select();e.on("keyup keypress focusout",function(a){if(f.behatsiterunning&&"focusout"===a.type){return}if("keypress"===a.type&&13===a.keyCode){var c=e.val();q(b);p(b,c)}if("keyup"===a.type&&27===a.keyCode||"focusout"===a.type){q(b)}})})},t=function(a,b){q(a);p(a,b)},u=function(b,c){var d,e=a("<select></select>").attr("id",r("id_inplacevalue_",20)).addClass("custom-select"),g=a("<label class=\"accesshide\">"+m.attr("data-editlabel")+"</label>").attr("for",e.attr("id"));for(d in c){e.append(a("<option>").attr("value",c[d].key).html(c[d].value))}e.val(b.attr("data-value"));b.html("").append(g).append(e);e.focus();e.select();e.on("keyup change focusout",function(a){if(f.behatsiterunning&&"focusout"===a.type){return}if("change"===a.type){var c=e.val();q(b);p(b,c)}if("keyup"===a.type&&27===a.keyCode||"focusout"===a.type){q(b)}})},v=function(b,f){var g,j=a("<select></select>").attr("id",r("id_inplacevalue_",20)).addClass("form-autocomplete-original-select").addClass("custom-select"),k=a("<label class=\"accesshide\">"+m.attr("data-editlabel")+"</label>").attr("for",j.attr("id")),l=f.options,n=f.attributes,o=a("<a href=\"#\"></a>"),s=a("<a href=\"#\"></a>");for(g in l){j.append(a("<option>").attr("value",l[g].key).html(l[g].value))}if(n.multiple){j.attr("multiple","true")}j.val(JSON.parse(b.attr("data-value")));e.get_string("savechanges","core").then(function(a){return c.renderPix("e/save","core",a)}).then(function(a){o.append(a)}).fail(d.exception);e.get_string("cancel","core").then(function(a){return c.renderPix("e/cancel","core",a)}).then(function(a){s.append(a)}).fail(d.exception);b.html("").append(k).append(j).append(o).append(s);j.focus();j.select();h.enhance(j,n.tags,n.ajax,n.placeholder,n.caseSensitive,n.showSuggestions,n.noSelectionString).then(function(){b.find("[role=combobox]").focus()}).fail(d.exception);j.on("keyup",function(a){if("keyup"===a.type&&27===a.keyCode||"focusout"===a.type){q(b)}});o.on("click",function(a){var c=JSON.stringify(j.val());j.empty();q(b);p(b,c);a.preventDefault()});s.on("click",function(a){j.empty();q(b);a.preventDefault()})};(function turnEditingOffEverywhere(){a("span.inplaceeditable.inplaceeditingon").each(function(){q(a(this))})})();(function turnEditingOn(b){b.addClass("inplaceeditingon");b.attr("data-oldcontent",b.html());var c=b.attr("data-type"),d=b.attr("data-options");if("toggle"===c){t(b,d)}else if("select"===c){u(b,a.parseJSON(d))}else if("autocomplete"===c){v(b,a.parseJSON(d))}else{s(b)}})(m);k.resolve()});return{}});
//# sourceMappingURL=inplace_editable.min.js.map