select-picker
Advanced tools
Comparing version 0.3.0 to 0.3.1
{ | ||
"name": "select-picker", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"authors": [ | ||
@@ -8,3 +8,3 @@ "Adam Uhlir <hello@adam-uhlir.me>" | ||
"description": "Multi-select tags like picker as plugin for jQuery", | ||
"main": "js/picker.js", | ||
"main": ["dist/picker.min.js", "dist/picker.min.css"], | ||
"keywords": [ | ||
@@ -18,3 +18,3 @@ "jquery", | ||
"dependencies": { | ||
"jquery": "~2.1.4" | ||
"jquery": "^3.1.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "devDependencies": { |
@@ -1,1 +0,1 @@ | ||
"use strict";!function(t,i,e,n){var r=function(i,e){this.elem=i,this.$elem=t(i),this.options=e,this.currentData=[]};r.prototype={defaults:{containerClass:"",containerWidth:!1,width:!1,search:!1,searchAutofocus:!1,autofocusScrollOffset:0,coloring:{},texts:{trigger:"Select value",noResult:"No results",search:"Search"}},config:{},init:function(){return this.config=t.extend({},this.defaults,this.options),this.$elem.is("select")?(this.config.multiple=this.$elem.is("select[multiple='multiple']")||this.$elem.is("select[multiple]"),this.config.width===!1||Math.floor(this.config.width)==this.config.width&&t.isNumeric(this.config.width)?this.config.containerWidth===!1||Math.floor(this.config.containerWidth)==this.config.containerWidth&&t.isNumeric(this.config.containerWidth)?0==this.$elem.find("option:not([hidden])").length?void console.log("Picker - Select has no options. Can not proceed!"):(this._build(),this.$elem.hide(),this._fillList(),this.$container.find(".pc-trigger").click(function(){var i=this.$container.find(".pc-list");i.toggle(),this.$elem.trigger(i.is(":visible")?"sp-open":"sp-close"),this.config.search&&this.config.searchAutofocus&&i.is(":visible")&&(i.find("input").focus(),t("html, body").animate({scrollTop:i.find("input").offset().top-this.config.autofocusScrollOffset},800))}.bind(this)),t(e).mouseup(function(t){var i=this.$container.find(".pc-list");i.is(t.target)||0!==i.has(t.target).length||this.$container.find(".pc-trigger").is(t.target)||(i.hide(),this.$elem.trigger("sp-close"),this.config.search&&(this.$container.find(".pc-list input").val(""),this._updateList(this.currentData)))}.bind(this)),this):void console.log("Picker - Container width is not a integer."):void console.log("Picker - Width is not a integer.")):void console.log("Picker - Element is not Selectbox")},pc_selected:function(i){var e=t(i.target),n=e.data("id");this._selectElement(n,e),this.$container.find(".pc-list").hide(),this.config.search&&(this.$container.find(".pc-list input").val(""),this._updateList(this.currentData)),this.$elem.trigger("sp-change")},pc_remove:function(i){var e=t(i.target),n=e.parent().data("id"),r=e.parent().data("order"),s=t("<li>").html(e.parent().text()).attr("data-id",n).attr("data-order",r);s.click(this.pc_selected.bind(this)),this.config.search&&this._insertIntoCurrentData(i);var a=this.$container.find(".pc-list li");this.$container.find(".pc-trigger").is(":visible")?1==a.size()?r>a.data("order")?s.insertAfter(a):s.insertBefore(a):a.each(function(i,e){if(e=t(e),e.is(":first-child")){if(r<e.data("order"))return s.insertBefore(e),!1;if(r>e.data("order")&&r<e.next().data("order"))return s.insertAfter(e),!1}else if(e.is(":last-child")){if(r>e.data("order"))return s.insertAfter(e),!1}else if(r>e.data("order")&&r<e.next().data("order"))return s.insertAfter(e),!1}):(this.$container.find(".pc-list ul").html("").append(s),this.$container.find(".pc-trigger").show()),this.$elem.find(" option[value='"+n+"']").removeAttr("selected"),e.parent().remove(),this.$elem.trigger("sp-change")},pc_search:function(i){var e=t(i.target).val().toLowerCase(),n=this._filterData(e);this._updateList(n,e)},_selectElement:function(t,i){return i==n&&(i=this.$container.find('.pc-list li[data-id="'+t+'"]'),0==i.length)?void console.log("Picker - ID to select not found!"):(this.config.multiple?(this.$container.prepend(this._createElement(i)),i.remove(),this.config.search?(this.currentData=this.currentData.filter(function(i){return i.id!=t}),0==this.currentData.length&&this.$container.find(".pc-trigger").hide()):0==this.$container.find(".pc-list li").size()&&this.$container.find(".pc-trigger").hide()):(this.$elem.find("option").removeAttr("selected"),this.config.coloring[t]?this.$container.find(".pc-trigger").removeClass().addClass(this.config.coloring[selectedId]+" pc-trigger pc-element").contents().first().replaceWith(i.text()):this.$container.find(".pc-trigger").contents().first().replaceWith(i.text())),void this.$elem.find("option[value='"+t+"']").attr("selected","selected"))},_insertIntoCurrentData:function(i){var e=t(i.target),n=e.parent().data("id"),r=e.parent().data("order");if(0==this.currentData.length)return void(this.currentData=[{id:n,text:e.parent().text(),order:r}]);var s;for(s=0;s<this.currentData.length;s++)if(0==s){if(r<this.currentData[s].order||1==this.currentData.length){this.currentData.splice(0,0,{id:n,text:e.parent().text(),order:r});break}}else if(s==this.currentData.length-1){if(r>this.currentData[s].order){this.currentData.splice(s,0,{id:n,text:e.parent().text(),order:r});break}}else if(this.currentData[s-1].order<r&&r<this.currentData[s].order){this.currentData.splice(s,0,{id:n,text:e.parent().text(),order:r});break}},_createElement:function(i){var e=this.config.coloring[i.data("id")],n=t("<span>").addClass("pc-element "+(e?e:"")).text(i.text()).attr("data-id",i.data("id")).attr("data-order",i.data("order"));return n.append(t('<span class="pc-close"></span>').click(this.pc_remove.bind(this))),n},_build:function(){var i=this.config.texts.trigger;this.$container=t("<div class='picker"+(this.config.containerClass?" "+this.config.containerClass:"")+"'><span class='pc-select'><span class='pc-element pc-trigger'>"+i+"</span><span class='pc-list' "+(this.config.width?"style='width:"+this.config.width+"px; display:none;'":"style='display:none;'")+"><ul></ul></span></span></div>"),this.config.containerWidth!==!1&&this.$container.width(this.config.containerWidth),this.$container.insertAfter(this.$elem),this.config.search&&this._buildSearch()},_buildSearch:function(){var i=t("<input type='search' placeholder='"+this.config.texts.search+"'>");i.on("input",this.pc_search.bind(this)),i.on("keypress",function(i){if(13==i.which){var e=t(i.target).val().toLowerCase(),n=this._filterData(e);if(1==n.length)return this.$container.find(".pc-list li").first().click(),!1}return!0}.bind(this)),this.$container.find(".pc-list").prepend(i)},_fillList:function(){var i=this.$container.find(".pc-list ul"),e=0;this.$elem.find("option:not([hidden])").each(function(n,r){var s=t("<li>").html(t(r).text()).attr("data-id",t(r).attr("value")).attr("data-order",e);s.click(this.pc_selected.bind(this)),i.append(s),this.config.search&&this.currentData.push({id:t(r).attr("value"),text:t(r).text(),order:e}),"selected"==t(r).attr("selected")&&s.click(),e++}.bind(this)),this.$container.find(".pc-trigger").show()},_filterData:function(t){return this.currentData.filter(function(i){return-1!=i.text.toLowerCase().indexOf(t)})},_updateList:function(i,e){var r=this.$container.find(".pc-list ul");if(0==i.length)return void r.html('<li class="not-found">'+this.config.texts.noResult+"</li>");r.html("");var s,a;for(s=0;s<i.length;s++){if(e!==n){var c=new RegExp("("+e+")","gi");a=i[s].text.replace(c,'<span class="searched">$1</span>')}else a=i[s].text;var o=t("<li>").html(a).attr("data-id",i[s].id).attr("data-order",i[s].order);o.click(this.pc_selected.bind(this)),r.append(o)}},api:function(t){return r.prototype["api_"+t[0]]?this["api_"+t[0]](t.slice(1)):void console.log("Picker - unknown command!")},api_destroy:function(){return this.$container.remove(),this.$elem.show(),this.$elem.removeData("plugin_picker"),this.$elem},api_get:function(){return this.$elem.val()},api_set:function(t){return 1!=t.length?void console.log("Picker - unknown number of arguments."):(this._selectElement(t[0]),this.$elem.trigger("sp-change"),this.$elem)},api_remove:function(t){if(1!=t.length)return void console.log("Picker - unknown number of arguments.");if(!this.config.multiple)return void console.log("Picker - remove method is allowed only with multiple-selection mode!");var i={};return i.target=this.$container.find('.pc-element[data-id="'+t[0]+'"] .pc-close')[0],this.pc_remove(i),this.$elem}},t.fn.picker=function(i){var e=arguments;if(1==t(this).length){var n=t(this).data("plugin_picker");return n?n.api(Array.prototype.slice.call(e)):(t(this).data("plugin_picker",new r(this,i).init()),this)}return this.each(function(){var n=t(this).data("plugin_picker");n?n.api(Array.prototype.slice.call(e)):t(this).data("plugin_picker",new r(this,i).init())})}}(jQuery,window,document); | ||
"use strict";!function(t,i,e,n){var r=function(i,e){this.elem=i,this.$elem=t(i),this.options=e,this.currentData=[]};r.prototype={defaults:{containerClass:"",containerWidth:!1,width:!1,search:!1,searchAutofocus:!1,autofocusScrollOffset:0,coloring:{},limit:n,texts:{trigger:"Select value",noResult:"No results",search:"Search"}},config:{},init:function(){return this.config=t.extend({},this.defaults,this.options),this.$elem.is("select")?(this.config.multiple=this.$elem.is("select[multiple='multiple']")||this.$elem.is("select[multiple]"),this.config.width===!1||Math.floor(this.config.width)==this.config.width&&t.isNumeric(this.config.width)?this.config.containerWidth===!1||Math.floor(this.config.containerWidth)==this.config.containerWidth&&t.isNumeric(this.config.containerWidth)?0==this.$elem.find("option:not([hidden])").length?void console.log("Picker - Select has no options. Can not proceed!"):(!this.config.multiple&&this.config.limit>0&&console.log("Picker - You are applying limit parameter on single-seleciton mode Picker!"),this.config.limit<0?void console.log("Picker - Limit has to be greater then 0!"):(this._build(),this.$elem.hide(),this._fillList(),this.$container.find(".pc-trigger").click(function(){var i=this.$container.find(".pc-list");i.toggle(),this.$elem.trigger(i.is(":visible")?"sp-open":"sp-close"),this.config.search&&this.config.searchAutofocus&&i.is(":visible")&&(i.find("input").focus(),t("html, body").animate({scrollTop:i.find("input").offset().top-this.config.autofocusScrollOffset},800))}.bind(this)),t(e).mouseup(function(t){var i=this.$container.find(".pc-list");i.is(t.target)||0!==i.has(t.target).length||this.$container.find(".pc-trigger").is(t.target)||(i.hide(),this.$elem.trigger("sp-close"),this.config.search&&(this.$container.find(".pc-list input").val(""),this._updateList(this.currentData)))}.bind(this)),this)):void console.log("Picker - Container width is not a integer."):void console.log("Picker - Width is not a integer.")):void console.log("Picker - Element is not Selectbox")},pc_selected:function(i){var e=t(i.target),n=e.data("id");this._selectElement(n,e),this.$container.find(".pc-list").hide(),this.config.search&&(this.$container.find(".pc-list input").val(""),this._updateList(this.currentData)),this.$elem.trigger("sp-change")},pc_remove:function(i){var e=t(i.target),n=e.parent().data("id"),r=e.parent().data("order"),s=t("<li>").html(e.parent().text()).attr("data-id",n).attr("data-order",r);s.click(this.pc_selected.bind(this)),this.config.search&&this._insertIntoCurrentData(i),this.$container.find(".pc-trigger").show();var a=this.$container.find(".pc-list li");0==this.$container.find(".pc-list li").length?this.$container.find(".pc-list ul").html("").append(s):1==a.length?r>a.data("order")?s.insertAfter(a):s.insertBefore(a):a.each(function(i,e){if(e=t(e),e.is(":first-child")){if(r<e.data("order"))return s.insertBefore(e),!1;if(r>e.data("order")&&r<e.next().data("order"))return s.insertAfter(e),!1}else if(e.is(":last-child")){if(r>e.data("order"))return s.insertAfter(e),!1}else if(r>e.data("order")&&r<e.next().data("order"))return s.insertAfter(e),!1}),this.$elem.find(" option[value='"+n+"']").removeAttr("selected"),e.parent().remove(),this.$elem.trigger("sp-change")},pc_search:function(i){var e=t(i.target).val().toLowerCase(),n=this._filterData(e);this._updateList(n,e)},_selectElement:function(t,i){if(i==n&&(i=this.$container.find('.pc-list li[data-id="'+t+'"]'),0==i.length))return void console.log("Picker - ID to select not found!");if(this.config.multiple){this.$container.prepend(this._createElement(i)),i.remove();var e=this.config.limit&&this.$container.find(".pc-element:not(.pc-trigger)").length>=this.config.limit;this.config.search?(this.currentData=this.currentData.filter(function(i){return i.id!=t}),(0==this.currentData.length||e)&&this.$container.find(".pc-trigger").hide()):(0==this.$container.find(".pc-list li").length||e)&&this.$container.find(".pc-trigger").hide()}else this.$elem.find("option").removeAttr("selected"),this.config.coloring[t]?this.$container.find(".pc-trigger").removeClass().addClass(this.config.coloring[selectedId]+" pc-trigger pc-element").contents().first().replaceWith(i.text()):this.$container.find(".pc-trigger").contents().first().replaceWith(i.text());this.$elem.find("option[value='"+t+"']").attr("selected","selected")},_insertIntoCurrentData:function(i){var e=t(i.target),n=e.parent().data("id"),r=e.parent().data("order");if(0==this.currentData.length)return void(this.currentData=[{id:n,text:e.parent().text(),order:r}]);var s;for(s=0;s<this.currentData.length;s++)if(0==s){if(r<this.currentData[s].order||1==this.currentData.length){this.currentData.splice(0,0,{id:n,text:e.parent().text(),order:r});break}}else if(s==this.currentData.length-1){if(r>this.currentData[s].order){this.currentData.splice(s,0,{id:n,text:e.parent().text(),order:r});break}}else if(this.currentData[s-1].order<r&&r<this.currentData[s].order){this.currentData.splice(s,0,{id:n,text:e.parent().text(),order:r});break}},_createElement:function(i){var e=this.config.coloring[i.data("id")],n=t("<span>").addClass("pc-element "+(e?e:"")).text(i.text()).attr("data-id",i.data("id")).attr("data-order",i.data("order"));return n.append(t('<span class="pc-close"></span>').click(this.pc_remove.bind(this))),n},_build:function(){var i=this.config.texts.trigger;this.$container=t("<div class='picker"+(this.config.containerClass?" "+this.config.containerClass:"")+"'><span class='pc-select'><span class='pc-element pc-trigger'>"+i+"</span><span class='pc-list' "+(this.config.width?"style='width:"+this.config.width+"px; display:none;'":"style='display:none;'")+"><ul></ul></span></span></div>"),this.config.containerWidth!==!1&&this.$container.width(this.config.containerWidth),this.$container.insertAfter(this.$elem),this.config.search&&this._buildSearch()},_buildSearch:function(){var i=t("<input type='search' placeholder='"+this.config.texts.search+"'>");i.on("input",this.pc_search.bind(this)),i.on("keypress",function(i){if(13==i.which){var e=t(i.target).val().toLowerCase(),n=this._filterData(e);if(1==n.length)return this.$container.find(".pc-list li").first().click(),!1}return!0}.bind(this)),this.$container.find(".pc-list").prepend(i)},_fillList:function(){var i=this.$container.find(".pc-list ul"),e=0;this.$elem.find("option:not([hidden])").each(function(n,r){var s=t("<li>").html(t(r).text()).attr("data-id",t(r).attr("value")).attr("data-order",e);s.click(this.pc_selected.bind(this)),i.append(s),this.config.search&&this.currentData.push({id:t(r).attr("value"),text:t(r).text(),order:e}),"selected"==t(r).attr("selected")&&s.click(),e++}.bind(this)),this.$container.find(".pc-trigger").show()},_filterData:function(t){return this.currentData.filter(function(i){return-1!=i.text.toLowerCase().indexOf(t)})},_updateList:function(i,e){var r=this.$container.find(".pc-list ul");if(0==i.length)return void r.html('<li class="not-found">'+this.config.texts.noResult+"</li>");r.html("");var s,a;for(s=0;s<i.length;s++){if(e!==n){var c=new RegExp("("+e+")","gi");a=i[s].text.replace(c,'<span class="searched">$1</span>')}else a=i[s].text;var o=t("<li>").html(a).attr("data-id",i[s].id).attr("data-order",i[s].order);o.click(this.pc_selected.bind(this)),r.append(o)}},api:function(t){return r.prototype["api_"+t[0]]?this["api_"+t[0]](t.slice(1)):void console.log("Picker - unknown command!")},api_destroy:function(){return this.$container.remove(),this.$elem.show(),this.$elem.removeData("plugin_picker"),this.$elem},api_get:function(){return this.$elem.val()},api_set:function(t){return 1!=t.length?void console.log("Picker - unknown number of arguments."):(this._selectElement(t[0]),this.$elem.trigger("sp-change"),this.$elem)},api_remove:function(t){if(1!=t.length)return void console.log("Picker - unknown number of arguments.");if(!this.config.multiple)return void console.log("Picker - remove method is allowed only with multiple-selection mode!");var i={};return i.target=this.$container.find('.pc-element[data-id="'+t[0]+'"] .pc-close')[0],this.pc_remove(i),this.$elem}},t.fn.picker=function(i){var e=arguments;if(1==t(this).length){var n=t(this).data("plugin_picker");return n?n.api(Array.prototype.slice.call(e)):(t(this).data("plugin_picker",new r(this,i).init()),this)}return this.each(function(){var n=t(this).data("plugin_picker");n?n.api(Array.prototype.slice.call(e)):t(this).data("plugin_picker",new r(this,i).init())})}}(jQuery,window,document); |
@@ -8,2 +8,3 @@ 'use strict'; | ||
var mainBowerFiles = require('main-bower-files'); | ||
var merge = require('merge-stream'); | ||
@@ -46,6 +47,3 @@ var LessPluginCleanCSS = require("less-plugin-clean-css"), | ||
gulp.task('doc:js', function() { | ||
var files = mainBowerFiles({'includeDev': true}); | ||
files.push('src/picker.js'); | ||
return gulp.src(files) | ||
var libs = gulp.src(mainBowerFiles({'includeDev': true})) | ||
.pipe(plugins.ignore.include('*.js')) | ||
@@ -56,2 +54,8 @@ .on('error', plugins.notify.onError("Error: <%= error.file %> <%= error.message %>")) | ||
.pipe(gulp.dest('doc/js/')); | ||
var src = gulp.src('src/picker.js') | ||
.pipe(gulp.dest('doc/js/')); | ||
return merge(libs, src) | ||
.pipe(plugins.notify('JS done!')); | ||
}); | ||
@@ -83,2 +87,3 @@ | ||
gulp.watch('doc/css/main.less', ['doc:css']); | ||
gulp.watch('src/picker.js', ['doc:js']); | ||
}); | ||
@@ -85,0 +90,0 @@ |
{ | ||
"name": "select-picker", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Multi-select tags like picker as plugin for jQuery", | ||
@@ -52,4 +52,5 @@ "main": "js/picker.js", | ||
"main-bower-files": "^2.4.1", | ||
"merge-stream": "^1.0.0", | ||
"phantomjs-prebuilt": "^2.1.3" | ||
} | ||
} |
@@ -32,2 +32,3 @@ "use strict"; | ||
coloring: {}, | ||
limit: undefined, | ||
texts: { | ||
@@ -69,2 +70,11 @@ trigger : "Select value", | ||
if(!this.config.multiple && this.config.limit > 0) { | ||
console.log("Picker - You are applying limit parameter on single-seleciton mode Picker!"); | ||
} | ||
if(this.config.limit < 0) { | ||
console.log("Picker - Limit has to be greater then 0!"); | ||
return; | ||
} | ||
this._build(); | ||
@@ -138,7 +148,8 @@ this.$elem.hide(); | ||
this.$container.find(".pc-trigger").show(); | ||
var currentList = this.$container.find('.pc-list li'); | ||
if(!this.$container.find(".pc-trigger").is(':visible')) { // Empty list | ||
if(this.$container.find(".pc-list li").length == 0) { // Empty list | ||
this.$container.find('.pc-list ul').html('').append(li); | ||
this.$container.find(".pc-trigger").show(); | ||
}else if(currentList.size() == 1) { // Only one item in list | ||
}else if(currentList.length == 1) { // Only one item in list | ||
if(order > currentList.data('order')){ | ||
@@ -204,2 +215,4 @@ li.insertAfter(currentList); | ||
var reachedLimit = this.config.limit && this.$container.find(".pc-element:not(.pc-trigger)").length >= this.config.limit; | ||
if(this.config.search){ | ||
@@ -210,6 +223,6 @@ this.currentData = this.currentData.filter(function (value) { | ||
if(this.currentData.length == 0){ | ||
if(this.currentData.length == 0 || reachedLimit){ | ||
this.$container.find(".pc-trigger").hide(); | ||
} | ||
}else if(this.$container.find(".pc-list li").size() == 0) { | ||
}else if(this.$container.find(".pc-list li").length == 0 || reachedLimit) { | ||
this.$container.find(".pc-trigger").hide(); | ||
@@ -216,0 +229,0 @@ } |
@@ -299,2 +299,18 @@ 'use strict'; | ||
it("Limited options", function () { | ||
var $select = $("#test"); | ||
fillOptions($select, 50); | ||
var $container = $(".container"); | ||
$select.picker({limit: 10}); | ||
createRandomArray(10, 0, 49).forEach(function (elem) { | ||
$container.find(".pc-list ul li[data-id='" + elem + "']").click(); | ||
}); | ||
expect($container.find(".pc-trigger")).toBeHidden(); | ||
$container.find(".pc-element:not(.pc-trigger) .pc-close").first().click(); | ||
expect($container.find(".pc-trigger")).toBeVisible(); | ||
}); | ||
}); | ||
@@ -301,0 +317,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
367057
23
2487
22