-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simpleselect.min.js
9 lines (9 loc) · 17.4 KB
/
jquery.simpleselect.min.js
1
2
3
4
5
6
7
8
9
/* jquery.simpleselect
-- version 0.1.1
-- copyright 2014 UndeadBane*2014
-- licensed under the MIT
--
-- https://github.com/UndeadBaneGitHub/jquery.simpleselect
--
*/
(function($){var _SimpleSelect=function(t,n,r,i){var s=function(e){var t=new Array;$(e).find("option").each(function(){t.push(this.innerText)});return t};var o=i;var u=t instanceof $?t[0]:t;var a=Object.prototype.toString.call(n)==="[object Array]"?n:s(n);var f={preserveElementMargin:false,wrapperMaxWidth:"css",wrapperMaxHeight:"css",wrapperWidth:"css",wrapperHeight:"css",wrapperMinWidth:"element",wrapperMinHeight:"element",itemFont:"element",useEscapedElementText:false,openEvent:"click",searchEvent:"keyup",emptyListItemContent:"Совпадений не найдено",forceValueToListValue:true,placeholderValue:"",placeholderStyle:{color:"lightgray"},scrollIntoViewIfNeeded:true,preventClosingOnSelect:false,doNotSelectOnClicksTo:"",drawArrow:true,drawPosition:"bottom left",doNotModifyElementText:false,itemGetElementTextFunction:null,itemSetElementTextFunction:null,openOnSearch:false,updatePositionOnOpen:false,arrowContent:{arrowUp:"▲",arrowDown:"▼",arrowWidth:"auto",arrowHeight:"auto"}};var l=null;var c=this;var h=null,p=null,d=false,v=false,m=1;var g=function(e){var t=u;var n={top:t.offsetTop+$(t).outerHeight(),left:$(t).position().left};var r=parseFloat($(h).css("height")||$(h).css("max-height")),i=parseFloat($(h).css("width")||$(h).css("max-width"));if(!r||!i){var s=y(h);r=s.height;i=s.width}if(!r||!i){return 0}if(f.drawPosition.indexOf(" right")!==-1){n.left-=i-$(t).outerWidth()}if(f.drawPosition.indexOf("right")===0){n.top-=$(t).outerHeight();n.left+=$(t).outerWidth()}for(var o=1;o<e;o++){t=t.parentNode;if(/OL/.test(t.nodeName)){continue}if(/BODY|HTML/.test(t.nodeName)){break}n.top+=t.offsetTop;n.left+=t.offsetLeft}return n};var y=function(e,t){var n=$(e).clone(false);n.css({visibility:"hidden",position:"absolute"});n.appendTo(t||"body");var r={height:n.outerHeight(true),width:n.outerWidth(true)};n.remove();return r};var b=function(e,t,n,r){if(f.drawPosition.indexOf(" right")!==-1){r.left-=t-$(e).outerWidth();r.right-=t-$(e).outerWidth();return}if(f.drawPosition.indexOf("right")===0){r.top-=$(e).outerHeight();r.bottom-=$(e).outerHeight();r.left+=$(e).outerWidth();r.right+=$(e).outerWidth()}};var w=function(e,t){var n=function(e,t,r,i,s,o,u){var a=1;var l=t?-1:1,c=t?0:1,p=t?1:0;if(!r){var d=parseFloat($(h).css("height")||$(h).css("max-height")),v=parseFloat($(h).css("width")||$(h).css("max-width"));if(!d||!v){var m=y(h);d=m.height;v=m.width}if(!d||!v){return 0}var g={top:$(e).offset().top+c*$(e).outerHeight(),left:$(e).offset().left,bottom:$(e).offset().top+c*$(e).outerHeight()+l*d,right:$(e).offset().left+v};b(e,v,d,g)}else{g=r}if(/BODY|HTML/.test(e.nodeName)){return a}var w=e.parentNode;var E=0;if(f.drawPosition.indexOf("right")===0){E=g.right}else if(f.drawPosition.indexOf("bottom right")===0){E=g.left}else if(f.drawPosition.indexOf("bottom left")===0){E=g.right}else{E=(g.right+g.left)/2}var S=g.bottom>$(w).offset().top+$(w).height(),x=E>$(w).offset().left+$(w).width();if(S){scrollableY=$(w).isScrollable("y")}if(x){scrollableX=$(w).isScrollable("x")}if(S&&!scrollableY||x&&!scrollableX||w.nodeName==="OL"){a+=n(w,t,g,N,L,A,M)}else{var T=10;var N=i?i:g.top+l*T,C=g.top,k=null;var L=s||null,A=o||null;A=e;var O=$(A).isScrollable("y");if(!O){while(!(O=$(A=A.parentNode).isScrollable("y"))&&!/HTML/.test(A.nodeName));}if(!O){return a}L=$(A).offset().top+c*$(A).height()-l*T;var M=u||false;while(l*N<l*g.bottom){if(l*N>l*L&&!M){$(A).scrollTop($(A).scrollTop()+(N-L));M=true}k=document.elementFromPoint(E,M?L:N);if(!k){return a}if(k===w){N+=l*T;if(M){$(A).scrollTop($(A).scrollTop()+l*T)}continue}if(k.parentNode===w.parentNode){a+=n(w,t,g,N,L,A,M);break}N+=l*T;if(M){$(A).scrollTop($(A).scrollTop()+l*T)}}}return a};return n(e,t)};var E=function(e){var t=w(u);if(!t){throw new Error("Failed to find insertion level!")}e.insertionLevel=t;x(e,true)};var S=function(e){var t=w(u);if(!t){throw new Error("Failed to find insertion level!")}if(t!==e.insertionLevel){e.insertionLevel=t;$(e).remove();var n=true}x(e,n);M()};var x=function(e,t){var n=g(e.insertionLevel);if(f.preserveElementMargin){var r=parseFloat($(u).css("margin-bottom"));n.top+=r}$(e).css({top:n.top,left:n.left});if(t){var i=u.parentNode;for(var s=1;s<e.insertionLevel;s++){i=i.parentNode}$(i).append(e)}};this.open=function(){if(d){return}var e=this;var t=this.getItemsWrapper();var n=false;var r=this.getElementText();u.oldText=r;v=t.id?$("#"+t.id).get(0)===t:false;if(!v){t.id="simple-select-items-wrapper-"+O();$(t).css({position:"absolute",maxWidth:this.getDimensionValue(u,"max-width"),width:this.getDimensionValue(u,"width"),minWidth:this.getDimensionValue(u,"min-width"),maxHeight:this.getDimensionValue(u,"max-height"),height:this.getDimensionValue(u,"height"),minHeight:this.getDimensionValue(u,"min-height")});E(t);t.style.display="";this.setScroll();if(o&&typeof o==="function"){o()}}else{if(f.updatePositionOnOpen){S(t)}t.style.display=""}if(r){$(t).children().each(function(e){var t=c.getElementText(this);if(t===r){if(!$(this).hasClass("hovered")){$(this).addClass("hovered")}$(this).scrollIntoViewIfNeeded()}else if($(this).hasClass("hovered")){$(this).removeClass("hovered")}})}if(f.scrollIntoViewIfNeeded){$(t).scrollIntoViewIfNeeded()}d=true;this.updateScroll(!v);if(!f.doNotModifyElementText){$(u).focus()}else{$(t).focus()}L(u,this.getElementText().length,this.getElementText().length);H();$(u).trigger("simple-select-post-open")};this.close=function(e){if(!d){return}var t=false;var n=this;var r=this.getItemsWrapper();if(e){this.setElementText(u.oldText);$(u).trigger("simple-select-input-canceled")}var i=this.getElementText();if(!i){N()}if(i){$(r).children().each(function(e){var n=c.getElementText(this);if(n===i){t=true;if(!$(this).hasClass("hovered")){$(this).addClass("hovered")}$(this).scrollIntoViewIfNeeded()}else if($(this).hasClass("hovered")){$(this).removeClass("hovered")}})}if(f.forceValueToListValue&&!f.doNotModifyElementText&&i&&!t&&i!==f.placeholderValue){return}this.getItemsWrapper().style.display="none";this.updateScroll();d=false;P();$(u).trigger("simple-select-post-close");if(i){$(u).trigger("simple-select-selected")}u.oldText=this.getElementText()};this.search=function(e){if(!d){if(!f.openOnSearch){return}else{this.open()}}C();var t=this;var n=this.getItemsWrapper();var r=e?e:this.getElementText();if($(n).children(".hovered").length&&r){return}$(n).children(".hovered").removeClass("hovered");$(n).children().each(function(e){$(this).removeHighlight();$(this).highlight(r,{caseSensitive:false});if(!$(this).find(".highlight").length&&r){this.style.display="none"}else{this.style.display=""}});this.updateScroll();$(u).trigger("simple-select-post-search")};this.isOpen=function(){return d};this.getDimensionValue=function(e,t){var n="";var r=null;switch(t){case"max-width":n=f.wrapperMaxWidth;r=$(e).outerWidth(true);break;case"width":n=f.wrapperWidth;r=$(e).outerWidth(true);break;case"min-width":n=f.wrapperMinWidth;r=$(e).outerWidth(true);break;case"max-height":n=f.wrapperMaxHeight;r=$(e).outerHeight(true);break;case"height":n=f.wrapperHeight;r=$(e).outerHeight(true);break;case"min-height":n=f.wrapperMinHeight;r=$(e).outerHeight(true);break}switch(n){case"css":return"";break;case"element":var i=r?r:$(e).css(t);return i;break;default:return n}};this.setItems=function(e){a=Object.prototype.toString.call(e)==="[object Array]"?e:s(e);if(h){var t=c.getItemsWrapper();c.updateScroll();T();S(t)}};var T=function(){$(h).children(".simple-select-item").not(".empty-list-item").remove();for(var e=0;e<a.length;e++){var t=document.createElement("div");t.className="simple-select-item";t.id="simple-select-item-"+e;if(a[e].nodeType===1){t.appendChild(a[e])}else{t.innerHTML=a[e]}if(f.itemFont==="element"){$(t).css({font:$(u).css("font"),fontFamily:$(u).css("font-family"),fontSize:$(u).css("font-size"),fontStyle:$(u).css("font-style"),fontWeight:$(u).css("font-weight")})}t.simpleSelect_ToString=f.itemGetElementTextFunction;t.simpleSelect_FromString=f.itemSetElementTextFunction;$(h).append(t);$(t).click(function(e){if($(e.target).not(f.doNotSelectOnClicksTo).length===0){return}e.preventDefault();C();$(this).removeHighlight();var t=c.getElementText(this);c.setElementText(t);D();_();if(!f.preventClosingOnSelect){c.close()}return false});$(t).hover(function(e){var t=$(this).siblings(".hovered");t.toggleClass("hovered");$(this).parent().data({isHovered:true})})}if(a.length){c.updateScroll()}};this.getItemsWrapper=function(){if(!h){h=document.createElement("div");h.className="simple-select-items-wrapper";h.id=null;p=document.createElement("div");p.className="simple-select-item empty-list-item";p.id="simple-select-item empty-list-item";p.innerHTML=f.emptyListItemContent;$(h).append(p);T();m=Math.max(m,A(h));$(h).hover(function(){$(this).data({isHovered:true})});$(h).mouseout(function(t){e=t.toElement||t.relatedTarget;var n=e;for(var r=0;r<m;r++){if(!n){return}if(n===this){return}n=n.parentNode}$(this).data({isHovered:false})})}return h};this.getElementText=function(e){if(f.doNotModifyElementText){return""}var t=e?e:u;if(typeof t["simpleSelect_ToString"]==="function"){var n=t["simpleSelect_ToString"].call(t)}else{var n=t.nodeName==="input"||t.nodeName==="textarea"?$(t).val():(f.useEscapedElementText?$(t).html():$(t).text()).replace(/( )/g," ")}if(l){if($(l).closest(t).length>0){n=n.replace(l.innerText,"")}}if(n){return k(n.trim())}else{return k(n)}};this.setElementText=function(e,t,n){if(f.doNotModifyElementText||!e){return}var r=e.trim();var i=n?n:u;if(typeof i["simpleSelect_FromString"]==="function"){i["simpleSelect_FromString"].call(i,r);return}else{i.nodeName==="input"||i.nodeName==="textarea"?$(i).val(r):f.useEscapedElementText?$(i).html(r):$(i).text(r)}if(!r&&!t){N()}else{L(i,r.length,r.length)}};this.setScroll=function(){};this.updateScroll=function(e){if(!$(this.getItemsWrapper()).children(".simple-select-item:visible").not(".empty-list-item").length){p.style.display=""}else{p.style.display="none"}};var N=function(e){if(!f.placeholderValue){return}var t=e?e:u;if(!c.getElementText(t)){$(t).data("hasPlaceholder",true);c.setElementText(f.placeholderValue,true,t);for(prop in f.placeholderStyle){t.style[prop]=f.placeholderStyle[prop]}$(t).focusin(C)}};var C=function(e){if(!$(e).text()){return}if(!f.placeholderValue){return}var t=e?e:u;if($(t).data("hasPlaceholder")){$(t).data("hasPlaceholder",false);c.setElementText("",true,t);for(prop in f.placeholderStyle){t.style[prop]=""}}};var k=function(e,t){if(!e){return}if(!f.placeholderValue){return e}var n=t?t:u;if($(n).data("hasPlaceholder")){return e.replace(f.placeholderValue,"")}return e};var L=function(e,t,n){if(!e.firstChild){return}var r=document.createRange();r.setStart(e.firstChild,t);r.setEnd(e.firstChild,n);var i=window.getSelection();i.removeAllRanges();i.addRange(r)};var A=function(e){var t=e.firstChild;var n=[];if(!t){return 0}while(t){n.push(A(t));t=t.nextSibling}return Math.max.apply(Math,n)+1};var O=function(){var e=0;while($("#simple-select-items-wrapper-"+e).length){e++}return e};var M=function(){if(!l){return _()}var e={height:$(l).outerHeight(true),width:$(l).outerWidth(true)};$(u).css("padding-right","14px");var t=u;var n={top:-parseFloat($(t).css("border-top-width")),left:$(t).outerWidth()-e.width};while($(t).css("position")!=="absolute"&&$(t).css("position")!=="relative"&&!/BODY|HTML/.test(t.nodeName)){n.top+=t.offsetTop;n.left+=t.offsetLeft;t=t.parentNode}$(l).css({top:n.top,right:n.right,left:n.left})};var _=function(){if(f.drawArrow){l=document.createElement("div");l.className="simple-select-arrow arrow-down";l.innerText=f.arrowContent.arrowDown;var e=y(l,u.parentNode);if(f.arrowContent.arrowWidth!=="css"){if(f.arrowContent.arrowWidth==="auto"){e.width=12}else{e.width=f.arrowContent.arrowWidth}}if(f.arrowContent.arrowHeight!=="css"){if(f.arrowContent.arrowHeight==="auto"){e.height=$(u).outerHeight()}else{e.height=f.arrowContent.arrowHeight}}$(u).css("padding-right","14px");var t=u;var n={top:-parseFloat($(t).css("border-top-width")),left:$(t).outerWidth()-e.width};while($(t).css("position")!=="absolute"&&$(t).css("position")!=="relative"&&!/BODY|HTML/.test(t.nodeName)){n.top+=t.offsetTop;n.left+=t.offsetLeft;t=t.parentNode}$(l).css({top:n.top,right:n.right,left:n.left});if(f.arrowContent.arrowHeight!=="css"){$(l).css({height:e.height})}$(t).append(l);var r=function(e){e.preventDefault();e.target=u;$(u).trigger(e);return false};$(l).off(f.openEvent,r);$(l).on(f.openEvent,r)}};var D=function(){if(l){$(l).remove()}};var P=function(){if(l){l.innerText=f.arrowContent.arrowDown;$(l).css({top:parseFloat($(l).css("top"))+1});$(l).removeClass("arrow-up").addClass("arrow-down")}M();$(l).css("line-height","")};var H=function(){if(l){l.innerText=f.arrowContent.arrowUp;$(l).css({top:parseFloat($(l).css("top"))-1});$(l).removeClass("arrow-down").addClass("arrow-up")}M()};var B=function(e){if(!c.isOpen()){return}if(!$(e.target).closest($(u)).length&&!$(e.target).closest($(c.getItemsWrapper())).length){c.close()}};var j=function(){if(r){$.extend(true,f,r)}$(u).off("keydown",_element_keydown);$(u).on("keydown",_element_keydown);$(u).off(f.openEvent,_openEvent_happened);$(u).on(f.openEvent,_openEvent_happened);if(f.searchEvent){$(u).off(f.searchEvent,_searchEvent_happened);$(u).on(f.searchEvent,_searchEvent_happened)}$(document).off("click",B);$(document).on("click",B);_();N()};this.removeSimpleSelect=function(){if(this.isOpen()){this.close()}$(u).off("keydown",_element_keydown);$(u).off(f.openEvent,_openEvent_happened);if(f.searchEvent){$(u).off(f.searchEvent,_searchEvent_happened)}if(h){h.parentNode.removeChild(h)}$(document).off("click",B);D();$(u).data("simpleSelect",null)};j()};var _openEvent_happened=function(e){var t=$(this).data("simpleSelect");if(!t){return}if(!t.isOpen()){t.open()}else{t.close()}};var _searchEvent_happened=function(e){$(this).data("simpleSelect").search()};var _element_keydown=function(e){var t=$(this).data("simpleSelect");var n=t.getItemsWrapper();var r=t.isOpen();if(!r){t.open()}var i=$(n).children().not(".empty-list-item").not(":hidden");var s=i.siblings(".hovered").get(0)||null;switch(e.which){case 10:case 13:case 27:e.preventDefault();if(typeof e.stopPropagation==="function"){e.stopPropagation()}t.close(e.which===27);break;case 38:case 40:if($(n).data("isHovered")){t.setElementText("",true);return false}if(!r){return false}e.preventDefault();if(typeof e.stopPropagation==="function"){e.stopPropagation()}i=$(n).children().not(".empty-list-item").not(":hidden");s=i.siblings(".hovered").get(0)||null;if(e.which===38){if(s){$(s).removeClass("hovered");s=s.previousSibling;while(s&&!$(s).is(":visible")&&!$(s).is(".empty-list.item")){s=s.previousSibling}}if(!s){s=i.last().get(0)}}else{if(s){$(s).removeClass("hovered");s=s.nextSibling;while(s&&!$(s).is(":visible")&&!$(s).is(".empty-list.item")){s=s.nextSibling}}if(!s){s=i.first().get(0)}}if(s){$(s).removeHighlight();if(!$(s).hasClass("hovered")){$(s).addClass("hovered")}t.setElementText(t.getElementText(s));$(s).scrollIntoViewIfNeeded()}else{t.setElementText("")}break;default:if(s){$(s).removeClass("hovered")}return}};$.fn.makeSimpleSelect=function(e,t,n){var r=this.length;return this.each(function(r,i){var i=$(this);if(i.data("simpleSelect"))return;var s=new _SimpleSelect(this,e,t,n);i.data("simpleSelect",s)})};$.fn.removeSimpleSelect=function(){return this.each(function(){var e=$(this);var t=e.data("simpleSelect");if(!t){return}t.removeSimpleSelect()})};if(typeof $.fn.scrollIntoViewIfNeeded!=="function"){$.fn.scrollIntoViewIfNeeded=function(e){return this.each(function(){if(typeof this.scrollIntoViewIfNeeded!=="function"){var t=this,n=this.parentNode.getBoundingClientRect(),r=t.getBoundingClientRect();var i=null;if(r.top<n.top&&!e){i=true}if(r.bottom>n.bottom&&!e){i=false}if(r.top<n.top||r.bottom>n.bottom||r.right>n.right||r.left<n.left)t.scrollIntoView(i)}else{this.scrollIntoViewIfNeeded(typeof e==="bool"?e:null)}})}}if(typeof $.fn.highlight!=="function"){jQuery.fn.highlight=function(e){function t(e,n){var r=0;if(e.nodeType==3){var i=e.data.toUpperCase().indexOf(n);if(i>=0){var s=document.createElement("span");s.className="highlight";var o=e.splitText(i);var u=o.splitText(n.length);var a=o.cloneNode(true);s.appendChild(a);o.parentNode.replaceChild(s,o);r=1}}else if(e.nodeType==1&&e.childNodes&&!/(script|style)/i.test(e.tagName)){for(var f=0;f<e.childNodes.length;++f){f+=t(e.childNodes[f],n)}}return r}return this.length&&e&&e.length?this.each(function(){t(this,e.toUpperCase())}):this}}if(typeof $.fn.removeHighlight!=="function"){jQuery.fn.removeHighlight=function(){var _normalize=function(e){var t=e.firstChild;while(t.nextSibling&&t.nextSibling.nodeType===3){if(t.nextSibling&&t.nextSibling.data){t.data+=t.nextSibling.data}t.parentNode.removeChild(t.nextSibling)}};return this.find("span.highlight").each(function(){var parent=this.parentNode;with(parent){replaceChild(this.firstChild,this);_normalize(parent)}}).end()}}$.fn.isScrollable=function(e){var t=new Array;this.each(function(n,r){var i,s;var o=!e||e==="vertical"||e==="y",u=e&&(e==="horizontal"||e==="x");if($(r).css("overflow")=="scroll"||$(r).css("overflowX")==="scroll"&&(u||!e)||$(r).css("overflowY")==="scroll"&&(o||!e)){t.push(true);return}if(o||!e){i=$.inArray($(r).css("overflowY"),["scroll","auto"])!==-1||$.inArray($(r).css("overflow"),["scroll","auto"])!==-1&&$.inArray($(r).css("overflowY"),["hidden"])===-1;if(!e){if(i){t.push(true);return}}else{t.push(i);return}}if(u||!e){s=$.inArray($(r).css("overflowX"),["scroll","auto"])!==-1||$.inArray($(r).css("overflow"),["scroll","auto"])!==-1&&$.inArray($(r).css("overflowX"),["hidden"])===-1;t.push(s)}});if(t.length===1){return t[0]}else{return t}}})(jQuery)