Skip to content

Commit 392d211

Browse files
committed
Added option to customize items box markup, fix for expandToItemText
1 parent 1dd9614 commit 392d211

File tree

6 files changed

+68
-29
lines changed

6 files changed

+68
-29
lines changed

img/browser-icons.png

3.38 KB
Loading

index.html

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@
8383
.copy a:hover {
8484
color: #F60;
8585
}
86+
87+
.customOptions .selectricItems .ico {
88+
display: inline-block;
89+
vertical-align: middle;
90+
zoom: 1;
91+
*display: inline;
92+
height: 30px;
93+
width: 30px;
94+
margin: 0 6px 0 0;
95+
background: url(img/browser-icons.png) no-repeat;
96+
}
97+
98+
.customOptions .selectricItems .ico-chrome { background-position: 0 0; }
99+
.customOptions .selectricItems .ico-firefox { background-position: -30px 0; }
100+
.customOptions .selectricItems .ico-ie { background-position: -60px 0; }
101+
.customOptions .selectricItems .ico-opera { background-position: -90px 0; }
102+
.customOptions .selectricItems .ico-safari { background-position: -120px 0; }
86103
</style>
87104
<link rel="stylesheet" href="selectric.css">
88105
</head>
@@ -294,7 +311,7 @@ <h2>How to use</h2>
294311
});</code></pre>
295312

296313
<h2>Options</h2>
297-
<p>You can pass a options object as the first parameters when you call the plugin. For example:</p>
314+
<p>You can pass an options object as the first parameter when you call the plugin. For example:</p>
298315
<pre><code class="language-javascript">$('select').selectric({
299316
maxHeight: 200
300317
});</code></pre>
@@ -310,19 +327,19 @@ <h2>Options</h2>
310327
<td>onOpen</td>
311328
<td><pre><code>function() {}</code></pre></td>
312329
<td>Function</td>
313-
<td>Function called when select options is opened</td>
330+
<td>Function called when select options open</td>
314331
</tr>
315332
<tr>
316333
<td>onChange</td>
317334
<td><pre><code>function() {}</code></pre></td>
318335
<td>Function</td>
319-
<td>Function called when select options is changed</td>
336+
<td>Function called when select options change</td>
320337
</tr>
321338
<tr>
322339
<td>onClose</td>
323340
<td><pre><code>function() {}</code></pre></td>
324341
<td>Function</td>
325-
<td>Function called when select options is closed</td>
342+
<td>Function called when select options close</td>
326343
</tr>
327344
<tr>
328345
<td>maxHeight</td>
@@ -334,7 +351,7 @@ <h2>Options</h2>
334351
<td>keySearchTimeout</td>
335352
<td><pre><code>500</code></pre></td>
336353
<td>Integer</td>
337-
<td>After this time without pressing any key, the search string is reseted</td>
354+
<td>After this time without pressing any key, the search string is reset</td>
338355
</tr>
339356
<tr>
340357
<td>arrowButtonMarkup</td>
@@ -380,7 +397,15 @@ <h2>Options</h2>
380397
camelCase: true
381398
}</code></pre></td>
382399
<td>Object</td>
383-
<td>Custom classes</td>
400+
<td>Custom classes. <br> Every class in 'postfixes' should be separate with a space and follow this exact order: 'Input Items Open Disabled TempShow HideSelect Wrapper Hover Responsive'</td>
401+
</tr>
402+
<tr>
403+
<td>optionsItemBuilder</td>
404+
<td><pre><code>{text}</code></pre></td>
405+
<td>String or Function</td>
406+
<td>Define how each option should be rendered inside its &lt;li&gt; element. <br><br>
407+
If it's a string, all keys wrapped in brackets will be replaced by the respective values in itemData. Available keys are: 'value', 'text', 'slug', 'disabled'. <br><br>
408+
If it's a function, it will be called with the following parameters: (itemData, element, index). The function must return a string, no keys will be replaced in this method.</td>
384409
</tr>
385410
</table>
386411

@@ -469,6 +494,15 @@ <h2>Demo</h2>
469494
<option value="orange">Orange</option>
470495
</select>
471496

497+
<select class="customOptions">
498+
<option value="">Select with icons</option>
499+
<option value="firefox">Firefox</option>
500+
<option value="chrome">Chrome</option>
501+
<option value="safari">Safari</option>
502+
<option value="ie">Internet Explorer</option>
503+
<option value="opera">Opera</option>
504+
</select>
505+
472506
<select disabled>
473507
<option value="">Select disabled</option>
474508
<option value="apple">Apple</option>
@@ -571,6 +605,12 @@ <h2>Demo</h2>
571605
<script src="js/jquery.selectric.js"></script>
572606
<script>
573607
$(function(){
608+
$('.customOptions').selectric({
609+
optionsItemBuilder: function(itemData, element, index){
610+
return element.val().length ? '<span class="ico ico-' + element.val() + '"></span>' + itemData.text : itemData.text;
611+
}
612+
});
613+
574614
$('select, .select').selectric();
575615
});
576616
</script>

js/jquery.selectric.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* /,'
1010
* /'
1111
*
12-
* Selectric Ϟ v1.6.6 - http://lcdsantos.github.io/jQuery-Selectric/
12+
* Selectric Ϟ v1.6.7 - http://lcdsantos.github.io/jQuery-Selectric/
1313
*
1414
* Copyright (c) 2014 Leonardo Santos; Dual licensed: MIT/GPL
1515
*
@@ -37,6 +37,8 @@
3737

3838
return s;
3939
},
40+
// https://gist.github.com/atesgoral/984375
41+
format = function(f){var a=arguments;return(""+f).replace(/{(\d+|(\w+))}/g,function(s,i,p){return p&&a[1]?a[1][p]:a[i]})},
4042
init = function(element, options) {
4143
var options = $.extend(true, {
4244
onOpen: $.noop,
@@ -53,7 +55,8 @@
5355
prefix: 'selectric',
5456
postfixes: 'Input Items Open Disabled TempShow HideSelect Wrapper Hover Responsive',
5557
camelCase: true
56-
}
58+
},
59+
optionsItemBuilder: '{text}', // function(itemData, element, index)
5760
}, options),
5861
customClass = options.customClass,
5962
postfixes = customClass.postfixes.split(' '),
@@ -63,11 +66,10 @@
6366
if (options.disableOnMobile && /android|ip(hone|od|ad)/i.test(navigator.userAgent)) return;
6467

6568
// generate classNames for elements
66-
while (currPostfix = postfixes.shift())
67-
arrClasses.push(customClass.camelCase ?
68-
customClass.prefix + currPostfix :
69-
(customClass.prefix + currPostfix).replace(/([A-Z])/g, "-$&").toLowerCase()
70-
);
69+
while (currPostfix = postfixes.shift()){
70+
var c = customClass.prefix + currPostfix;
71+
arrClasses.push(customClass.camelCase ? c : c.replace(/([A-Z])/g, "-$&").toLowerCase());
72+
}
7173

7274
var $original = $(element),
7375
_input = $('<input type="text" class="' + arrClasses[0] + '"/>'),
@@ -109,7 +111,8 @@
109111
$options.each(function(i){
110112
var $elm = $(this),
111113
optionText = $elm.html(),
112-
selectDisabled = $elm.prop('disabled');
114+
selectDisabled = $elm.prop('disabled'),
115+
itemBuilder = options.optionsItemBuilder;
113116

114117
selectItems[i] = {
115118
value: $elm.val(),
@@ -118,7 +121,10 @@
118121
disabled: selectDisabled
119122
};
120123

121-
_$li += '<li class="' + (i == currValue ? selectStr : '') + (i == optionsLength - 1 ? ' last' : '') + (selectDisabled ? ' disabled' : '') + '">' + optionText + '</li>';
124+
_$li += format('<li class="{1}">{2}</li>',
125+
$.trim([i == currValue ? selectStr : '', i == optionsLength - 1 ? 'last' : '', selectDisabled ? 'disabled' : ''].join(' ')),
126+
$.isFunction(itemBuilder) ? itemBuilder(selectItems[i], $elm, i) : format(itemBuilder, selectItems[i])
127+
);
122128
});
123129

124130
$items.html(_$li + '</ul>');
@@ -135,8 +141,6 @@
135141
$(this).toggleClass(arrClasses[7]);
136142
});
137143

138-
_input.prop('disabled', false);
139-
140144
// Click on label and :focus on original select will open the options box
141145
options.openOnHover && $wrapper.on('mouseenter' + bindSufix, _open);
142146

@@ -153,7 +157,7 @@
153157
}
154158
}
155159

156-
_input.off().on({
160+
_input.prop('disabled', false).off().on({
157161
keypress: _handleSystemKeys,
158162
keydown: function(e){
159163
_handleSystemKeys(e);
@@ -231,7 +235,7 @@
231235

232236
// Set a really long width for $outerWrapper
233237
$outerWrapper.width(9e4);
234-
finalWidth = itemsWidth;
238+
finalWidth = $items.width();
235239
// Set scroll bar to auto
236240
$items.css('overflow', '');
237241
$outerWrapper.width('');
@@ -256,12 +260,10 @@
256260
isOpen = true;
257261
itemsHeight = $items.outerHeight();
258262

259-
_isInViewport();
260-
261263
// Give dummy input focus
262264
_input.val('').is(':focus') || _input.focus();
263265

264-
$doc.on(clickBind, _close);
266+
$doc.on(clickBind, _close).on('scroll' + bindSufix, _isInViewport);
265267

266268
// Delay close effect when openOnHover is true
267269
if (options.openOnHover){
@@ -280,11 +282,8 @@
280282

281283
// Detect is the options box is inside the window
282284
function _isInViewport() {
283-
if (isOpen){
284285
_calculateOptionsDimensions();
285286
$items.css('top', ($outerWrapper.offset().top + $outerWrapper.outerHeight() + itemsHeight > $win.scrollTop() + $win.height()) ? -itemsHeight : '');
286-
setTimeout(_isInViewport, 100);
287-
}
288287
}
289288

290289
// Close the select options box

js/jquery.selectric.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-selectric",
3-
"version": "1.6.6",
3+
"version": "1.6.7",
44
"devDependencies": {
55
"grunt": "~0.4.4",
66
"grunt-contrib-uglify": "~0.4.0"

selectric.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"input",
1111
"ui"
1212
],
13-
"version": "1.6.6",
13+
"version": "1.6.7",
1414
"author": {
1515
"name": "Leonardo Santos",
1616
"email": "leocs.1991@gmail.com"

0 commit comments

Comments
 (0)