-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-datalist.min.js
6 lines (6 loc) · 3.5 KB
/
bootstrap-datalist.min.js
1
2
3
4
5
6
/*!
* bootstrap-datalist 1.00.01
* https://github.com/ckamte/bootstrap-datalist
* Licensed MIT
*/
(function(t,e){"function"==typeof define&&define.amd?define(["jquery"],function(t){return e(t)}):"object"==typeof exports?module.exports=e(require("jquery")):e(jQuery)})(0,function(t){const e=function(){"use strict";function t(){return{css:e()}}function e(){return{top:{borderBottomLeftRadius:"0",borderBottomRightRadius:"0",borderBottomColor:"var(--bs-border-color)",clipPath:"inset(-0.25rem -0.25rem 0 -0.25rem)"},bottom:{borderTopLeftRadius:"0",borderTopRightRadius:"0",borderTop:"none",borderColor:"#86b7fe",outline:"0",boxShadow:"0 0 0 0.25rem rgba(13, 110, 253, 0.25)",clipPath:"inset(0px -0.25rem -0.25rem -0.25rem)",transition:"border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out"}}}return t()}();(function(){"use strict";let o,n={initialize:function(e){o={},o.wrapper=e.parent(".datalist-wrapper");let n=o.wrapper.width();if(o.shown=e,o.wrapper.children(".datalist-value").length<1){let t=e.prop("name");o.shown=e.removeAttr("name list"),o.wrapper.prepend('<input type="hidden" class="datalist-value" name="'+t+'">')}return o.hidden=o.wrapper.children(".datalist-value"),o.datalist=o.wrapper.children("datalist").addClass("dropdown-menu option-list").css("width",n),o.options=o.datalist.find("option").addClass("dropdown-item"),o.options.length>0&&(o.options.removeClass("d-none active"),o.options.each(function(){t(this).text()||t(this).text(t(this).val()),t(this).val()||t(this).val(t(this).text())}),!0)},show:function(t){t.shown.css(e.css.top),t.datalist.addClass("dropdown-menu option-list show").css(e.css.bottom),t.wrapper.parents("form").on("reset",function(){t.hidden.val("")}),n.selected(t),n.mouse(t),n.input(t),n.keydown(t)},hide:function(t){""===t.hidden.val()&&t.shown.val(""),t.datalist.removeClass("show"),t.shown.removeAttr("style")},mouse:function(e){e.options.each(function(){t(this).mouseenter(function(o){o.stopImmediatePropagation(),e.shown.val(t(this).text())}),t(this).mouseleave(function(t){t.stopImmediatePropagation(),""!==e.hidden.val()?n.selected(e):e.shown.val("")}),t(this).mousedown(function(o){o.stopImmediatePropagation(),n.select(e,t(this)),e.shown.trigger("blur")})})},input:function(e){e.shown.on("input",function(o){o.stopImmediatePropagation(),e.hidden.val("");let i=t(this).val();e.options.each(function(){t(this).removeClass("active d-none");let o=t(this).text().toLowerCase();-1===o.indexOf(i.toLowerCase())&&t(this).addClass("d-none"),i===t(this).text()&&n.select(e,t(this))})})},keydown:function(){o.shown.on("keydown",function(t){switch(t.stopImmediatePropagation(),t.keyCode){case 40:n.change(o,"next");break;case 38:n.change(o,"prev");break;case 9:case 13:t.preventDefault(),o.shown.trigger("blur")}})},change:function(e,o){let i=e.datalist.find('option:not(".d-none")'),s=n.active(i),a=i.length;a<1||("next"===o?(s++,s>=a&&(s=0)):(s--,s<0&&(s=a-1)),n.select(e,t(i[s])))},active:function(e){let o=-1;if(e.length>0&&e.hasClass("active")){let n=-1;e.each(function(){n++,t(this).hasClass("active")&&(o=n)})}return o},select:function(t,e){t.hidden.val(e.val()),t.shown.val(e.text()),t.options.removeClass("active"),e.addClass("active")},selected:function(e){let o=e.hidden.val(),i=e.shown.val();""!==o&&""!==i&&e.options.each(function(){t(this).val()===o&&n.select(e,t(this))}),""===o&&""!==i&&e.options.each(function(){t(this).text()===i&&n.select(e,t(this))})}};t.fn.bootstrapDatalist=function(){t(this).on("focus",function(){!0===n.initialize(t(this))&&n.show(o)}),t(this).on("focusout blur",function(){n.hide(o)})}})()});