mobius1-selectr
Advanced tools
Comparing version 1.0.7 to 1.0.8
{ | ||
"name": "mobius1-selectr", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
{ | ||
"name": "mobius1-selectr", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "selectr.min.js", |
@@ -17,2 +17,6 @@ # Selectr | ||
### Roadmap | ||
Selectr started out as a plugin I wrote for personal use to replace Select2 on my sites due to me dumping jQuery. This means that it isn't as streamlined as I'd like it to be for distribution. Therefore, in the coming months I'll be working on vastly improving Selectr. | ||
### Changelog | ||
@@ -19,0 +23,0 @@ |
/*! | ||
* Selectr 1.0.7 | ||
* Selectr 1.0.8 | ||
* http://mobiuswebdesign.co.uk/plugins/selectr | ||
@@ -78,2 +78,6 @@ * | ||
var _isInt = function(value) { | ||
return !isNaN(value) && (function(x) { return (x | 0) === x; })(parseFloat(value)) | ||
}; | ||
/** | ||
@@ -153,3 +157,3 @@ * PLUGIN | ||
_each(data, function(idx, itm) { | ||
let opt = _newElement('option', { value: itm.value }); | ||
var opt = _newElement('option', { value: itm.value }); | ||
opt.textContent = itm.text; | ||
@@ -270,3 +274,3 @@ docFrag.appendChild(opt); | ||
if ( opt.nodeName === 'OPTGROUP' ) { | ||
let group = _newElement('li', { class: 'selectr-optgroup', html: opt.label }); | ||
var group = _newElement('li', { class: 'selectr-optgroup', html: opt.label }); | ||
_append(_this.optsFrag, group); | ||
@@ -509,8 +513,8 @@ | ||
{ | ||
let _this = this; | ||
let opts = this.optsOptions; | ||
let scrollTop = opts.scrollTop; | ||
let scrollHeight = opts.scrollHeight; | ||
let offsetHeight = opts.offsetHeight; | ||
let atBottom = scrollTop >= (scrollHeight - offsetHeight); | ||
var _this = this; | ||
var opts = this.optsOptions; | ||
var scrollTop = opts.scrollTop; | ||
var scrollHeight = opts.scrollHeight; | ||
var offsetHeight = opts.offsetHeight; | ||
var atBottom = scrollTop >= (scrollHeight - offsetHeight); | ||
@@ -523,3 +527,3 @@ if ( atBottom && _this.pageIndex < _this.options.data.length ) { | ||
_each(data, function(i, item) { | ||
let option = _newElement('option', { value: item.value, text: item.text }); | ||
var option = _newElement('option', { value: item.value, text: item.text }); | ||
selectFrag.appendChild(option); | ||
@@ -564,5 +568,5 @@ _this.buildOption(i, option, optsFrag); | ||
_each(_this.opts, function(i, option) { | ||
let opt = _this.list[i]; | ||
let val = option.textContent.trim(); | ||
let val2 = value.trim(); | ||
var opt = _this.list[i]; | ||
var val = option.textContent.trim(); | ||
var val2 = value.trim(); | ||
if ( !val.toLowerCase().includes(val2.toLowerCase()) ) { | ||
@@ -578,3 +582,3 @@ _addClass(opt, 'excluded'); | ||
} else { | ||
let result = new RegExp(val2, 'i').exec(val); | ||
var result = new RegExp(val2, 'i').exec(val); | ||
opt.innerHTML = option.textContent.replace(result[0], '<span>'+result[0]+'</span>'); | ||
@@ -663,3 +667,3 @@ } | ||
let old = _this.optsOptions.getElementsByClassName('selected')[0]; | ||
var old = _this.optsOptions.getElementsByClassName('selected')[0]; | ||
if ( old ) _removeClass(old, 'selected'); | ||
@@ -738,6 +742,6 @@ | ||
_each(parsedItems, function(i, item) { | ||
let result = ajax.formatResults(item) || item.text; | ||
var result = ajax.formatResults(item) || item.text; | ||
// Create the item | ||
let li = _newElement('li', { | ||
var li = _newElement('li', { | ||
class: 'selectr-option', | ||
@@ -750,3 +754,3 @@ 'data-value': item.value, | ||
// Create the option | ||
let opt = _newElement('option', { | ||
var opt = _newElement('option', { | ||
value: item.value | ||
@@ -819,3 +823,3 @@ }); | ||
let old = _this.optsOptions.getElementsByClassName('selected')[0]; | ||
var old = _this.optsOptions.getElementsByClassName('selected')[0]; | ||
if ( old ) _removeClass(old, 'selected'); | ||
@@ -856,4 +860,4 @@ | ||
let tag = _newElement('li', { class: 'selectr-tag', html: content }); | ||
let btn = _newElement('button', { class: 'selectr-tag-remove', type: 'button' }); | ||
var tag = _newElement('li', { class: 'selectr-tag', html: content }); | ||
var btn = _newElement('button', { class: 'selectr-tag-remove', type: 'button' }); | ||
@@ -947,3 +951,3 @@ _append(tag, btn); | ||
_each(this.list, function(i, elem) { | ||
let option = _this.opts[i]; | ||
var option = _this.opts[i]; | ||
elem.innerHTML = _this.customOption ? _this.options.renderOption(option) : option.textContent; | ||
@@ -1056,3 +1060,3 @@ _removeClass(elem, 'match'); | ||
let old = _this.optsOptions.getElementsByClassName('selected')[0]; | ||
var old = _this.optsOptions.getElementsByClassName('selected')[0]; | ||
if ( old ) { | ||
@@ -1094,10 +1098,16 @@ _removeClass(old, 'selected'); | ||
{ | ||
var w = this.options.width || this.elemRect.width; | ||
var w = this.options.width; | ||
if ( this.options.width === 'auto' ) { | ||
w = '100%'; | ||
} else if ( this.options.width.includes('%') ) { | ||
w = this.options.width; | ||
if ( w ) { | ||
if ( _isInt(w) ) { | ||
w += 'px'; | ||
} else { | ||
if ( w === 'auto' ) { | ||
w = '100%'; | ||
} else if ( this.options.width.indexOf('%') > -1 ) { | ||
w = this.options.width; | ||
} | ||
} | ||
} else { | ||
w += 'px'; | ||
w = this.elemRect.width; | ||
} | ||
@@ -1104,0 +1114,0 @@ |
/*! | ||
* Selectr 1.0.7 | ||
* Selectr 1.0.8 | ||
* http://mobiuswebdesign.co.uk/plugins/selectr | ||
@@ -7,2 +7,2 @@ * | ||
*/ | ||
String.prototype.includes||(String.prototype.includes=function(a,b){"use strict";return"number"!=typeof b&&(b=0),!(b+a.length>this.length)&&this.indexOf(a,b)!==-1}),function(a,b){var c="Selectr";"function"==typeof define&&define.amd?define([],b(c)):"object"==typeof exports?module.exports=b(c):a[c]=b(c)}(this,function(a){"use strict";function k(a,c){if(null===a)throw new Error("Selectr requires an element to work.");var d={minChars:1,width:"auto",emptyOption:!0,searchable:!0,selectedIndex:null,selectedValue:null,selectedIndexes:[],selectedValues:[],containerClass:"",maxSelections:null,pagination:0};this.elem=a,this.elemRect=this.elem.getBoundingClientRect(),this.selectedVal=null,this.selectedVals=[],this.ajaxOpts=!1,this.tags=[],this.opts=[],this.values=[],this.list=[],this.lastLen=0,this.disabled=!1,this.opened=!1,this.searching=!1,this.searchList=[],this.activeIdx=0,this.remote=!1,this.options=b(d,c),this.customOption=this.options.hasOwnProperty("renderOption")&&"function"==typeof this.options.renderOption,this.customSelected=this.options.hasOwnProperty("renderSelection")&&"function"==typeof this.options.renderSelection,this.initialise()}var b=function(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},c=function(a,b){var c=document,d=c.createElement(a);if(b&&"object"==typeof b){var e;for(e in b)if("html"===e)d.innerHTML=b[e];else if("text"===e){var f=c.createTextNode(b[e]);d.appendChild(f)}else d.setAttribute(e,b[e])}return d},d=function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a)){var d;for(d in a)Object.prototype.hasOwnProperty.call(a,d)&&b.call(c,d,a[d],a)}else for(var e=0,f=a.length;e<f;e++)b.call(c,e,a[e],a)},e=function(a,b,c){var d;return function(){var e=this,f=arguments,g=function(){d=null,c||a.apply(e,f)},h=c&&!d;clearTimeout(d),d=setTimeout(g,b),h&&a.apply(e,f)}},f=function(a,b){return a.classList?a.classList.contains(b):!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},g=function(a,b){a.classList?a.classList.add(b):f(b)||(a.className=a.className.trim()+" "+b)},h=function(a,b){a.classList?a.classList.remove(b):f(b)&&(a.className=a.className.replace(new RegExp("(^|\\b)"+b.split(" ").join("|")+"(\\b|$)","gi")," "))},i=function(a,b){a.appendChild(b)},j=function(a,b,c,d){a.addEventListener(b,c,d||!1)};return k.prototype={initialise:function(){if(!this.initialised){var a=this;if(this.on=function(b,c){this.elem.addEventListener(b,function(b){c.call(this,b,a)})},a.options.multiple&&(a.elem.setAttribute("multiple",!0),a.elem.multiple=!0),this.options.data){var b=document.createDocumentFragment();this.pageIndex=this.options.pagination;var e=this.options.pagination?this.options.data.slice(0,this.options.pagination):this.options.data;d(e,function(a,d){let e=c("option",{value:d.value});e.textContent=d.text,b.appendChild(e)}),this.elem.appendChild(b)}else a.options.ajax&&"object"==typeof a.options.ajax?a.setAjaxUrl():this.setSelections();this.build(),this.initialised=!0,setTimeout(function(){a.emit("selectr.init")},100)}},setSelections:function(){var a=this;"OPTGROUP"===this.elem.options[0].parentNode.nodeName&&(this.hasOptGroups=!0),this.elem.multiple?(this.options.emptyOption=!1,(this.options.selectedIndexes.length||this.options.selectedValues.length)&&d(this.elem.options,function(b,c){c.selected=!1,(a.options.selectedIndexes.indexOf(b)>-1||a.options.selectedValues.indexOf(c.value)>-1)&&(c.selected=!0)})):null!==this.options.selectedIndex?this.elem.value=this.elem.options[this.options.selectedIndex].value:null!==this.options.selectedValue&&(this.elem.value=this.options.selectedValue,this.selectedVal=this.options.selectedValue)},setAjaxUrl:function(){this.ajaxOpts=!0;var a=this,b=a.options.ajax;a.ajax_url=b.url,b.queryParam&&(a.ajax_url+="?",b.params&&d(b.params,function(b,c){a.ajax_url+=b+"="+c+"&"}),a.ajax_url+=b.queryParam+"="),"function"!=typeof a.options.ajax.formatSelected&&(a.options.ajax.formatSelected=function(a){return a.text})},build:function(){var a=this;this.optsFrag=document.createDocumentFragment(),g(this.elem,"hidden-input"),this.container=c("div",{id:"selectr-"+a.elem.id,class:"selectr-container "+this.options.containerClass}),this.selected=c("div",{class:"selectr-selected"}),this.txt=c(this.elem.multiple?"ul":"span",{class:"selectr-text"}),this.optsContainer=c("div",{class:"selectr-options-container"}),this.optsOptions=c("ul",{class:"selectr-options"}),this.notice=c("div",{class:"selectr-notice"}),this.elem.multiple&&(g(this.txt,"selectr-tags"),g(this.container,"multiple")),this.options.searchable&&(this.input=c("input",{class:"selectr-input"}),this.clear=c("button",{class:"selectr-clear",type:"button"}),this.inputContainer=c("div",{class:"selectr-input-container"})),a.options.ajax||(this.hasOptGroups?(g(this.optsOptions,"optgroups"),d(this.elem.children,function(b,e){if("OPTGROUP"===e.nodeName){let b=c("li",{class:"selectr-optgroup",html:e.label});i(a.optsFrag,b),e.children&&d(e.children,function(b,c){a.buildOption(b,c)})}})):d(this.elem.options,function(b,c){a.options.pagination&&b>=a.options.pagination||a.buildOption(b,c)}),g(this.list[this.activeIdx],"active")),i(this.optsOptions,this.optsFrag),i(this.selected,this.txt),i(this.container,this.selected),this.options.searchable&&(i(this.inputContainer,this.input),i(this.inputContainer,this.clear),i(this.optsContainer,this.inputContainer)),i(this.optsContainer,this.optsOptions),i(this.optsContainer,this.notice),i(this.container,this.optsContainer);var b=this.options.placeholder||this.elem.getAttribute("placeholder")||"Choose...";this.placeElem=c("div",{class:"selectr-placeholder",html:b}),i(this.selected,this.placeElem),(!this.elem.multiple&&this.elem.value.length||this.elem.multiple&&this.txt.children.length)&&g(this.container,"has-selected"),this.ajaxOpts||!this.options.emptyOption||this.elem.multiple||this.elem.options[0].hasAttribute("selected")||this.selectedVal!==this.elem.options[0].value||this.options.selectedValue==this.elem.options[0].value||0==this.options.selectedIndex||(this.emptyOpt=!0,this.txt.innerHTML=null,this.elem.value="",this.elem.options[0].selected=!1,h(this.list[0],"selected"),h(this.container,"has-selected")),this.elem.parentNode.insertBefore(this.container,this.elem),i(this.container,this.elem),this.setDimensions(),this.attachEventListeners()},buildOption:function(a,b,d){if("OPTION"===b.nodeName&&b.value){var e=this.customOption?this.options.renderOption(b):b.textContent.trim(),f=c("li",{class:"selectr-option",html:e});if(b.hasAttribute("selected")&&(b.selected=!0),i(d?d:this.optsFrag,f),b.selected&&(g(f,"selected"),this.elem.multiple?(this.createTag(b),this.selectedVals.push(b.value)):(this.txt.innerHTML=e,this.selectedIndex=a,this.selectedVal=b.value)),b.disabled)return void g(f,"disabled");this.opts.push(b),this.values.push(b.value),this.list.push(f)}},attachEventListeners:function(){var a=this;this.requiresPagination=this.options.data&&this.options.data.length>this.options.pagination,this.handleClickEvents=this.handleEvents.bind(this),this.handleDismiss=this.dismiss.bind(this),this.handleNavigate=this.navigate.bind(this),j(this.container,"click",this.handleClickEvents),j(this.selected,"mousedown",function(a){a.preventDefault()}),j(this.optsOptions,"mousedown",function(a){a.preventDefault()}),this.options.searchable&&(j(this.input,"keyup",this.search.bind(this)),j(this.clear,"click",this.clearOptions.bind(this))),j(document,"click",this.handleDismiss),j(document,"keydown",this.handleNavigate),this.update=e(function(){a.setDimensions()},50),j(window,"resize",this.update),j(window,"scroll",this.update),this.requiresPagination&&(this.paginateItems=e(function(){a.paginate()},50),j(this.optsOptions,"scroll",this.paginateItems))},handleEvents:function(a){a=a||window.event;var b=a.target;b!==this.txt&&b!==this.placeElem||(b=this.placeElem.parentNode),b===this.selected&&this.toggleOptions(),f(b,"selectr-option")&&this.selectOption(a),f(b,"selectr-tag-remove")&&this.removeTags(a),a.preventDefault()},navigate:function(a){a=a||window.event;var b=this,c=a.keyCode,d=[13,38,40];if(b.opened&&!(d.indexOf(c)<0)){a.preventDefault();var f,e=this.searching?this.searchList:this.list;switch(c){case 13:return void b.selectOption(a);case 38:f="up",b.activeIdx>0&&b.activeIdx--;break;case 40:f="down",b.activeIdx<e.length-1&&b.activeIdx++}var i=e[b.activeIdx],j=i.getBoundingClientRect(),k=b.optsOptions.scrollTop,l=window.scrollY||window.pageYOffset,m=b.optsRect.top+l;if("up"===f){var n=m,o=j.top+l,p=k+(o-n);0===b.activeIdx?b.optsOptions.scrollTop=0:o-n<0&&(b.optsOptions.scrollTop=p)}else{var n=m+b.optsRect.height,q=j.top+l+j.height,p=k+q-n;0===b.activeIdx?b.optsOptions.scrollTop=0:q>n&&(b.optsOptions.scrollTop=p),console.log(b.activeIdx),b.requiresPagination&&b.paginate()}h(b.optsOptions.getElementsByClassName("active")[0],"active"),g(e[b.activeIdx],"active")}},paginate:function(){let a=this,b=this.optsOptions,e=b.scrollTop,f=b.scrollHeight,g=b.offsetHeight,h=e>=f-g;if(h&&a.pageIndex<a.options.data.length){var i=document.createDocumentFragment(),j=document.createDocumentFragment(),k=a.options.data.slice(a.pageIndex,a.options.pagination+a.pageIndex);d(k,function(b,d){let e=c("option",{value:d.value,text:d.text});i.appendChild(e),a.buildOption(b,e,j)}),a.elem.appendChild(i),b.appendChild(j),a.pageIndex+=a.options.pagination,a.emit("selectr.paginate")}},search:function(a){var b=this,c=b.input.value,e=c.length,f=38===a.keyCode||40===a.keyCode;if(!(e<this.options.minChars&&e>=this.lastLen||f)){if(this.ajaxOpts)return void this.ajaxSearch();this.container.classList.contains("notice")||(c.length>0?g(this.inputContainer,"active"):h(this.inputContainer,"active")),b.searching=!0,b.searchList=[],d(b.opts,function(a,d){let e=b.list[a],f=d.textContent.trim(),i=c.trim();if(f.toLowerCase().includes(i.toLowerCase())){if(b.searchList.push(e),b.customOption)g(e,"match");else{let a=new RegExp(i,"i").exec(f);e.innerHTML=d.textContent.replace(a[0],"<span>"+a[0]+"</span>")}h(e,"excluded")}else g(e,"excluded"),h(e,"match")}),b.searchList.length?this.open():(b.notify("No results."),this.input.focus()),this.lastLen=this.input.value.length}},selectOption:function(a){var b=this,c=a.target,e=this.searching?this.searchList:this.list;if("keydown"===a.type&&(c=e[b.activeIdx]),"LI"===c.nodeName&&!c.classList.contains("disabled")){if(b.ajaxOpts)return void b.selectRemoteOption(c);var f=b.list.indexOf(c),i=b.opts[f],j=!1;if(b.elem.multiple){if(c.classList.contains("selected")){var k;d(b.tags,function(a,b){b.getAttribute("data-value")===i.value&&(k=b)}),k&&b.removeTag(k)}else{if(null!==b.options.maxSelections&&b.selectedVals.length>=b.options.maxSelections)return void b.notify("A maximum of "+b.options.maxSelections+" items can be selected.");b.selectedVals.push(i.value),b.createTag(i),i.selected=!0,g(c,"selected"),b.emit("selectr.select"),b.input.value=""}b.txt.children.length&&(j=!0)}else if(b.selectedIndex!==f||this.emptyOpt){this.emptyOpt=!1;let a=b.optsOptions.getElementsByClassName("selected")[0];a&&h(a,"selected"),b.txt.innerHTML=b.customSelected?b.options.renderSelection(i):i.textContent,i.selected=!0,g(c,"selected"),b.selectedVal=i.value,b.selectedIndex=f,b.emit("selectr.select"),b.activeIdx=f,j=!0}else b.txt.innerHTML="",i.selected=!1,h(c,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");j?g(b.container,"has-selected"):h(b.container,"has-selected"),b.reset(),b.elem.multiple||b.close(),b.emit("selectr.change")}},ajaxSearch:function(){function f(e){var f=document.createDocumentFragment(),g=document.createDocumentFragment();b.searchList=[],b.opts=[],d(e,function(d,e){let h=a.formatResults(e)||e.text,j=c("li",{class:"selectr-option","data-value":e.value,"data-text":e.text||"",html:h}),k=c("option",{value:e.value});k.textContent=e.text,b.searchList.push(j),b.opts.push(k),i(f,j),i(g,k)}),b.optsOptions.innerHTML="",i(b.optsOptions,f),b.elem.innerHTML="",i(b.elem,g)}this.searching=!0,g(this.inputContainer,"loading");var a=this.options.ajax,b=this,e=new XMLHttpRequest;e.onload=function(){if(4===e.readyState&&200===e.status){var c=JSON.parse(e.responseText);if("function"!=typeof a.parseResults)return;var d=a.parseResults(c);f(d),h(b.inputContainer,"loading"),b.remoteItems=d}},e.open("GET",b.ajax_url+b.input.value,!0),e.send()},selectRemoteOption:function(a){var c,e,f,b=this,i=a.getAttribute("data-value");if(d(b.remoteItems,function(a,d){d.value==i&&(e=d,f=a,c=b.opts[a])}),this.elem.multiple)if(a.classList.contains("selected")&&b.selectedVals.indexOf(i)>-1){var j;d(b.tags,function(a,b){b.getAttribute("data-value")===i&&(j=b)}),j&&b.removeTag(j)}else b.createTag(a,e),b.selectedVals.push(i),b.opts[f].selected=!0,b.emit("selectr.select"),g(a,"selected");else if(b.selectedIndex===f)b.txt.innerHTML="",c.selected=!1,h(a,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");else{let d=b.optsOptions.getElementsByClassName("selected")[0];d&&h(d,"selected"),b.txt.innerHTML=this.options.ajax.formatSelected(e)||c.getAttribute("data-text")||c.textContent,c.selected=!0,g(a,"selected"),b.selectedVal=c.value,b.selectedIndex=f,b.emit("selectr.select")}this.elem.value?g(this.container,"has-selected"):h(this.container,"has-selected"),this.emit("selectr.select"),this.emit("selectr.change"),this.close()},createTag:function(a,b){var f,e=document.createDocumentFragment();f=b?this.options.ajax.formatSelected(b)||a.getAttribute("data-text")||a.textContent:this.customSelected?this.options.renderSelection(a):a.textContent;let j=c("li",{class:"selectr-tag",html:f}),k=c("button",{class:"selectr-tag-remove",type:"button"});i(j,k),i(e,j),i(this.txt,e),this.tags.push(j),this.txt.children.length?g(this.container,"has-selected"):h(this.container,"has-selected"),b?j.setAttribute("data-value",a.getAttribute("data-value")):j.setAttribute("data-value",a.value)},removeTags:function(a){this.disabled||(a.preventDefault(),a.stopPropagation(),this.removeTag(a.target.parentNode))},removeTag:function(a){var e,f,b=this,c=a.getAttribute("data-value");d(this.opts,function(a,d){d.value==c&&(f=a,e=b.opts[f])}),e.selected=!1,h(this.list[f],"selected"),this.selectedVals.splice(this.selectedVals.indexOf(e.value),1),this.tags.splice(this.tags.indexOf(a),1),this.txt.removeChild(a),this.tags.length||h(this.container,"has-selected"),this.emit("selectr.deselect")},toggleOptions:function(){var b=this.container.classList.contains("open");this.disabled||(b?this.close():this.open())},clearOptions:function(){this.options.searchable&&(this.input.value=null,this.searching=!1,h(this.inputContainer,"active"),h(this.container,"notice"),g(this.container,"open"),this.input.focus()),this.reset()},reset:function(){var a=this;d(this.list,function(b,c){let d=a.opts[b];c.innerHTML=a.customOption?a.options.renderOption(d):d.textContent,h(c,"match"),h(c,"excluded")})},open:function(){var a=this,b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;b>c?g(this.container,"inverted"):h(this.container,"inverted"),g(this.container,"open"),h(this.container,"notice"),this.options.searchable&&setTimeout(function(){a.input.focus()},10),this.optsRect=this.optsOptions.getBoundingClientRect(),this.opened=!0,this.ajaxOpts&&(this.searching=!0),this.emit("selectr.open")},close:function(){var a=this.container.classList.contains("notice");this.options.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(this.container.classList.remove("notice"),this.notice.textContent=""),h(this.container,"open"),this.opened=!1,this.emit("selectr.close")},dismiss:function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},notify:function(a){this.close(),this.container.classList.add("notice"),this.notice.textContent=a},setValue:function(a){var c,b=this;if(Array.isArray(a))return void(b.elem.multiple&&d(a,function(a,d){d=d.toString(),c=[].slice.call(b.values).indexOf(d),c>-1&&!b.hasSelectedValue(d)&&(b.createTag(b.opts[c]),b.updateValues(c,d,!0))}));if(a=a.toString(),c=[].slice.call(b.values).indexOf(a),!(c<0))if(b.elem.multiple)b.hasSelectedValue(a)||(b.createTag(b.opts[c]),b.updateValues(c,a,!0));else{b.txt.innerHTML=b.customOption?b.options.renderOption(b.opts[c]):b.opts[c].textContent;let d=b.optsOptions.getElementsByClassName("selected")[0];d&&h(d,"selected"),b.updateValues(c,a)}},updateValues:function(a,b,c){c?this.selectedVals.push(b):this.selectedVal=b,g(this.list[a],"selected"),g(this.container,"has-selected"),this.opts[a].selected=!0},hasSelectedValue:function(a){return this.selectedVals.indexOf(a)>-1},getValue:function(){return this.elem.multiple?this.selectedVals:this.selectedVal},setDimensions:function(){var a=this.options.width||this.elemRect.width;if("auto"===this.options.width?a="100%":this.options.width.includes("%")?a=this.options.width:a+="px",this.opened){this.elemRect=this.elem.getBoundingClientRect();var b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;this.close(),b>c?g(this.container,"inverted"):h(this.container,"inverted")}this.container.style.cssText+="width: "+a+"; "},emit:function(a){this.elem.dispatchEvent(new Event(a))},enable:function(){this.disabled=!1,h(this.container,"disabled")},disable:function(){this.disabled=!0,g(this.container,"disabled")},destroy:function(){if(this.initialised){var a=this,b=a.container.parentNode;b.insertBefore(a.elem,a.container),b.removeChild(a.container),h(a.elem,"hidden-input"),a.container=null,a.selected=null,a.txt=null,a.optsContainer=null,a.optsOptions=null,a.input=null,a.clear=null,a.inputContainer=null,window.removeEventListener("resize",a.resize),document.removeEventListener("click",a.handleDismiss),document.removeEventListener("keydown",a.handleNavigate),a.initialised=!1}}},k}); | ||
String.prototype.includes||(String.prototype.includes=function(a,b){"use strict";return"number"!=typeof b&&(b=0),!(b+a.length>this.length)&&this.indexOf(a,b)!==-1}),function(a,b){var c="Selectr";"function"==typeof define&&define.amd?define([],b(c)):"object"==typeof exports?module.exports=b(c):a[c]=b(c)}(this,function(a){"use strict";function l(a,c){if(null===a)throw new Error("Selectr requires an element to work.");var d={minChars:1,width:"auto",emptyOption:!0,searchable:!0,selectedIndex:null,selectedValue:null,selectedIndexes:[],selectedValues:[],containerClass:"",maxSelections:null,pagination:0};this.elem=a,this.elemRect=this.elem.getBoundingClientRect(),this.selectedVal=null,this.selectedVals=[],this.ajaxOpts=!1,this.tags=[],this.opts=[],this.values=[],this.list=[],this.lastLen=0,this.disabled=!1,this.opened=!1,this.searching=!1,this.searchList=[],this.activeIdx=0,this.remote=!1,this.options=b(d,c),this.customOption=this.options.hasOwnProperty("renderOption")&&"function"==typeof this.options.renderOption,this.customSelected=this.options.hasOwnProperty("renderSelection")&&"function"==typeof this.options.renderSelection,this.initialise()}var b=function(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},c=function(a,b){var c=document,d=c.createElement(a);if(b&&"object"==typeof b){var e;for(e in b)if("html"===e)d.innerHTML=b[e];else if("text"===e){var f=c.createTextNode(b[e]);d.appendChild(f)}else d.setAttribute(e,b[e])}return d},d=function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a)){var d;for(d in a)Object.prototype.hasOwnProperty.call(a,d)&&b.call(c,d,a[d],a)}else for(var e=0,f=a.length;e<f;e++)b.call(c,e,a[e],a)},e=function(a,b,c){var d;return function(){var e=this,f=arguments,g=function(){d=null,c||a.apply(e,f)},h=c&&!d;clearTimeout(d),d=setTimeout(g,b),h&&a.apply(e,f)}},f=function(a,b){return a.classList?a.classList.contains(b):!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},g=function(a,b){a.classList?a.classList.add(b):f(b)||(a.className=a.className.trim()+" "+b)},h=function(a,b){a.classList?a.classList.remove(b):f(b)&&(a.className=a.className.replace(new RegExp("(^|\\b)"+b.split(" ").join("|")+"(\\b|$)","gi")," "))},i=function(a,b){a.appendChild(b)},j=function(a,b,c,d){a.addEventListener(b,c,d||!1)},k=function(a){return!isNaN(a)&&function(a){return(0|a)===a}(parseFloat(a))};return l.prototype={initialise:function(){if(!this.initialised){var a=this;if(this.on=function(b,c){this.elem.addEventListener(b,function(b){c.call(this,b,a)})},a.options.multiple&&(a.elem.setAttribute("multiple",!0),a.elem.multiple=!0),this.options.data){var b=document.createDocumentFragment();this.pageIndex=this.options.pagination;var e=this.options.pagination?this.options.data.slice(0,this.options.pagination):this.options.data;d(e,function(a,d){var e=c("option",{value:d.value});e.textContent=d.text,b.appendChild(e)}),this.elem.appendChild(b)}else a.options.ajax&&"object"==typeof a.options.ajax?a.setAjaxUrl():this.setSelections();this.build(),this.initialised=!0,setTimeout(function(){a.emit("selectr.init")},100)}},setSelections:function(){var a=this;"OPTGROUP"===this.elem.options[0].parentNode.nodeName&&(this.hasOptGroups=!0),this.elem.multiple?(this.options.emptyOption=!1,(this.options.selectedIndexes.length||this.options.selectedValues.length)&&d(this.elem.options,function(b,c){c.selected=!1,(a.options.selectedIndexes.indexOf(b)>-1||a.options.selectedValues.indexOf(c.value)>-1)&&(c.selected=!0)})):null!==this.options.selectedIndex?this.elem.value=this.elem.options[this.options.selectedIndex].value:null!==this.options.selectedValue&&(this.elem.value=this.options.selectedValue,this.selectedVal=this.options.selectedValue)},setAjaxUrl:function(){this.ajaxOpts=!0;var a=this,b=a.options.ajax;a.ajax_url=b.url,b.queryParam&&(a.ajax_url+="?",b.params&&d(b.params,function(b,c){a.ajax_url+=b+"="+c+"&"}),a.ajax_url+=b.queryParam+"="),"function"!=typeof a.options.ajax.formatSelected&&(a.options.ajax.formatSelected=function(a){return a.text})},build:function(){var a=this;this.optsFrag=document.createDocumentFragment(),g(this.elem,"hidden-input"),this.container=c("div",{id:"selectr-"+a.elem.id,class:"selectr-container "+this.options.containerClass}),this.selected=c("div",{class:"selectr-selected"}),this.txt=c(this.elem.multiple?"ul":"span",{class:"selectr-text"}),this.optsContainer=c("div",{class:"selectr-options-container"}),this.optsOptions=c("ul",{class:"selectr-options"}),this.notice=c("div",{class:"selectr-notice"}),this.elem.multiple&&(g(this.txt,"selectr-tags"),g(this.container,"multiple")),this.options.searchable&&(this.input=c("input",{class:"selectr-input"}),this.clear=c("button",{class:"selectr-clear",type:"button"}),this.inputContainer=c("div",{class:"selectr-input-container"})),a.options.ajax||(this.hasOptGroups?(g(this.optsOptions,"optgroups"),d(this.elem.children,function(b,e){if("OPTGROUP"===e.nodeName){var f=c("li",{class:"selectr-optgroup",html:e.label});i(a.optsFrag,f),e.children&&d(e.children,function(b,c){a.buildOption(b,c)})}})):d(this.elem.options,function(b,c){a.options.pagination&&b>=a.options.pagination||a.buildOption(b,c)}),g(this.list[this.activeIdx],"active")),i(this.optsOptions,this.optsFrag),i(this.selected,this.txt),i(this.container,this.selected),this.options.searchable&&(i(this.inputContainer,this.input),i(this.inputContainer,this.clear),i(this.optsContainer,this.inputContainer)),i(this.optsContainer,this.optsOptions),i(this.optsContainer,this.notice),i(this.container,this.optsContainer);var b=this.options.placeholder||this.elem.getAttribute("placeholder")||"Choose...";this.placeElem=c("div",{class:"selectr-placeholder",html:b}),i(this.selected,this.placeElem),(!this.elem.multiple&&this.elem.value.length||this.elem.multiple&&this.txt.children.length)&&g(this.container,"has-selected"),this.ajaxOpts||!this.options.emptyOption||this.elem.multiple||this.elem.options[0].hasAttribute("selected")||this.selectedVal!==this.elem.options[0].value||this.options.selectedValue==this.elem.options[0].value||0==this.options.selectedIndex||(this.emptyOpt=!0,this.txt.innerHTML=null,this.elem.value="",this.elem.options[0].selected=!1,h(this.list[0],"selected"),h(this.container,"has-selected")),this.elem.parentNode.insertBefore(this.container,this.elem),i(this.container,this.elem),this.setDimensions(),this.attachEventListeners()},buildOption:function(a,b,d){if("OPTION"===b.nodeName&&b.value){var e=this.customOption?this.options.renderOption(b):b.textContent.trim(),f=c("li",{class:"selectr-option",html:e});if(b.hasAttribute("selected")&&(b.selected=!0),i(d?d:this.optsFrag,f),b.selected&&(g(f,"selected"),this.elem.multiple?(this.createTag(b),this.selectedVals.push(b.value)):(this.txt.innerHTML=e,this.selectedIndex=a,this.selectedVal=b.value)),b.disabled)return void g(f,"disabled");this.opts.push(b),this.values.push(b.value),this.list.push(f)}},attachEventListeners:function(){var a=this;this.requiresPagination=this.options.data&&this.options.data.length>this.options.pagination,this.handleClickEvents=this.handleEvents.bind(this),this.handleDismiss=this.dismiss.bind(this),this.handleNavigate=this.navigate.bind(this),j(this.container,"click",this.handleClickEvents),j(this.selected,"mousedown",function(a){a.preventDefault()}),j(this.optsOptions,"mousedown",function(a){a.preventDefault()}),this.options.searchable&&(j(this.input,"keyup",this.search.bind(this)),j(this.clear,"click",this.clearOptions.bind(this))),j(document,"click",this.handleDismiss),j(document,"keydown",this.handleNavigate),this.update=e(function(){a.setDimensions()},50),j(window,"resize",this.update),j(window,"scroll",this.update),this.requiresPagination&&(this.paginateItems=e(function(){a.paginate()},50),j(this.optsOptions,"scroll",this.paginateItems))},handleEvents:function(a){a=a||window.event;var b=a.target;b!==this.txt&&b!==this.placeElem||(b=this.placeElem.parentNode),b===this.selected&&this.toggleOptions(),f(b,"selectr-option")&&this.selectOption(a),f(b,"selectr-tag-remove")&&this.removeTags(a),a.preventDefault()},navigate:function(a){a=a||window.event;var b=this,c=a.keyCode,d=[13,38,40];if(b.opened&&!(d.indexOf(c)<0)){a.preventDefault();var f,e=this.searching?this.searchList:this.list;switch(c){case 13:return void b.selectOption(a);case 38:f="up",b.activeIdx>0&&b.activeIdx--;break;case 40:f="down",b.activeIdx<e.length-1&&b.activeIdx++}var i=e[b.activeIdx],j=i.getBoundingClientRect(),k=b.optsOptions.scrollTop,l=window.scrollY||window.pageYOffset,m=b.optsRect.top+l;if("up"===f){var n=m,o=j.top+l,p=k+(o-n);0===b.activeIdx?b.optsOptions.scrollTop=0:o-n<0&&(b.optsOptions.scrollTop=p)}else{var n=m+b.optsRect.height,q=j.top+l+j.height,p=k+q-n;0===b.activeIdx?b.optsOptions.scrollTop=0:q>n&&(b.optsOptions.scrollTop=p),console.log(b.activeIdx),b.requiresPagination&&b.paginate()}h(b.optsOptions.getElementsByClassName("active")[0],"active"),g(e[b.activeIdx],"active")}},paginate:function(){var a=this,b=this.optsOptions,e=b.scrollTop,f=b.scrollHeight,g=b.offsetHeight,h=e>=f-g;if(h&&a.pageIndex<a.options.data.length){var i=document.createDocumentFragment(),j=document.createDocumentFragment(),k=a.options.data.slice(a.pageIndex,a.options.pagination+a.pageIndex);d(k,function(b,d){var e=c("option",{value:d.value,text:d.text});i.appendChild(e),a.buildOption(b,e,j)}),a.elem.appendChild(i),b.appendChild(j),a.pageIndex+=a.options.pagination,a.emit("selectr.paginate")}},search:function(a){var b=this,c=b.input.value,e=c.length,f=38===a.keyCode||40===a.keyCode;if(!(e<this.options.minChars&&e>=this.lastLen||f)){if(this.ajaxOpts)return void this.ajaxSearch();this.container.classList.contains("notice")||(c.length>0?g(this.inputContainer,"active"):h(this.inputContainer,"active")),b.searching=!0,b.searchList=[],d(b.opts,function(a,d){var e=b.list[a],f=d.textContent.trim(),i=c.trim();if(f.toLowerCase().includes(i.toLowerCase())){if(b.searchList.push(e),b.customOption)g(e,"match");else{var j=new RegExp(i,"i").exec(f);e.innerHTML=d.textContent.replace(j[0],"<span>"+j[0]+"</span>")}h(e,"excluded")}else g(e,"excluded"),h(e,"match")}),b.searchList.length?this.open():(b.notify("No results."),this.input.focus()),this.lastLen=this.input.value.length}},selectOption:function(a){var b=this,c=a.target,e=this.searching?this.searchList:this.list;if("keydown"===a.type&&(c=e[b.activeIdx]),"LI"===c.nodeName&&!c.classList.contains("disabled")){if(b.ajaxOpts)return void b.selectRemoteOption(c);var f=b.list.indexOf(c),i=b.opts[f],j=!1;if(b.elem.multiple){if(c.classList.contains("selected")){var k;d(b.tags,function(a,b){b.getAttribute("data-value")===i.value&&(k=b)}),k&&b.removeTag(k)}else{if(null!==b.options.maxSelections&&b.selectedVals.length>=b.options.maxSelections)return void b.notify("A maximum of "+b.options.maxSelections+" items can be selected.");b.selectedVals.push(i.value),b.createTag(i),i.selected=!0,g(c,"selected"),b.emit("selectr.select"),b.input.value=""}b.txt.children.length&&(j=!0)}else if(b.selectedIndex!==f||this.emptyOpt){this.emptyOpt=!1;var l=b.optsOptions.getElementsByClassName("selected")[0];l&&h(l,"selected"),b.txt.innerHTML=b.customSelected?b.options.renderSelection(i):i.textContent,i.selected=!0,g(c,"selected"),b.selectedVal=i.value,b.selectedIndex=f,b.emit("selectr.select"),b.activeIdx=f,j=!0}else b.txt.innerHTML="",i.selected=!1,h(c,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");j?g(b.container,"has-selected"):h(b.container,"has-selected"),b.reset(),b.elem.multiple||b.close(),b.emit("selectr.change")}},ajaxSearch:function(){function f(e){var f=document.createDocumentFragment(),g=document.createDocumentFragment();b.searchList=[],b.opts=[],d(e,function(d,e){var h=a.formatResults(e)||e.text,j=c("li",{class:"selectr-option","data-value":e.value,"data-text":e.text||"",html:h}),k=c("option",{value:e.value});k.textContent=e.text,b.searchList.push(j),b.opts.push(k),i(f,j),i(g,k)}),b.optsOptions.innerHTML="",i(b.optsOptions,f),b.elem.innerHTML="",i(b.elem,g)}this.searching=!0,g(this.inputContainer,"loading");var a=this.options.ajax,b=this,e=new XMLHttpRequest;e.onload=function(){if(4===e.readyState&&200===e.status){var c=JSON.parse(e.responseText);if("function"!=typeof a.parseResults)return;var d=a.parseResults(c);f(d),h(b.inputContainer,"loading"),b.remoteItems=d}},e.open("GET",b.ajax_url+b.input.value,!0),e.send()},selectRemoteOption:function(a){var c,e,f,b=this,i=a.getAttribute("data-value");if(d(b.remoteItems,function(a,d){d.value==i&&(e=d,f=a,c=b.opts[a])}),this.elem.multiple)if(a.classList.contains("selected")&&b.selectedVals.indexOf(i)>-1){var j;d(b.tags,function(a,b){b.getAttribute("data-value")===i&&(j=b)}),j&&b.removeTag(j)}else b.createTag(a,e),b.selectedVals.push(i),b.opts[f].selected=!0,b.emit("selectr.select"),g(a,"selected");else if(b.selectedIndex===f)b.txt.innerHTML="",c.selected=!1,h(a,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");else{var k=b.optsOptions.getElementsByClassName("selected")[0];k&&h(k,"selected"),b.txt.innerHTML=this.options.ajax.formatSelected(e)||c.getAttribute("data-text")||c.textContent,c.selected=!0,g(a,"selected"),b.selectedVal=c.value,b.selectedIndex=f,b.emit("selectr.select")}this.elem.value?g(this.container,"has-selected"):h(this.container,"has-selected"),this.emit("selectr.select"),this.emit("selectr.change"),this.close()},createTag:function(a,b){var f,e=document.createDocumentFragment();f=b?this.options.ajax.formatSelected(b)||a.getAttribute("data-text")||a.textContent:this.customSelected?this.options.renderSelection(a):a.textContent;var j=c("li",{class:"selectr-tag",html:f}),k=c("button",{class:"selectr-tag-remove",type:"button"});i(j,k),i(e,j),i(this.txt,e),this.tags.push(j),this.txt.children.length?g(this.container,"has-selected"):h(this.container,"has-selected"),b?j.setAttribute("data-value",a.getAttribute("data-value")):j.setAttribute("data-value",a.value)},removeTags:function(a){this.disabled||(a.preventDefault(),a.stopPropagation(),this.removeTag(a.target.parentNode))},removeTag:function(a){var e,f,b=this,c=a.getAttribute("data-value");d(this.opts,function(a,d){d.value==c&&(f=a,e=b.opts[f])}),e.selected=!1,h(this.list[f],"selected"),this.selectedVals.splice(this.selectedVals.indexOf(e.value),1),this.tags.splice(this.tags.indexOf(a),1),this.txt.removeChild(a),this.tags.length||h(this.container,"has-selected"),this.emit("selectr.deselect")},toggleOptions:function(){var b=this.container.classList.contains("open");this.disabled||(b?this.close():this.open())},clearOptions:function(){this.options.searchable&&(this.input.value=null,this.searching=!1,h(this.inputContainer,"active"),h(this.container,"notice"),g(this.container,"open"),this.input.focus()),this.reset()},reset:function(){var a=this;d(this.list,function(b,c){var d=a.opts[b];c.innerHTML=a.customOption?a.options.renderOption(d):d.textContent,h(c,"match"),h(c,"excluded")})},open:function(){var a=this,b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;b>c?g(this.container,"inverted"):h(this.container,"inverted"),g(this.container,"open"),h(this.container,"notice"),this.options.searchable&&setTimeout(function(){a.input.focus()},10),this.optsRect=this.optsOptions.getBoundingClientRect(),this.opened=!0,this.ajaxOpts&&(this.searching=!0),this.emit("selectr.open")},close:function(){var a=this.container.classList.contains("notice");this.options.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(this.container.classList.remove("notice"),this.notice.textContent=""),h(this.container,"open"),this.opened=!1,this.emit("selectr.close")},dismiss:function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},notify:function(a){this.close(),this.container.classList.add("notice"),this.notice.textContent=a},setValue:function(a){var c,b=this;if(Array.isArray(a))return void(b.elem.multiple&&d(a,function(a,d){d=d.toString(),c=[].slice.call(b.values).indexOf(d),c>-1&&!b.hasSelectedValue(d)&&(b.createTag(b.opts[c]),b.updateValues(c,d,!0))}));if(a=a.toString(),c=[].slice.call(b.values).indexOf(a),!(c<0))if(b.elem.multiple)b.hasSelectedValue(a)||(b.createTag(b.opts[c]),b.updateValues(c,a,!0));else{b.txt.innerHTML=b.customOption?b.options.renderOption(b.opts[c]):b.opts[c].textContent;var e=b.optsOptions.getElementsByClassName("selected")[0];e&&h(e,"selected"),b.updateValues(c,a)}},updateValues:function(a,b,c){c?this.selectedVals.push(b):this.selectedVal=b,g(this.list[a],"selected"),g(this.container,"has-selected"),this.opts[a].selected=!0},hasSelectedValue:function(a){return this.selectedVals.indexOf(a)>-1},getValue:function(){return this.elem.multiple?this.selectedVals:this.selectedVal},setDimensions:function(){var a=this.options.width;if(a?k(a)?a+="px":"auto"===a?a="100%":this.options.width.indexOf("%")>-1&&(a=this.options.width):a=this.elemRect.width,this.opened){this.elemRect=this.elem.getBoundingClientRect();var b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;this.close(),b>c?g(this.container,"inverted"):h(this.container,"inverted")}this.container.style.cssText+="width: "+a+"; "},emit:function(a){this.elem.dispatchEvent(new Event(a))},enable:function(){this.disabled=!1,h(this.container,"disabled")},disable:function(){this.disabled=!0,g(this.container,"disabled")},destroy:function(){if(this.initialised){var a=this,b=a.container.parentNode;b.insertBefore(a.elem,a.container),b.removeChild(a.container),h(a.elem,"hidden-input"),a.container=null,a.selected=null,a.txt=null,a.optsContainer=null,a.optsOptions=null,a.input=null,a.clear=null,a.inputContainer=null,window.removeEventListener("resize",a.resize),document.removeEventListener("click",a.handleDismiss),document.removeEventListener("keydown",a.handleNavigate),a.initialised=!1}}},l}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61957
1238
37