forked from will-v-king/jQuery-Skinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-skinner.js
349 lines (348 loc) · 14 KB
/
jquery-skinner.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
/************************************************************************
*************************************************************************
@Name : skinner - jQuery Plugin
@Revison : 1.3.5
@Date : 17/10/2011
@Author: Andrea (anbi) Bianchin - http://www.andreabianchin.it#projects - http://twitter.com/#!/anbi
@License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
*************************************************************************
@Usage : allowed parameters
# type : - left > float left (default)
- right > float right
- block > display block; float:none;
# width : - auto > (default)
- pixel > e.g. 100px
- percent > e.g. 80%
# textwrap : - false > text doesn't wrap on dropdown (default)
- true > text wrap on dropdown
# maxitem : - 2,3,4,5... "n" > number of item to show (default = 6)
- false > show all item without scollbar
# minWidth # width : - auto > (default) number of the min-width of the text
- pixel > e.g. 100px
- percent > e.g. 80%
# itemMinWidth - "n" > number of the min-width of the dropdown
- pixel > e.g. 100px
- percent > e.g. 80%
# mode - 'select'(default) > layout as select;
- 'pureText' > layout as pure text;
# placeHolder - 'please select'(default) the text to show when there's no value
# valueNullable - false(default) the select must be selected
, the first option is the default option.
- true the first option is the empty option,
and it won't be show in the list.
# disabled - false(default) the disabled status of the select
- true when the disabled attr of the select is 'disabled',
true will be set automatically.
**************************************************************************
@modifiedDate : 15/01/2015
@modifiedBy : Will.V.King
@modification : add supports for setting the min-width attribute of the text of the select and its dropdown-list; add a ' pure text ' visual mode.
**************************************************************************
@modifiedData : 16/01/2015
@modifiedBy : Will.V.King
@modification : added placeHolder;
added valueNullable;
Fixed Variable Scope Bug;
Improved the function usage (_li_click,_apply_max_item is added
, checkSelect has been changed to _init_li);
added dorpdownlist auto hide logic;
************************************************************************
@modifiedData : 16/01/2015
@modifiedBy : Will.V.King
@modification : changed the value of valueNullable from string to boolean.
*************************************************************************
@modifiedData : 26/05/2015
@modifiedBy : Will.V.King
@modification : improve the drop down list behaviour.
*************************************************************************
@modifiedData : 05/06/2015
@modifiedBy : Will.V.King
@modification : added disabled option; added beforeClickItem/whenClickItem/afterClickItem
*************************************************************************/
(function($) {
$.fn.skinner = function(opt) {
var cfg = {
'type': 'block',
'minWidth': '',
'width': 'auto',
'textwrap': false,
'maxitem': '6',
'itemMinWidth': '',
'mode': 'select',
'placeHolder': 'please select',
'valueNullable': false,
'disabled': false,
'dropdownList': {
'hideMode': 'auto'
}
};
if (undefined !== opt) {
$.extend(cfg, opt);
}
var element = this;
cfg.disabled = $(element).prop('disabled');
var skin = {
$: {
select: null, // select
container: null, // select-skinned
controller: null, // select-skinned-text
acceptor: null // UL
}
};
skin = $.extend(skin, {
_li_click: function(i /*the index of the option of select*/ , e) {
var $this = this,
itemUL = skin.$.acceptor;
itemUL.hide();
$(element).triggerHandler('whenClickItem', [i, $this, e]);
var selectSkinnedContHTML = (skin.$.controller).children('.select-skinned-cont').html(),
$select = skin.$.select,
$selectOptions = $select.find('option'),
$selectOptionI = $selectOptions.eq(i),
$selectOption0 = $selectOptions.eq(0);
selectSkinnedContHTML = (selectSkinnedContHTML === ' ' ? '' : selectSkinnedContHTML);
if ('multiple' !== cfg.multiple) {
if (selectSkinnedContHTML !== $selectOptionI.text()) {
$selectOptions.removeAttr('selected');
$selectOptionI.attr('selected', 'selected');
(skin.$.controller).html('<div class="select-skinned-cont">' + $selectOptionI.text() + '</div>');
$this.addClass('select-skinned-li-selected');
itemUL.nextAll('select:first').change();
} else {
if (true === cfg.valueNullable) {
$selectOptionI.removeAttr('selected');
$selectOption0.attr('selected', 'selected'); // the first option is the default value, as select thought;
(skin.$.controller).html('<div class="select-skinned-cont">' + $selectOption0.html() || (cfg.placeHolder || ' ') + '</div>');
$this.removeClass('select-skinned-li-selected');
itemUL.nextAll('select:first').change();
} else {
itemUL.nextAll('select:first').trigger('unchange');
}
}
} else { // TO DO support multiple select
/*
* for multiple select
if(selectSkinnedContHTML !== $selectOptions.eq(i).text()){// select
// $selectOptions.removeAttr('selected');
$selectOptions.eq(i).attr('selected','selected');
itemUL.prev('.select-skinned-text').html('<div class="select-skinned-cont">'+$selectOptions.eq(i).text()+'</div>');
$this.addClass('select-skinned-li-selected');
}else{// select again
$selectOptions.eq(i).removeAttr('selected');
if(0 === $select.find('option[selected]').size()){// the first option is the default value, as select thought;
$selectOption0.attr('selected','selected');
}
itemUL.prev('.select-skinned-text').html('<div class="select-skinned-cont">'+(cfg.placeHolder||' ')+'</div>');
$this.removeClass('select-skinned-li-selected');
}
*/
}
},
_init_li: function(elem) {
var $elem = $(elem),
$UL = (skin.$.acceptor);
$UL.html('');
$elem.children('option').each(function(i, el) {
var $el = $(el),
myText = ($el.html() === '') ? ' ' : $el.html();
if (0 === i) {
if (true === cfg.valueNullable) { // hide the first option;
return;
} else {
if (0 === $elem.children('option[selected]').size()) { // select the first option
$el.attr('selected', 'selected');
} else {}
}
} else {}
var itemLI = $('<li>' + myText + '</li>').click(function(ev) {
return $(element).triggerHandler('beforeClickItem', [i, itemLI, $elem]);
}).click(function(ev) {
if (false === ev.result) {} else {
skin._li_click.call(itemLI, i, $elem);
$(element).triggerHandler('afterClickItem', [i, itemLI, $elem]);
}
});
if (true === $el.prop('selected')) { // add selected style to li
itemLI.addClass('select-skinned-li-selected');
} else {}
$UL.append(itemLI);
});
},
_apply_max_item: function() {
var $UL = $(this),
$LI = $UL.children('li'),
maxitem = cfg.maxitem;
if (undefined !== maxitem && false !== maxitem) { // apply the max item to show;
var hTotItem = 0,
i = maxitem;
while (i-- > 0) {
hTotItem += $LI.eq(i).outerHeight();
}
maxitem = cfg.maxitem;
$UL.css({
'overflow-y': 'scroll',
'height': hTotItem + 'px'
});
}
}
});
skin = $.extend(skin, {
selectskinned: function(element) {
var $element = $(element);
skin.$.select = $element;
$element.wrap('<div class="select-skinned" />');
skin.$.container = $element.parent();
var childrennn = $('<ul style="display:none;"></ul>');
skin.$.acceptor = childrennn;
$element.parent().on('mouseleave', function() {
if ('auto' === cfg.dropdownList.hideMode || 'leave' === cfg.dropdownList.hideMode) {
childrennn.hide();
}
});
// will init the li in skin._init_li
if (true === cfg.valueNullable) {
if (0 === $element.children('option[selected]').size()) { // select the first option
$element.children('option').eq(0).attr('selected', 'selected');
} else {}
} else {
if (0 === $element.children('option[selected]').size()) { // select the first option
$element.children('option').eq(0).attr('selected', 'selected');
} else {}
}
var selectedOptions = $element.children('option[selected]'), // 'option:selected' will always has selected content.(default is the first item.)
testoSel = (selectedOptions.size() <= 0 ? cfg.placeHolder : (selectedOptions.text() === '' ? ' ' : selectedOptions.text())),
selectedItem = $('<div class="select-skinned-text"><div class="select-skinned-cont">' + testoSel + '</div></div>');
skin.$.controller = selectedItem;
selectedItem.click(function() {
var $this = $(this),
elemUL = $this.nextAll('ul:first'),
pos = skin.$.container.offset(),
parentScrollable = skin.$.container.parent(':scrollable'),
parentTop = 0,
parentLeft = 0;
if (0 === parentScrollable.size()) {
parentScrollable = $(window);
} else {
parentTop = parentScrollable.offset().top;
parentLeft = parentScrollable.offset().left;
}
var parentScrollTop = parentScrollable.scrollTop();
if ((elemUL.outerHeight() + (15)) < (pos.top - parentScrollTop) && (parentScrollable.height() + parentTop <= (pos.top + elemUL.outerHeight() + (15) - parentScrollTop))) {
elemUL.css({
'top': 'auto',
'bottom': '0'
});
} else {
elemUL.css({
'top': $this.prev('.select-skinned-text').children('.select-skinned-cont').height(),
'bottom': 'auto'
});
}
if (parentScrollable.width() + parentLeft <= (pos.left + elemUL.outerWidth())) {
elemUL.css({
'left': 'auto',
'right': '0'
});
} else {
elemUL.css({
'left': '0',
'right': 'auto'
});
}
if (elemUL.is(':visible')) {
elemUL.hide();
} else {
cfg.disabled = $(this).siblings('select:first').prop('disabled');
if (true === cfg.disabled) {
elemUL.hide();
} else {
elemUL.show();
}
}
skin._apply_max_item.call(elemUL.get(0));
});
$element.before(selectedItem);
$element.before(childrennn);
$element.change(function() {
skin._init_li(element);
skin.addStyle();
});
$element.hide();
skin._init_li(element);
skin.addStyle();
},
resetSelect: function(e) {
if ($(e).prev('ul').size() !== 0) { // use '!==' to compare with '0' is recommended.
$(e).prev('ul').prev('.select-skinned-text').html('<div class="select-skinned-cont">' + $(e).find('option').eq(0).text() + '</div>');
}
},
addStyle: function() {
var $element = $(element),
$selectSkinned = skin.$.container,
$LIs = $selectSkinned.children('ul').children('li');
if ('auto' === cfg.width) { // set width accroding to the width of the select element
cfg.width = $element.width();
} else {}
$selectSkinned.css("min-width", (cfg.minWidth || cfg.width)).width(cfg.width).children('ul').css("min-width", (cfg.itemMinWidth || cfg.width)); //.width(cfg.width);
$selectSkinned.css({
'height': skin.$.controller.height() // \9
});
if (true === cfg.textwrap) {
$LIs.css({
'white-space': 'normal'
});
}
$LIs.hover(function() {
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
});
(skin.$.controller).hover(function() {
cfg.disabled = $(this).siblings('select:first').prop('disabled');
}, function() {});
if ('puretext' === cfg.mode.toLowerCase()) {
var spt = "select-skinned-pure-text";
(skin.$.controller).addClass(spt).hover(function() {
cfg.disabled = $(this).siblings('select:first').prop('disabled');
if (false === cfg.disabled) {
$(this).removeClass(spt);
} else {}
}, function() {
if (false === cfg.disabled) {
$(this).addClass(spt);
} else {}
});
} else {}
if ('block' !== cfg.type) {
$selectSkinned.css({
'float': cfg.type
});
}
}
});
if ($(element).parent().is('.select-skinned')) {} else {
skin.selectskinned(element);
}
return this;
};
// e.g.
// (function($) {
// $('.rightSelect').each(function(){
// var $this = $(this);
// $this.skinner({'type':$this.css("float"),'maxitem':'20'
// ,'minWidth':($this.css('min-width')||"40px")
// ,'mode': ($this.hasClass('pureText')?'pureText':'select')
// ,'valueNullable': ($this.hasClass('valueNullable')?true:false)
// });
// });
// $('.leftSelect').each(function(){
// var $this = $(this);
// $this.skinner({'type':$this.css("float"),'maxitem':'20'
// ,'minWidth':($this.css('min-width')||"40px")
// ,'mode': ($this.hasClass('pureText')?'pureText':'select')
// ,'valueNullable': ($this.hasClass('valueNullable')?true:false)
// });
// });
// })(jQuery);
})(jQuery);
/* vim: set si sts=4 ts=4 sw=4 fdm=indent :*/