weekly release 3.9dev
[moodle.git] / lib / amd / build / form-autocomplete.min.js
... / ...
CommitLineData
1define ("core/form-autocomplete",["jquery","core/log","core/str","core/templates","core/notification","core/loadingicon"],function(a,b,c,d,f,g){var h={DOWN:40,ENTER:13,SPACE:32,ESCAPE:27,COMMA:44,UP:38},i=Date.now(),j=function(b,c){var d=a(document.getElementById(c.selectionId)),e=d.children("[aria-selected=true]").length;b=b%e;while(0>b){b+=e}var f=a(d.children("[aria-selected=true]").get(b)),g=c.selectionId+"-"+b;d.children().attr("data-active-selection",!1).attr("id","");f.attr("data-active-selection",!0).attr("id",g);d.attr("aria-activedescendant",g);return a.Deferred().resolve()},k=function(b,c,e){var g="form-autocomplete-updateSelectionList-"+c.inputId;M.util.js_pending(g);var h=[],i=a(document.getElementById(c.selectionId)),k=i.attr("aria-activedescendant"),l=!1;if(k){l=a(document.getElementById(k)).attr("data-value")}e.children("option").each(function(b,c){if(a(c).prop("selected")){var d;if(a(c).data("html")){d=a(c).data("html")}else{d=a(c).html()}if(""!==d){h.push({label:d,value:a(c).attr("value")})}}});var m=a.extend({items:h},b,c);return d.render("core/form_autocomplete_selection",m).then(function(b,e){d.replaceNodeContents(i,b,e);if(!1!==l){i.children("[aria-selected=true]").each(function(b,d){if(a(d).attr("data-value")===l){j(b,c)}})}return l}).then(function(){return M.util.js_complete(g)}).catch(f.exception)},l=function(a){if("undefined"!=typeof M.core_formchangechecker){M.core_formchangechecker.set_form_changed()}a.change()},m=function(b,c,d,e){var f=a(d).attr("data-value");e.children("option").each(function(b,c){if(a(c).attr("value")==f){a(c).prop("selected",!1);if(a(c).attr("data-iscustom")){a(c).remove()}}});return k(b,c,e).then(function(){l(e)})},n=function(b,c){var d=a(document.getElementById(c.inputId)),e=a(document.getElementById(c.suggestionsId)),f=e.children("[aria-hidden=false]").length;b=b%f;while(0>b){b+=f}var g=a(e.children("[aria-hidden=false]").get(b)),h=a(e.children("[role=option]")).index(g),i=c.suggestionsId+"-"+h;e.children().attr("aria-selected",!1).attr("id","");g.attr("aria-selected",!0).attr("id",i);d.attr("aria-activedescendant",i);var j=g.offset().top-e.offset().top+e.scrollTop()-e.height()/2;return e.animate({scrollTop:j},100).promise()},o=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);return n(e+1,b)},p=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]");if(!d){return j(0,b)}var e=c.children("[aria-selected=true]").index(d);return j(e-1,b)},q=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]"),e=0;if(d){e=c.children("[aria-selected=true]").index(d);e=e+1}else{e=0}return j(e,b)},r=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);return n(e-1,b)},s=function(b){var c=a(document.getElementById(b.inputId)),d=a(document.getElementById(b.suggestionsId));c.attr("aria-expanded",!1).attr("aria-activedescendant",b.selectionId);d.hide().attr("aria-hidden",!0);return a.Deferred().resolve()},t=function(b,e,g,h){var i="form-autocomplete-updateSuggestions-"+e.inputId;M.util.js_pending(i);var j=a(document.getElementById(e.inputId)),k=a(document.getElementById(e.suggestionsId)),l=!1,m=[];h.children("option").each(function(b,c){if(!0!==a(c).prop("selected")){m[m.length]={label:c.innerHTML,value:a(c).attr("value")}}});var o=e.caseSensitive?g:g.toLocaleLowerCase(),p=a.extend({options:m},b,e),q=d.render("core/form_autocomplete_suggestions",p).then(function(f,g){d.replaceNode(k,f,g);k=a(document.getElementById(e.suggestionsId));k.show().attr("aria-hidden",!1);k.children().each(function(c,d){d=a(d);if(b.caseSensitive&&-1<d.text().indexOf(o)||!b.caseSensitive&&-1<d.text().toLocaleLowerCase().indexOf(o)){d.show().attr("aria-hidden",!1);l=!0}else{d.hide().attr("aria-hidden",!0)}});j.attr("aria-expanded",!0);if(h.attr("data-notice")){k.html(h.attr("data-notice"))}else if(l){if(!b.tags){n(0,e)}}else{c.get_string("nosuggestions","form").done(function(a){k.html(a)})}return k}).then(function(){return M.util.js_complete(i)}).catch(f.exception);return q},u=function(b,c,d){var e=a(document.getElementById(c.inputId)),f=e.val(),g=f.split(","),h=!1;a.each(g,function(c,e){e=e.trim();if(""!==e){if(!b.multiple){d.children("option").prop("selected",!1)}d.children("option").each(function(b,c){if(a(c).attr("value")==e){h=!0;a(c).prop("selected",!0)}});if(!h){var f=a("<option>");f.append(document.createTextNode(e));f.attr("value",e);d.append(f);f.prop("selected",!0);f.attr("data-iscustom",!0)}}});return k(b,c,d).then(function(){l(d)}).then(function(){e.val("")}).then(function(){return s(c)})},v=function(b,c,d){var e=a(document.getElementById(c.inputId)),f=a(document.getElementById(c.suggestionsId)),g=f.children("[aria-selected=true]").attr("data-value");if(!b.multiple){d.children("option").prop("selected",!1)}d.children("option").each(function(b,c){if(a(c).attr("value")==g){a(c).prop("selected",!0)}});return k(b,c,d).then(function(){l(d)}).then(function(){if(b.closeSuggestionsOnSelect){e.val("");return s(c)}else{e.focus();return t(b,c,e.val(),d)}})},w=function(b,c,d,e,f){var h=y("updateAjax"),i=a(document.getElementById(d.selectId)).parent();g.addIconToContainerRemoveOnCompletion(i,h);var j=a(b.currentTarget).val();f.transport(c.selector,j,function(b){var g=f.processResults(c.selector,b),i=[];if(!c.multiple){e.children("option").remove()}e.children("option").each(function(b,c){c=a(c);if(!c.prop("selected")){c.remove()}else{i.push(c.attr("value")+"")}});if(!c.multiple&&0===e.children("option").length){var j=a("<option>");e.append(j)}if(a.isArray(g)){a.each(g,function(b,c){if(-1===i.indexOf(c.value+"")){var d=a("<option>");d.append(c.label);d.attr("value",c.value);e.append(d)}});e.attr("data-notice","")}else{e.attr("data-notice",g)}h.resolve(t(c,d,"",e))},function(a){h.reject(a)});return h},x=function(b,c,d){var g=a(document.getElementById(c.inputId));g.on("keydown",function(f){var e=y("addNavigation-"+c.inputId+"-"+f.keyCode);switch(f.keyCode){case h.DOWN:if(!b.showSuggestions){e.resolve();return!0}else if("true"===g.attr("aria-expanded")){e.resolve(o(c))}else{if(!g.val()&&b.ajax){require([b.ajax],function(a){e.resolve(w(f,b,c,d,a))})}else{e.resolve(t(b,c,g.val(),d))}}f.preventDefault();return!1;case h.UP:e.resolve(r(c));f.preventDefault();return!1;case h.ENTER:var i=a(document.getElementById(c.suggestionsId));if("true"===g.attr("aria-expanded")&&0<i.children("[aria-selected=true]").length){e.resolve(v(b,c,d))}else if(b.tags){e.resolve(u(b,c,d))}else{e.resolve()}f.preventDefault();return!1;case h.ESCAPE:if("true"===g.attr("aria-expanded")){e.resolve(s(c))}else{e.resolve()}f.preventDefault();return!1;}e.resolve();return!0});g.on("keypress",function(a){if(a.keyCode===h.COMMA){if(b.tags){y("keypress-"+a.keyCode).resolve(u(b,c,d))}a.preventDefault();return!1}return!0});g.closest("form").on("submit",function(){if(b.tags){y("form-autocomplete-submit").resolve(u(b,c,d))}return!0});g.on("blur",function(){var e=y("form-autocomplete-blur");window.setTimeout(function(){var f=a(document.activeElement),h=a.Deferred();if(f.is(document.getElementById(c.suggestionsId))){g.focus()}else if(!f.is(g)&&a(document.getElementById(c.inputId)).length){if(b.tags){h.then(function(){return u(b,c,d)}).catch()}h.then(function(){return s(c)}).catch()}h.then(function(){return e.resolve()}).catch();h.resolve()},500)});if(b.showSuggestions){var i=a(document.getElementById(c.downArrowId));i.on("click",function(a){var e=y("form-autocomplete-show-suggestions");g.focus();if(!g.val()&&b.ajax){require([b.ajax],function(f){e.resolve(w(a,b,c,d,f))})}else{e.resolve(t(b,c,g.val(),d))}})}var j=a(document.getElementById(c.suggestionsId));j.parent().prop("onclick",null).off("click");j.parent().on("click","[role=option]",function(f){var e=y("form-autocomplete-parent"),g=a(f.currentTarget).closest("[role=option]"),h=a(document.getElementById(c.suggestionsId)),i=h.children("[aria-hidden=false]").index(g);n(i,c).then(function(){return v(b,c,d)}).then(function(){return e.resolve()}).catch()});var k=a(document.getElementById(c.selectionId));k.on("click","[role=listitem]",function(f){var e=y("form-autocomplete-clicks");e.resolve(m(b,c,a(f.currentTarget),d))});k.on("keydown",function(f){var e=y("form-autocomplete-keydown-"+f.keyCode);switch(f.keyCode){case h.DOWN:f.preventDefault();e.resolve(q(c));return!1;case h.UP:f.preventDefault();e.resolve(p(c));return!1;case h.SPACE:case h.ENTER:var g=a(document.getElementById(c.selectionId)).children("[data-active-selection=true]");if(g){f.preventDefault();e.resolve(m(b,c,g,d))}return!1;}e.resolve();return!0});if(b.showSuggestions){if(b.ajax){require([b.ajax],function(a){var h=null,i=!1,j="autocomplete-throttledhandler",k=function(g){h=null;i=!0;w(g,b,c,d,a).then(function(){if(null===h){M.util.js_complete(j)}i=!1;return arguments[0]}).catch(f.exception)},l=function(a){window.clearTimeout(h);if(i){h=window.setTimeout(l.bind(this,a),100);return}if(null===h){M.util.js_pending(j)}h=window.setTimeout(k.bind(this,a),300)};g.on("input",l)})}else{g.on("input",function(f){var e=a(f.currentTarget).val(),g=a(f.currentTarget).data("last-value");if(g!==e){t(b,c,e,d)}a(f.currentTarget).data("last-value",e)})}}},y=function(b){var c="form-autocomplete:"+b;M.util.js_pending(c);var d=a.Deferred();d.then(function(){M.util.js_complete(c);return arguments[0]}).catch(f.exception);return d};return{enhance:function enhance(e,g,h,j,l,m,n,o){var p={selector:e,tags:!1,ajax:!1,placeholder:j,caseSensitive:!1,showSuggestions:!0,noSelectionString:n},q="autocomplete-setup-"+e;M.util.js_pending(q);if("undefined"!=typeof g){p.tags=g}if("undefined"!=typeof h){p.ajax=h}if("undefined"!=typeof l){p.caseSensitive=l}if("undefined"!=typeof m){p.showSuggestions=m}if("undefined"==typeof n){c.get_string("noselection","form").done(function(a){p.noSelectionString=a}).fail(f.exception)}var r=a(e);if(!r){b.debug("Selector not found: "+e);M.util.js_complete(q);return!1}r.css("visibility","hidden").attr("aria-hidden",!0);var s={selectId:r.attr("id"),inputId:"form_autocomplete_input-"+i,suggestionsId:"form_autocomplete_suggestions-"+i,selectionId:"form_autocomplete_selection-"+i,downArrowId:"form_autocomplete_downarrow-"+i};i++;p.multiple=r.attr("multiple");if("undefined"!=typeof o){p.closeSuggestionsOnSelect=o}else{p.closeSuggestionsOnSelect=!p.multiple}var t=a("[for="+s.selectId+"]"),u=[];r.children("option").each(function(b,c){u[b]={label:c.innerHTML,value:a(c).attr("value")}});var v=a.extend({},p,s);v.options=u;v.items=[];var w="",y=d.render("core/form_autocomplete_input",v).then(function(a,b){w+=b;return a}),z=d.render("core/form_autocomplete_suggestions",v).then(function(a,b){w+=b;return a}),A=d.render("core/form_autocomplete_selection",v).then(function(a,b){w+=b;return a});return a.when(y,z,A).then(function(b,c,e){r.hide();r.after(c);r.after(b);r.after(e);d.runTemplateJS(w);t.attr("for",s.inputId);x(p,s,r);var f=a(document.getElementById(s.suggestionsId));f.hide().attr("aria-hidden",!0)}).then(function(){return k(p,s,r)}).then(function(){return M.util.js_complete(q)}).catch(function(a){M.util.js_complete(q);f.exception(a)})}}});
2//# sourceMappingURL=form-autocomplete.min.js.map