Permalink
Browse files

get the latest version of gentleSelect with disallowEmpty support

  • Loading branch information...
1 parent 0642a4e commit d5dc478e2bd145193af734de9f814e03a5141a4a @shawnchin committed May 2, 2012
View
2 gentleSelect/jquery-gentleSelect-min.js
@@ -1 +1 @@
-(function(d){var g={minWidth:100,itemWidth:undefined,columns:undefined,rows:undefined,title:undefined,prompt:"Make A Selection",maxDisplay:0,openSpeed:400,closeSpeed:400,openEffect:"slide",closeEffect:"slide",hideOnMouseOut:true};function h(i){if(typeof i=="undefined"){return false}else{return true}}function a(j,i){if(h(i.columns)&&h(i.rows)){d.error("gentleSelect: You cannot supply both 'rows' and 'columns'");return true}if(h(i.columns)&&!h(i.itemWidth)){d.error("gentleSelect: itemWidth must be supplied if 'columns' is specified");return true}if(h(i.rows)&&!h(i.itemWidth)){d.error("gentleSelect: itemWidth must be supplied if 'rows' is specified");return true}if(!h(i.openSpeed)||typeof i.openSpeed!="number"&&(typeof i.openSpeed=="string"&&(i.openSpeed!="slow"&&i.openSpeed!="fast"))){d.error('gentleSelect: openSpeed must be an integer or "slow" or "fast"');return true}if(!h(i.closeSpeed)||typeof i.closeSpeed!="number"&&(typeof i.closeSpeed=="string"&&(i.closeSpeed!="slow"&&i.closeSpeed!="fast"))){d.error('gentleSelect: closeSpeed must be an integer or "slow" or "fast"');return true}if(!h(i.openEffect)||(i.openEffect!="fade"&&i.openEffect!="slide")){d.error("gentleSelect: openEffect must be either 'fade' or 'slide'!");return true}if(!h(i.closeEffect)||(i.closeEffect!="fade"&&i.closeEffect!="slide")){d.error("gentleSelect: closeEffect must be either 'fade' or 'slide'!");return true}if(!h(i.hideOnMouseOut)||(typeof i.hideOnMouseOut!="boolean")){d.error('gentleSelect: hideOnMouseOut must be supplied and either "true" or "false"!');return true}return false}function f(j,i){if(j.attr("multiple")){i.hideOnMouseOut=true}}function c(j,k){if(j.length<1){return k.prompt}if(k.maxDisplay!=0&&j.length>k.maxDisplay){var i=j.slice(0,k.maxDisplay).map(function(){return d(this).text()});i.push("...")}else{var i=j.map(function(){return d(this).text()})}return i.get().join(", ")}var b={init:function(w){var l=d.extend({},g,w);if(a(this,l)){return this}f(this,l);this.hide();label_text=c(this.find(":selected"),l);var s=d("<span class='gentleselect-label'>"+label_text+"</span>").insertBefore(this).bind("mouseenter.gentleselect",e.labelHoverIn).bind("mouseleave.gentleselect",e.labelHoverOut).bind("click.gentleselect",e.labelClick).data("root",this);this.data("label",s).data("options",l);var p=d("<ul></ul>");this.find("option").each(function(){var i=d("<li>"+d(this).text()+"</li>").data("value",d(this).attr("value")).data("name",d(this).text()).appendTo(p);if(d(this).attr("selected")){i.addClass("selected")}});var q=d("<div class='gentleselect-dialog'></div>").append(p).insertAfter(s).bind("click.gentleselect",e.dialogClick).bind("mouseleave.gentleselect",e.dialogHoverOut).data("label",s).data("root",this);this.data("dialog",q);if(h(l.columns)||h(l.rows)){p.css("float","left").find("li").width(l.itemWidth).css("float","left");var n=p.find("li:first");var k=l.itemWidth+parseInt(n.css("padding-left"))+parseInt(n.css("padding-right"));var t=p.find("li").length;if(h(l.columns)){var r=parseInt(l.columns);var v=Math.ceil(t/r)}else{var v=parseInt(l.rows);var r=Math.ceil(t/v)}q.width(k*r);for(var m=0;m<(v*r)-t;m++){d("<li style='float:left' class='gentleselect-dummy'><span>&nbsp;</span></li>").appendTo(p)}var j=[];var u=0;p.find("li").each(function(){if(u<v){j[u]=d(this)}else{var i=u%v;d(this).insertAfter(j[i]);j[i]=d(this)}u++})}else{if(typeof l.minWidth=="number"){q.css("min-width",l.minWidth)}}if(h(l.title)){d("<div class='gentleselect-title'>"+l.title+"</div>").prependTo(q)}d(document).bind("keyup.gentleselect",e.keyUp);return this},update:function(){var k=this.data("options");var i=(this.attr("multiple"))?this.val():[this.val()];d("li",this.data("dialog")).each(function(){var m=d(this);var l=(d.inArray(m.data("value"),i)!=-1);m.toggleClass("selected",l)});var j=c(this.find(":selected"),k);this.data("label").text(j);return this}};var e={labelHoverIn:function(){d(this).addClass("gentleselect-label-highlight")},labelHoverOut:function(){d(this).removeClass("gentleselect-label-highlight")},labelClick:function(){var l=d(this);var m=l.position();var i=l.data("root");var k=i.data("options");var j=i.data("dialog").css("top",m.top+l.height()).css("left",m.left+1);if(k.openEffect=="fade"){j.fadeIn(k.openSpeed)}else{j.slideDown(k.openSpeed)}},dialogHoverOut:function(){var i=d(this);if(i.data("root").data("options").hideOnMouseOut){i.hide()}},dialogClick:function(k){var m=d(k.target);var l=d(this);var n=l.data("root");var i=n.data("options");if(!n.attr("multiple")){if(i.closeEffect=="fade"){l.fadeOut(i.closeSpeed)}else{l.slideUp(i.closeSpeed)}}if(m.is("li")&&!m.hasClass("gentleselect-dummy")){var p=m.data("value");var j=m.data("name");var o=l.data("label");if(l.data("root").attr("multiple")){m.toggleClass("selected");var r=l.find("li.selected");o.text(c(r,i));var q=r.map(function(){return d(this).data("value")});n.val(q.get()).trigger("change")}else{l.find("li.selected").removeClass("selected");m.addClass("selected");o.text(m.data("name"));n.val(p).trigger("change")}}},keyUp:function(i){if(i.keyCode==27){d(".gentleselect-dialog").hide()}}};d.fn.gentleSelect=function(i){if(b[i]){return b[i].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof i==="object"||!i){return b.init.apply(this,arguments)}else{d.error("Method "+i+" does not exist on jQuery.gentleSelect")}}}})(jQuery);
+(function(d){var c={minWidth:100,itemWidth:undefined,columns:undefined,rows:undefined,title:undefined,prompt:"Make A Selection",maxDisplay:0,openSpeed:400,closeSpeed:400,openEffect:"slide",closeEffect:"slide",disallowEmpty:false,hideOnMouseOut:true};function b(j){if(typeof j=="undefined"){return false}else{return true}}function g(k,j){if(b(j.columns)&&b(j.rows)){d.error("gentleSelect: You cannot supply both 'rows' and 'columns'");return true}if(b(j.columns)&&!b(j.itemWidth)){d.error("gentleSelect: itemWidth must be supplied if 'columns' is specified");return true}if(b(j.rows)&&!b(j.itemWidth)){d.error("gentleSelect: itemWidth must be supplied if 'rows' is specified");return true}if(!b(j.openSpeed)||typeof j.openSpeed!="number"&&(typeof j.openSpeed=="string"&&(j.openSpeed!="slow"&&j.openSpeed!="fast"))){d.error('gentleSelect: openSpeed must be an integer or "slow" or "fast"');return true}if(!b(j.closeSpeed)||typeof j.closeSpeed!="number"&&(typeof j.closeSpeed=="string"&&(j.closeSpeed!="slow"&&j.closeSpeed!="fast"))){d.error('gentleSelect: closeSpeed must be an integer or "slow" or "fast"');return true}if(!b(j.openEffect)||(j.openEffect!="fade"&&j.openEffect!="slide")){d.error("gentleSelect: openEffect must be either 'fade' or 'slide'!");return true}if(!b(j.closeEffect)||(j.closeEffect!="fade"&&j.closeEffect!="slide")){d.error("gentleSelect: closeEffect must be either 'fade' or 'slide'!");return true}if(!b(j.hideOnMouseOut)||(typeof j.hideOnMouseOut!="boolean")){d.error('gentleSelect: hideOnMouseOut must be supplied and either "true" or "false"!');return true}return false}function h(k,j){if(k.attr("multiple")){j.hideOnMouseOut=true}}function f(k,l){if(k.length<1){return l.prompt}if(l.maxDisplay!=0&&k.length>l.maxDisplay){var j=k.slice(0,l.maxDisplay).map(function(){return d(this).text()});j.push("...")}else{var j=k.map(function(){return d(this).text()})}return j.get().join(", ")}function i(m,l){var m=d(m);if(m.attr("multiple")&&l.disallowEmpty){var k=m.data("dialog").find("li"),j=k.filter(".selected");k.removeClass("sole-selected");if(j.length==1){j.addClass("sole-selected")}}}var a={init:function(x){var l=d.extend({},c,x),n=this.find("option");if(g(this,l)){return this}h(this,l);this.hide();if(this.attr("multiple")&&l.disallowEmpty){if(n.length==0){d.error("gentleSelect: disallowEmpty conflicts with empty <select>")}if(this[0].selectedIndex<0){this[0].selectedIndex=0}}label_text=f(this.find(":selected"),l);var u=d("<span class='gentleselect-label'>"+label_text+"</span>").insertBefore(this).bind("mouseenter.gentleselect",e.labelHoverIn).bind("mouseleave.gentleselect",e.labelHoverOut).bind("click.gentleselect",e.labelClick).data("root",this);this.data("label",u).data("options",l);var q=d("<ul></ul>");n.each(function(){var o=d("<li>"+d(this).text()+"</li>").data("value",d(this).attr("value")).data("name",d(this).text()).appendTo(q);if(d(this).attr("selected")){o.addClass("selected")}});var r=d("<div class='gentleselect-dialog'></div>").append(q).insertAfter(u).bind("click.gentleselect",e.dialogClick).bind("mouseleave.gentleselect",e.dialogHoverOut).data("label",u).data("root",this);this.data("dialog",r);if(b(l.columns)||b(l.rows)){q.css("float","left").find("li").width(l.itemWidth).css("float","left");var p=q.find("li:first");var k=l.itemWidth+parseInt(p.css("padding-left"))+parseInt(p.css("padding-right"));var t=q.find("li").length;if(b(l.columns)){var s=parseInt(l.columns);var w=Math.ceil(t/s)}else{var w=parseInt(l.rows);var s=Math.ceil(t/w)}r.width(k*s);for(var m=0;m<(w*s)-t;m++){d("<li style='float:left' class='gentleselect-dummy'><span>&nbsp;</span></li>").appendTo(q)}var j=[];var v=0;q.find("li").each(function(){if(v<w){j[v]=d(this)}else{var o=v%w;d(this).insertAfter(j[o]);j[o]=d(this)}v++})}else{if(typeof l.minWidth=="number"){r.css("min-width",l.minWidth)}}if(b(l.title)){d("<div class='gentleselect-title'>"+l.title+"</div>").prependTo(r)}d(document).bind("keyup.gentleselect",e.keyUp);i(this,l);return this},update:function(){var l=this.data("options");var j=(this.attr("multiple"))?this.val():[this.val()];d("li",this.data("dialog")).each(function(){var n=d(this);var m=(d.inArray(n.data("value"),j)!=-1);n.toggleClass("selected",m)});var k=f(this.find(":selected"),l);this.data("label").text(k);i(this,l);return this}};var e={labelHoverIn:function(){d(this).addClass("gentleselect-label-highlight")},labelHoverOut:function(){d(this).removeClass("gentleselect-label-highlight")},labelClick:function(){var m=d(this);var n=m.position();var j=m.data("root");var l=j.data("options");var k=j.data("dialog").css("top",n.top+m.height()).css("left",n.left+1);if(l.openEffect=="fade"){k.fadeIn(l.openSpeed)}else{k.slideDown(l.openSpeed)}},dialogHoverOut:function(){var j=d(this);if(j.data("root").data("options").hideOnMouseOut){j.hide()}},dialogClick:function(l){var n=d(l.target);var m=d(this);var o=m.data("root");var j=o.data("options");if(!o.attr("multiple")){if(j.closeEffect=="fade"){m.fadeOut(j.closeSpeed)}else{m.slideUp(j.closeSpeed)}}if(n.is("li")&&!n.hasClass("gentleselect-dummy")){var q=n.data("value");var k=n.data("name");var p=m.data("label");if(m.data("root").attr("multiple")){if(j.disallowEmpty&&n.hasClass("selected")&&(o.find(":selected").length==1)){return}n.toggleClass("selected");var t=m.find("li.selected");p.text(f(t,j));var r=t.map(function(){return d(this).data("value")});o.val(r.get()).trigger("change");i(o,j)}else{m.find("li.selected").removeClass("selected");n.addClass("selected");p.text(n.data("name"));o.val(q).trigger("change")}}},keyUp:function(j){if(j.keyCode==27){d(".gentleselect-dialog").hide()}}};d.fn.gentleSelect=function(j){if(a[j]){return a[j].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof j==="object"||!j){return a.init.apply(this,arguments)}else{d.error("Method "+j+" does not exist on jQuery.gentleSelect")}}}})(jQuery);
View
18 gentleSelect/jquery-gentleSelect.css
@@ -42,20 +42,36 @@
.gentleselect-dialog > ul > li {
margin: 0;
padding: 3px 20px 3px 25px;
-
cursor: pointer;
+ -webkit-touch-callout: none;
+ -webkit-user-select: none;
+ -khtml-user-select: none;
+ -moz-user-select: none;
+ -ms-user-select: none;
+ user-select: none;
}
.gentleselect-dialog > ul > li.selected {
font-weight: bold;
color: #369;
background-color: #eee;
}
+
.gentleselect-dialog > ul > li.gentleselect-dummy:hover { background-color: #fff; }
.gentleselect-dialog > ul > li:hover {
background-color: #69c;
color: #fff;
}
+/* override CSS for lone selected item when disallowEmpty=true */
+.gentleselect-dialog > ul > li.sole-selected {
+ cursor: default;
+}
+.gentleselect-dialog > ul > li.sole-selected:hover {
+ color: #369;
+ background-color: #eee;
+}
+
+
.gentleselect-dialog > .gentleselect-title {
display: block;
text-align: center;
View
38 gentleSelect/jquery-gentleSelect.js
@@ -33,6 +33,7 @@
closeSpeed : 400,
openEffect : "slide",
closeEffect : "slide",
+ disallowEmpty : false,
hideOnMouseOut : true
}
@@ -98,15 +99,39 @@
}
return arr.get().join(", ");
}
+
+ function updateState(c, o) {
+ var c = $(c);
+ if (c.attr("multiple") && o.disallowEmpty) {
+ var all_items = c.data("dialog").find("li"),
+ selected_items = all_items.filter(".selected");
+
+ // mark element if only one selected
+ all_items.removeClass("sole-selected");
+ if (selected_items.length == 1) {
+ selected_items.addClass("sole-selected");
+ }
+ }
+ }
var methods = {
init : function(options) {
- var o = $.extend({}, defaults, options);
+ var o = $.extend({}, defaults, options),
+ select_items = this.find("option");
if (hasError(this, o)) { return this; }; // check for errors
optionOverrides(this, o); //
this.hide(); // hide original select box
+ if (this.attr("multiple") && o.disallowEmpty)
+ {
+ if (select_items.length == 0) {
+ $.error("gentleSelect: disallowEmpty conflicts with empty <select>");
+ }
+ // default to first item if none selected
+ if (this[0].selectedIndex < 0) { this[0].selectedIndex = 0; }
+ }
+
// initialise <span> to replace select box
label_text = getSelectedAsText(this.find(":selected"), o);
var label = $("<span class='gentleselect-label'>" + label_text + "</span>")
@@ -120,7 +145,7 @@
// generate list of options
var ul = $("<ul></ul>");
- this.find("option").each(function() {
+ select_items.each(function() {
var li = $("<li>" + $(this).text() + "</li>")
.data("value", $(this).attr("value"))
.data("name", $(this).text())
@@ -188,6 +213,7 @@
// ESC key should hide all dialog boxes
$(document).bind("keyup.gentleselect", event_handlers.keyUp);
+ updateState(this, o);
return this;
},
@@ -208,6 +234,7 @@
var label = getSelectedAsText(this.find(":selected"), opts);
this.data("label").text(label);
+ updateState(this, opts);
return this;
}
};
@@ -263,12 +290,19 @@
var label = $this.data("label")
if ($this.data("root").attr("multiple")) {
+ if (opts.disallowEmpty
+ && clicked.hasClass("selected")
+ && (root.find(":selected").length == 1)) {
+ // sole item clicked. For now, do nothing.
+ return;
+ }
clicked.toggleClass("selected");
var s = $this.find("li.selected");
label.text(getSelectedAsText(s, opts));
var v = s.map(function(){ return $(this).data("value"); });
// update actual selectbox and trigger change event
root.val(v.get()).trigger("change");
+ updateState(root, opts);
} else {
$this.find("li.selected").removeClass("selected");
clicked.addClass("selected");

0 comments on commit d5dc478

Please sign in to comment.