mobius1-selectr
Advanced tools
Comparing version 2.0.3 to 2.1.0
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.0.3 | ||
* Selectr 2.1.0 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -7,2 +7,2 @@ * | ||
*/ | ||
!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 q(a,c){var d={width:"auto",searchable:!0,clearable:!1,sortSelected:!1};if(null===a)throw new Error("Selectr requires an element to work.");if(this.el=a,"string"==typeof this.el&&(this.el=document.querySelector(a)),null===this.el)throw new Error("The element you passed to Selectr can not be found.");if("select"!==this.el.nodeName.toLowerCase())throw new Error("The element you passed to Selectr is not a HTMLSelectElement.");if(!this.el.options.length&&!c.data)throw new Error("You don't have any options in your select!");this.settings=b.extend(d,c),p.mixin(this),this.settings.multiple&&(this.el.multiple=!0),this.multiple="select-multiple"==this.el.type,this.hasOptGroups=!1,this.el.getElementsByTagName("optgroup").length&&(this.hasOptGroups=!0),this.items=[],this.list=[],this.activeIdx=0,this.navigating=!1,this.elRect=this.el.getBoundingClientRect(),e.call(this),this.update();var f=this;setTimeout(function(){f.emit("selectr.init")},20)}var b={extend:function(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},each:function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a))for(var 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)},createElement: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},hasClass:function(a,b){return a.classList?a.classList.contains(b):!!a.className&&!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},addClass:function(a,c){b.hasClass(a,c)||(a.classList?a.classList.add(c):a.className=a.className.trim()+" "+c)},removeClass:function(a,c){b.hasClass(a,c)&&(a.classList?a.classList.remove(c):a.className=a.className.replace(new RegExp("(^|\\s)"+c.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(a,c){return a&&a!==document.body&&(c(a)?a:b.closest(a.parentNode,c))},append:function(a,b){return a&&b&&a.appendChild(b)},listen:function(a,b,c,d){a.addEventListener(b,c,d||!1)},isObject:function(a){return"[object Object]"===Object.prototype.toString.call(a)},isArray:function(a){return"[object Array]"===Object.prototype.toString.call(a)},isInt:function(a){return!isNaN(a)&&function(a){return(a||0)===a}(parseFloat(a))},debounce: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)}},getBoundingRect:function(a){var b=window,c=document,d=c.body,e=a.getBoundingClientRect(),f=void 0!==b.pageXOffset?b.pageXOffset:(c.documentElement||d.parentNode||d).scrollLeft,g=void 0!==b.pageYOffset?b.pageYOffset:(c.documentElement||d.parentNode||d).scrollTop;return{bottom:e.bottom+g,height:e.height,left:e.left+f,right:e.right+f,top:e.top+g,width:e.width}},preventDefault:function(a){if(a=a||window.event,a.preventDefault)return a.preventDefault()},includes:function(a,b){return a.indexOf(b)>-1}},c=function(){var a=this.settings,c=this.multiple;b.each(this.el.options,function(d,e){(c&&a.selectedValue&&b.includes(a.selectedValue,e.value)||!c&&a.selectedValue&&e.value==a.selectedValue)&&(e.defaultSelected=!0)})},d=function(){var a=this.settings.width;a&&(b.isInt(a)?a+="px":"auto"===a?a="100%":b.includes(this.settings.width,"%")&&(a=this.settings.width),this.width=a)},e=function(){var a=this,e=a.settings;if(b.addClass(a.el,"hidden-input"),e.data){a.pageIndex=1;var h=e.pagination?e.data.slice(0,e.pagination):e.data;b.each(h,function(b,c){var d=new Option(c.text,c.value,c.selected||!1,c.selected||!1);c.disabled&&(d.disabled=!0),a.el.add(d)}),e.pagination&&(this.pages=e.data.map(function(a,b){return b%e.pagination===0?e.data.slice(b,b+e.pagination):null}).filter(function(a){return a}))}c.call(a),a.customOption=e.hasOwnProperty("renderOption")&&"function"==typeof e.renderOption,a.customSelected=e.hasOwnProperty("renderSelection")&&"function"==typeof e.renderSelection,a.container=b.createElement("div",{class:"selectr-container"}),e.customClass&&b.addClass(a.container,e.customClass),d.call(a),this.container.style.width=this.width,a.selected=b.createElement("div",{class:"selectr-selected"}),a.label=b.createElement(a.multiple?"ul":"span",{class:"selectr-label"});var i=b.createElement("div",{class:"selectr-options-container"});if(a.optsOptions=b.createElement("ul",{class:"selectr-options"}),a.notice=b.createElement("div",{class:"selectr-notice"}),a.multiple&&(b.addClass(a.label,"selectr-tags"),b.addClass(a.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),a.hasOptGroups){var j=0;b.addClass(a.optsOptions,"optgroups")}var k=0;b.each(a.el.children,function(c,d){if("OPTGROUP"===d.nodeName){var g=b.createElement("ul",{class:"selectr-optgroup"});b.append(g,b.createElement("li",{class:"selectr-optgroup--label",text:d.label})),b.append(a.optsOptions,g),d.children&&b.each(d.children,function(b,c){e.pagination&&e.data&&j>=e.pagination||(f.call(a,j,c,g),j++)})}else{if(e.pagination&&e.data&&k>=e.pagination)return;f.call(a,k,d),k++}}),b.addClass(a.list[a.activeIdx],"active"),a.selected.appendChild(a.label),e.clearable&&(a.selectClear=b.createElement("button",{class:"selectr-clear",type:"button"}),b.append(a.selected,a.selectClear)),e.searchable&&(a.input=b.createElement("input",{class:"selectr-input"}),a.inputClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.inputContainer=b.createElement("div",{class:"selectr-input-container"}),b.append(a.inputContainer,a.input),b.append(a.inputContainer,a.inputClear),b.append(i,a.inputContainer)),b.append(i,a.notice),b.append(i,a.optsOptions),b.append(a.container,a.selected),b.append(a.container,i);var l=e.placeholder||a.el.getAttribute("placeholder")||"Choose...";a.placeEl=b.createElement("div",{class:"selectr-placeholder",html:l}),b.append(a.selected,a.placeEl),a.el.parentNode.insertBefore(a.container,a.el),a.container.appendChild(a.el),g.call(this)},f=function(a,c,d){if("OPTION"===c.nodeName&&c.value){var e=this.customOption?this.settings.renderOption(c):c.textContent.trim(),f=b.createElement("li",{class:"selectr-option",html:e});f.idx=a,this.items.push(f),c.defaultSelected&&h.call(this,a,!0),c.disabled&&b.addClass(f,"disabled"),d?b.append(d,f):b.append(this.optsOptions,f),c.disabled||this.list.push(f)}},g=function(){var a=this;a.requiresPagination=a.settings.data&&a.settings.data.length>a.settings.pagination,a.handleDismiss=n.bind(a),b.listen(a.container,"click",function(c){c=c||window.event;var d=c.target,e=b.closest(d,function(b){return b&&b==a.selected});if(a.settings.clearable&&d===a.selectClear)return void a.clear();if(b.hasClass(d,"selectr-tag-remove")&&j.call(a,d.parentNode.tag),d!==a.label&&d!==a.placeEl||(d=a.placeEl.parentNode),(e||d===a.selected)&&a.toggle(),b.hasClass(d,"selectr-option")){var f=a.items.indexOf(d);h.call(a,f)}b.preventDefault(c)}),b.listen(a.selected,"mousedown",function(a){b.preventDefault(a)}),b.listen(a.optsOptions,"mousedown",function(a){b.preventDefault(a)}),a.settings.searchable&&b.listen(a.inputClear,"click",function(b){k.call(a)}),b.listen(document,"click",a.handleDismiss),b.listen(document,"keydown",l.bind(a)),b.listen(document,"keyup",function(b){a.settings.searchable&&b.target==a.input&&a.search(a.input.value,!0),a.navigating&&13!=b.keyCode&&(a.navigating=!1)}),a.update=b.debounce(function(){a.opened&&a.close(),this.width&&(this.container.style.width=this.width)},50),b.listen(window,"resize",a.update),b.listen(window,"scroll",a.update),a.requiresPagination&&(a.paginateItems=b.debounce(function(){m.call(a)},50),b.listen(a.optsOptions,"scroll",a.paginateItems))},h=function(a,c){var d=this,e=d.items[a],f=d.el.options[a];if(!f.disabled){if(c)return void(d.multiple?i.call(d,a):(b.addClass(d.items[a],"selected"),b.addClass(d.container,"has-selected"),d.label.innerHTML=d.customSelected?d.settings.renderSelection(f):f.textContent,d.selectedValue=f.value,d.selectedIndex=a));d.multiple?b.hasClass(e,"selected")?j.call(d,f.value):i.call(d,a):(b.hasClass(e,"selected")?(f.defaultSelected=!1,b.removeClass(d.items[a],"selected"),b.removeClass(d.container,"has-selected"),d.label.innerHTML="",d.selectedValue=null,d.emit("selectr.deselect",f)):(f.defaultSelected=!0,b.addClass(d.items[a],"selected"),b.addClass(d.container,"has-selected"),d.label.innerHTML=d.customSelected?d.settings.renderSelection(f):f.textContent,d.selectedValue=f.value,d.selectedIndex=a,d.emit("selectr.select",f)),b.each(d.items,function(c,e){c!=a&&(b.removeClass(e,"selected"),d.el.options[c].defaultSelected=!1)})),d.opened&&!d.multiple&&d.close()}},i=function(a){var c=this;if(!b.includes(c.selectedIndexes,a)){if(c.multiple){var d=c.settings.maxSelections;if(d&&c.tags.length==d)return void c.setMessage("A maximum of "+d+" items can be selected.",!0)}var e=c.el.options[a],f=document.createDocumentFragment(),g=c.customSelected?c.settings.renderSelection(e):e.textContent,h=b.createElement("li",{class:"selectr-tag",html:g}),i=b.createElement("button",{class:"selectr-tag-remove",type:"button"});if(b.append(h,i),h.idx=a,h.tag=e.value,c.selectedValues.push(h.tag),c.selectedIndexes.push(a),c.tags.push(h),c.settings.sortSelected){var j=c.tags.slice();j.sort(function(a,b){var c=[],d=[];for(a.tag.replace(/(\d+)|(\D+)/g,function(a,b,d){c.push([b||1/0,d||""])}),b.tag.replace(/(\d+)|(\D+)/g,function(a,b,c){d.push([b||1/0,c||""])});c.length&&d.length;){var e=c.shift(),f=d.shift(),g=e[0]-f[0]||e[1].localeCompare(f[1]);if(g)return g}return c.length-d.length}),b.each(j,function(a,b){f.appendChild(b)}),c.label.innerHTML=""}else b.append(f,h);b.append(c.label,f),e.defaultSelected=!0,b.addClass(c.items[a],"selected"),c.emit("selectr.select",e),c.label.children.length?b.addClass(c.container,"has-selected"):b.removeClass(c.container,"has-selected")}},j=function(a){var e,f,c=this,d=this.selectedValues.indexOf(a);if(b.each(this.el.options,function(b,c){a==c.value&&(e=b)}),e>-1){f=c.el.options[e],c.tags[d].parentNode.removeChild(c.tags[d]),c.selectedValues.splice(d,1),f.defaultSelected=!1,c.tags.splice(d,1),b.removeClass(c.items[e],"selected");var g=c.selectedIndexes.indexOf(e);c.selectedIndexes.splice(g,1),c.emit("selectr.deselect",f)}this.tags.length||b.removeClass(this.container,"has-selected")},k=function(){var a=this;a.input.value=null,a.searching=!1,b.removeClass(a.inputContainer,"active"),b.hasClass(a.container,"notice")&&(b.removeClass(a.container,"notice"),b.addClass(a.container,"open"),a.input.focus()),b.each(a.list,function(c,d){b.removeClass(d,"match"),b.removeClass(d,"excluded"),a.customOption||(d.innerHTML=d.textContent)})},l=function(a){a=a||window.event;var c=this,d=a.keyCode,e=[13,38,40];if(c.opened&&!(e.indexOf(d)<0)){b.preventDefault(a);var g,f=this.searching?this.searchItems:this.list;switch(d){case 13:var i=c.optsOptions.querySelector(".active"),j=c.items.indexOf(i);return void h.call(c,j);case 38:g="up",c.activeIdx>0&&c.activeIdx--;break;case 40:g="down",c.activeIdx<f.length-1&&c.activeIdx++}this.navigating=!0;var p,q,k=f[c.activeIdx],l=b.getBoundingRect(k),n=c.optsOptions.scrollTop,o=c.optsRect.top;if("up"===g){var r=l.top;p=o,q=n+(r-p),0===c.activeIdx?c.optsOptions.scrollTop=0:r-p<0&&(c.optsOptions.scrollTop=q)}else{var s=l.top+l.height;p=o+c.optsRect.height,q=n+s-p,0===c.activeIdx?c.optsOptions.scrollTop=0:s>p&&(c.optsOptions.scrollTop=q),c.requiresPagination&&m.call(c)}b.removeClass(c.optsOptions.querySelector(".active"),"active"),b.addClass(f[c.activeIdx],"active")}},m=function(){var a=this,c=a.optsOptions,d=c.scrollTop,e=c.scrollHeight,g=c.offsetHeight,h=d>=e-g;if(h&&a.pageIndex<a.pages.length){var i=document.createDocumentFragment(),j=document.createDocumentFragment();b.each(a.pages[a.pageIndex],function(c,d){var e=b.createElement("option",{value:d.value,text:d.text});i.appendChild(e),f.call(a,c,e,j)}),a.el.appendChild(i),c.appendChild(j),a.pageIndex++,a.emit("selectr.paginate",{items:a.items.length,total:a.settings.data.length,page:a.pageIndex,pages:a.pages.length})}},n=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},o=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},p=function(){};return p.prototype={on:function(a,b){this._events=this._events||{},this._events[a]=this._events[a]||[],this._events[a].push(b)},off:function(a,b){this._events=this._events||{},a in this._events!=!1&&this._events[a].splice(this._events[a].indexOf(b),1)},emit:function(a){if(this._events=this._events||{},a in this._events!=!1)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}},p.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=p.prototype[b[c]]:a[b[c]]=p.prototype[b[c]];return a},q.prototype.setValue=function(a){var c=this,d=b.isArray(a);d||(a=a.toString().trim()),!c.multiple&&d||b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},q.prototype.getValue=function(a,c){var e,d=this;if(d.multiple)a?d.selectedIndexes.length&&(e={},e.values=[],b.each(d.selectedIndexes,function(a,b){var c=d.el.options[b];e.values[a]={value:c.value,text:c.textContent}})):e=d.selectedValues.slice();else if(a){var f=d.el.options[d.selectedIndex];e={value:f.value,text:f.textContent}}else e=d.selectedValue;return a&&c&&(e=JSON.stringify(e)),e},q.prototype.search=function(a,c){a=a.trim();var d=this;if(!d.navigating){if(b.hasClass(d.container,"notice")||(a.length>0?b.addClass(d.inputContainer,"active"):b.removeClass(d.inputContainer,"active")),d.searching=!0,d.searchItems=[],d.searchQuery=a,b.each(d.list,function(e,f){var g=f.textContent.trim();b.includes(g.toLowerCase(),a.toLowerCase())?(d.searchItems.push(f),c&&(d.customOption||(f.innerHTML=o(a,f,g)),b.addClass(f,"match"),b.removeClass(f,"excluded"))):(b.addClass(f,"excluded"),b.removeClass(f,"match"))}),c)d.searchItems.length?d.open():(d.setMessage("No results."),d.input.focus());else if(d.searchItems.length){var e={};return e.values=[],b.each(d.searchItems,function(a,b){var c=d.el.options[b.idx];e.values[a]={value:c.value,text:c.textContent}}),e}d.emit("selectr.search",a,d.searchItems)}},q.prototype.toggle=function(){if(!this.disabled){var a=b.hasClass(this.container,"open");a?this.close():this.open()}},q.prototype.open=function(){var a=this;b.addClass(a.container,"open"),a.optsRect=b.getBoundingRect(a.optsOptions);var c=window.innerHeight,d=a.optsOptions.scrollHeight,e=a.elRect.top+a.elRect.height+a.optsRect.height>c;d<=a.optsRect.height&&a.requiresPagination&&m.call(a),e?(b.addClass(a.container,"inverted"),this.isInverted=!0):(b.removeClass(a.container,"inverted"),this.isInverted=!1),b.removeClass(a.container,"notice"),a.settings.searchable&&setTimeout(function(){a.input.focus()},10),a.optsRect=b.getBoundingRect(a.optsOptions),a.opened=!0,a.emit("selectr.open")},q.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(b.removeClass(this.container,"notice"),this.notice.textContent=""),b.removeClass(this.container,"open"),this.opened=!1,this.emit("selectr.close")},q.prototype.reset=function(a){this.clear(),c.call(this),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset")},q.prototype.clear=function(a){if(this.multiple){var c=this.selectedIndexes.slice();b.each(c,function(a,b){h.call(this,b)},this)}else h.call(this,this.selectedIndex);k.call(this),this.emit("selectr.clear")},q.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},q}); | ||
!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 u(a,b){if(null===a)throw new Error("Selectr requires an element to work.");if(this.el=a,"string"==typeof this.el&&(this.el=document.querySelector(a)),null===this.el)throw new Error("The element you passed to Selectr can not be found.");if("select"!==this.el.nodeName.toLowerCase())throw new Error("The element you passed to Selectr is not a HTMLSelectElement.");if(!this.el.options.length&&!b.data&&!b.taggable)throw new Error("You don't have any options in your select!");this.originalType=this.el.type,this.render(b)}var b={extend:function(a,b){b=b||{};var c;for(c in a)b.hasOwnProperty(c)||(b[c]=a[c]);return b},each:function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a))for(var 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)},createElement: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},hasClass:function(a,b){return a.classList?a.classList.contains(b):!!a.className&&!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},addClass:function(a,c){b.hasClass(a,c)||(a.classList?a.classList.add(c):a.className=a.className.trim()+" "+c)},removeClass:function(a,c){b.hasClass(a,c)&&(a.classList?a.classList.remove(c):a.className=a.className.replace(new RegExp("(^|\\s)"+c.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(a,c){return a&&a!==document.body&&(c(a)?a:b.closest(a.parentNode,c))},on:function(a,b,c,d){a.addEventListener(b,c,d||!1)},off:function(a,b,c){a.removeEventListener(b,c)},isObject:function(a){return"[object Object]"===Object.prototype.toString.call(a)},isArray:function(a){return"[object Array]"===Object.prototype.toString.call(a)},isInt:function(a){return!isNaN(a)&&function(a){return(a||0)===a}(parseFloat(a))},debounce: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)}},getBoundingRect:function(a){var b=window,c=document,d=c.body,e=a.getBoundingClientRect(),f=void 0!==b.pageXOffset?b.pageXOffset:(c.documentElement||d.parentNode||d).scrollLeft,g=void 0!==b.pageYOffset?b.pageYOffset:(c.documentElement||d.parentNode||d).scrollTop;return{bottom:e.bottom+g,height:e.height,left:e.left+f,right:e.right+f,top:e.top+g,width:e.width}},preventDefault:function(a){if(a=a||window.event,a.preventDefault)return a.preventDefault()},includes:function(a,b){return a.indexOf(b)>-1}},c=function(){var a=this.settings,c=this.el.options;if(a.selectedValue){var d=a.selectedValue.toString();b.each(c,function(a,c){b.includes(d,c.value)&&(c.defaultSelected=!0)})}!c[0].defaultSelected&&c[0].selected&&(this.el.selectedIndex=-1)},d=function(){var a=this.settings.width;a&&(b.isInt(a)?a+="px":"auto"===a?a="100%":b.includes(this.settings.width,"%")&&(a=this.settings.width),this.width=a)},e=function(){var a=this,e=a.settings;if(b.addClass(a.el,"hidden-input"),e.data){a.pageIndex=1;var h=e.pagination?e.data.slice(0,e.pagination):e.data,i=!1;b.each(h,function(b,c){var d=c.hasOwnProperty("selected")&&c.selected===!0,e=new Option(c.text,c.value,d,d);c.disabled&&(e.disabled=!0),0===b&&d&&(i=!0),a.el.add(e)}),i||(this.el.selectedIndex=-1),e.pagination&&(this.pages=e.data.map(function(a,b){return b%e.pagination===0?e.data.slice(b,b+e.pagination):null}).filter(function(a){return a}))}c.call(a),a.customOption=e.hasOwnProperty("renderOption")&&"function"==typeof e.renderOption,a.customSelected=e.hasOwnProperty("renderSelection")&&"function"==typeof e.renderSelection,a.container=b.createElement("div",{class:"selectr-container"}),e.customClass&&b.addClass(a.container,e.customClass),d.call(a),this.container.style.width=this.width,a.selected=b.createElement("div",{class:"selectr-selected"}),a.label=b.createElement(a.el.multiple?"ul":"span",{class:"selectr-label"});var j=b.createElement("div",{class:"selectr-options-container"});if(a.optsOptions=b.createElement("ul",{class:"selectr-options"}),a.notice=b.createElement("div",{class:"selectr-notice"}),a.el.multiple&&(b.addClass(a.label,"selectr-tags"),b.addClass(a.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),a.selected.appendChild(a.label),e.clearable&&(a.selectClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.selected.appendChild(a.selectClear)),e.taggable){var k=b.createElement("li",{class:"input-tag"});a.input=b.createElement("input",{class:"selectr-tag-input",placeholder:"Enter a tag..."}),k.appendChild(a.input),a.label.appendChild(k),b.addClass(a.container,"taggable"),this.tagSeperators=[","],a.settings.tagSeperators&&(this.tagSeperators=this.tagSeperators.concat(a.settings.tagSeperators))}if(e.searchable&&(a.input=b.createElement("input",{class:"selectr-input"}),a.inputClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.inputContainer=b.createElement("div",{class:"selectr-input-container"}),a.inputContainer.appendChild(a.input),a.inputContainer.appendChild(a.inputClear),j.appendChild(a.inputContainer)),a.hasOptGroups){var l=0;b.addClass(a.optsOptions,"optgroups")}var m=0;b.each(a.el.children,function(c,d){if("OPTGROUP"===d.nodeName){var g=b.createElement("ul",{class:"selectr-optgroup"});g.appendChild(b.createElement("li",{class:"selectr-optgroup--label",text:d.label})),a.optsOptions.appendChild(g),d.children&&b.each(d.children,function(b,c){e.pagination&&e.data&&l>=e.pagination||(f.call(a,l,c,g),l++)})}else{if(e.pagination&&e.data&&m>=e.pagination)return;f.call(a,m,d),m++}}),b.addClass(a.list[a.activeIdx],"active"),j.appendChild(a.notice),j.appendChild(a.optsOptions),a.container.appendChild(a.selected),a.container.appendChild(j);var n=e.placeholder||a.el.getAttribute("placeholder")||"Choose...";a.placeEl=b.createElement("div",{class:"selectr-placeholder",html:n}),a.selected.appendChild(a.placeEl),a.el.parentNode.insertBefore(a.container,a.el),a.container.appendChild(a.el),g.call(this)},f=function(a,c,d){if("OPTION"===c.nodeName&&c.value){var e=this.customOption?this.settings.renderOption(c):c.textContent.trim(),f=b.createElement("li",{class:"selectr-option",html:e});f.idx=a,this.items.push(f),c.defaultSelected&&h.call(this,a,!0),c.disabled&&(f.disabled=!0,b.addClass(f,"disabled")),d?d.appendChild(f):this.optsOptions.appendChild(f),c.disabled||this.list.push(f)}},g=function(){var a=this;this.events={},this.events.keyup=n.bind(a),this.events.navigate=o.bind(a),this.events.dismiss=q.bind(a),a.requiresPagination=a.settings.data&&a.settings.data.length>a.settings.pagination,b.on(a.container,"click",function(c){c=c||window.event;var d=c.target,e=b.closest(d,function(b){return b&&b==a.selected});if(d!==a.input){if(a.settings.clearable&&d===a.selectClear)return void a.clear();if(b.hasClass(d,"selectr-tag-remove")&&j.call(a,d.parentNode.idx),d!==a.label&&d!==a.placeEl||(d=a.placeEl.parentNode),(e||d===a.selected)&&a.toggle(),b.hasClass(d,"selectr-option")){var f=a.items.indexOf(d);h.call(a,f)}b.preventDefault(c)}}),b.on(a.optsOptions,"mousedown",function(a){b.preventDefault(a)}),a.settings.searchable&&b.on(a.inputClear,"click",function(b){m.call(a)}),b.on(document,"click",this.events.dismiss),b.on(document,"keydown",this.events.navigate),b.on(document,"keyup",this.events.keyup),a.update=b.debounce(function(){a.opened&&a.close(),this.width&&(this.container.style.width=this.width)},50),b.on(window,"resize",a.update),b.on(window,"scroll",a.update),a.requiresPagination&&(a.paginateItems=b.debounce(function(){p.call(a)},50),b.on(a.optsOptions,"scroll",a.paginateItems))},h=function(a,c){var d=this,e=d.items[a],f=d.el.options[a];if(!f.disabled){if(c)return void i.call(d,a);b.hasClass(e,"selected")?j.call(d,a):i.call(d,a),d.opened&&!d.el.multiple&&d.close()}},i=function(a){var c=this.el.options[a];if(this.el.multiple){if(b.includes(this.selectedIndexes,a))return!1;var d=this.settings.maxSelections;if(d&&this.tags.length==d)return this.setMessage("A maximum of "+d+" items can be selected.",!0),!1;this.selectedValues.push(c.value),this.selectedIndexes.push(a),k.call(this,a)}else this.label.innerHTML=this.customSelected?this.settings.renderSelection(c):c.textContent,this.selectedValue=c.value,this.selectedIndex=a,b.each(this.el.options,function(c,d){var e=this.items[c];c!==a&&(e&&b.removeClass(e,"selected"),d.defaultSelected=!1)},this);b.addClass(this.items[a],"selected"),b.addClass(this.container,"has-selected"),c.defaultSelected=!0,this.emit("selectr.select",c),this.emit("selectr.change",c)},j=function(a){var c=this.el.options[a];if(b.removeClass(this.items[a],"selected"),this.el.multiple){var d=this.selectedIndexes.indexOf(a);this.selectedIndexes.splice(d,1);var e=this.selectedValues.indexOf(c.value);this.selectedValues.splice(e,1),l.call(this,a),this.tags.length||b.removeClass(this.container,"has-selected")}else this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=-1,b.removeClass(this.container,"has-selected");c.defaultSelected=!1,this.emit("selectr.deselect",c),this.emit("selectr.change",c)},k=function(a){var c=this,d=document.createDocumentFragment(),e=c.el.options[a],f=c.customSelected?c.settings.renderSelection(e):e.textContent,g=b.createElement("li",{class:"selectr-tag",html:f}),h=b.createElement("button",{class:"selectr-tag-remove",type:"button"});if(g.appendChild(h),g.idx=a,g.tag=e.value,c.tags.push(g),c.settings.sortSelected){var i=c.tags.slice();i.sort(function(a,b){var f,g,d=[],e=[];for(c.settings.sortSelected===!0?(f=a.tag,g=b.tag):"text"===c.settings.sortSelected&&(f=a.textContent,g=b.textContent),f.replace(/(\d+)|(\D+)/g,function(a,b,c){d.push([b||1/0,c||""])}),g.replace(/(\d+)|(\D+)/g,function(a,b,c){e.push([b||1/0,c||""])});d.length&&e.length;){var h=d.shift(),i=e.shift(),j=h[0]-i[0]||h[1].localeCompare(i[1]);if(j)return j}return d.length-e.length}),b.each(i,function(a,b){d.appendChild(b)}),c.label.innerHTML=""}else d.appendChild(g);c.settings.taggable?c.label.insertBefore(d,c.input.parentNode):c.label.appendChild(d)},l=function(a){var c=!1;if(b.each(this.tags,function(b,d){d.idx===a&&(c=d)}),c){this.label.removeChild(c);var d=this.tags.indexOf(c);this.tags.splice(d,1)}},m=function(){var a=this;(a.settings.searchable||a.settings.taggable)&&(a.input.value=null,a.searching=!1,a.settings.searchable&&b.removeClass(a.inputContainer,"active"),b.hasClass(a.container,"notice")&&(b.removeClass(a.container,"notice"),b.addClass(a.container,"open"),a.input.focus()),b.each(a.list,function(c,d){b.removeClass(d,"match"),b.removeClass(d,"excluded"),a.customOption||(d.innerHTML=d.textContent)}))},n=function(a){var c=this;if(a.target==c.input&&(c.input=a.target,c.search(c.input.value,!0)),c.navigating&&13!=a.keyCode&&(c.navigating=!1),c.input.value.length){var d=c.input.value.trim();if(13===a.which||b.includes(c.tagSeperators,a.key)){b.each(c.tagSeperators,function(a,b){d=d.replace(b,"")});var e=c.addOption({value:d,text:d,selected:!0});e?(c.close(),m.call(c)):(c.input.value="",c.setMessage("That tag is already in use."))}}},o=function(a){a=a||window.event;var c=this,d=a.which,e=[13,38,40];if(c.opened&&b.includes(e,d)){b.preventDefault(a);var g,f=this.searching?this.searchItems:this.list;if(f.length){switch(d){case 13:if(c.settings.taggable&&c.input===document.activeElement)return;var i=c.optsOptions.querySelector(".active"),j=c.items.indexOf(i);return void h.call(c,j);case 38:g=1,c.activeIdx>0&&c.activeIdx--;break;case 40:g=-1,c.activeIdx<f.length-1&&c.activeIdx++}this.navigating=!0;var o,q,k=f[c.activeIdx],l=b.getBoundingRect(k),m=c.optsOptions.scrollTop,n=c.optsRect.top;if(g>1){var r=l.top;o=n,q=m+(r-o),0===c.activeIdx?c.optsOptions.scrollTop=0:r-o<0&&(c.optsOptions.scrollTop=q)}else{var s=l.top+l.height;o=n+c.optsRect.height,q=m+s-o,0===c.activeIdx?c.optsOptions.scrollTop=0:s>o&&(c.optsOptions.scrollTop=q),c.requiresPagination&&p.call(c)}b.removeClass(c.optsOptions.querySelector(".active"),"active"),b.addClass(f[c.activeIdx],"active")}}},p=function(){var a=this,c=a.optsOptions,d=c.scrollTop,e=c.scrollHeight,g=c.offsetHeight,h=d>=e-g;if(h&&a.pageIndex<a.pages.length){var i=document.createDocumentFragment(),j=document.createDocumentFragment();b.each(a.pages[a.pageIndex],function(c,d){var e=b.createElement("option",{value:d.value,text:d.text});i.appendChild(e),f.call(a,c,e,j)}),a.el.appendChild(i),c.appendChild(j),a.pageIndex++,a.emit("selectr.paginate",{items:a.items.length,total:a.settings.data.length,page:a.pageIndex,pages:a.pages.length})}},q=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},r=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},s=function(){};s.prototype={on:function(a,b){this._events=this._events||{},this._events[a]=this._events[a]||[],this._events[a].push(b)},off:function(a,b){this._events=this._events||{},a in this._events!=!1&&this._events[a].splice(this._events[a].indexOf(b),1)},emit:function(a){if(this._events=this._events||{},a in this._events!=!1)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}},s.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=s.prototype[b[c]]:a[b[c]]=s.prototype[b[c]];return a};var t={width:"auto",searchable:!0,clearable:!1,sortSelected:!1};return u.prototype.render=function(a){if(!this.rendered){this.settings=b.extend(t,a),(this.settings.multiple||this.settings.taggable)&&(this.el.multiple=!0),this.settings.taggable&&(this.settings.searchable=!1),this.hasOptGroups=!1,this.el.getElementsByTagName("optgroup").length&&(this.hasOptGroups=!0),this.items=[],this.list=[],this.activeIdx=0,this.navigating=!1,this.elRect=this.el.getBoundingClientRect(),s.mixin(this),e.call(this),this.update(),this.rendered=!0;var c=this;setTimeout(function(){c.emit("selectr.init")},20)}},u.prototype.destroy=function(){if(this.rendered){if(this.emit("selectr.destroy"),this.settings.data)for(;this.el.hasChildNodes();)this.el.removeChild(this.el.lastChild);"select-one"===this.originalType&&(this.el.multiple=!1),b.removeClass(this.el,"hidden-input"),b.off(document,"click",this.events.dismiss),b.off(document,"keydown",this.events.navigate),b.off(document,"keyup",this.events.keyup),b.off(window,"resize",this.update),b.off(window,"scroll",this.update),this.container.parentNode.replaceChild(this.el,this.container),this.rendered=!1}},u.prototype.setValue=function(a){var c=this,d=b.isArray(a);d||(a=a.toString().trim()),!c.el.multiple&&d||b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},u.prototype.getValue=function(a,c){var e,d=this;if(d.el.multiple)a?d.selectedIndexes.length&&(e={},e.values=[],b.each(d.selectedIndexes,function(a,b){var c=d.el.options[b];e.values[a]={value:c.value,text:c.textContent}})):e=d.selectedValues.slice();else if(a){var f=d.el.options[d.selectedIndex];e={value:f.value,text:f.textContent}}else e=d.selectedValue;return a&&c&&(e=JSON.stringify(e)),e},u.prototype.addOption=function(a){var c=this;if(a&&b.isObject(a)&&a.value){var d=c.settings.data?c.settings.data:c.el.options,e=!1;if(b.each(d,function(b,c){c.value.toLowerCase()===a.value.toLowerCase()&&(e=!0)}),e){if(!c.settings.taggable)throw new Error("That value is already in use.");return!1}var g=b.createElement("option",a);return c.settings.data&&c.settings.data.push({value:a.value,text:a.text}),a.selected&&(g.defaultSelected=!0),g.innerHTML=c.customOption?c.settings.renderOption(g):g.textContent,c.settings.pagination||(c.el.add(g),f.call(c,c.el.options.length-1,g)),g}return!1},u.prototype.search=function(a,c){a=a.trim();var d=this;if(!d.navigating){if(b.hasClass(d.container,"notice")||d.settings.searchable&&(a.length>0?b.addClass(d.inputContainer,"active"):b.removeClass(d.inputContainer,"active")),d.searching=!0,d.searchItems=[],d.searchQuery=a,b.each(d.items,function(e,f){var g=f.textContent.trim(),h=b.includes(g.toLowerCase(),a.toLowerCase());f.disabled||!h?(b.addClass(f,"excluded"),b.removeClass(f,"match")):(d.searchItems.push(f),c&&(d.customOption||(f.innerHTML=r(a,f,g)),b.addClass(f,"match"),b.removeClass(f,"excluded")))}),c)d.searchItems.length||d.settings.taggable?d.open():(d.setMessage("No results."),d.input.focus());else if(d.searchItems.length){var e={};return e.values=[],b.each(d.searchItems,function(a,b){var c=d.el.options[b.idx];e.values[a]={value:c.value,text:c.textContent}}),e}d.emit("selectr.search",a,d.searchItems)}},u.prototype.toggle=function(){if(!this.disabled){var a=b.hasClass(this.container,"open");a?this.close():this.open()}},u.prototype.open=function(){var a=this;b.addClass(a.container,"open"),a.optsRect=b.getBoundingRect(a.optsOptions);var c=window.innerHeight,d=a.optsOptions.scrollHeight,e=a.elRect.top+a.elRect.height+a.optsRect.height>c;d<=a.optsRect.height&&a.requiresPagination&&p.call(a),e?(b.addClass(a.container,"inverted"),this.isInverted=!0):(b.removeClass(a.container,"inverted"),this.isInverted=!1),b.removeClass(a.container,"notice"),a.settings.searchable&&!a.settings.taggable&&setTimeout(function(){a.input.focus()},10),a.optsRect=b.getBoundingRect(a.optsOptions),this.opened||a.emit("selectr.open"),a.opened=!0},u.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(b.removeClass(this.container,"notice"),this.notice.textContent=""),b.removeClass(this.container,"open"),m.call(this),this.opened&&this.emit("selectr.close"),this.opened=!1},u.prototype.reset=function(a){this.clear(),c.call(this),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset")},u.prototype.clear=function(a){if(this.el.multiple){var c=this.selectedIndexes.slice();b.each(c,function(a,b){j.call(this,b)},this)}else j.call(this,this.selectedIndex);this.emit("selectr.clear")},u.prototype.serialise=function(a){var c=[];return b.each(this.el.options,function(a,b){var d={value:b.value,text:b.textContent};b.defaultSelected&&(d.selected=!0),b.disabled&&(d.disabled=!0),c[a]=d}),a?JSON.stringify(c):c},u.prototype.serialize=function(a){return this.serialise(a)},u.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},u}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
108
README.md
@@ -1,2 +0,2 @@ | ||
# Selectr [![Build Status](https://travis-ci.org/Mobius1/Selectr.svg?branch=master)](https://travis-ci.org/Mobius1/Selectr) | ||
# Selectr [![Build Status](https://travis-ci.org/Mobius1/Selectr.svg?branch=master)](https://travis-ci.org/Mobius1/Selectr) [![npm version](https://badge.fury.io/js/mobius1-selectr.svg)](https://badge.fury.io/js/mobius1-selectr) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/Mobius1/Selectr/blob/master/LICENSE) | ||
A lightweight dependency-free select box replacement written in vanilla javascript. Similar to Select2 and Chosen without the dependencies. | ||
@@ -9,2 +9,3 @@ | ||
* Searchable options | ||
* Tagging support | ||
* Custom events | ||
@@ -14,9 +15,100 @@ * Custom styling | ||
### Check the demos out [here](http://mobius.ovh/docs/selectr/pages/demos) and the playground [here](http://codepen.io/Mobius1/full/jBqpze/). | ||
--- | ||
## Install with bower | ||
``` | ||
bower install mobius1-selectr --save | ||
``` | ||
## Install with npm | ||
``` | ||
npm install mobius1-selectr --save | ||
``` | ||
## Manual Installation | ||
Include the CSS file ... | ||
```html | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/selectr/latest/selectr.min.css"> | ||
``` | ||
... and the JS file... | ||
```html | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/selectr/latest/selectr.min.js"></script> | ||
``` | ||
CDN courtesy of [jsDelivr](http://www.jsdelivr.com/) | ||
## Initialisation | ||
#### HTML | ||
Start with a standard select box you want to convert: | ||
```html | ||
<select id="mySelect"> | ||
<option vlaue="value-1">Value 1</option> | ||
<option vlaue="value-2">Value 2</option> | ||
<option vlaue="value-3">Value 3</option> | ||
... | ||
</select> | ||
``` | ||
#### Javascript | ||
You can then instantiate Selectr by passing a reference to your select box as the first parameter: | ||
You can either pass a DOM node or a CSS3 selector string: | ||
```javascript | ||
new Selectr(document.getElementById('#mySelect')); | ||
// or | ||
new Selectr('#mySelect'); | ||
``` | ||
Selectr accepts a second parameter as an object with the options you want to set: | ||
```javascript | ||
new Selectr('#mySelect', { | ||
searchable: false, | ||
width: 300 | ||
}); | ||
``` | ||
Supports most modern browsers including IE9 and above. | ||
[Demos & Documentation](http://mobius.ovh/docs/selectr) | ||
[Demos & Full Documentation](http://mobius.ovh/docs/selectr) | ||
#Change Log | ||
--- | ||
## v.2.0.0 | ||
## Change Log | ||
### v.2.1.0 | ||
* New tagging feature (see [docs](http://mobius.ovh/docs/selectr/pages/options)) | ||
New functions: | ||
* `addOption()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
* `serialize()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
* `destroy()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
* `render()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
New options: | ||
* `taggable` (see [docs](http://mobius.ovh/docs/selectr/pages/options)) | ||
* `tagSeperators` (see [docs](http://mobius.ovh/docs/selectr/pages/options)) | ||
Updated options: | ||
* `sortSelected` (see [docs](http://mobius.ovh/docs/selectr/pages/options)) | ||
### v.2.0.0 | ||
* Complete rewrite | ||
@@ -32,7 +124,7 @@ * New options added | ||
Added options: | ||
* `clearable` (see [docs](http://mobius.ovh/docs/selectr/pages/options-2)) | ||
* `sortSelected` (see [docs](http://mobius.ovh/docs/selectr/pages/options-2)) | ||
* `clearable` (see [docs](http://mobius.ovh/docs/selectr/pages/options)) | ||
* `sortSelected` (see [docs](http://mobius.ovh/docs/selectr/pages/options)) | ||
Updated options: | ||
* `selectedValue` is now used for both single and multi selects (see [docs](http://mobius.ovh/docs/selectr/pages/options-2)) | ||
* `selectedValue` is now used for both single and multi selects (see [docs](http://mobius.ovh/docs/selectr/pages/options)) | ||
@@ -48,3 +140,3 @@ Removed options: | ||
Removed events: | ||
* `selectr.change` (see [docs](http://mobius.ovh/docs/selectr/pages/events-2)) | ||
* `selectr.change` (see [docs](http://mobius.ovh/docs/selectr/pages/events)) | ||
@@ -51,0 +143,0 @@ # License |
/*! | ||
* Selectr 2.0.3 | ||
* Selectr 2.1.0 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -22,6 +22,10 @@ * | ||
extend: function(src, props) { | ||
props = props || {}; | ||
var p; | ||
for (p in props) | ||
if (props.hasOwnProperty(p)) src[p] = props[p]; | ||
return src; | ||
for (p in src) { | ||
if (!props.hasOwnProperty(p)) { | ||
props[p] = src[p]; | ||
} | ||
} | ||
return props; | ||
}, | ||
@@ -68,8 +72,8 @@ each: function(a, b, c) { | ||
}, | ||
append: function(p, e) { | ||
return p && e && p.appendChild(e); | ||
}, | ||
listen: function(e, type, callback, capture) { | ||
on: function(e, type, callback, capture) { | ||
e.addEventListener(type, callback, capture || false); | ||
}, | ||
off: function(e, type, callback) { | ||
e.removeEventListener(type, callback); | ||
}, | ||
isObject: function(a) { | ||
@@ -130,11 +134,16 @@ return "[object Object]" === Object.prototype.toString.call(a); | ||
var o = this.settings; | ||
var m = this.multiple; | ||
var opts = this.el.options; | ||
util.each(this.el.options, function(i, opt) { | ||
if ((m && o.selectedValue && util.includes(o.selectedValue, opt.value)) || (!m && o.selectedValue && opt.value == o.selectedValue)) { | ||
// Setting the selected property does not change defaultSelected to true | ||
// which we"ll need to check for later | ||
opt.defaultSelected = true; | ||
} | ||
}); | ||
if ( o.selectedValue ) { | ||
var val = o.selectedValue.toString(); | ||
util.each(opts, function(i, opt) { | ||
if ( util.includes(val, opt.value) ) { | ||
opt.defaultSelected = true; | ||
} | ||
}); | ||
} | ||
if ( !opts[0].defaultSelected && opts[0].selected ) { | ||
this.el.selectedIndex = -1; | ||
} | ||
}; | ||
@@ -169,5 +178,7 @@ | ||
var data = o.pagination ? o.data.slice(0, o.pagination) : o.data; | ||
var firstSelected = false; | ||
util.each(data, function(idx, itm) { | ||
var option = new Option(itm.text, itm.value, itm.selected || false, itm.selected || false); | ||
var selected = itm.hasOwnProperty('selected') && itm.selected === true; | ||
var option = new Option(itm.text, itm.value, selected, selected); | ||
@@ -178,5 +189,13 @@ if ( itm.disabled ) { | ||
if ( idx === 0 && selected ) { | ||
firstSelected = true; | ||
} | ||
_.el.add(option); | ||
}); | ||
if ( !firstSelected ) { | ||
this.el.selectedIndex = -1; | ||
} | ||
if ( o.pagination ) { | ||
@@ -211,3 +230,3 @@ this.pages = o.data.map( function(v, i) { | ||
_.label = util.createElement(_.multiple ? "ul" : "span", { | ||
_.label = util.createElement(_.el.multiple ? "ul" : "span", { | ||
class: "selectr-label" | ||
@@ -228,3 +247,3 @@ }); | ||
if (_.multiple) { | ||
if (_.el.multiple) { | ||
util.addClass(_.label, "selectr-tags"); | ||
@@ -243,2 +262,48 @@ util.addClass(_.container, "multiple"); | ||
_.selected.appendChild(_.label); | ||
if ( o.clearable ) { | ||
_.selectClear = util.createElement("button", { | ||
class: "selectr-clear", | ||
type: "button" | ||
}); | ||
_.selected.appendChild(_.selectClear); | ||
} | ||
if ( o.taggable ) { | ||
var li = util.createElement('li', { class: 'input-tag' }); | ||
_.input = util.createElement("input", { | ||
class: "selectr-tag-input", | ||
placeholder: 'Enter a tag...' | ||
}); | ||
li.appendChild(_.input); | ||
_.label.appendChild( li); | ||
util.addClass(_.container, "taggable"); | ||
this.tagSeperators = [","]; | ||
if ( _.settings.tagSeperators ) { | ||
this.tagSeperators = this.tagSeperators.concat(_.settings.tagSeperators); | ||
} | ||
} | ||
if (o.searchable) { | ||
_.input = util.createElement("input", { | ||
class: "selectr-input" | ||
}); | ||
_.inputClear = util.createElement("button", { | ||
class: "selectr-clear", | ||
type: "button" | ||
}); | ||
_.inputContainer = util.createElement("div", { | ||
class: "selectr-input-container" | ||
}); | ||
_.inputContainer.appendChild(_.input); | ||
_.inputContainer.appendChild(_.inputClear); | ||
optsContainer.appendChild(_.inputContainer); | ||
} | ||
// Check for optgroups | ||
@@ -256,7 +321,7 @@ if (_.hasOptGroups) { | ||
}); | ||
util.append(group, util.createElement("li", { | ||
group.appendChild(util.createElement("li", { | ||
class: "selectr-optgroup--label", | ||
text: opt.label | ||
})); | ||
util.append(_.optsOptions, group); | ||
_.optsOptions.appendChild(group); | ||
@@ -279,36 +344,8 @@ if (opt.children) { | ||
_.selected.appendChild(_.label); | ||
optsContainer.appendChild(_.notice); | ||
optsContainer.appendChild(_.optsOptions); | ||
if ( o.clearable ) { | ||
_.selectClear = util.createElement("button", { | ||
class: "selectr-clear", | ||
type: "button" | ||
}); | ||
_.container.appendChild(_.selected); | ||
_.container.appendChild(optsContainer); | ||
util.append(_.selected, _.selectClear); | ||
} | ||
if (o.searchable) { | ||
_.input = util.createElement("input", { | ||
class: "selectr-input" | ||
}); | ||
_.inputClear = util.createElement("button", { | ||
class: "selectr-clear", | ||
type: "button" | ||
}); | ||
_.inputContainer = util.createElement("div", { | ||
class: "selectr-input-container" | ||
}); | ||
util.append(_.inputContainer, _.input); | ||
util.append(_.inputContainer, _.inputClear); | ||
util.append(optsContainer, _.inputContainer); | ||
} | ||
util.append(optsContainer, _.notice); | ||
util.append(optsContainer, _.optsOptions); | ||
util.append(_.container, _.selected); | ||
util.append(_.container, optsContainer); | ||
// Set the placeholder | ||
@@ -320,3 +357,3 @@ var placeholder = o.placeholder || _.el.getAttribute("placeholder") || "Choose..."; | ||
}); | ||
util.append(_.selected, _.placeEl); | ||
_.selected.appendChild(_.placeEl); | ||
@@ -344,6 +381,7 @@ _.el.parentNode.insertBefore(_.container, _.el); | ||
if (option.defaultSelected) { | ||
selectOption.call(this, i, true); | ||
change.call(this, i, true); | ||
} | ||
if (option.disabled) { | ||
opt.disabled = true; | ||
util.addClass(opt, "disabled"); | ||
@@ -353,5 +391,5 @@ } | ||
if (group) { | ||
util.append(group, opt); | ||
group.appendChild(opt); | ||
} else { | ||
util.append(this.optsOptions, opt); | ||
this.optsOptions.appendChild(opt); | ||
} | ||
@@ -367,8 +405,12 @@ | ||
this.events = {}; | ||
this.events.keyup = keyup.bind(_); | ||
this.events.navigate = navigate.bind(_); | ||
this.events.dismiss = dismiss.bind(_); | ||
_.requiresPagination = _.settings.data && _.settings.data.length > _.settings.pagination; | ||
_.handleDismiss = dismiss.bind(_); | ||
// Global listener | ||
util.listen(_.container, "click", function(e) { | ||
util.on(_.container, "click", function(e) { | ||
e = e || window.event; | ||
@@ -382,3 +424,7 @@ | ||
if ( target === _.input ) { | ||
return; | ||
} | ||
// Clear | ||
@@ -392,3 +438,3 @@ if ( _.settings.clearable && target === _.selectClear ) { | ||
if (util.hasClass(target, "selectr-tag-remove")) { | ||
removeTag.call(_, target.parentNode.tag); | ||
deselect.call(_, target.parentNode.idx); | ||
} | ||
@@ -409,3 +455,3 @@ | ||
var index = _.items.indexOf(target); | ||
selectOption.call(_, index); | ||
change.call(_, index); | ||
} | ||
@@ -418,7 +464,6 @@ | ||
// Prevent text selection | ||
util.listen(_.selected, "mousedown", function(e) { util.preventDefault(e); }); | ||
util.listen(_.optsOptions, "mousedown", function(e) { util.preventDefault(e); }); | ||
util.on(_.optsOptions, "mousedown", function(e) { util.preventDefault(e); }); | ||
if (_.settings.searchable) { | ||
util.listen(_.inputClear, "click", function(e) { | ||
util.on(_.inputClear, "click", function(e) { | ||
clearSearch.call(_); | ||
@@ -428,12 +473,5 @@ }); | ||
util.listen(document, "click", _.handleDismiss); | ||
util.listen(document, "keydown", navigate.bind(_)); | ||
util.listen(document, "keyup", function(e) { | ||
if ( _.settings.searchable && e.target == _.input ) { | ||
_.search(_.input.value, true); | ||
} | ||
if (_.navigating && e.keyCode != 13) { | ||
_.navigating = false; | ||
} | ||
}); | ||
util.on(document, "click", this.events.dismiss); | ||
util.on(document, "keydown", this.events.navigate); | ||
util.on(document, "keyup", this.events.keyup); | ||
@@ -449,4 +487,4 @@ _.update = util.debounce(function() { | ||
util.listen(window, "resize", _.update); | ||
util.listen(window, "scroll", _.update); | ||
util.on(window, "resize", _.update); | ||
util.on(window, "scroll", _.update); | ||
@@ -458,7 +496,7 @@ if (_.requiresPagination) { | ||
util.listen(_.optsOptions, "scroll", _.paginateItems); | ||
util.on(_.optsOptions, "scroll", _.paginateItems); | ||
} | ||
}; | ||
var selectOption = function(index, init) { | ||
var change = function(index, init) { | ||
var _ = this; | ||
@@ -473,71 +511,101 @@ var opt = _.items[index]; | ||
if (init) { | ||
if (_.multiple) { | ||
addTag.call(_, index); | ||
} else { | ||
util.addClass(_.items[index], "selected"); | ||
util.addClass(_.container, "has-selected"); | ||
_.label.innerHTML = _.customSelected ? _.settings.renderSelection(option) : option.textContent; | ||
_.selectedValue = option.value; | ||
_.selectedIndex = index; | ||
} | ||
select.call(_, index); | ||
return; | ||
} | ||
if (_.multiple) { | ||
if (!util.hasClass(opt, "selected")) { | ||
addTag.call(_, index); | ||
} else { | ||
removeTag.call(_, option.value); | ||
} | ||
if (util.hasClass(opt, "selected")) { | ||
deselect.call(_, index); | ||
} else { | ||
if (util.hasClass(opt, "selected")) { | ||
option.defaultSelected = false; | ||
util.removeClass(_.items[index], "selected"); | ||
util.removeClass(_.container, "has-selected"); | ||
_.label.innerHTML = ""; | ||
_.selectedValue = null; | ||
select.call(_, index); | ||
} | ||
_.emit("selectr.deselect", option); | ||
} else { | ||
option.defaultSelected = true; | ||
util.addClass(_.items[index], "selected"); | ||
util.addClass(_.container, "has-selected"); | ||
_.label.innerHTML = _.customSelected ? _.settings.renderSelection(option) : option.textContent; | ||
_.selectedValue = option.value; | ||
_.selectedIndex = index; | ||
if (_.opened && !_.el.multiple) { | ||
_.close(); | ||
} | ||
}; | ||
_.emit("selectr.select", option); | ||
var select = function(index) { | ||
var option = this.el.options[index]; | ||
if ( this.el.multiple ) { | ||
if (util.includes(this.selectedIndexes, index) ) { | ||
return false; | ||
} | ||
util.each(_.items, function(i, o) { | ||
if (i != index) { | ||
util.removeClass(o, "selected"); | ||
_.el.options[i].defaultSelected = false; | ||
var max = this.settings.maxSelections; | ||
if ( max && this.tags.length == max ) { | ||
this.setMessage("A maximum of " + max + " items can be selected.", true); | ||
return false; | ||
} | ||
this.selectedValues.push(option.value); | ||
this.selectedIndexes.push(index); | ||
addTag.call(this, index); | ||
} else { | ||
this.label.innerHTML = this.customSelected ? this.settings.renderSelection(option) : option.textContent; | ||
this.selectedValue = option.value; | ||
this.selectedIndex = index; | ||
util.each(this.el.options, function(i, option) { | ||
var opt = this.items[i]; | ||
if ( i !== index ) { | ||
if ( opt ) { | ||
util.removeClass(opt, "selected"); | ||
} | ||
option.defaultSelected = false; | ||
} | ||
}); | ||
}, this); | ||
} | ||
if (_.opened && !_.multiple) { | ||
_.close(); | ||
} | ||
util.addClass(this.items[index], "selected"); | ||
util.addClass(this.container, "has-selected"); | ||
option.defaultSelected = true; | ||
this.emit("selectr.select", option); | ||
this.emit("selectr.change", option); | ||
}; | ||
var addTag = function(index) { | ||
var _ = this; | ||
var deselect = function(index) { | ||
var option = this.el.options[index]; | ||
if (util.includes(_.selectedIndexes, index) ) { | ||
return; | ||
} | ||
util.removeClass(this.items[index], "selected"); | ||
if ( _.multiple ) { | ||
var max = _.settings.maxSelections; | ||
if ( max && _.tags.length == max ) { | ||
_.setMessage("A maximum of " + max + " items can be selected.", true); | ||
return; | ||
if ( this.el.multiple ) { | ||
var selIndex = this.selectedIndexes.indexOf(index); | ||
this.selectedIndexes.splice(selIndex, 1); | ||
var valIndex = this.selectedValues.indexOf(option.value); | ||
this.selectedValues.splice(valIndex, 1); | ||
removeTag.call(this, index); | ||
if ( !this.tags.length ) { | ||
util.removeClass(this.container, "has-selected"); | ||
} | ||
} else { | ||
this.label.innerHTML = ""; | ||
this.selectedValue = null; | ||
this.el.selectedIndex = -1; | ||
util.removeClass(this.container, "has-selected"); | ||
} | ||
option.defaultSelected = false; | ||
this.emit("selectr.deselect", option); | ||
this.emit("selectr.change", option); | ||
}; | ||
var addTag = function(index) { | ||
var _ = this; | ||
var docFrag = document.createDocumentFragment(); | ||
var option = _.el.options[index]; | ||
var docFrag = document.createDocumentFragment(); | ||
var content = _.customSelected ? _.settings.renderSelection(option) : option.textContent; | ||
var tag = util.createElement("li", { | ||
@@ -552,3 +620,3 @@ class: "selectr-tag", | ||
util.append(tag, btn); | ||
tag.appendChild(btn); | ||
@@ -558,4 +626,2 @@ // Set property to check against later | ||
tag.tag = option.value; | ||
_.selectedValues.push(tag.tag); | ||
_.selectedIndexes.push(index); | ||
@@ -569,7 +635,14 @@ _.tags.push(tag); | ||
tags.sort(function(a, b) { | ||
var x = [], y = []; | ||
var x = [], y = [], ac, bc; | ||
if ( _.settings.sortSelected === true ) { | ||
ac = a.tag; | ||
bc = b.tag; | ||
} else if ( _.settings.sortSelected === 'text' ) { | ||
ac = a.textContent; | ||
bc = b.textContent; | ||
} | ||
// Deal with values that contain numbers | ||
a.tag.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { x.push([$1 || Infinity, $2 || ""]); }); | ||
b.tag.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { y.push([$1 || Infinity, $2 || ""]); }); | ||
ac.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { x.push([$1 || Infinity, $2 || ""]); }); | ||
bc.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { y.push([$1 || Infinity, $2 || ""]); }); | ||
@@ -593,70 +666,92 @@ while(x.length && y.length) { | ||
} else { | ||
util.append(docFrag, tag); | ||
docFrag.appendChild(tag); | ||
} | ||
util.append(_.label, docFrag); | ||
if ( _.settings.taggable ) { | ||
_.label.insertBefore(docFrag, _.input.parentNode); | ||
} else { | ||
_.label.appendChild(docFrag); | ||
} | ||
}; | ||
var removeTag = function(index) { | ||
var tag = false; | ||
option.defaultSelected = true; | ||
util.addClass(_.items[index], "selected"); | ||
_.emit("selectr.select", option); | ||
util.each(this.tags, function(i, t) { | ||
if (t.idx === index) { | ||
tag = t; | ||
} | ||
}); | ||
if (!!_.label.children.length) { | ||
util.addClass(_.container, "has-selected"); | ||
} else { | ||
util.removeClass(_.container, "has-selected"); | ||
if (tag) { | ||
this.label.removeChild(tag); | ||
var tagIdx = this.tags.indexOf(tag); | ||
this.tags.splice(tagIdx, 1); | ||
} | ||
}; | ||
var removeTag = function(tag) { | ||
var _ = this, | ||
tagIdx = this.selectedValues.indexOf(tag), | ||
index, option; | ||
var clearSearch = function() { | ||
var _ = this; | ||
util.each(this.el.options, function(i, option) { | ||
if (tag == option.value) { | ||
index = i; | ||
if ( _.settings.searchable || _.settings.taggable ) { | ||
_.input.value = null; | ||
_.searching = false; | ||
if ( _.settings.searchable ) { | ||
util.removeClass(_.inputContainer, "active"); | ||
} | ||
}); | ||
if (index > -1) { | ||
option = _.el.options[index]; | ||
_.tags[tagIdx].parentNode.removeChild(_.tags[tagIdx]); | ||
_.selectedValues.splice(tagIdx, 1); | ||
option.defaultSelected = false; | ||
_.tags.splice(tagIdx, 1); | ||
util.removeClass(_.items[index], "selected"); | ||
if ( util.hasClass(_.container, "notice") ) { | ||
util.removeClass(_.container, "notice"); | ||
util.addClass(_.container, "open"); | ||
_.input.focus(); | ||
} | ||
var idxIndex = _.selectedIndexes.indexOf(index); | ||
_.selectedIndexes.splice(idxIndex, 1); | ||
util.each(_.list, function(i, el) { | ||
util.removeClass(el, "match"); | ||
util.removeClass(el, "excluded"); | ||
_.emit("selectr.deselect", option); | ||
if ( !_.customOption ) { | ||
el.innerHTML = el.textContent; | ||
} | ||
}); | ||
} | ||
}; | ||
if (!this.tags.length) { | ||
util.removeClass(this.container, "has-selected"); | ||
var keyup = function(e) { | ||
var _ = this; | ||
if ( e.target == _.input ) { | ||
_.input = e.target; | ||
_.search(_.input.value, true); | ||
} | ||
}; | ||
if (_.navigating && e.keyCode != 13) { | ||
_.navigating = false; | ||
} | ||
var clearSearch = function() { | ||
var _ = this; | ||
_.input.value = null; | ||
_.searching = false; | ||
util.removeClass(_.inputContainer, "active"); | ||
if ( _.input.value.length ) { | ||
var value = _.input.value.trim(); | ||
if ( util.hasClass(_.container, "notice") ) { | ||
util.removeClass(_.container, "notice"); | ||
util.addClass(_.container, "open"); | ||
_.input.focus(); | ||
} | ||
if ( e.which === 13 || util.includes(_.tagSeperators, e.key) ) { | ||
util.each(_.list, function(i, el) { | ||
util.removeClass(el, "match"); | ||
util.removeClass(el, "excluded"); | ||
util.each(_.tagSeperators, function(i,k) { | ||
value = value.replace(k, ''); | ||
}); | ||
if ( !_.customOption ) { | ||
el.innerHTML = el.textContent; | ||
var option = _.addOption({ | ||
value: value, | ||
text: value, | ||
selected: true | ||
}); | ||
if ( !option ) { | ||
_.input.value = ''; | ||
_.setMessage('That tag is already in use.'); | ||
} else { | ||
_.close(); | ||
clearSearch.call(_); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
@@ -668,7 +763,7 @@ | ||
var _ = this, | ||
keyCode = e.keyCode, | ||
keyCodes = [13, 38, 40]; | ||
key = e.which, | ||
keys = [13, 38, 40]; | ||
// Filter out the keys we don"t want | ||
if (!_.opened || keyCodes.indexOf(keyCode) < 0) return; | ||
if (!_.opened || !util.includes(keys, key)) return; | ||
@@ -680,14 +775,19 @@ util.preventDefault(e); | ||
switch (keyCode) { | ||
if ( !list.length ) return; | ||
switch (key) { | ||
case 13: | ||
if ( _.settings.taggable && _.input === document.activeElement ) { | ||
return; | ||
} | ||
var opt = _.optsOptions.querySelector(".active"); | ||
var index = _.items.indexOf(opt); | ||
return void selectOption.call(_, index); | ||
return void change.call(_, index); | ||
case 38: | ||
dir = "up"; | ||
dir = 1; | ||
if ( _.activeIdx > 0 ) { _.activeIdx--; } | ||
break; | ||
case 40: | ||
dir = "down"; | ||
dir = -1; | ||
if ( _.activeIdx < list.length - 1 ) { _.activeIdx++; } | ||
@@ -704,3 +804,3 @@ } | ||
if (dir === "up") { | ||
if (dir > 1) { | ||
var nextTop = nextRect.top; | ||
@@ -774,3 +874,2 @@ currentOffset = offset; | ||
var match = function(query, opt, text) { | ||
@@ -818,2 +917,9 @@ var result = new RegExp(query, "i").exec(text); | ||
var defaults = { | ||
width: "auto", | ||
searchable: true, | ||
clearable: false, | ||
sortSelected: false | ||
}; | ||
/** | ||
@@ -825,8 +931,2 @@ * Selectr | ||
function Selectr(el, options) { | ||
var defaults = { | ||
width: "auto", | ||
searchable: true, | ||
clearable: false, | ||
sortSelected: false | ||
}; | ||
@@ -852,16 +952,29 @@ // Checks | ||
if (!this.el.options.length && !options.data) { | ||
if (!this.el.options.length && !options.data && !options.taggable) { | ||
throw new Error("You don't have any options in your select!"); | ||
} | ||
this.originalType = this.el.type; | ||
this.render(options); | ||
} | ||
/** | ||
* Render the instance | ||
*/ | ||
Selectr.prototype.render = function(options) { | ||
if ( this.rendered ) return; | ||
this.settings = util.extend(defaults, options); | ||
Emitter.mixin(this); | ||
if (this.settings.multiple) { | ||
if (this.settings.multiple || this.settings.taggable) { | ||
this.el.multiple = true; | ||
} | ||
this.multiple = this.el.type == "select-multiple"; | ||
if ( this.settings.taggable ) { | ||
this.settings.searchable = false; | ||
} | ||
// Check for optgroupd | ||
this.hasOptGroups = false; | ||
@@ -882,2 +995,4 @@ if (this.el.getElementsByTagName("optgroup").length) { | ||
Emitter.mixin(this); | ||
build.call(this); | ||
@@ -887,2 +1002,4 @@ | ||
this.rendered = true; | ||
var _ = this; | ||
@@ -892,5 +1009,42 @@ setTimeout(function() { | ||
}, 20); | ||
} | ||
}; | ||
/** | ||
* Destroy the instance | ||
*/ | ||
Selectr.prototype.destroy = function() { | ||
if ( !this.rendered ) return; | ||
this.emit("selectr.destroy"); | ||
// Remove custom options set with the data option | ||
if ( this.settings.data ) { | ||
while(this.el.hasChildNodes()) { | ||
this.el.removeChild(this.el.lastChild); | ||
} | ||
} | ||
// Revert to select-single if programtically set to multiple | ||
if ( this.originalType === 'select-one' ) { | ||
this.el.multiple = false; | ||
} | ||
// Remove the className from select element | ||
util.removeClass(this.el, 'hidden-input'); | ||
// Remove event listeners attached to doc and win | ||
util.off(document, "click", this.events.dismiss); | ||
util.off(document, "keydown", this.events.navigate); | ||
util.off(document, "keyup", this.events.keyup); | ||
util.off(window, "resize", this.update); | ||
util.off(window, "scroll", this.update); | ||
// Replace the container with the original select element | ||
this.container.parentNode.replaceChild(this.el, this.container); | ||
this.rendered = false; | ||
}; | ||
/** | ||
* Programmatically set selected values | ||
@@ -908,3 +1062,3 @@ * @param {mixed} value A string or an array of strings | ||
// Can"t pass array to select-one | ||
if ( !_.multiple && isArray ) { | ||
if ( !_.el.multiple && isArray ) { | ||
return; | ||
@@ -915,3 +1069,3 @@ } | ||
if (isArray && util.includes(value.toString(), opt.value) || opt.value === value) { | ||
selectOption.call(_, i); | ||
change.call(_, i); | ||
} | ||
@@ -930,3 +1084,3 @@ }); | ||
if ( _.multiple ) { | ||
if ( _.el.multiple ) { | ||
if ( toObject) { | ||
@@ -966,2 +1120,50 @@ if ( _.selectedIndexes.length ) { | ||
Selectr.prototype.addOption = function(data) { | ||
var _ = this; | ||
if ( data && util.isObject(data) && data.value ) { | ||
// Don't add if already there | ||
var options = _.settings.data ? _.settings.data : _.el.options; | ||
var dupe = false; | ||
util.each(options, function(i,option) { | ||
if ( option.value.toLowerCase() === data.value.toLowerCase() ) { | ||
dupe = true; | ||
} | ||
}); | ||
if ( dupe ) { | ||
if ( !_.settings.taggable ) { | ||
throw new Error('That value is already in use.'); | ||
} | ||
return false; | ||
} | ||
var option = util.createElement('option', data); | ||
if ( _.settings.data ) { | ||
_.settings.data.push({ | ||
value: data.value, | ||
text: data.text | ||
}); | ||
} | ||
if ( data.selected ) { | ||
option.defaultSelected = true; | ||
} | ||
option.innerHTML = _.customOption ? _.settings.renderOption(option) : option.textContent; | ||
if ( !_.settings.pagination ) { | ||
_.el.add(option); | ||
buildOption.call(_, _.el.options.length - 1, option); | ||
} | ||
return option; | ||
} | ||
return false; | ||
}; | ||
/** | ||
@@ -980,6 +1182,8 @@ * Perform a search | ||
if (!util.hasClass(_.container, "notice")) { | ||
if (query.length > 0) { | ||
util.addClass(_.inputContainer, "active"); | ||
} else { | ||
util.removeClass(_.inputContainer, "active"); | ||
if ( _.settings.searchable ) { | ||
if (query.length > 0) { | ||
util.addClass(_.inputContainer, "active"); | ||
} else { | ||
util.removeClass(_.inputContainer, "active"); | ||
} | ||
} | ||
@@ -991,5 +1195,6 @@ } | ||
util.each(_.list, function(i, opt) { | ||
util.each(_.items, function(i, opt) { | ||
var text = opt.textContent.trim(); | ||
if ( !util.includes(text.toLowerCase(), query.toLowerCase()) ) { | ||
var includes = util.includes(text.toLowerCase(), query.toLowerCase()); | ||
if ( opt.disabled || !includes ) { | ||
util.addClass(opt, "excluded"); | ||
@@ -1013,3 +1218,3 @@ util.removeClass(opt, "match"); | ||
if ( open ) { | ||
if (!_.searchItems.length) { | ||
if (!_.searchItems.length && !_.settings.taggable) { | ||
_.setMessage("No results."); | ||
@@ -1083,3 +1288,3 @@ _.input.focus(); | ||
if (_.settings.searchable) { | ||
if (_.settings.searchable && !_.settings.taggable) { | ||
setTimeout(function() { | ||
@@ -1092,5 +1297,7 @@ _.input.focus(); | ||
if ( !this.opened ) { | ||
_.emit("selectr.open"); | ||
} | ||
_.opened = true; | ||
_.emit("selectr.open"); | ||
}; | ||
@@ -1116,5 +1323,9 @@ | ||
clearSearch.call(this); | ||
if ( this.opened ) { | ||
this.emit("selectr.close"); | ||
} | ||
this.opened = false; | ||
this.emit("selectr.close"); | ||
}; | ||
@@ -1132,3 +1343,3 @@ | ||
if ( opt.defaultSelected ) { | ||
selectOption.call(this, i); | ||
change.call(this, i); | ||
} | ||
@@ -1144,17 +1355,40 @@ }, this); | ||
Selectr.prototype.clear = function(init) { | ||
if ( this.multiple ) { | ||
if ( this.el.multiple ) { | ||
// Copy the array for reference otherwise it'll chuck an error | ||
var indexes = this.selectedIndexes.slice(); | ||
util.each(indexes, function(i, index) { | ||
selectOption.call(this, index); | ||
deselect.call(this, index); | ||
}, this); | ||
} else { | ||
selectOption.call(this, this.selectedIndex); | ||
deselect.call(this, this.selectedIndex); | ||
} | ||
clearSearch.call(this); | ||
this.emit("selectr.clear"); | ||
}; | ||
Selectr.prototype.serialise = function(toJson) { | ||
var data = []; | ||
util.each(this.el.options, function(i, option) { | ||
var obj = { | ||
value: option.value, | ||
text: option.textContent | ||
}; | ||
if ( option.defaultSelected ) { | ||
obj.selected = true; | ||
} | ||
if ( option.disabled ) { | ||
obj.disabled = true; | ||
} | ||
data[i] = obj; | ||
}); | ||
return toJson ? JSON.stringify(data) : data; | ||
}; | ||
/* Friends from across the pond */ | ||
Selectr.prototype.serialize = function(toJson) { | ||
return this.serialise(toJson); | ||
}; | ||
/** | ||
@@ -1173,2 +1407,2 @@ * Display a message | ||
return Selectr; | ||
})); | ||
})); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
72012
12
1573
148