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

Update jquery.ddslick.js #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
90 changes: 55 additions & 35 deletions jquery.ddslick.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Title: Custom DropDown plugin by PC
//Title: Custom DropDown plugin by PC
//Documentation: http://designwithpc.com/Plugins/ddslick
//Author: PC
//Website: http://designwithpc.com
Expand Down Expand Up @@ -31,7 +31,7 @@
imagePosition: "left",
showSelectedHTML: true,
clickOffToClose: true,
embedCSS: true,
embedCSS: true,
onSelected: function () { }
},

Expand Down Expand Up @@ -61,18 +61,18 @@
//Preserve the original defaults by passing an empty object as the target
//The object is used to get global flags like embedCSS.
var options = $.extend({}, defaults, userOptions);

//CSS styles are only added once.
if ($('#css-ddslick').length <= 0 && options.embedCSS) {
$(ddslickCSS).appendTo('head');
}
if ($('#css-ddslick').length <= 0 && options.embedCSS) {
$(ddslickCSS).appendTo('head');
}

//Apply on all selected elements
return this.each(function () {
//Preserve the original defaults by passing an empty object as the target
//The object is used to save drop-down's corresponding settings and data.
var options = $.extend({}, defaults, userOptions);

var obj = $(this),
data = obj.data('ddslick');
//If the plugin has not been initialized yet
Expand All @@ -98,7 +98,7 @@
else options.data = $.merge(ddSelect, options.data);

//Replace HTML select with empty placeholder, keep the original
var original = obj, placeholder = $('<div').attr('id', obj.attr('id') + '-dd-placeholder');
var original = obj, placeholder = $('<div>').attr('id', obj.attr('id') + '-dd-placeholder');
obj.replaceWith(placeholder);
obj = placeholder;

Expand Down Expand Up @@ -173,7 +173,7 @@
ddOptions.addClass('dd-click-off-close');
obj.on('click.ddslick', function (e) { e.stopPropagation(); });
$('body').on('click', function () {
$('.dd-open').removeClass('dd-open');
$('.dd-open').removeClass('dd-open');
$('.dd-click-off-close').slideUp(50).siblings('.dd-select').find('.dd-pointer').removeClass('dd-pointer-up');
});
}
Expand All @@ -184,7 +184,7 @@
//Public method to select an option by its index
methods.select = function (options) {
return this.each(function () {
if (options.index!==undefined)
if (options.index !== undefined)
selectIndex($(this), options.index);
if (options.id)
selectId($(this), options.id);
Expand Down Expand Up @@ -228,13 +228,31 @@
}
});
}

//Private: Select id

//Public method to disable dropdown
methods.disable = function () {
return this.each(function () {
var $this = $(this);

$this.addClass('dd-disabled');
});
};

//Public method to enable a disabled dropdown
methods.enable = function () {
return this.each(function () {
var $this = $(this);
if ($this.hasClass('dd-disabled'))
$this.removeClass('dd-disabled');
});
};

//Private: Select id
function selectId(obj, id) {
var index = obj.find(".dd-option-value[value= '" + id + "']").parents("li").prevAll().length;
selectIndex(obj, index);

var index = obj.find(".dd-option-value[value= '" + id + "']").parents("li").prevAll().length;
selectIndex(obj, index);

}

//Private: Select index
Expand Down Expand Up @@ -271,7 +289,7 @@
);

}
//Else only display text as selection
//Else only display text as selection
else ddSelected.html(selectedData.text);

//Updating selected option value
Expand All @@ -295,30 +313,31 @@

//Private: Close the drop down options
function open(obj) {

var $this = obj.find('.dd-select'),
if (!obj.hasClass('dd-disabled')) {
var $this = obj.find('.dd-select'),
ddOptions = $this.siblings('.dd-options'),
ddPointer = $this.find('.dd-pointer'),
wasOpen = ddOptions.is(':visible');

//Close all open options (multiple plugins) on the page
$('.dd-click-off-close').not(ddOptions).slideUp(50);
$('.dd-pointer').removeClass('dd-pointer-up');
$this.removeClass('dd-open');

if (wasOpen) {
ddOptions.slideUp('fast');
ddPointer.removeClass('dd-pointer-up');
//Close all open options (multiple plugins) on the page
$('.dd-click-off-close').not(ddOptions).slideUp(50);
$('.dd-pointer').removeClass('dd-pointer-up');
$this.removeClass('dd-open');
}
else {
$this.addClass('dd-open');
ddOptions.slideDown('fast');
ddPointer.addClass('dd-pointer-up');
}

//Fix text height (i.e. display title in center), if there is no description
adjustOptionsHeight(obj);
if (wasOpen) {
ddOptions.slideUp('fast');
ddPointer.removeClass('dd-pointer-up');
$this.removeClass('dd-open');
}
else {
$this.addClass('dd-open');
ddOptions.slideDown('fast');
ddPointer.addClass('dd-pointer-up');
}

//Fix text height (i.e. display title in center), if there is no description
adjustOptionsHeight(obj);
}
}

//Private: Close the drop down options
Expand Down Expand Up @@ -357,3 +376,4 @@
}

})(jQuery);