From 502f2e8dd5b37f7154fd9e1ee4acc322f90b7c8a Mon Sep 17 00:00:00 2001 From: Bryan Chow Date: Fri, 11 Jan 2013 00:08:08 -0800 Subject: [PATCH 1/2] Fix syntax errors --- jquery.ddslick.js | 29 ++++++++++++++++------------- jquery.ddslick.min.js | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/jquery.ddslick.js b/jquery.ddslick.js index 6cf434f..4cb8121 100644 --- a/jquery.ddslick.js +++ b/jquery.ddslick.js @@ -54,12 +54,17 @@ '.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}​'; + '.dd-container{ position:relative;} .dd-selected-text { font-weight:bold}'; + + //CSS styles are only added once. + if ($('#css-ddslick').length <= 0) { + $(ddslickCSS).appendTo('head'); + } //Public methods methods.init = function (options) { //Preserve the original defaults by passing an empty object as the target - var options = $.extend({}, defaults, options); + options = $.extend({}, defaults, options); //CSS styles are only added once. if ($('#css-ddslick').length <= 0 && options.embedCSS) { @@ -101,8 +106,8 @@ obj.addClass('dd-container').append(ddSelectHtml).append(ddOptionsHtml); //Get newly created ddOptions and ddSelect to manipulate - var ddSelect = obj.find('.dd-select'), - ddOptions = obj.find('.dd-options'); + ddSelect = obj.find('.dd-select'); + ddOptions = obj.find('.dd-options'); //Set widths ddOptions.css({ width: options.width }); @@ -110,7 +115,7 @@ obj.css({ width: options.width }); //Set height - if (options.height != null) + if (options.height !== null) ddOptions.css({ height: options.height, overflow: 'auto' }); //Add ddOptions to the container. Replace with template engine later. @@ -133,17 +138,15 @@ selectedIndex: -1, selectedItem: null, selectedData: null - } + }; obj.data('ddslick', pluginData); //Check if needs to show the select text, otherwise show selected or default selection - if (options.selectText.length > 0 && options.defaultSelectedIndex == null) { + if (options.selectText.length > 0 && options.defaultSelectedIndex === null) { obj.find('.dd-selected').html(options.selectText); } else { - var index = (options.defaultSelectedIndex != null && options.defaultSelectedIndex >= 0 && options.defaultSelectedIndex < options.data.length) - ? options.defaultSelectedIndex - : 0; + var index = (options.defaultSelectedIndex !== null && options.defaultSelectedIndex >= 0 && options.defaultSelectedIndex < options.data.length) ? options.defaultSelectedIndex : 0; selectIndex(obj, index); } @@ -176,7 +179,7 @@ if (options.index!==undefined) selectIndex($(this), options.index); }); - } + }; //Public method to open drop down methods.open = function () { @@ -214,7 +217,7 @@ $this.removeData('ddslick').unbind('.ddslick').replaceWith(originalElement); } }); - } + }; //Private: Select index function selectIndex(obj, index) { @@ -331,4 +334,4 @@ }); } -})(jQuery); \ No newline at end of file +})(jQuery); diff --git a/jquery.ddslick.min.js b/jquery.ddslick.min.js index 6a7191f..6b560a1 100644 --- a/jquery.ddslick.min.js +++ b/jquery.ddslick.min.js @@ -1 +1 @@ -(function(e){e.fn.ddslick=function(l){if(c[l]){return c[l].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof l==="object"||!l){return c.init.apply(this,arguments)}else{e.error("Method "+l+" does not exists.")}}};var c={},d={data:[],keepJSONItemsOnTop:false,width:260,height:null,background:"#eee",selectText:"",defaultSelectedIndex:null,truncateDescription:true,imagePosition:"left",showSelectedHTML:true,clickOffToClose:true,embedCSS:true,onSelected:function(){}},i='
',a='',b='';c.init=function(l){var l=e.extend({},d,l);if(e("#css-ddslick").length<=0&&l.embedCSS){e(b).appendTo("head")}return this.each(function(){var p=e(this),q=p.data("ddslick");if(!q){var n=[],o=l.data;p.find("option").each(function(){var w=e(this),v=w.data();n.push({text:e.trim(w.text()),value:w.val(),selected:w.is(":selected"),description:v.description,imageSrc:v.imagesrc})});if(l.keepJSONItemsOnTop){e.merge(l.data,n)}else{l.data=e.merge(n,l.data)}var m=p,s=e('
');p.replaceWith(s);p=s;p.addClass("dd-container").append(i).append(a);var n=p.find(".dd-select"),u=p.find(".dd-options");u.css({width:l.width});n.css({width:l.width,background:l.background});p.css({width:l.width});if(l.height!=null){u.css({height:l.height,overflow:"auto"})}e.each(l.data,function(v,w){if(w.selected){l.defaultSelectedIndex=v}u.append('
  • '+(w.value?' ':"")+(w.imageSrc?' ':"")+(w.text?' ":"")+(w.description?' '+w.description+"":"")+"
  • ")});var t={settings:l,original:m,selectedIndex:-1,selectedItem:null,selectedData:null};p.data("ddslick",t);if(l.selectText.length>0&&l.defaultSelectedIndex==null){p.find(".dd-selected").html(l.selectText)}else{var r=(l.defaultSelectedIndex!=null&&l.defaultSelectedIndex>=0&&l.defaultSelectedIndex':"")+(t.text?'":"")+(t.description?''+t.description+"":""))}else{r.html(t.text)}n.val(t.value);u.original.val(t.value);q.data("ddslick",u);k(q);g(q);if(typeof o.onSelected=="function"){o.onSelected.call(this,u)}}function f(p){var o=p.find(".dd-select"),m=o.siblings(".dd-options"),l=o.find(".dd-pointer"),n=m.is(":visible");e(".dd-click-off-close").not(m).slideUp(50);e(".dd-pointer").removeClass("dd-pointer-up");if(n){m.slideUp("fast");l.removeClass("dd-pointer-up")}else{m.slideDown("fast");l.addClass("dd-pointer-up")}h(p)}function k(l){l.find(".dd-options").slideUp(50);l.find(".dd-pointer").removeClass("dd-pointer-up").removeClass("dd-pointer-up")}function g(o){var n=o.find(".dd-select").css("height");var m=o.find(".dd-selected-description");var l=o.find(".dd-selected-image");if(m.length<=0&&l.length>0){o.find(".dd-selected-text").css("lineHeight",n)}}function h(l){l.find(".dd-option").each(function(){var p=e(this);var n=p.css("height");var o=p.find(".dd-option-description");var m=l.find(".dd-option-image");if(o.length<=0&&m.length>0){p.find(".dd-option-text").css("lineHeight",n)}})}})(jQuery); +(function(e){e.fn.ddslick=function(l){if(c[l]){return c[l].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof l==="object"||!l){return c.init.apply(this,arguments)}else{e.error("Method "+l+" does not exists.")}}};var c={},d={data:[],keepJSONItemsOnTop:false,width:260,height:null,background:"#eee",selectText:"",defaultSelectedIndex:null,truncateDescription:true,imagePosition:"left",showSelectedHTML:true,clickOffToClose:true,embedCSS:true,onSelected:function(){}},i='
    ',a='',b='';if(e("#css-ddslick").length<=0){e(b).appendTo("head")}c.init=function(l){l=e.extend({},d,l);if(e("#css-ddslick").length<=0&&l.embedCSS){e(b).appendTo("head")}return this.each(function(){var s=e(this),q=s.data("ddslick");if(!q){var n=[],r=l.data;s.find("option").each(function(){var v=e(this),u=v.data();n.push({text:e.trim(v.text()),value:v.val(),selected:v.is(":selected"),description:u.description,imageSrc:u.imagesrc})});if(l.keepJSONItemsOnTop){e.merge(l.data,n)}else{l.data=e.merge(n,l.data)}var p=s,t=e('
    ');s.replaceWith(t);s=t;s.addClass("dd-container").append(i).append(a);n=s.find(".dd-select");ddOptions=s.find(".dd-options");ddOptions.css({width:l.width});n.css({width:l.width,background:l.background});s.css({width:l.width});if(l.height!==null){ddOptions.css({height:l.height,overflow:"auto"})}e.each(l.data,function(u,v){if(v.selected){l.defaultSelectedIndex=u}ddOptions.append('
  • '+(v.value?' ':"")+(v.imageSrc?' ':"")+(v.text?' ":"")+(v.description?' '+v.description+"":"")+"
  • ")});var m={settings:l,original:p,selectedIndex:-1,selectedItem:null,selectedData:null};s.data("ddslick",m);if(l.selectText.length>0&&l.defaultSelectedIndex===null){s.find(".dd-selected").html(l.selectText)}else{var o=(l.defaultSelectedIndex!==null&&l.defaultSelectedIndex>=0&&l.defaultSelectedIndex':"")+(t.text?'":"")+(t.description?''+t.description+"":""))}else{r.html(t.text)}n.val(t.value);u.original.val(t.value);q.data("ddslick",u);k(q);g(q);if(typeof o.onSelected=="function"){o.onSelected.call(this,u)}}function f(p){var o=p.find(".dd-select"),m=o.siblings(".dd-options"),l=o.find(".dd-pointer"),n=m.is(":visible");e(".dd-click-off-close").not(m).slideUp(50);e(".dd-pointer").removeClass("dd-pointer-up");if(n){m.slideUp("fast");l.removeClass("dd-pointer-up")}else{m.slideDown("fast");l.addClass("dd-pointer-up")}h(p)}function k(l){l.find(".dd-options").slideUp(50);l.find(".dd-pointer").removeClass("dd-pointer-up").removeClass("dd-pointer-up")}function g(o){var n=o.find(".dd-select").css("height");var m=o.find(".dd-selected-description");var l=o.find(".dd-selected-image");if(m.length<=0&&l.length>0){o.find(".dd-selected-text").css("lineHeight",n)}}function h(l){l.find(".dd-option").each(function(){var p=e(this);var n=p.css("height");var o=p.find(".dd-option-description");var m=l.find(".dd-option-image");if(o.length<=0&&m.length>0){p.find(".dd-option-text").css("lineHeight",n)}})}})(jQuery); \ No newline at end of file From 7a63499df22f0f2c59f776e74a9bdb7c18cecec6 Mon Sep 17 00:00:00 2001 From: Bryan Chow Date: Thu, 31 Jan 2013 16:25:12 -0800 Subject: [PATCH 2/2] Prevent onSelected callback from being called at initialization --- jquery.ddslick.js | 11 ++++++++--- jquery.ddslick.min.js | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/jquery.ddslick.js b/jquery.ddslick.js index 4cb8121..944113a 100644 --- a/jquery.ddslick.js +++ b/jquery.ddslick.js @@ -147,7 +147,7 @@ } else { var index = (options.defaultSelectedIndex !== null && options.defaultSelectedIndex >= 0 && options.defaultSelectedIndex < options.data.length) ? options.defaultSelectedIndex : 0; - selectIndex(obj, index); + selectIndex(obj, index, false); } //EVENTS @@ -220,7 +220,12 @@ }; //Private: Select index - function selectIndex(obj, index) { + function selectIndex(obj, index, doCallback) { + + // If true, fire the onSelected callback; true by if not specified + if (typeof doCallback === 'undefined') { + doCallback = true; + } //Get plugin data var pluginData = obj.data('ddslick'); @@ -270,7 +275,7 @@ adjustSelectedHeight(obj); //Callback function on selection - if (typeof settings.onSelected == 'function') { + if (doCallback && typeof settings.onSelected == 'function') { settings.onSelected.call(this, pluginData); } } diff --git a/jquery.ddslick.min.js b/jquery.ddslick.min.js index 6b560a1..53d35aa 100644 --- a/jquery.ddslick.min.js +++ b/jquery.ddslick.min.js @@ -1 +1 @@ -(function(e){e.fn.ddslick=function(l){if(c[l]){return c[l].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof l==="object"||!l){return c.init.apply(this,arguments)}else{e.error("Method "+l+" does not exists.")}}};var c={},d={data:[],keepJSONItemsOnTop:false,width:260,height:null,background:"#eee",selectText:"",defaultSelectedIndex:null,truncateDescription:true,imagePosition:"left",showSelectedHTML:true,clickOffToClose:true,embedCSS:true,onSelected:function(){}},i='
    ',a='
      ',b='';if(e("#css-ddslick").length<=0){e(b).appendTo("head")}c.init=function(l){l=e.extend({},d,l);if(e("#css-ddslick").length<=0&&l.embedCSS){e(b).appendTo("head")}return this.each(function(){var s=e(this),q=s.data("ddslick");if(!q){var n=[],r=l.data;s.find("option").each(function(){var v=e(this),u=v.data();n.push({text:e.trim(v.text()),value:v.val(),selected:v.is(":selected"),description:u.description,imageSrc:u.imagesrc})});if(l.keepJSONItemsOnTop){e.merge(l.data,n)}else{l.data=e.merge(n,l.data)}var p=s,t=e('
      ');s.replaceWith(t);s=t;s.addClass("dd-container").append(i).append(a);n=s.find(".dd-select");ddOptions=s.find(".dd-options");ddOptions.css({width:l.width});n.css({width:l.width,background:l.background});s.css({width:l.width});if(l.height!==null){ddOptions.css({height:l.height,overflow:"auto"})}e.each(l.data,function(u,v){if(v.selected){l.defaultSelectedIndex=u}ddOptions.append('
    • '+(v.value?' ':"")+(v.imageSrc?' ':"")+(v.text?' ":"")+(v.description?' '+v.description+"":"")+"
    • ")});var m={settings:l,original:p,selectedIndex:-1,selectedItem:null,selectedData:null};s.data("ddslick",m);if(l.selectText.length>0&&l.defaultSelectedIndex===null){s.find(".dd-selected").html(l.selectText)}else{var o=(l.defaultSelectedIndex!==null&&l.defaultSelectedIndex>=0&&l.defaultSelectedIndex':"")+(t.text?'":"")+(t.description?''+t.description+"":""))}else{r.html(t.text)}n.val(t.value);u.original.val(t.value);q.data("ddslick",u);k(q);g(q);if(typeof o.onSelected=="function"){o.onSelected.call(this,u)}}function f(p){var o=p.find(".dd-select"),m=o.siblings(".dd-options"),l=o.find(".dd-pointer"),n=m.is(":visible");e(".dd-click-off-close").not(m).slideUp(50);e(".dd-pointer").removeClass("dd-pointer-up");if(n){m.slideUp("fast");l.removeClass("dd-pointer-up")}else{m.slideDown("fast");l.addClass("dd-pointer-up")}h(p)}function k(l){l.find(".dd-options").slideUp(50);l.find(".dd-pointer").removeClass("dd-pointer-up").removeClass("dd-pointer-up")}function g(o){var n=o.find(".dd-select").css("height");var m=o.find(".dd-selected-description");var l=o.find(".dd-selected-image");if(m.length<=0&&l.length>0){o.find(".dd-selected-text").css("lineHeight",n)}}function h(l){l.find(".dd-option").each(function(){var p=e(this);var n=p.css("height");var o=p.find(".dd-option-description");var m=l.find(".dd-option-image");if(o.length<=0&&m.length>0){p.find(".dd-option-text").css("lineHeight",n)}})}})(jQuery); \ No newline at end of file +(function(e){e.fn.ddslick=function(l){if(c[l]){return c[l].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof l==="object"||!l){return c.init.apply(this,arguments)}else{e.error("Method "+l+" does not exists.")}}};var c={},d={data:[],keepJSONItemsOnTop:false,width:260,height:null,background:"#eee",selectText:"",defaultSelectedIndex:null,truncateDescription:true,imagePosition:"left",showSelectedHTML:true,clickOffToClose:true,embedCSS:true,onSelected:function(){}},i='
      ',a='
        ',b='';if(e("#css-ddslick").length<=0){e(b).appendTo("head")}c.init=function(l){l=e.extend({},d,l);if(e("#css-ddslick").length<=0&&l.embedCSS){e(b).appendTo("head")}return this.each(function(){var s=e(this),q=s.data("ddslick");if(!q){var n=[],r=l.data;s.find("option").each(function(){var v=e(this),u=v.data();n.push({text:e.trim(v.text()),value:v.val(),selected:v.is(":selected"),description:u.description,imageSrc:u.imagesrc})});if(l.keepJSONItemsOnTop){e.merge(l.data,n)}else{l.data=e.merge(n,l.data)}var p=s,t=e('
        ');s.replaceWith(t);s=t;s.addClass("dd-container").append(i).append(a);n=s.find(".dd-select");ddOptions=s.find(".dd-options");ddOptions.css({width:l.width});n.css({width:l.width,background:l.background});s.css({width:l.width});if(l.height!==null){ddOptions.css({height:l.height,overflow:"auto"})}e.each(l.data,function(u,v){if(v.selected){l.defaultSelectedIndex=u}ddOptions.append('
      • '+(v.value?' ':"")+(v.imageSrc?' ':"")+(v.text?' ":"")+(v.description?' '+v.description+"":"")+"
      • ")});var m={settings:l,original:p,selectedIndex:-1,selectedItem:null,selectedData:null};s.data("ddslick",m);if(l.selectText.length>0&&l.defaultSelectedIndex===null){s.find(".dd-selected").html(l.selectText)}else{var o=(l.defaultSelectedIndex!==null&&l.defaultSelectedIndex>=0&&l.defaultSelectedIndex':"")+(t.text?'":"")+(t.description?''+t.description+"":""))}else{r.html(t.text)}n.val(t.value);v.original.val(t.value);q.data("ddslick",v);k(q);g(q);if(u&&typeof o.onSelected=="function"){o.onSelected.call(this,v)}}function f(p){var o=p.find(".dd-select"),m=o.siblings(".dd-options"),l=o.find(".dd-pointer"),n=m.is(":visible");e(".dd-click-off-close").not(m).slideUp(50);e(".dd-pointer").removeClass("dd-pointer-up");if(n){m.slideUp("fast");l.removeClass("dd-pointer-up")}else{m.slideDown("fast");l.addClass("dd-pointer-up")}h(p)}function k(l){l.find(".dd-options").slideUp(50);l.find(".dd-pointer").removeClass("dd-pointer-up").removeClass("dd-pointer-up")}function g(o){var n=o.find(".dd-select").css("height");var m=o.find(".dd-selected-description");var l=o.find(".dd-selected-image");if(m.length<=0&&l.length>0){o.find(".dd-selected-text").css("lineHeight",n)}}function h(l){l.find(".dd-option").each(function(){var p=e(this);var n=p.css("height");var o=p.find(".dd-option-description");var m=l.find(".dd-option-image");if(o.length<=0&&m.length>0){p.find(".dd-option-text").css("lineHeight",n)}})}})(jQuery); \ No newline at end of file