MDL-69525 core: Publish a single event when toggling slave checkboxes
[moodle.git] / lib / amd / build / checkbox-toggleall.min.js
1 define ("core/checkbox-toggleall",["jquery","core/pubsub"],function(a,b){var c=!1,d={checkboxToggled:"core/checkbox-toggleall:checkboxToggled"},e=function(a,b,c){if(c){return a.find("[data-action=\"toggle\"][data-togglegroup=\""+b+"\"]")}else{return a.find("[data-action=\"toggle\"][data-togglegroup^=\""+b+"\"]")}},f=function(a,b){return e(a,b,!1).filter("[data-toggle=\"slave\"]")},g=function(a,b,c){return e(a,b,c).filter("[data-toggle=\"master\"]")},h=function(a,b){return e(a,b,!0).filter("[data-toggle=\"action\"]")},i=function(b){var c=b.data.root,d=a(b.target),e=d.data("togglegroup"),f;if(d.is(":checkbox")){f=d.is(":checked")}else{f=1===d.data("checkall")}l(c,e,f)},j=function(a,b){var c=f(a,b);if(0<c.length){var d=c.filter(":checked"),e=c.length===d.length;o(a,b,e,!0);n(a,b,!d.length)}},k=function(a){var b=a.split(" "),c=[],d="";b.forEach(function(a){d+=" "+a;c.push(d.trim())});return c},l=function(a,c,e){var g=f(a,c);g.prop("checked",e).trigger("change");var h=g.filter(":checked");o(a,c,e,!1);n(a,c,!h.length);var i=k(c).filter(function(a){return a!==c});i.forEach(function(b){j(a,b)});b.publish(d.checkboxToggled,{root:a,toggleGroupName:c,slaves:g,checkedSlaves:h,anyChecked:e})},m=function(c){var e=c.data.root,g=a(c.target),h=g.data("togglegroup"),i=f(e,h),l=i.filter(":checked"),m=k(h);m.forEach(function(a){j(e,a)});b.publish(d.checkboxToggled,{root:e,toggleGroupName:h,slaves:i,checkedSlaves:l,anyChecked:!!l.length})},n=function(a,b,c){h(a,b).prop("disabled",c)},o=function(b,c,d,e){var f=g(b,c,e);f.prop("checked",d);f.each(function(c,e){e=a(e);var f;if(d){f=e.data("toggle-deselectall")}else{f=e.data("toggle-selectall")}if(e.is(":checkbox")){var g=b.find("[for=\""+e.attr("id")+"\"]");if(g.length){if(g.html()!==f){g.html(f)}}}else{e.text(f);e.data("checkall",d?0:1)}})},p=function(){if(!c){c=!0;var b=a(document.body);b.on("click","[data-action=\"toggle\"][data-toggle=\"master\"]",{root:b},i);b.on("click","[data-action=\"toggle\"][data-toggle=\"slave\"]",{root:b},m)}};return{init:function init(){p()},events:d,setGroupState:function setGroupState(b,c,d){b=a(b);o(b,c,d,!0);l(b,c,d)},updateSlavesFromMasterState:function updateSlavesFromMasterState(b,c){b=a(b);var d=g(b,c,!1),e;if(d.is(":checkbox")){e=d.is(":checked")}else{e=1===d.data("checkall")}l(b,c,e)}}});
2 //# sourceMappingURL=checkbox-toggleall.min.js.map