forked from prashantchaudhary/ddslick
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.ddslick.min.js
1 lines (1 loc) · 6.87 KB
/
jquery.ddslick.min.js
1
(function(e){function o(e,t){var n=e.find(".dd-option-value[value= '"+t+"']").parents("li").prevAll().length;u(e,n)}function u(e,t){var n=e.data("ddslick");var r=e.find(".dd-selected"),i=r.siblings(".dd-selected-value"),s=e.find(".dd-options"),o=r.siblings(".dd-pointer"),u=e.find(".dd-option").eq(t),a=u.closest("li"),c=n.settings,h=n.settings.data[t];e.find(".dd-option").removeClass("dd-option-selected");u.addClass("dd-option-selected");n.selectedIndex=t;n.selectedItem=a;n.selectedData=h;if(c.showSelectedHTML){r.html((h.imageSrc?'<img class="dd-selected-image'+(c.imagePosition=="right"?" dd-image-right":"")+'" src="'+h.imageSrc+'" />':"")+(h.text?'<label class="dd-selected-text">'+h.text+"</label>":"")+(h.description?'<small class="dd-selected-description dd-desc'+(c.truncateDescription?" dd-selected-description-truncated":"")+'" >'+h.description+"</small>":""))}else r.html(h.text);i.val(h.value);n.original.val(h.value);e.data("ddslick",n);f(e);l(e);if(typeof c.onSelected=="function"){c.onSelected.call(this,n)}}function a(t){var n=t.find(".dd-select"),r=n.siblings(".dd-options"),i=n.find(".dd-pointer"),s=r.is(":visible");e(".dd-click-off-close").not(r).slideUp(50);e(".dd-pointer").removeClass("dd-pointer-up");n.removeClass("dd-open");if(s){r.slideUp("fast");i.removeClass("dd-pointer-up");n.removeClass("dd-open")}else{n.addClass("dd-open");r.slideDown("fast");i.addClass("dd-pointer-up")}c(t)}function f(e){e.find(".dd-select").removeClass("dd-open");e.find(".dd-options").slideUp(50);e.find(".dd-pointer").removeClass("dd-pointer-up").removeClass("dd-pointer-up")}function l(e){var t=e.find(".dd-select").css("height");var n=e.find(".dd-selected-description");var r=e.find(".dd-selected-image");if(n.length<=0&&r.length>0){e.find(".dd-selected-text").css("lineHeight",t)}}function c(t){t.find(".dd-option").each(function(){var n=e(this);var r=n.css("height");var i=n.find(".dd-option-description");var s=t.find(".dd-option-image");if(i.length<=0&&s.length>0){n.find(".dd-option-text").css("lineHeight",r)}})}e.fn.ddslick=function(n){if(t[n]){return t[n].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof n==="object"||!n){return t.init.apply(this,arguments)}else{e.error("Method "+n+" does not exists.")}};var t={},n={data:[],keepJSONItemsOnTop:false,width:260,height:null,background:"#eee",selectText:"",defaultSelectedIndex:null,truncateDescription:true,imagePosition:"left",showSelectedHTML:true,clickOffToClose:true,embedCSS:true,onSelected:function(){}},r='<div class="dd-select"><input class="dd-selected-value" type="hidden" name="{{name}}"/><a class="dd-selected"></a><span class="dd-pointer dd-pointer-down"></span></div>',i='<ul class="dd-options"></ul>',s='<style id="css-ddslick" type="text/css">'+".dd-select{ border-radius:2px; border:solid 1px #ccc; position:relative; cursor:pointer;}"+".dd-desc { color:#aaa; display:block; overflow: hidden; font-weight:normal; line-height: 1.4em; }"+".dd-selected{ overflow:hidden; display:block; padding:10px; font-weight:bold;}"+".dd-pointer{ width:0; height:0; position:absolute; right:10px; top:50%; margin-top:-3px;}"+".dd-pointer-down{ border:solid 5px transparent; border-top:solid 5px #000; }"+".dd-pointer-up{border:solid 5px transparent !important; border-bottom:solid 5px #000 !important; margin-top:-8px;}"+".dd-options{ border:solid 1px #ccc; border-top:none; list-style:none; box-shadow:0px 1px 5px #ddd; display:none; position:absolute; z-index:2000; margin:0; padding:0;background:#fff; overflow:auto;}"+".dd-option{ padding:10px; display:block; border-bottom:solid 1px #ddd; overflow:hidden; text-decoration:none; color:#333; cursor:pointer;-webkit-transition: all 0.25s ease-in-out; -moz-transition: all 0.25s ease-in-out;-o-transition: all 0.25s ease-in-out;-ms-transition: all 0.25s ease-in-out; }"+".dd-options > li:last-child > .dd-option{ border-bottom:none;}"+".dd-option:hover{ background:#f3f3f3; color:#000;}"+".dd-selected-description-truncated { text-overflow: ellipsis; white-space:nowrap; }"+".dd-option-selected { background:#f6f6f6; }"+".dd-option-image, .dd-selected-image { vertical-align:middle; float:left; margin-right:5px; max-width:64px;}"+".dd-image-right { float:right; margin-right:15px; margin-left:5px;}"+".dd-container{ position:relative;} .dd-selected-text { font-weight:bold}</style>";t.init=function(t){var o=e.extend({},n,t);if(e("#css-ddslick").length<=0&&o.embedCSS){e(s).appendTo("head")}return this.each(function(){var s=e.extend({},n,t);var o=e(this),f=o.data("ddslick");if(!f){var l=[],c=s.data;o.find("option").each(function(){var t=e(this),n=t.data();l.push({text:e.trim(t.text()),value:t.val(),name:t.attr("name"),selected:t.is(":selected"),description:n.description,imageSrc:n.imagesrc})});if(s.keepJSONItemsOnTop)e.merge(s.data,l);else s.data=e.merge(l,s.data);var h=o,p=e("<div>").attr("id",o.attr("id")+"-dd-placeholder");o.replaceWith(p);o=p;var d=r.replace("{{name}}",s.name||"");o.addClass("dd-container").append(d).append(i);o.find("input.dd-selected-value").attr("id",e(h).attr("id")).attr("name",e(h).attr("name"));var l=o.find(".dd-select"),v=o.find(".dd-options");v.css({width:s.width});l.css({width:s.width,background:s.background});o.css({width:s.width});if(s.height!=null)v.css({height:s.height,overflow:"auto"});e.each(s.data,function(e,t){if(t.selected)s.defaultSelectedIndex=e;v.append("<li>"+'<a class="dd-option">'+(t.value?' <input class="dd-option-value" type="hidden" value="'+t.value+'" />':"")+(t.imageSrc?' <img class="dd-option-image'+(s.imagePosition=="right"?" dd-image-right":"")+'" src="'+t.imageSrc+'" />':"")+(t.text?' <label class="dd-option-text">'+t.text+"</label>":"")+(t.description?' <small class="dd-option-description dd-desc">'+t.description+"</small>":"")+"</a>"+"</li>")});var m={settings:s,original:h,selectedIndex:-1,selectedItem:null,selectedData:null};o.data("ddslick",m);if(s.selectText.length>0&&s.defaultSelectedIndex==null){o.find(".dd-selected").html(s.selectText)}else{var g=s.defaultSelectedIndex!=null&&s.defaultSelectedIndex>=0&&s.defaultSelectedIndex<s.data.length?s.defaultSelectedIndex:0;u(o,g)}o.find(".dd-select").on("click.ddslick",function(){a(o)});o.find(".dd-option").on("click.ddslick",function(){u(o,e(this).closest("li").index())});if(s.clickOffToClose){v.addClass("dd-click-off-close");o.on("click.ddslick",function(e){e.stopPropagation()});e("body").on("click",function(){e(".dd-open").removeClass("dd-open");e(".dd-click-off-close").slideUp(50).siblings(".dd-select").find(".dd-pointer").removeClass("dd-pointer-up")})}}})};t.select=function(t){return this.each(function(){if(t.index!==undefined)u(e(this),t.index);if(t.id)o(e(this),t.id)})};t.open=function(){return this.each(function(){var t=e(this),n=t.data("ddslick");if(n)a(t)})};t.close=function(){return this.each(function(){var t=e(this),n=t.data("ddslick");if(n)f(t)})};t.destroy=function(){return this.each(function(){var t=e(this),n=t.data("ddslick");if(n){var r=n.original;t.removeData("ddslick").unbind(".ddslick").replaceWith(r)}})}})(jQuery)