forked from prashantchaudhary/ddslick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ddslick.js
359 lines (301 loc) · 15.2 KB
/
jquery.ddslick.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//Title: Custom DropDown plugin by PC
//Documentation: http://designwithpc.com/Plugins/ddslick
//Author: PC
//Website: http://designwithpc.com
//Twitter: http://twitter.com/chaudharyp
(function ($) {
$.fn.ddslick = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exists.');
}
};
var methods = {},
//Set defauls for the control
defaults = {
data: [],
keepJSONItemsOnTop: false,
width: 260,
height: null,
background: "#eee",
selectText: "",
defaultSelectedIndex: null,
truncateDescription: true,
imagePosition: "left",
showSelectedHTML: true,
clickOffToClose: true,
embedCSS: true,
onSelected: function () { }
},
ddSelectHtml = '<div class="dd-select"><input class="dd-selected-value" type="hidden" /><a class="dd-selected"></a><span class="dd-pointer dd-pointer-down"></span></div>',
ddOptionsHtml = '<ul class="dd-options"></ul>',
//CSS for ddSlick
ddslickCSS = '<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>';
//Public methods
methods.init = function (userOptions) {
//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');
}
//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
if (!data) {
var ddSelect = [], ddJson = options.data;
//Get data from HTML select options
obj.find('option').each(function () {
var $this = $(this), thisData = $this.data();
ddSelect.push({
text: $.trim($this.text()),
value: $this.val(),
selected: $this.is(':selected'),
description: thisData.description,
imageSrc: thisData.imagesrc //keep it lowercase for HTML5 data-attributes
});
});
//Update Plugin data merging both HTML select data and JSON data for the dropdown
if (options.keepJSONItemsOnTop)
$.merge(options.data, ddSelect);
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');
obj.replaceWith(placeholder);
obj = placeholder;
//Add classes and append ddSelectHtml & ddOptionsHtml to the container
obj.addClass('dd-container').append(ddSelectHtml).append(ddOptionsHtml);
// Inherit name attribute from original element
obj.find("input.dd-selected-value")
.attr("id", $(original).attr("id"))
.attr("name", $(original).attr("name"));
//Get newly created ddOptions and ddSelect to manipulate
var 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)
ddOptions.css({ height: options.height, overflow: 'auto' });
//Add ddOptions to the container. Replace with template engine later.
$.each(options.data, function (index, item) {
if (item.selected) options.defaultSelectedIndex = index;
ddOptions.append('<li>' +
'<a class="dd-option">' +
(item.value ? ' <input class="dd-option-value" type="hidden" value="' + item.value + '" />' : '') +
(item.imageSrc ? ' <img class="dd-option-image' + (options.imagePosition == "right" ? ' dd-image-right' : '') + '" src="' + item.imageSrc + '" />' : '') +
(item.text ? ' <label class="dd-option-text">' + item.text + '</label>' : '') +
(item.description ? ' <small class="dd-option-description dd-desc">' + item.description + '</small>' : '') +
'</a>' +
'</li>');
});
//Save plugin data.
var pluginData = {
settings: options,
original: original,
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) {
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);
}
//EVENTS
//Displaying options
obj.find('.dd-select').on('click.ddslick', function () {
open(obj);
});
//Selecting an option
obj.find('.dd-option').on('click.ddslick', function () {
selectIndex(obj, $(this).closest('li').index());
});
//Click anywhere to close
if (options.clickOffToClose) {
ddOptions.addClass('dd-click-off-close');
obj.on('click.ddslick', function (e) { e.stopPropagation(); });
$('body').on('click', function () {
$('.dd-open').removeClass('dd-open');
$('.dd-click-off-close').slideUp(50).siblings('.dd-select').find('.dd-pointer').removeClass('dd-pointer-up');
});
}
}
});
};
//Public method to select an option by its index
methods.select = function (options) {
return this.each(function () {
if (options.index!==undefined)
selectIndex($(this), options.index);
if (options.id)
selectId($(this), options.id);
});
}
//Public method to open drop down
methods.open = function () {
return this.each(function () {
var $this = $(this),
pluginData = $this.data('ddslick');
//Check if plugin is initialized
if (pluginData)
open($this);
});
};
//Public method to close drop down
methods.close = function () {
return this.each(function () {
var $this = $(this),
pluginData = $this.data('ddslick');
//Check if plugin is initialized
if (pluginData)
close($this);
});
};
//Public method to destroy. Unbind all events and restore the original Html select/options
methods.destroy = function () {
return this.each(function () {
var $this = $(this),
pluginData = $this.data('ddslick');
//Check if already destroyed
if (pluginData) {
var originalElement = pluginData.original;
$this.removeData('ddslick').unbind('.ddslick').replaceWith(originalElement);
}
});
}
//Private: Select id
function selectId(obj, id) {
var index = obj.find(".dd-option-value[value= '" + id + "']").parents("li").prevAll().length;
selectIndex(obj, index);
}
//Private: Select index
function selectIndex(obj, index) {
//Get plugin data
var pluginData = obj.data('ddslick');
//Get required elements
var ddSelected = obj.find('.dd-selected'),
ddSelectedValue = ddSelected.siblings('.dd-selected-value'),
ddOptions = obj.find('.dd-options'),
ddPointer = ddSelected.siblings('.dd-pointer'),
selectedOption = obj.find('.dd-option').eq(index),
selectedLiItem = selectedOption.closest('li'),
settings = pluginData.settings,
selectedData = pluginData.settings.data[index];
//Highlight selected option
obj.find('.dd-option').removeClass('dd-option-selected');
selectedOption.addClass('dd-option-selected');
//Update or Set plugin data with new selection
pluginData.selectedIndex = index;
pluginData.selectedItem = selectedLiItem;
pluginData.selectedData = selectedData;
//If set to display to full html, add html
if (settings.showSelectedHTML) {
ddSelected.html(
(selectedData.imageSrc ? '<img class="dd-selected-image' + (settings.imagePosition == "right" ? ' dd-image-right' : '') + '" src="' + selectedData.imageSrc + '" />' : '') +
(selectedData.text ? '<label class="dd-selected-text">' + selectedData.text + '</label>' : '') +
(selectedData.description ? '<small class="dd-selected-description dd-desc' + (settings.truncateDescription ? ' dd-selected-description-truncated' : '') + '" >' + selectedData.description + '</small>' : '')
);
}
//Else only display text as selection
else ddSelected.html(selectedData.text);
//Updating selected option value
ddSelectedValue.val(selectedData.value);
//BONUS! Update the original element attribute with the new selection
pluginData.original.val(selectedData.value);
obj.data('ddslick', pluginData);
//Close options on selection
close(obj);
//Adjust appearence for selected option
adjustSelectedHeight(obj);
//Callback function on selection
if (typeof settings.onSelected == 'function') {
settings.onSelected.call(this, pluginData);
}
}
//Private: Close the drop down options
function open(obj) {
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');
$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
function close(obj) {
//Close drop down and adjust pointer direction
obj.find('.dd-select').removeClass('dd-open');
obj.find('.dd-options').slideUp(50);
obj.find('.dd-pointer').removeClass('dd-pointer-up').removeClass('dd-pointer-up');
}
//Private: Adjust appearence for selected option (move title to middle), when no desripction
function adjustSelectedHeight(obj) {
//Get height of dd-selected
var lSHeight = obj.find('.dd-select').css('height');
//Check if there is selected description
var descriptionSelected = obj.find('.dd-selected-description');
var imgSelected = obj.find('.dd-selected-image');
if (descriptionSelected.length <= 0 && imgSelected.length > 0) {
obj.find('.dd-selected-text').css('lineHeight', lSHeight);
}
}
//Private: Adjust appearence for drop down options (move title to middle), when no desripction
function adjustOptionsHeight(obj) {
obj.find('.dd-option').each(function () {
var $this = $(this);
var lOHeight = $this.css('height');
var descriptionOption = $this.find('.dd-option-description');
var imgOption = obj.find('.dd-option-image');
if (descriptionOption.length <= 0 && imgOption.length > 0) {
$this.find('.dd-option-text').css('lineHeight', lOHeight);
}
});
}
})(jQuery);