on-demand release 4.0dev+
[moodle.git] / availability / yui / build / moodle-core_availability-form / moodle-core_availability-form-min.js
1 YUI.add("moodle-core_availability-form",function(c,i){M.core_availability=M.core_availability||{},M.core_availability.form={plugins:{},field:null,mainDiv:null,rootList:null,idCounter:0,restrictByGroup:null,init:function(i){var t,e,a,l,n,o,s;for(t in i)e=i[t],(a=M[e[0]].form).init.apply(a,e);if(this.field=c.one("#id_availabilityconditionsjson"),this.field.setAttribute("aria-hidden","true"),this.mainDiv=c.Node.create('<div class="availability-field fcontainer"></div>'),this.field.insert(this.mainDiv,"after"),n=null,""!==(l=this.field.get("value")))try{n=c.JSON.parse(l)}catch(r){this.field.set("value","")}this.rootList=new M.core_availability.List(n,!0),this.mainDiv.appendChild(this.rootList.node),this.update(),this.rootList.renumber(),this.mainDiv.setAttribute("aria-live","polite"),this.field.ancestor("form").on("submit",function(){this.mainDiv.all("input,textarea,select").set("disabled",!0)},this),this.restrictByGroup=c.one("#restrictbygroup"),this.restrictByGroup&&(this.restrictByGroup.on("click",this.addRestrictByGroup,this),o=c.one("#id_groupmode"),s=c.one("#id_groupingid"),o&&o.on("change",this.updateRestrictByGroup,this),s&&s.on("change",this.updateRestrictByGroup,this),this.updateRestrictByGroup())},update:function(){var i=this.rootList.getValue(),t=[];this.rootList.fillErrors(t),0!==t.length&&(i.errors=t),this.field.set("value",c.JSON.stringify(i)),this.updateRestrictByGroup()},updateRestrictByGroup:function(){var i,t,e,a;this.restrictByGroup&&("&"!==this.rootList.getValue().op||(this.rootList.hasItemOfType("group")||this.rootList.hasItemOfType("grouping"))?this.restrictByGroup.set("disabled",!0):(i=c.one("#id_groupmode"),t=c.one("#id_groupingid"),e=1===Number(this.restrictByGroup.getData("groupavailability")),a=1===Number(this.restrictByGroup.getData("groupingavailability")),i&&0!==Number(i.get("value"))&&e||t&&0!==Number(t.get("value"))&&a?this.restrictByGroup.set("disabled",!1):this.restrictByGroup.set("disabled",!0)))},addRestrictByGroup:function(i){var t,e,a,l,n;i.preventDefault(),t=c.one("#id_groupmode"),e=c.one("#id_groupingid"),a=1===Number(this.restrictByGroup.getData("groupavailability")),l=1===Number(this.restrictByGroup.getData("groupingavailability")),e&&0!==Number(e.get("value"))&&l?n=new M.core_availability.Item({type:"grouping",id:Number(e.get("value"))},!0):t&&a&&(n=new M.core_availability.Item({type:"group"},!0)),null!==n&&(this.rootList.addChild(n),this.update(),this.rootList.renumber(),this.rootList.updateHtml())}},M.core_availability.plugin={allowAdd:!1,init:function(i,t,e){var a=i.replace(/^availability_/,"");this.allowAdd=t,(M.core_availability.form.plugins[a]=this).initInner.apply(this,e)},initInner:function(){},getNode:function(){throw"getNode not implemented"},fillValue:function(){throw"fillValue not implemented"},fillErrors:function(){},focusAfterAdd:function(i){i.one("input:not([disabled]),select:not([disabled])").focus()}},M.core_availability.List=function(i,t,e){var a,l,n,o,s,r,d;if(this.children=[],t!==undefined&&(this.root=t),this.node=c.Node.create('<div class="availability-list"><h3 class="accesshide"></h3><div class="availability-inner"><div class="availability-header mb-1"><span>'+M.util.get_string("listheader_sign_before","availability")+'</span> <label><span class="accesshide">'+M.util.get_string("label_sign","availability")+' </span><select class="availability-neg custom-select mx-1" title="'+M.util.get_string("label_sign","availability")+'"><option value="">'+M.util.get_string("listheader_sign_pos","availability")+'</option><option value="!">'+M.util.get_string("listheader_sign_neg","availability")+'</option></select></label> <span class="availability-single">'+M.util.get_string("listheader_single","availability")+'</span><span class="availability-multi">'+M.util.get_string("listheader_multi_before","availability")+' <label><span class="accesshide">'+M.util.get_string("label_multi","availability")+' </span><select class="availability-op custom-select mx-1" title="'+M.util.get_string("label_multi","availability")+'"><option value="&">'+M.util.get_string("listheader_multi_and","availability")+'</option><option value="|">'+M.util.get_string("listheader_multi_or","availability")+"</option></select></label> "+M.util.get_string("listheader_multi_after","availability")+'</span></div><div class="availability-children"></div><div class="availability-none"><span class="px-3">'+M.util.get_string("none","moodle")+'</span></div><div class="clearfix mt-1"></div><div class="availability-button"></div></div><div class="clearfix"></div></div>'),t||this.node.addClass("availability-childlist d-sm-flex align-items-center"),this.inner=this.node.one("> .availability-inner"),a=!0,t?(i&&i.show!==undefined&&(a=i.show),this.eyeIcon=new M.core_availability.EyeIcon(!1,a),this.node.one(".availability-header").get("firstChild").insert(this.eyeIcon.span,"before")):e&&(i&&i.showc!==undefined&&(a=i.showc),this.eyeIcon=new M.core_availability.EyeIcon(!1,a),this.inner.insert(this.eyeIcon.span,"before")),t||(l=new M.core_availability.DeleteIcon(this),(n=this.node.one(".availability-none")).appendChild(document.createTextNode(" ")),n.appendChild(l.span),n.appendChild(c.Node.create('<span class="mt-1 badge badge-warning">'+M.util.get_string("invalid","availability")+"</span>"))),(o=c.Node.create('<button type="button" class="btn btn-secondary mt-1">'+M.util.get_string("addrestriction","availability")+"</button>")).on("click",function(){this.clickAdd()},this),this.node.one("div.availability-button").appendChild(o),i){switch(i.op){case"&":case"|":this.node.one(".availability-neg").set("value","");break;case"!&":case"!|":this.node.one(".availability-neg").set("value","!")}switch(i.op){case"&":case"!&":this.node.one(".availability-op").set("value","&");break;case"|":case"!|":this.node.one(".availability-op").set("value","|")}for(s=0;s<i.c.length;s++)r=i.c[s],this.root&&i&&i.showc!==undefined&&(r.showc=i.showc[s]),d=r.type!==undefined?new M.core_availability.Item(r,this.root
2 ):new M.core_availability.List(r,!1,this.root),this.addChild(d)}this.node.one(".availability-neg").on("change",function(){M.core_availability.form.update(),this.updateHtml()},this),this.node.one(".availability-op").on("change",function(){M.core_availability.form.update(),this.updateHtml()},this),this.updateHtml()},M.core_availability.List.prototype.addChild=function(i){0<this.children.length&&this.inner.one(".availability-children").appendChild(c.Node.create('<div class="availability-connector"><span class="label"></span></div>')),this.children.push(i),this.inner.one(".availability-children").appendChild(i.node)},M.core_availability.List.prototype.focusAfterAdd=function(){this.inner.one("button").focus()},M.core_availability.List.prototype.isIndividualShowIcons=function(){var i,t;if(!this.root)throw"Can only call this on root list";return i="!"===this.node.one(".availability-neg").get("value"),t="|"===this.node.one(".availability-op").get("value"),!i&&!t||i&&t},M.core_availability.List.prototype.renumber=function(i){var t,e,a,l={count:this.children.length};for(t=i===undefined?l.number="":(l.number=i+":",i+"."),e=M.util.get_string("setheading","availability",l),this.node.one("> h3").set("innerHTML",e),a=0;a<this.children.length;a++)this.children[a].renumber(t+(a+1))},M.core_availability.List.prototype.updateHtml=function(){var i,t,e,a;if(0<this.children.length?(this.inner.one("> .availability-children").removeAttribute("aria-hidden"),this.inner.one("> .availability-none").setAttribute("aria-hidden","true"),this.inner.one("> .availability-header").removeAttribute("aria-hidden"),1<this.children.length?(this.inner.one(".availability-single").setAttribute("aria-hidden","true"),this.inner.one(".availability-multi").removeAttribute("aria-hidden")):(this.inner.one(".availability-single").removeAttribute("aria-hidden"),this.inner.one(".availability-multi").setAttribute("aria-hidden","true"))):(this.inner.one("> .availability-children").setAttribute("aria-hidden","true"),this.inner.one("> .availability-none").removeAttribute("aria-hidden"),this.inner.one("> .availability-header").setAttribute("aria-hidden","true")),this.root){for(i=this.isIndividualShowIcons(),t=0;t<this.children.length;t++)e=this.children[t],i?e.eyeIcon.span.removeAttribute("aria-hidden"):e.eyeIcon.span.setAttribute("aria-hidden","true");i?this.eyeIcon.span.setAttribute("aria-hidden","true"):this.eyeIcon.span.removeAttribute("aria-hidden")}a="&"===this.inner.one(".availability-op").get("value")?M.util.get_string("and","availability"):M.util.get_string("or","availability"),this.inner.all("> .availability-children > .availability-connector span.label").each(function(i){i.set("innerHTML",a)})},M.core_availability.List.prototype.deleteDescendant=function(i){var t,e,a;for(t=0;t<this.children.length;t++){if((e=this.children[t])===i)return this.children.splice(t,1),a=e.node,0<this.children.length&&(a.previous(".availability-connector")?a.previous(".availability-connector").remove():a.next(".availability-connector").remove()),this.inner.one("> .availability-children").removeChild(a),M.core_availability.form.update(),this.updateHtml(),this.inner.one("> .availability-button").one("button").focus(),!0;if(e instanceof M.core_availability.List&&e.deleteDescendant(i))return!0}return!1},M.core_availability.List.prototype.clickAdd=function(){var i,t,e,a,l,n,o=c.Node.create('<div><ul class="list-unstyled container-fluid"></ul><div class="availability-buttons mdl-align"><button type="button" class="btn btn-secondary">'+M.util.get_string("cancel","moodle")+"</button></div></div>"),s=o.one("button"),r={dialog:null},d=o.one("ul");for(l in M.core_availability.form.plugins)M.core_availability.form.plugins[l].allowAdd&&(i=c.Node.create('<li class="clearfix row"></li>'),t="availability_addrestriction_"+l,(e=c.Node.create('<div class="col-6"><button type="button" class="btn btn-secondary w-100"id="'+t+'">'+M.util.get_string("title","availability_"+l)+"</button></div>")).on("click",this.getAddHandler(l,r),this),i.appendChild(e),a=c.Node.create('<div class="col-6"><label for="'+t+'">'+M.util.get_string("description","availability_"+l)+"</label></div>"),i.appendChild(a),d.appendChild(i));i=c.Node.create('<li class="clearfix row"></li>'),t="availability_addrestriction_list_",(e=c.Node.create('<div class="col-6"><button type="button" class="btn btn-secondary w-100"id="'+t+'">'+M.util.get_string("condition_group","availability")+"</button></div>")).on("click",this.getAddHandler(null,r),this),i.appendChild(e),a=c.Node.create('<div class="col-6"><label for="'+t+'">'+M.util.get_string("condition_group_info","availability")+"</label></div>"),i.appendChild(a),d.appendChild(i),n={headerContent:M.util.get_string("addrestriction","availability"),bodyContent:o,additionalBaseClass:"availability-dialogue",draggable:!0,modal:!0,closeButton:!1,width:"450px"},r.dialog=new M.core.dialogue(n),r.dialog.show(),s.on("click",function(){r.dialog.destroy(),this.inner.one("> .availability-button").one("button").focus()},this)},M.core_availability.List.prototype.getAddHandler=function(t,e){return function(){var i;i=t?new M.core_availability.Item({type:t,creating:!0},this.root):new M.core_availability.List({c:[],showc:!0},!1,this.root),this.addChild(i),M.core_availability.form.update(),M.core_availability.form.rootList.renumber(),this.updateHtml(),e.dialog.destroy(),i.focusAfterAdd()}},M.core_availability.List.prototype.getValue=function(){var i,t={};for(t.op=this.node.one(".availability-neg").get("value")+this.node.one(".availability-op").get("value"),t.c=[],i=0;i<this.children.length;i++)t.c.push(this.children[i].getValue());if(this.root)if(this.isIndividualShowIcons())for(t.showc=[],i=0;i<this.children.length;i++)t.showc.push(!this.children[i].eyeIcon.isHidden());else t.show=!this.eyeIcon.isHidden();return t},M.core_availability.List.prototype.fillErrors=function(i){0!==this.children.length||this.root||i.push("availability:error_list_nochildren");for(
3 var t=0;t<this.children.length;t++)this.children[t].fillErrors(i)},M.core_availability.List.prototype.hasItemOfType=function(i){var t,e;for(t=0;t<this.children.length;t++)if((e=this.children[t])instanceof M.core_availability.List){if(e.hasItemOfType(i))return!0}else if(e.pluginType===i)return!0;return!1},M.core_availability.List.prototype.eyeIcon=null,M.core_availability.List.prototype.root=!1,M.core_availability.List.prototype.children=null,M.core_availability.List.prototype.node=null,M.core_availability.List.prototype.inner=null,M.core_availability.Item=function(i,t){var e,a;this.pluginType=i.type,M.core_availability.form.plugins[i.type]===undefined?(this.plugin=null,this.pluginNode=c.Node.create('<div class="availability-warning">'+M.util.get_string("missingplugin","availability")+"</div>")):(this.plugin=M.core_availability.form.plugins[i.type],this.pluginNode=this.plugin.getNode(i),this.pluginNode.addClass("availability_"+i.type)),this.node=c.Node.create('<div class="availability-item d-sm-flex align-items-center"><h3 class="accesshide"></h3></div>'),t&&(e=!0,i.showc!==undefined&&(e=i.showc),this.eyeIcon=new M.core_availability.EyeIcon(!0,e),this.node.appendChild(this.eyeIcon.span)),this.pluginNode.addClass("availability-plugincontrols"),this.node.appendChild(this.pluginNode),a=new M.core_availability.DeleteIcon(this),this.node.appendChild(a.span),this.node.appendChild(document.createTextNode(" ")),this.node.appendChild(c.Node.create('<span class="badge badge-warning"/>'))},M.core_availability.Item.prototype.getValue=function(){var i={type:this.pluginType};return this.plugin&&this.plugin.fillValue(i,this.pluginNode),i},M.core_availability.Item.prototype.fillErrors=function(i){var t,e=i.length;this.plugin?this.plugin.fillErrors(i,this.pluginNode):i.push("core_availability:item_unknowntype"),t=this.node.one("> .badge-warning"),i.length===e||t.get("firstChild")?i.length===e&&t.get("firstChild")&&t.get("firstChild").remove():t.appendChild(document.createTextNode(M.util.get_string("invalid","availability")))},M.core_availability.Item.prototype.renumber=function(i){var t,e={number:i};this.plugin?e.type=M.util.get_string("title","availability_"+this.pluginType):e.type="["+this.pluginType+"]",e.number=i+":",t=M.util.get_string("itemheading","availability",e),this.node.one("> h3").set("innerHTML",t)},M.core_availability.Item.prototype.focusAfterAdd=function(){this.plugin.focusAfterAdd(this.pluginNode)},M.core_availability.Item.prototype.pluginType=null,M.core_availability.Item.prototype.plugin=null,M.core_availability.Item.prototype.eyeIcon=null,M.core_availability.Item.prototype.node=null,M.core_availability.Item.prototype.pluginNode=null,M.core_availability.EyeIcon=function(i,t){var e,a,l,n,o;this.individual=i,this.span=c.Node.create('<a class="availability-eye col-form-label" href="#" role="button">'),e=c.Node.create("<img />"),this.span.appendChild(e),a=i?"_individual":"_all",l=function(){var i=M.util.get_string("hidden"+a,"availability");e.set("src",M.util.image_url("i/show","core")),e.set("alt",i),this.span.set("title",i+" • "+M.util.get_string("show_verb","availability"))},n=function(){var i=M.util.get_string("shown"+a,"availability");e.set("src",M.util.image_url("i/hide","core")),e.set("alt",i),this.span.set("title",i+" • "+M.util.get_string("hide_verb","availability"))},t?n.call(this):l.call(this),o=function(i){i.preventDefault(),this.isHidden()?n.call(this):l.call(this),M.core_availability.form.update()},this.span.on("click",o,this),this.span.on("key",o,"up:32",this),this.span.on("key",function(i){i.preventDefault()},"down:32",this)},M.core_availability.EyeIcon.prototype.individual=!1,M.core_availability.EyeIcon.prototype.span=null,M.core_availability.EyeIcon.prototype.isHidden=function(){var i=this.individual?"_individual":"_all",t=M.util.get_string("hidden"+i,"availability");return this.span.one("img").get("alt")===t},M.core_availability.DeleteIcon=function(t){var i,e;this.span=c.Node.create('<a class="d-inline-block col-form-label availability-delete px-3" href="#" title="'+M.util.get_string("delete","moodle")+'" role="button">'),i=c.Node.create('<img src="'+M.util.image_url("t/delete","core")+'" alt="'+M.util.get_string("delete","moodle")+'" />'),this.span.appendChild(i),e=function(i){i.preventDefault(),M.core_availability.form.rootList.deleteDescendant(t),M.core_availability.form.rootList.renumber()},this.span.on("click",e,this),this.span.on("key",e,"up:32",this),this.span.on("key",function(i){i.preventDefault()},"down:32",this)},M.core_availability.DeleteIcon.prototype.span=null},"@VERSION@",{requires:["base","node","event","event-delegate","panel","moodle-core-notification-dialogue","json"]});