mobius1-selectr
Advanced tools
Comparing version 2.1.4 to 2.1.5
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.1.4 | ||
* Selectr 2.1.5 | ||
* 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 v(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,b){var c=window,d=a.getBoundingClientRect(),e=b?c.pageXOffset:0,f=b?c.pageYOffset:0;return{bottom:d.bottom+f,height:d.height,left:d.left+e,right:d.right+e,top:d.top+f,width:d.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(a){var c=this.settings,d=this.el.options;if(c.selectedValue){var e=c.selectedValue.toString();b.each(d,function(a,c){b.includes(e,c.value)&&(c.defaultSelected=!0)})}!d[0].defaultSelected&&d[0].selected&&(this.el.selectedIndex=-1),a&&c.data&&b.each(c.data,function(a,c){var e=c.hasOwnProperty("selected")&&c.selected===!0;b.each(d,function(a,b){b.value===c.value&&e&&(b.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,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",tabIndex:a.el.tabIndex}),a.el.tabIndex=-1,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),(a.el.multiple&&e.clearable||!a.el.multiple&&e.allowDeselect)&&(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...",autocomplete:"off",tagIndex:0}),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",autocomplete:"off",tagIndex:-1}),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=r.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.settings.closeOnScroll&&a.close(),this.width&&(this.container.style.width=this.width),q.call(a)},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));var c=a.el.form;c&&b.on(c,"reset",a.reset.bind(a))},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(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{if(!this.settings.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=-1,b.removeClass(this.container,"has-selected")}b.removeClass(this.items[a],"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.search(c.input.value,!0),c.navigating&&13!==a.keyCode&&(c.navigating=!1),c.settings.taggable&&c.input.value.length){var d=c.input.value.trim();if(c.searchItems.length)return!1;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:c.settings.taggable&&c.input===document.activeElement&&(c.input.value="");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>0){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||a.activeIdx===a.list.length-1){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(){var a=b.getBoundingRect(this.selected),c=this.optsOptions.parentNode.offsetHeight,d=window.innerHeight,e=a.top+a.height+c>d;e?(b.addClass(this.container,"inverted"),this.isInverted=!0):(b.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=b.getBoundingRect(this.optsOptions)},r=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},s=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},t=function(){};t.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))}},t.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=t.prototype[b[c]]:a[b[c]]=t.prototype[b[c]];return a};var u={width:"auto",searchable:!0,clearable:!1,sortSelected:!1,allowDeselect:!1,closeOnScroll:!1};return v.prototype.render=function(a){if(!this.rendered){this.settings=b.extend(u,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,t.mixin(this),e.call(this),this.update(),this.optsRect=b.getBoundingRect(this.optsOptions),this.rendered=!0;var c=this;setTimeout(function(){c.emit("selectr.init")},20)}},v.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}},v.prototype.setValue=function(a){var c=this,d=b.isArray(a);return d||(a=a.toString().trim()),!(!c.el.multiple&&d)&&void b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},v.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},v.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},v.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=s(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)}},v.prototype.toggle=function(){if(!this.disabled){var a=b.hasClass(this.container,"open");a?this.close():this.open()}},v.prototype.open=function(){var a=this;b.addClass(a.container,"open"),q.call(a);var c=this.optsOptions.scrollHeight;c<=a.optsRect.height&&a.requiresPagination&&p.call(a),b.removeClass(a.container,"notice"),a.settings.searchable&&!a.settings.taggable&&setTimeout(function(){a.input.focus(),a.input.tabIndex=0},10),this.opened||a.emit("selectr.open"),a.opened=!0},v.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.input.tabIndex=-1,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},v.prototype.reset=function(a){this.clear(),c.call(this,!0),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset")},v.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")},v.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},v.prototype.serialize=function(a){return this.serialise(a)},v.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},v}); | ||
!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 v(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)},attr:function(a,c,d){b.isObject(c)?b.each(c,function(b,c){a.setAttribute(b,c)}):a.setAttribute(c,d)},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)}},getRect:function(a,b){var c=window,d=a.getBoundingClientRect(),e=b?c.pageXOffset:0,f=b?c.pageYOffset:0;return{bottom:d.bottom+f,height:d.height,left:d.left+e,right:d.right+e,top:d.top+f,width:d.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(a){var c=this.settings,d=this.el.options;if(c.selectedValue){var e=c.selectedValue.toString();b.each(d,function(a,c){b.includes(e,c.value)&&(c.selected=!0,c.defaultSelected=!0)})}!d[0].defaultSelected&&d[0].selected&&(this.el.selectedIndex=-1),a&&c.data&&b.each(c.data,function(a,c){var e=c.hasOwnProperty("selected")&&c.selected===!0;b.each(d,function(a,b){b.value===c.value&&e&&(b.selected=!0,b.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"),a.el.tabIndex=-1,b.attr(a.el,{"aria-hidden":!0}),a.disabled&&(a.el.disabled=!0),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",disabled:a.disabled,tabIndex:a.el.tabIndex,"aria-expanded":!1}),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",role:"tree","aria-hidden":!0,"aria-expanded":!1}),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),(a.el.multiple&&e.clearable||!a.el.multiple&&e.allowDeselect)&&(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...",tagIndex:0,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),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",tagIndex:-1,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),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",role:"group"});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,role:"treeitem","aria-selected":!1});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=r.bind(a),this.events.reset=this.reset.bind(a),a.requiresPagination=a.settings.data&&a.settings.data.length>a.settings.pagination,b.on(a.container,"click",function(c){if(a.disabled)return!1;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)}),b.on(a.optsOptions,"mouseover",function(c){var d=c.target;b.hasClass(d,"selectr-option")&&(b.removeClass(a.items[a.activeIdx],"active"),b.addClass(d,"active"),a.activeIdx=[].slice.call(a.optsOptions.children).indexOf(d))}),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.settings.closeOnScroll&&a.close(),this.width&&(this.container.style.width=this.width),q.call(a)},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));var c=a.el.form;c&&b.on(c,"reset",this.events.reset)},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.selected=!1,d.defaultSelected=!1)},this);b.attr(this.items[a],{"aria-selected":!0}),b.addClass(this.items[a],"selected"),b.addClass(this.container,"has-selected"),c.selected=!0,c.defaultSelected=!0,this.emit("selectr.select",c),this.emit("selectr.change",c)},j=function(a){var c=this.el.options[a];if(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{if(!this.settings.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=-1,b.removeClass(this.container,"has-selected")}b.attr(this.items[a],{"aria-selected":!1}),b.removeClass(this.items[a],"selected"),c.selected=!1,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.search(c.input.value,!0),c.navigating&&13!==a.keyCode&&(c.navigating=!1),c.settings.taggable&&c.input.value.length){var d=c.input.value.trim();if(c.searchItems.length)return!1;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:c.settings.taggable&&c.input===document.activeElement&&(c.input.value="");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.getRect(k),m=c.optsOptions.scrollTop,n=c.optsRect.top;if(g>0){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||a.activeIdx===a.list.length-1){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(){var a=b.getRect(this.selected),c=this.optsOptions.parentNode.offsetHeight,d=window.innerHeight,e=a.top+a.height+c>d;e?(b.addClass(this.container,"inverted"),this.isInverted=!0):(b.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=b.getRect(this.optsOptions)},r=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},s=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},t=function(){};t.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))}},t.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=t.prototype[b[c]]:a[b[c]]=t.prototype[b[c]];return a};var u={width:"auto",disabled:!1,searchable:!0,clearable:!1,sortSelected:!1,allowDeselect:!1,closeOnScroll:!1};return v.prototype.render=function(a){if(!this.rendered){this.disabled=!1,this.settings=b.extend(u,a),(this.settings.multiple||this.settings.taggable)&&(this.el.multiple=!0),this.settings.taggable&&(this.settings.searchable=!1),this.settings.disabled&&(this.disabled=!0),this.hasOptGroups=!1,this.el.getElementsByTagName("optgroup").length&&(this.hasOptGroups=!0),this.items=[],this.list=[],this.activeIdx=0,this.navigating=!1,t.mixin(this),e.call(this),this.update(),this.optsRect=b.getRect(this.optsOptions),this.rendered=!0;var c=this;setTimeout(function(){c.emit("selectr.init")},20)}},v.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");var a=this.el.form;a&&b.off(a,"reset",this.events.reset),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}},v.prototype.setValue=function(a){var c=this,d=b.isArray(a);return d||(a=a.toString().trim()),!(!c.el.multiple&&d)&&void b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},v.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},v.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.selected=!0,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},v.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=s(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)}},v.prototype.toggle=function(){if(!this.disabled){var a=b.hasClass(this.container,"open");a?this.close():this.open()}},v.prototype.open=function(){var a=this;b.addClass(a.container,"open"),q.call(a);var c=this.optsOptions.scrollHeight;c<=a.optsRect.height&&a.requiresPagination&&p.call(a),b.removeClass(a.container,"notice"),b.attr(a.selected,"aria-expanded",!0),b.attr(a.optsOptions,{"aria-hidden":!1,"aria-expanded":!0}),a.settings.searchable&&!a.settings.taggable&&setTimeout(function(){a.input.focus(),a.input.tabIndex=0},10),this.opened||a.emit("selectr.open"),a.opened=!0},v.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),a&&(b.removeClass(this.container,"notice"),this.notice.textContent=""),b.removeClass(this.container,"open"),b.attr(this.selected,"aria-expanded",!1),b.attr(this.optsOptions,{"aria-hidden":!0,"aria-expanded":!1}),m.call(this),this.opened&&this.emit("selectr.close"),this.opened=!1},v.prototype.reset=function(){this.clear(),c.call(this,!0),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset")},v.prototype.clear=function(){if(this.el.multiple){var a=this.selectedIndexes.slice();b.each(a,function(a,b){j.call(this,b)},this)}else j.call(this,this.selectedIndex);this.emit("selectr.clear")},v.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},v.prototype.serialize=function(a){return this.serialise(a)},v.prototype.enable=function(){this.disabled&&(this.disabled=!1,this.el.disabled=!1,b.removeClass(this.container,"disabled"))},v.prototype.disable=function(){this.disabled||(this.disabled=!0,this.el.disabled=!0,b.addClass(this.container,"disabled"))},v.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},v}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
@@ -88,6 +88,12 @@ # 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) | ||
### v2.1.5 | ||
* Improved accessibility | ||
* New methods `disable()` and `enable()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
* Fixed navigation bug ([#5](https://github.com/Mobius1/Selectr/issues/5)) | ||
### v2.1.4 | ||
* Fixed form.reset() not clearing Selectr elements ([#13](https://github.com/Mobius1/Selectr/issues/13)) | ||
* Fixed form.reset() not resetting Selectr elements ([#13](https://github.com/Mobius1/Selectr/issues/13)) | ||
* Fixed autocomplete bug in Chrome ([#12](https://github.com/Mobius1/Selectr/issues/12)) | ||
* Fixed pagination not incrementing when navigating with the down key | ||
* Fixed `reset()` method not checking `data` | ||
@@ -94,0 +100,0 @@ ### v2.1.3 |
/*! | ||
* Selectr 2.1.4 | ||
* Selectr 2.1.5 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -77,2 +77,11 @@ * | ||
}, | ||
attr: function(el, attr, val) { | ||
if ( util.isObject(attr) ) { | ||
util.each(attr, function(a,v) { | ||
el.setAttribute(a,v); | ||
}); | ||
} else { | ||
el.setAttribute(attr, val); | ||
} | ||
}, | ||
isObject: function(a) { | ||
@@ -103,15 +112,15 @@ return "[object Object]" === Object.prototype.toString.call(a); | ||
}, | ||
getBoundingRect: function(el, abs) { | ||
var win = window; | ||
var rect = el.getBoundingClientRect(); | ||
var offsetX = abs ? win.pageXOffset : 0; | ||
var offsetY = abs ? win.pageYOffset : 0; | ||
getRect: function(el, abs) { | ||
var w = window; | ||
var r = el.getBoundingClientRect(); | ||
var x = abs ? w.pageXOffset : 0; | ||
var y = abs ? w.pageYOffset : 0; | ||
return { | ||
bottom: rect.bottom + offsetY, | ||
height: rect.height, | ||
left : rect.left + offsetX, | ||
right : rect.right + offsetX, | ||
top : rect.top + offsetY, | ||
width : rect.width | ||
bottom: r.bottom + y, | ||
height: r.height, | ||
left : r.left + x, | ||
right : r.right + x, | ||
top : r.top + y, | ||
width : r.width | ||
}; | ||
@@ -132,9 +141,10 @@ }, | ||
var o = this.settings; | ||
var opts = this.el.options; | ||
var ops = this.el.options; | ||
if ( o.selectedValue ) { | ||
var val = o.selectedValue.toString(); | ||
util.each(opts, function(i, opt) { | ||
if ( util.includes(val, opt.value) ) { | ||
opt.defaultSelected = true; | ||
util.each(ops, function(i, op) { | ||
if ( util.includes(val, op.value) ) { | ||
op.selected = true; | ||
op.defaultSelected = true; | ||
} | ||
@@ -144,3 +154,3 @@ }); | ||
if ( !opts[0].defaultSelected && opts[0].selected ) { | ||
if ( !ops[0].defaultSelected && ops[0].selected ) { | ||
this.el.selectedIndex = -1; | ||
@@ -154,5 +164,6 @@ } | ||
var selected = itm.hasOwnProperty('selected') && itm.selected === true; | ||
util.each(opts, function(i, opt) { | ||
if ( opt.value === itm.value && selected ) { | ||
opt.defaultSelected = true; | ||
util.each(ops, function(i, op) { | ||
if ( op.value === itm.value && selected ) { | ||
op.selected = true; | ||
op.defaultSelected = true; | ||
} | ||
@@ -166,3 +177,4 @@ }); | ||
var setWidth = function() { | ||
var w = this.settings.width; | ||
var o = this.settings; | ||
var w = o.width; | ||
@@ -175,4 +187,4 @@ if (w) { | ||
w = "100%"; | ||
} else if (util.includes(this.settings.width, "%")) { | ||
w = this.settings.width; | ||
} else if (util.includes(o.width, "%")) { | ||
w = o.width; | ||
} | ||
@@ -188,4 +200,18 @@ } | ||
var o = _.settings; | ||
util.addClass(_.el, "hidden-input"); | ||
// We don't want to focus on the native select box | ||
_.el.tabIndex = -1; | ||
util.attr(_.el, { | ||
"aria-hidden": true | ||
}); | ||
if ( _.disabled ) { | ||
_.el.disabled = true; | ||
} | ||
// Check for data | ||
@@ -243,8 +269,7 @@ if ( o.data ) { | ||
class: "selectr-selected", | ||
tabIndex: _.el.tabIndex // enable tabIndex (#9) | ||
disabled: _.disabled, | ||
tabIndex: _.el.tabIndex, // enable tabIndex (#9) | ||
"aria-expanded": false | ||
}); | ||
// We don't want to focus on the native select box | ||
_.el.tabIndex = -1; | ||
_.label = util.createElement(_.el.multiple ? "ul" : "span", { | ||
@@ -259,3 +284,6 @@ class: "selectr-label" | ||
_.optsOptions = util.createElement("ul", { | ||
class: "selectr-options" | ||
class: "selectr-options", | ||
role: "tree", | ||
"aria-hidden": true, | ||
"aria-expanded": false | ||
}); | ||
@@ -298,4 +326,9 @@ | ||
placeholder: "Enter a tag...", | ||
tagIndex: 0, | ||
autocomplete: "off", | ||
tagIndex: 0 | ||
autocorrect: "off", | ||
autocapitalize: "off", | ||
spellcheck: "false", | ||
role: "textbox", | ||
type: "search" | ||
}); | ||
@@ -316,4 +349,9 @@ | ||
class: "selectr-input", | ||
tagIndex: -1, | ||
autocomplete: "off", | ||
tagIndex: -1 | ||
autocorrect: "off", | ||
autocapitalize: "off", | ||
spellcheck: "false", | ||
role: "textbox", | ||
type: "search" | ||
}); | ||
@@ -343,3 +381,4 @@ _.inputClear = util.createElement("button", { | ||
var group = util.createElement("ul", { | ||
class: "selectr-optgroup" | ||
class: "selectr-optgroup", | ||
role: "group" | ||
}); | ||
@@ -394,3 +433,5 @@ group.appendChild(util.createElement("li", { | ||
class: "selectr-option", | ||
html: content | ||
html: content, | ||
role: "treeitem", | ||
"aria-selected": false | ||
}); | ||
@@ -431,2 +472,3 @@ | ||
this.events.dismiss = dismiss.bind(_); | ||
this.events.reset = this.reset.bind(_); | ||
@@ -437,11 +479,14 @@ _.requiresPagination = _.settings.data && _.settings.data.length > _.settings.pagination; | ||
util.on(_.container, "click", function(e) { | ||
if ( _.disabled ) return false; | ||
e = e || window.event; | ||
var target = e.target; | ||
var t = e.target; | ||
var isSelected = util.closest(target, function(el) { | ||
var isSelected = util.closest(t, function(el) { | ||
return (el && el == _.selected); | ||
}); | ||
if ( target === _.input ) { | ||
if ( t === _.input ) { | ||
return; | ||
@@ -452,3 +497,3 @@ } | ||
// Clear | ||
if ( _.settings.clearable && target === _.selectClear ) { | ||
if ( _.settings.clearable && t === _.selectClear ) { | ||
_.clear(); | ||
@@ -459,13 +504,13 @@ return; | ||
// Remove tag button | ||
if (util.hasClass(target, "selectr-tag-remove")) { | ||
deselect.call(_, target.parentNode.idx); | ||
if (util.hasClass(t, "selectr-tag-remove")) { | ||
deselect.call(_, t.parentNode.idx); | ||
} | ||
// Click on placeholder or selected text | ||
if (target === _.label || target === _.placeEl) { | ||
target = _.placeEl.parentNode; | ||
if (t === _.label || t === _.placeEl) { | ||
t = _.placeEl.parentNode; | ||
} | ||
// Open / close dropdown | ||
if (isSelected || target === _.selected) { | ||
if (isSelected || t === _.selected) { | ||
_.toggle(); | ||
@@ -475,4 +520,4 @@ } | ||
// Select option | ||
if (util.hasClass(target, "selectr-option")) { | ||
var index = _.items.indexOf(target); | ||
if (util.hasClass(t, "selectr-option")) { | ||
var index = _.items.indexOf(t); | ||
change.call(_, index); | ||
@@ -488,2 +533,12 @@ } | ||
// Mouseover list items | ||
util.on(_.optsOptions, "mouseover", function(e) { | ||
var t = e.t; | ||
if ( util.hasClass(t, "selectr-option") ) { | ||
util.removeClass(_.items[_.activeIdx], "active"); | ||
util.addClass(t, "active"); | ||
_.activeIdx = [].slice.call(_.optsOptions.children).indexOf(t); | ||
} | ||
}); | ||
if (_.settings.searchable) { | ||
@@ -524,3 +579,3 @@ util.on(_.inputClear, "click", function(e) { | ||
if ( form ) { | ||
util.on(form, "reset", _.reset.bind(_)); | ||
util.on(form, "reset", this.events.reset); | ||
} | ||
@@ -578,3 +633,3 @@ }; | ||
util.each(this.el.options, function(i, option) { | ||
util.each(this.el.options, function(i, o) { | ||
var opt = this.items[i]; | ||
@@ -586,3 +641,4 @@ | ||
} | ||
option.defaultSelected = false; | ||
o.selected = false; | ||
o.defaultSelected = false; | ||
} | ||
@@ -592,5 +648,10 @@ }, this); | ||
util.attr(this.items[index], { | ||
"aria-selected": true | ||
}); | ||
util.addClass(this.items[index], "selected"); | ||
util.addClass(this.container, "has-selected"); | ||
option.selected = true; | ||
option.defaultSelected = true; | ||
@@ -630,4 +691,9 @@ | ||
util.attr(this.items[index], { | ||
"aria-selected": false | ||
}); | ||
util.removeClass(this.items[index], "selected"); | ||
option.selected = false; | ||
option.defaultSelected = false; | ||
@@ -763,3 +829,3 @@ | ||
if ( _.settings.taggable && _.input.value.length ) { | ||
var value = _.input.value.trim(); | ||
var val = _.input.value.trim(); | ||
@@ -773,8 +839,8 @@ if ( _.searchItems.length ) { | ||
util.each(_.tagSeperators, function(i,k) { | ||
value = value.replace(k, ''); | ||
val = val.replace(k, ''); | ||
}); | ||
var option = _.addOption({ | ||
value: value, | ||
text: value, | ||
value: val, | ||
text: val, | ||
selected: true | ||
@@ -832,27 +898,27 @@ }); | ||
var nextElem = list[_.activeIdx]; | ||
var nextRect = util.getBoundingRect(nextElem); | ||
var optsTop = _.optsOptions.scrollTop; | ||
var nxtEl = list[_.activeIdx]; | ||
var nxtRct = util.getRect(nxtEl); | ||
var opsTop = _.optsOptions.scrollTop; | ||
var offset = _.optsRect.top; | ||
var currentOffset, nextOffset; | ||
var curOffset, nxtOffset; | ||
if (dir > 0) { | ||
var nextTop = nextRect.top; | ||
currentOffset = offset; | ||
nextOffset = optsTop + (nextTop - currentOffset); | ||
var nxtTp = nxtRct.top; | ||
curOffset = offset; | ||
nxtOffset = opsTop + (nxtTp - curOffset); | ||
if (_.activeIdx === 0) { | ||
_.optsOptions.scrollTop = 0; | ||
} else if (nextTop - currentOffset < 0) { | ||
_.optsOptions.scrollTop = nextOffset; | ||
} else if (nxtTp - curOffset < 0) { | ||
_.optsOptions.scrollTop = nxtOffset; | ||
} | ||
} else { | ||
var nextBottom = nextRect.top + nextRect.height; | ||
currentOffset = offset + _.optsRect.height; | ||
nextOffset = optsTop + (nextBottom - currentOffset); | ||
var nxtBottom = nxtRct.top + nxtRct.height; | ||
curOffset = offset + _.optsRect.height; | ||
nxtOffset = opsTop + (nxtBottom - curOffset); | ||
if (_.activeIdx === 0) { | ||
_.optsOptions.scrollTop = 0; | ||
} else if (nextBottom > currentOffset) { | ||
_.optsOptions.scrollTop = nextOffset; | ||
} else if (nxtBottom > curOffset) { | ||
_.optsOptions.scrollTop = nxtOffset; | ||
} | ||
@@ -903,3 +969,3 @@ | ||
var checkInvert = function() { | ||
var s = util.getBoundingRect(this.selected); | ||
var s = util.getRect(this.selected); | ||
var o = this.optsOptions.parentNode.offsetHeight; | ||
@@ -917,3 +983,3 @@ var wh = window.innerHeight; | ||
this.optsRect = util.getBoundingRect(this.optsOptions); | ||
this.optsRect = util.getRect(this.optsOptions); | ||
}; | ||
@@ -972,2 +1038,3 @@ | ||
width: "auto", | ||
disabled: false, | ||
searchable: true, | ||
@@ -1017,2 +1084,4 @@ clearable: false, | ||
* Render the instance | ||
* @param {object} options | ||
* @return {void} | ||
*/ | ||
@@ -1023,2 +1092,4 @@ Selectr.prototype.render = function(options) { | ||
this.disabled = false; | ||
this.settings = util.extend(defaults, options); | ||
@@ -1034,2 +1105,6 @@ | ||
if ( this.settings.disabled ) { | ||
this.disabled = true; | ||
} | ||
// Check for optgroupd | ||
@@ -1056,3 +1131,3 @@ this.hasOptGroups = false; | ||
this.optsRect = util.getBoundingRect(this.optsOptions); | ||
this.optsRect = util.getRect(this.optsOptions); | ||
@@ -1069,2 +1144,3 @@ this.rendered = true; | ||
* Destroy the instance | ||
* @return {void} | ||
*/ | ||
@@ -1092,2 +1168,8 @@ Selectr.prototype.destroy = function() { | ||
// Remove reset listener from parent form | ||
var form = this.el.form; | ||
if ( form ) { | ||
util.off(form, "reset", this.events.reset); | ||
} | ||
// Remove event listeners attached to doc and win | ||
@@ -1174,2 +1256,6 @@ util.off(document, "click", this.events.dismiss); | ||
/** | ||
* Add a new option | ||
* @param {object} data | ||
*/ | ||
Selectr.prototype.addOption = function(data) { | ||
@@ -1207,2 +1293,3 @@ var _ = this; | ||
if ( data.selected ) { | ||
option.selected = true; | ||
option.defaultSelected = true; | ||
@@ -1298,2 +1385,3 @@ } | ||
* Toggle the dropdown | ||
* @return {void} | ||
*/ | ||
@@ -1313,2 +1401,3 @@ Selectr.prototype.toggle = function() { | ||
* Open the dropdown | ||
* @return {void} | ||
*/ | ||
@@ -1332,2 +1421,8 @@ Selectr.prototype.open = function() { | ||
util.attr(_.selected, "aria-expanded", true); | ||
util.attr(_.optsOptions, { | ||
"aria-hidden": false, | ||
"aria-expanded": true | ||
}); | ||
if (_.settings.searchable && !_.settings.taggable) { | ||
@@ -1350,2 +1445,3 @@ setTimeout(function() { | ||
* Close the dropdown | ||
* @return {void} | ||
*/ | ||
@@ -1369,2 +1465,8 @@ Selectr.prototype.close = function() { | ||
util.attr(this.selected, "aria-expanded", false); | ||
util.attr(this.optsOptions, { | ||
"aria-hidden": true, | ||
"aria-expanded": false | ||
}); | ||
clearSearch.call(this); | ||
@@ -1381,4 +1483,5 @@ | ||
* Reset to initial state | ||
* @return {void} | ||
*/ | ||
Selectr.prototype.reset = function(init) { | ||
Selectr.prototype.reset = function() { | ||
this.clear(); | ||
@@ -1399,4 +1502,5 @@ | ||
* Clear all selections | ||
* @return {void} | ||
*/ | ||
Selectr.prototype.clear = function(init) { | ||
Selectr.prototype.clear = function() { | ||
if ( this.el.multiple ) { | ||
@@ -1415,2 +1519,7 @@ // Copy the array for reference otherwise it'll chuck an error | ||
/** | ||
* Return serialised data | ||
* @param {boolean} toJson | ||
* @return {mixed} Returns either an object or JSON string | ||
*/ | ||
Selectr.prototype.serialise = function(toJson) { | ||
@@ -1436,3 +1545,5 @@ var data = []; | ||
/* Friends from across the pond */ | ||
/** | ||
* Localised version of serialise() method | ||
*/ | ||
Selectr.prototype.serialize = function(toJson) { | ||
@@ -1443,2 +1554,26 @@ return this.serialise(toJson); | ||
/** | ||
* Enable the element | ||
* @return {void} | ||
*/ | ||
Selectr.prototype.enable = function() { | ||
if ( this.disabled ) { | ||
this.disabled = false; | ||
this.el.disabled = false; | ||
util.removeClass(this.container, "disabled"); | ||
} | ||
}; | ||
/** | ||
* Disable the element | ||
* @return {void} | ||
*/ | ||
Selectr.prototype.disable = function() { | ||
if ( !this.disabled ) { | ||
this.disabled = true; | ||
this.el.disabled = true; | ||
util.addClass(this.container, "disabled"); | ||
} | ||
}; | ||
/** | ||
* Display a message | ||
@@ -1456,2 +1591,2 @@ * @param {string} message The 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
77515
1734
141