Files
moodle/lib/amd/build/form-autocomplete.min.js

1 line
8.2 KiB
JavaScript

define(["jquery","core/log","core/str","core/templates","core/notification"],function(a,b,c,d,e){var f={DOWN:40,ENTER:13,SPACE:32,ESCAPE:27,COMMA:188,UP:38},g=function(b,c){var d=a(document.getElementById(c.selectionId)),e=d.children("[aria-selected=true]").length;for(b%=e;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)},h=function(b,c,f){var h=[],i=a(document.getElementById(c.selectionId)),j=i.attr("aria-activedescendant"),k=!1;j&&(k=a(document.getElementById(j)).attr("data-value")),f.children("option").each(function(b,c){a(c).prop("selected")&&h.push({label:a(c).html(),value:a(c).attr("value")})});var l=a.extend({items:h},b,c);d.render("core/form_autocomplete_selection",l).done(function(b){i.empty().append(a(b).html()),k!==!1&&i.children("[aria-selected=true]").each(function(b,d){a(d).attr("data-value")===k&&g(b,c)})}).fail(e.exception),"undefined"!=typeof M.core_formchangechecker&&M.core_formchangechecker.set_form_changed(),f.change()},i=function(b,c,d,e){var f=a(d).attr("data-value");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())}),h(b,c,e)},j=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;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;e.animate({scrollTop:j},100)},k=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);j(e+1,b)},l=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]");if(!d)return void g(0,b);var e=c.children("[aria-selected=true]").index(d);g(e-1,b)},m=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]");if(!d)return void g(0,b);var e=c.children("[aria-selected=true]").index(d);g(e+1,b)},n=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);j(e-1,b)},o=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)},p=function(b,f,g,h){var i=a(document.getElementById(f.inputId)),k=a(document.getElementById(f.suggestionsId)),l=!1,m=[];h.children("option").each(function(b,c){a(c).prop("selected")!==!0&&(m[m.length]={label:c.innerHTML,value:a(c).attr("value")})});var n=f.caseSensitive?g:g.toLocaleLowerCase(),o=a.extend({options:m},b,f);d.render("core/form_autocomplete_suggestions",o).done(function(d){k.replaceWith(d),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(n)>-1||!b.caseSensitive&&d.text().toLocaleLowerCase().indexOf(n)>-1?(d.show().attr("aria-hidden",!1),l=!0):d.hide().attr("aria-hidden",!0)}),i.attr("aria-expanded",!0),l?b.tags||j(0,f):c.get_string("nosuggestions","form").done(function(a){k.html(a)})}).fail(e.exception)},q=function(b,c,d){var e=a(document.getElementById(c.inputId)),f=e.val(),g=f.split(","),i=!1;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&&(i=!0,a(c).prop("selected",!0))}),!i)){var f=a("<option>");f.append(e),f.attr("value",e),d.append(f),f.prop("selected",!0),f.attr("data-iscustom",!0)}}),h(b,c,d),e.val(""),o(c)},r=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");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)}),h(b,c,d),e.val(""),o(c)},s=function(b,c,d,f,g){var h=a(b.currentTarget).val();g.transport(c.selector,h,function(b){var e=g.processResults(c.selector,b),h=[];if(f.children("option").each(function(b,c){c=a(c),c.prop("selected")?h.push(String(c.attr("value"))):c.remove()}),!c.multiple&&0===f.children("option").length){var i=a("<option>");f.append(i)}a.each(e,function(b,c){if(-1===h.indexOf(String(c.value))){var d=a("<option>");d.append(c.label),d.attr("value",c.value),f.append(d)}}),p(c,d,"",f)},e.exception)},t=function(b,c,d){var e=a(document.getElementById(c.inputId));if(e.on("keydown",function(g){switch(g.keyCode){case f.DOWN:return b.showSuggestions?("true"===e.attr("aria-expanded")?k(c):!e.val()&&b.ajax?require([b.ajax],function(a){s(g,b,c,d,a)}):p(b,c,e.val(),d),g.preventDefault(),!1):!0;case f.COMMA:return b.tags&&q(b,c,d),g.preventDefault(),!1;case f.UP:return n(c),g.preventDefault(),!1;case f.ENTER:var h=a(document.getElementById(c.suggestionsId));return"true"===e.attr("aria-expanded")&&h.children("[aria-selected=true]").length>0?r(b,c,d):b.tags&&q(b,c,d),g.preventDefault(),!1;case f.ESCAPE:return"true"===e.attr("aria-expanded")&&o(c),g.preventDefault(),!1}return!0}),e.on("behat:set-value",function(){var f=a(document.getElementById(c.suggestionsId));"true"===e.attr("aria-expanded")&&f.children("[aria-selected=true]").length>0?r(b,c,d):b.tags&&q(b,c,d)}),e.on("blur",function(){window.setTimeout(function(){var f=a(document.activeElement);f.attr("id")!=e.attr("id")&&(b.tags&&q(b,c,d),o(c))},500)}),b.showSuggestions){var g=a(document.getElementById(c.downArrowId));g.on("click",function(){e.focus(),p(b,c,e.val(),d)})}var h=a(document.getElementById(c.suggestionsId));h.parent().on("click","[role=option]",function(e){var f=a(e.currentTarget).closest("[role=option]"),g=a(document.getElementById(c.suggestionsId)),h=g.children("[aria-hidden=false]").index(f);j(h,c),r(b,c,d)});var t=a(document.getElementById(c.selectionId));t.on("click","[role=listitem]",function(e){var f=a(e.currentTarget);i(b,c,f,d)}),t.on("keydown",function(e){switch(e.keyCode){case f.DOWN:return m(c),e.preventDefault(),!1;case f.UP:return l(c),e.preventDefault(),!1;case f.SPACE:case f.ENTER:var g=a(document.getElementById(c.selectionId)).children("[data-active-selection=true]");return g&&(i(b,c,g,d),e.preventDefault()),!1}return!0}),b.showSuggestions&&e.on("input",function(e){var f=a(e.currentTarget).val(),g=a(e.currentTarget).data("last-value");g!==f&&p(b,c,f,d),a(e.currentTarget).data("last-value",f)})};return{enhance:function(e,f,g,i,j,k,l){var m={selector:e,tags:!1,ajax:!1,placeholder:i,caseSensitive:!1,showSuggestions:!0,noSelectionString:l};"undefined"!=typeof f&&(m.tags=f),"undefined"!=typeof g&&(m.ajax=g),"undefined"!=typeof j&&(m.caseSensitive=j),"undefined"!=typeof k&&(m.showSuggestions=k),"undefined"==typeof l&&(m.noSelectionString=c.get_string("noselection","form"));var n=a(e);if(!n)return b.debug("Selector not found: "+e),!1;n.hide().attr("aria-hidden",!0);var o={selectId:n.attr("id"),inputId:"form_autocomplete_input-"+a.now(),suggestionsId:"form_autocomplete_suggestions-"+a.now(),selectionId:"form_autocomplete_selection-"+a.now(),downArrowId:"form_autocomplete_downarrow-"+a.now()};m.multiple=n.attr("multiple");var p=a("[for="+o.selectId+"]"),q=[];n.children("option").each(function(b,c){q[b]={label:c.innerHTML,value:a(c).attr("value")}});var r=a.extend({},m,o);r.options=q,r.items=[];var u=d.render("core/form_autocomplete_input",r),v=d.render("core/form_autocomplete_suggestions",r),w=d.render("core/form_autocomplete_selection",r);a.when(u,v,w).done(function(b,c,d){n.after(c),n.after(b),n.after(d),p.attr("for",o.inputId),t(m,o,n);var e=a(document.getElementById(o.inputId)),f=a(document.getElementById(o.suggestionsId));f.hide().attr("aria-hidden",!0),m.ajax&&require([m.ajax],function(b){var c=null,d=function(a){s(a,m,o,n,b)},f=function(a){null!==c&&(window.clearTimeout(c),c=null),c=window.setTimeout(d.bind(this,a),300)};e.on("input",f);var g=a(document.getElementById(o.downArrowId));g.on("click",d)}),h(m,o,n)})}}});