MDL-69107 form_autocomplete: Rewrite item selection
[moodle.git] / lib / amd / build / form-autocomplete.min.js
1 define ("core/form-autocomplete",["jquery","core/log","core/str","core/templates","core/notification","core/loadingicon","core/aria"],function(a,b,c,d,f,g,h){var i={DOWN:40,ENTER:13,SPACE:32,ESCAPE:27,COMMA:44,UP:38,LEFT:37,RIGHT:39},j=Date.now(),k=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",null).attr("id","");f.attr("data-active-selection",!0).attr("id",g);d.attr("aria-activedescendant",g);d.attr("data-active-value",f.attr("data-value"));return a.Deferred().resolve()},l=function(b){var c=a(document.getElementById(b.selectionId)),d=c.attr("aria-activedescendant");if(d){var e=a(document.getElementById(d));if(e.length){return e}}var f=c.attr("data-active-value");return c.find("[data-value=\""+f+"\"]")},m=function(b){var c=l(b),d=c.attr("data-value"),e=a(document.getElementById(b.selectionId));if(d){var f=e.find("[aria-selected=true]").index(c);if(-1!==f){k(f,b);return}}k(0,b)},n=function(b,c,e){var g="form-autocomplete-updateSelectionList-"+c.inputId;M.util.js_pending(g);var h=[],i=a(document.getElementById(c.selectionId));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")})}}});if(!o(c,h)){M.util.js_complete(g);return Promise.resolve()}c.items=h;var j=a.extend(b,c);return d.render(b.templates.items,j).then(function(a,b){d.replaceNodeContents(i,a,b);m(c)}).then(function(){return M.util.js_complete(g)}).catch(f.exception)},o=function(a,b){if(a.items.length!==b.length){return!0}return 0<a.items.filter(function(a){return-1===b.indexOf(a)}).length},p=function(a){if("undefined"!=typeof M.core_formchangechecker){M.core_formchangechecker.set_form_changed()}a[0].dispatchEvent(new Event("change"))},q=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 n(b,c,e).then(function(){p(e)})},r=function(b,c){var d=a(document.getElementById(c.inputId)),e=a(document.getElementById(c.suggestionsId)),f=e.children(":not([aria-hidden])").length;b=b%f;while(0>b){b+=f}var g=a(e.children(":not([aria-hidden])").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()},s=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children(":not([aria-hidden])").index(d);return r(e+1,b)},t=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection]");if(!d){return k(0,b)}var e=c.children("[aria-selected=true]").index(d);return k(e-1,b)},u=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection]"),e=0;if(d){e=c.children("[aria-selected=true]").index(d);e=e+1}else{e=0}return k(e,b)},v=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children(":not([aria-hidden])").index(d);return r(e-1,b)},w=function(b){var c=a(document.getElementById(b.inputId)),d=a(document.getElementById(b.suggestionsId));if("true"===c.attr("aria-expanded")){c.attr("aria-expanded",!1)}c.attr("aria-activedescendant",b.selectionId);h.hide(d.get());d.hide();return a.Deferred().resolve()},x=function(b,e,g,i){var j="form-autocomplete-updateSuggestions-"+e.inputId;M.util.js_pending(j);var k=a(document.getElementById(e.inputId)),l=a(document.getElementById(e.suggestionsId)),m=!1,n=[];i.children("option").each(function(b,c){if(!0!==a(c).prop("selected")){n[n.length]={label:c.innerHTML,value:a(c).attr("value")}}});var o=e.caseSensitive?g:g.toLocaleLowerCase(),p=a.extend({options:n},b,e),q=d.render("core/form_autocomplete_suggestions",p).then(function(f,g){d.replaceNode(l,f,g);l=a(document.getElementById(e.suggestionsId));h.unhide(l.get());l.show();l.children().each(function(c,d){d=a(d);if(b.caseSensitive&&-1<d.text().indexOf(o)||!b.caseSensitive&&-1<d.text().toLocaleLowerCase().indexOf(o)){h.unhide(d.get());d.show();m=!0}else{d.hide();h.hide(d.get())}});k.attr("aria-expanded",!0);if(i.attr("data-notice")){l.html(i.attr("data-notice"))}else if(m){if(!b.tags){r(0,e)}}else{c.get_string("nosuggestions","form").done(function(a){l.html(a)})}return l}).then(function(){return M.util.js_complete(j)}).catch(f.exception);return q},y=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 n(b,c,d).then(function(){p(d)}).then(function(){e.val("")}).then(function(){return w(c)})},z=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 n(b,c,d).then(function(){p(d)}).then(function(){if(b.closeSuggestionsOnSelect){e.val("");return w(c)}else{e.focus();return x(b,c,e.val(),d)}})},A=function(b,c,d,e,f){var h=C("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(x(c,d,"",e))},function(a){h.reject(a)});return h},B=function(b,c,d){var g=a(document.getElementById(c.inputId));g.on("keydown",function(f){var e=C("addNavigation-"+c.inputId+"-"+f.keyCode);switch(f.keyCode){case i.DOWN:if(!b.showSuggestions){e.resolve();return!0}else if("true"===g.attr("aria-expanded")){e.resolve(s(c))}else{if(!g.val()&&b.ajax){require([b.ajax],function(a){e.resolve(A(f,b,c,d,a))})}else{e.resolve(x(b,c,g.val(),d))}}f.preventDefault();return!1;case i.UP:e.resolve(v(c));f.preventDefault();return!1;case i.ENTER:var h=a(document.getElementById(c.suggestionsId));if("true"===g.attr("aria-expanded")&&0<h.children("[aria-selected=true]").length){e.resolve(z(b,c,d))}else if(b.tags){e.resolve(y(b,c,d))}else{e.resolve()}f.preventDefault();return!1;case i.ESCAPE:if("true"===g.attr("aria-expanded")){e.resolve(w(c))}else{e.resolve()}f.preventDefault();return!1;}e.resolve();return!0});g.on("keypress",function(a){if(a.keyCode===i.COMMA){if(b.tags){C("keypress-"+a.keyCode).resolve(y(b,c,d))}a.preventDefault();return!1}return!0});g.closest("form").on("submit",function(){if(b.tags){C("form-autocomplete-submit").resolve(y(b,c,d))}return!0});g.on("blur",function(){var e=C("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 y(b,c,d)}).catch()}h.then(function(){return w(c)}).catch()}h.then(function(){return e.resolve()}).catch();h.resolve()},500)});if(b.showSuggestions){var h=a(document.getElementById(c.downArrowId));h.on("click",function(a){var e=C("form-autocomplete-show-suggestions");g.focus();if(!g.val()&&b.ajax){require([b.ajax],function(f){e.resolve(A(a,b,c,d,f))})}else{e.resolve(x(b,c,g.val(),d))}})}var j=a(document.getElementById(c.suggestionsId));j.parent().prop("onclick",null).off("click");j.parent().on("click","#".concat(c.suggestionsId," [role=option]"),function(f){var e=C("form-autocomplete-parent"),g=a(f.currentTarget).closest("[role=option]"),h=a(document.getElementById(c.suggestionsId)),i=h.children(":not([aria-hidden])").index(g);r(i,c).then(function(){return z(b,c,d)}).then(function(){return e.resolve()}).catch()});var k=a(document.getElementById(c.selectionId));k.on("click","[role=option]",function(f){var e=C("form-autocomplete-clicks");e.resolve(q(b,c,a(f.currentTarget),d))});k.on("focus",function(){m(c)});k.on("keydown",function(f){var e=C("form-autocomplete-keydown-"+f.keyCode);switch(f.keyCode){case i.RIGHT:case i.DOWN:f.preventDefault();e.resolve(u(c));return;case i.LEFT:case i.UP:f.preventDefault();e.resolve(t(c));return;case i.SPACE:case i.ENTER:var g=a(document.getElementById(c.selectionId)).children("[data-active-selection]");if(g){f.preventDefault();e.resolve(q(b,c,g,d))}return;}e.resolve()});if(b.showSuggestions){g.on("focus",function(b){var c=a(b.currentTarget).val();a(b.currentTarget).data("last-value",c)});if(b.ajax){require([b.ajax],function(h){var i=null,j=!1,k="autocomplete-throttledhandler",l=function(a){i=null;j=!0;A(a,b,c,d,h).then(function(){if(null===i){M.util.js_complete(k)}j=!1;return arguments[0]}).catch(f.exception)},m=function(a){window.clearTimeout(i);if(j){i=window.setTimeout(m.bind(this,a),100);return}if(null===i){M.util.js_pending(k)}i=window.setTimeout(l.bind(this,a),300)};g.on("input",function(b){var c=a(b.currentTarget).val(),d=a(b.currentTarget).data("last-value");if(d!==c){m(b)}a(b.currentTarget).data("last-value",c)})})}else{g.on("input",function(f){var e=a(f.currentTarget).val(),g=a(f.currentTarget).data("last-value");if(g!==e){x(b,c,e,d)}a(f.currentTarget).data("last-value",e)})}}},C=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,i,k,l,m,o,p,q){var r={selector:e,tags:!1,ajax:!1,placeholder:k,caseSensitive:!1,showSuggestions:!0,noSelectionString:o,templates:a.extend({input:"core/form_autocomplete_input",items:"core/form_autocomplete_selection_items",layout:"core/form_autocomplete_layout",selection:"core/form_autocomplete_selection",suggestions:"core/form_autocomplete_suggestions"},q)},s="autocomplete-setup-"+e;M.util.js_pending(s);if("undefined"!=typeof g){r.tags=g}if("undefined"!=typeof i){r.ajax=i}if("undefined"!=typeof l){r.caseSensitive=l}if("undefined"!=typeof m){r.showSuggestions=m}if("undefined"==typeof o){c.get_string("noselection","form").done(function(a){r.noSelectionString=a}).fail(f.exception)}var t=a(e);if(!t){b.debug("Selector not found: "+e);M.util.js_complete(s);return!1}h.hide(t.get());t.css("visibility","hidden");var u={selectId:t.attr("id"),inputId:"form_autocomplete_input-"+j,suggestionsId:"form_autocomplete_suggestions-"+j,selectionId:"form_autocomplete_selection-"+j,downArrowId:"form_autocomplete_downarrow-"+j,items:[]};j++;r.multiple=t.attr("multiple");if(!r.multiple){t.prepend("<option>")}if("undefined"!=typeof p){r.closeSuggestionsOnSelect=p}else{r.closeSuggestionsOnSelect=!r.multiple}var v=a("[for="+u.selectId+"]"),w=[];t.children("option").each(function(b,c){w[b]={label:c.innerHTML,value:a(c).attr("value")}});var x=a.extend({},r,u);x.options=w;x.items=[];var y="",z=d.render(r.templates.layout,{}).then(function(b){return a(b)}),A=d.render(r.templates.input,x).then(function(b,c){y+=c;return a(b)}),C=d.render(r.templates.suggestions,x).then(function(b,c){y+=c;return a(b)}),D=d.render(r.templates.selection,x).then(function(b,c){y+=c;return a(b)});return a.when(z,A,C,D).then(function(b,c,e,f){t.hide();var g=t.parent();a(c).find("input").attr("data-fieldtype","autocomplete");g.append(b);g.find("[data-region=\"form_autocomplete-input\"]").replaceWith(c);g.find("[data-region=\"form_autocomplete-suggestions\"]").replaceWith(e);g.find("[data-region=\"form_autocomplete-selection\"]").replaceWith(f);d.runTemplateJS(y);v.attr("for",u.inputId);B(r,u,t);var i=a(document.getElementById(u.suggestionsId));i.hide();h.hide(i.get())}).then(function(){return n(r,u,t)}).then(function(){return M.util.js_complete(s)}).catch(function(a){M.util.js_complete(s);f.exception(a)})}}});
2 //# sourceMappingURL=form-autocomplete.min.js.map