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