Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent onSelected callback from being called at initialization #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions jquery.ddslick.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}​</style>';
'.dd-container{ position:relative;} .dd-selected-text { font-weight:bold}</style>';

//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) {
Expand Down Expand Up @@ -101,16 +106,16 @@
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 });
ddSelect.css({ width: options.width, background: options.background });
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.
Expand All @@ -133,18 +138,16 @@
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;
selectIndex(obj, index);
var index = (options.defaultSelectedIndex !== null && options.defaultSelectedIndex >= 0 && options.defaultSelectedIndex < options.data.length) ? options.defaultSelectedIndex : 0;
selectIndex(obj, index, false);
}

//EVENTS
Expand Down Expand Up @@ -176,7 +179,7 @@
if (options.index!==undefined)
selectIndex($(this), options.index);
});
}
};

//Public method to open drop down
methods.open = function () {
Expand Down Expand Up @@ -214,10 +217,15 @@
$this.removeData('ddslick').unbind('.ddslick').replaceWith(originalElement);
}
});
}
};

//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');
Expand Down Expand Up @@ -267,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);
}
}
Expand Down Expand Up @@ -331,4 +339,4 @@
});
}

})(jQuery);
})(jQuery);
2 changes: 1 addition & 1 deletion jquery.ddslick.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.