jquery-selectbox 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  # Jquery Selectbox
2
2
 
3
- Custom select box replacement inspired by jQuery UI source.
3
+ Custom select box replacement inspired by jQuery UI source. Demo [here](http://www.bulgaria-web-developers.com/projects/javascript/selectbox/)
4
4
 
5
5
  ## Installation
6
6
 
@@ -19,9 +19,11 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  `application.js`
22
+
22
23
  //= require jquery.selectbox
23
24
 
24
25
  `application.css`
26
+
25
27
  *= require jquery.selectbox
26
28
 
27
29
  or
@@ -30,10 +32,6 @@ or
30
32
 
31
33
  @import "jquery.selectbox";
32
34
 
33
- ## Credit
34
-
35
- See options and configuration on [jquery.selectbox project site](http://www.bulgaria-web-developers.com/projects/javascript/selectbox/).
36
-
37
35
  ## Contributing
38
36
 
39
37
  1. Fork it
@@ -41,3 +39,8 @@ See options and configuration on [jquery.selectbox project site](http://www.bulg
41
39
  3. Commit your changes (`git commit -am 'Added some feature'`)
42
40
  4. Push to the branch (`git push origin my-new-feature`)
43
41
  5. Create new Pull Request
42
+
43
+ ## Credits
44
+
45
+ See options and configuration on [jquery.selectbox project site](http://www.bulgaria-web-developers.com/projects/javascript/selectbox/).
46
+
@@ -0,0 +1,7 @@
1
+ file 'vendor/assets/images/jquery-selectbox/select-icons.png', 'https://select-box.googlecode.com/svn/trunk/select-icons.png'
2
+ file 'vendor/assets/javascripts/jquery.selectbox.js', 'https://select-box.googlecode.com/svn/trunk/jquery.selectbox-0.2.js'
3
+ file 'vendor/assets/stylesheets/jquery.selectbox.css.erb', 'https://select-box.googlecode.com/svn/trunk/jquery.selectbox.css' do |path|
4
+ rewrite(path) do |content|
5
+ content.gsub!("select-icons.png", "<%= asset_path('jquery-selectbox/select-icons.png') %>")
6
+ end
7
+ end
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "jquery-selectbox"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Jquery::Selectbox::VERSION
17
+
18
+ gem.add_development_dependency('vendorer')
17
19
  end
@@ -1,5 +1,5 @@
1
1
  module Jquery
2
2
  module Selectbox
3
- VERSION = "0.0.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,430 +1,513 @@
1
1
  /*!
2
- * jQuery Selectbox plugin 0.1.3
2
+ * jQuery Selectbox plugin 0.2
3
3
  *
4
- * Copyright 2011, Dimitar Ivanov (http://www.bulgaria-web-developers.com/projects/javascript/selectbox/)
4
+ * Copyright 2011-2012, Dimitar Ivanov (http://www.bulgaria-web-developers.com/projects/javascript/selectbox/)
5
5
  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6
- *
7
- * Date: Wed Jul 29 23:20:57 2011 +0200
8
- */
6
+ *
7
+ * Date: Tue Jul 17 19:58:36 2012 +0300
8
+ */
9
9
  (function ($, undefined) {
10
- var PROP_NAME = 'selectbox',
11
- FALSE = false,
12
- TRUE = true;
13
-
14
- /**
15
- * Selectbox manager.
16
- * Use the singleton instance of this class, $.selectbox, to interact with the select box.
17
- * Settings for (groups of) select boxes are maintained in an instance object,
18
- * allowing multiple different settings on the same page
19
- */
20
- function Selectbox() {
21
- this._state = [];
22
- this._defaults = { // Global defaults for all the select box instances
23
- classHolder: "sbHolder",
24
- classHolderDisabled: "sbHolderDisabled",
25
- classSelector: "sbSelector",
26
- classOptions: "sbOptions",
27
- classGroup: "sbGroup",
28
- classSub: "sbSub",
29
- classDisabled: "sbDisabled",
30
- classToggleOpen: "sbToggleOpen",
31
- classToggle: "sbToggle",
32
- speed: 200,
33
- effect: "slide", // "slide" or "fade"
34
- onChange: null, //Define a callback function when the selectbox is changed
35
- onOpen: null, //Define a callback function when the selectbox is open
36
- onClose: null //Define a callback function when the selectbox is closed
37
- };
38
- }
39
-
40
- $.extend(Selectbox.prototype, {
41
- /**
42
- * Is the first field in a jQuery collection open as a selectbox
43
- *
44
- * @param {Object} target
45
- * @return {Boolean}
46
- */
47
- _isOpenSelectbox: function (target) {
48
- if (!target) {
49
- return FALSE;
50
- }
51
- var inst = this._getInst(target);
52
- return inst.isOpen;
53
- },
54
-
55
- /**
56
- * Is the first field in a jQuery collection disabled as a selectbox
57
- *
58
- * @param {HTMLElement} target
59
- * @return {Boolean}
60
- */
61
- _isDisabledSelectbox: function (target) {
62
- if (!target) {
63
- return FALSE;
64
- }
65
- var inst = this._getInst(target);
66
- return inst.isDisabled;
67
- },
68
-
69
- /**
70
- * Attach the select box to a jQuery selection.
71
- *
72
- * @param {HTMLElement} target
73
- * @param {Object} settings
74
- */
75
- _attachSelectbox: function (target, settings) {
76
- if (this._getInst(target)) {
77
- return FALSE;
78
- }
79
- var $target = $(target),
80
- self = this,
81
- inst = self._newInst($target),
82
- sbHolder, sbSelector, sbToggle, sbOptions,
83
- s = FALSE, optGroup = $target.find("optgroup"), opts = $target.find("option"), olen = opts.length;
84
-
85
- $target.attr("sb", inst.uid);
86
-
87
- $.extend(inst.settings, self._defaults, settings);
88
- self._state[inst.uid] = FALSE;
89
- $target.hide();
90
-
91
- function closeOthers() {
92
- var key, uid = this.attr("id").split("_")[1];
93
- for (key in self._state) {
94
- if (key !== uid) {
95
- if (self._state.hasOwnProperty(key)) {
96
- if ($(":input[sb='" + key + "']")[0]) {
97
- self._closeSelectbox($(":input[sb='" + key + "']")[0]);
98
- }
99
- }
100
- }
101
- }
102
- }
103
-
104
- sbHolder = $("<div>", {
105
- "id": "sbHolder_" + inst.uid,
106
- "class": inst.settings.classHolder
107
- });
108
-
109
- sbSelector = $("<a>", {
110
- "id": "sbSelector_" + inst.uid,
111
- "href": "#",
112
- "class": inst.settings.classSelector,
113
- "click": function (e) {
114
- e.preventDefault();
115
- closeOthers.apply($(this), []);
116
- var uid = $(this).attr("id").split("_")[1];
117
- if (self._state[uid]) {
118
- self._closeSelectbox(target);
119
- } else {
120
- self._openSelectbox(target);
121
- }
122
- }
123
- });
124
-
125
- sbToggle = $("<a>", {
126
- "id": "sbToggle_" + inst.uid,
127
- "href": "#",
128
- "class": inst.settings.classToggle,
129
- "click": function (e) {
130
- e.preventDefault();
131
- closeOthers.apply($(this), []);
132
- var uid = $(this).attr("id").split("_")[1];
133
- if (self._state[uid]) {
134
- self._closeSelectbox(target);
135
- } else {
136
- self._openSelectbox(target);
137
- }
138
- }
139
- });
140
- sbToggle.appendTo(sbHolder);
141
-
142
- sbOptions = $("<ul>", {
143
- "id": "sbOptions_" + inst.uid,
144
- "class": inst.settings.classOptions,
145
- "css": {
146
- "display": "none"
147
- }
148
- });
149
-
150
- $target.children().each(function(i) {
151
- var that = $(this), li, config = {};
152
- if (that.is("option")) {
153
- getOptions(that);
154
- } else if (that.is("optgroup")) {
155
- li = $("<li>");
156
- $("<span>", {
157
- "text": that.attr("label")
158
- }).addClass(inst.settings.classGroup).appendTo(li);
159
- li.appendTo(sbOptions);
160
- if (that.is(":disabled")) {
161
- config.disabled = true;
162
- }
163
- config.sub = true;
164
- getOptions(that.find("option"), config);
165
- }
166
- });
167
-
168
- function getOptions () {
169
- var sub = arguments[1] && arguments[1].sub ? true : false,
170
- disabled = arguments[1] && arguments[1].disabled ? true : false;
171
- arguments[0].each(function (i) {
172
- var that = $(this),
173
- li = $("<li>"),
174
- child;
175
- if (that.is(":selected")) {
176
- sbSelector.text(that.text());
177
- s = TRUE;
178
- }
179
- if (i === olen - 1) {
180
- li.addClass("last");
181
- }
182
- if (!that.is(":disabled") && !disabled) {
183
- child = $("<a>", {
184
- "href": "#" + that.val(),
185
- "rel": that.val(),
186
- "text": that.text(),
187
- "click": function (e) {
188
- e.preventDefault();
189
- var t = sbToggle,
190
- uid = t.attr("id").split("_")[1];
191
- self._changeSelectbox(target, $(this).attr("rel"), $(this).text());
192
- self._closeSelectbox(target);
193
- }
194
- });
195
- if (sub) {
196
- child.addClass(inst.settings.classSub);
197
- }
198
- child.appendTo(li);
199
- } else {
200
- child = $("<span>", {
201
- "text": that.text()
202
- }).addClass(inst.settings.classDisabled);
203
- if (sub) {
204
- child.addClass(inst.settings.classSub);
205
- }
206
- child.appendTo(li);
207
- }
208
- li.appendTo(sbOptions);
209
- });
210
- }
211
-
212
- if (!s) {
213
- sbSelector.text(opts.first().text());
214
- }
215
-
216
- $.data(target, PROP_NAME, inst);
217
-
218
- sbSelector.appendTo(sbHolder);
219
- sbOptions.appendTo(sbHolder);
220
- sbHolder.insertAfter($target);
221
- },
222
-
223
- /**
224
- * Remove the selectbox functionality completely. This will return the element back to its pre-init state.
225
- *
226
- * @param {HTMLElement} target
227
- */
228
- _detachSelectbox: function (target) {
229
- var inst = this._getInst(target);
230
- if (!inst) {
231
- return FALSE;
232
- }
233
- $("#sbHolder_" + inst.uid).remove();
234
- $.data(target, PROP_NAME, null);
235
- $(target).show();
236
- },
237
-
238
- /**
239
- * Change selected attribute of the selectbox.
240
- *
241
- * @param {HTMLElement} target
242
- * @param {String} value
243
- * @param {String} text
244
- */
245
- _changeSelectbox: function (target, value, text) {
246
- var inst = this._getInst(target),
247
- onChange = this._get(inst, 'onChange');
248
- $("#sbSelector_" + inst.uid).text(text);
249
- $(target).find("option[value='" + value + "']").attr("selected", TRUE);
250
- if (onChange) {
251
- onChange.apply((inst.input ? inst.input[0] : null), [value, inst]);
252
- } else if (inst.input) {
253
- inst.input.trigger('change');
254
- }
255
- },
256
-
257
- /**
258
- * Enable the selectbox.
259
- *
260
- * @param {HTMLElement} target
261
- */
262
- _enableSelectbox: function (target) {
263
- var inst = this._getInst(target);
264
- if (!inst || !inst.isDisabled) {
265
- return FALSE;
266
- }
267
- $("#sbHolder_" + inst.uid).removeClass(inst.settings.classHolderDisabled);
268
- inst.isDisabled = FALSE;
269
- $.data(target, PROP_NAME, inst);
270
- },
271
-
272
- /**
273
- * Disable the selectbox.
274
- *
275
- * @param {HTMLElement} target
276
- */
277
- _disableSelectbox: function (target) {
278
- var inst = this._getInst(target);
279
- if (!inst || inst.isDisabled) {
280
- return FALSE;
281
- }
282
- $("#sbHolder_" + inst.uid).addClass(inst.settings.classHolderDisabled);
283
- inst.isDisabled = TRUE;
284
- $.data(target, PROP_NAME, inst);
285
- },
286
-
287
- /**
288
- * Get or set any selectbox option. If no value is specified, will act as a getter.
289
- *
290
- * @param {HTMLElement} target
291
- * @param {String} name
292
- * @param {Object} value
293
- */
294
- _optionSelectbox: function (target, name, value) {
295
- var inst = this._getInst(target);
296
- if (!inst) {
297
- return FALSE;
298
- }
299
- //TODO check name
300
- inst[name] = value;
301
- $.data(target, PROP_NAME, inst);
302
- },
303
-
304
- /**
305
- * Call up attached selectbox
306
- *
307
- * @param {HTMLElement} target
308
- */
309
- _openSelectbox: function (target) {
310
- var inst = this._getInst(target);
311
- //if (!inst || this._state[inst.uid] || inst.isDisabled) {
312
- if (!inst || inst.isOpen || inst.isDisabled) {
313
- return;
314
- }
315
- var el = $("#sbOptions_" + inst.uid),
316
- viewportHeight = parseInt($(window).height(), 10),
317
- offset = $("#sbHolder_" + inst.uid).offset(),
318
- scrollTop = $(window).scrollTop(),
319
- height = el.prev().height(),
320
- diff = viewportHeight - (offset.top - scrollTop) - height / 2,
321
- onOpen = this._get(inst, 'onOpen');
322
- el.css({
323
- "top": height + "px",
324
- "maxHeight": (diff - height) + "px"
325
- });
326
- inst.settings.effect === "fade" ? el.fadeIn(inst.settings.speed) : el.slideDown(inst.settings.speed);
327
- $("#sbToggle_" + inst.uid).addClass(inst.settings.classToggleOpen);
328
- this._state[inst.uid] = TRUE;
329
- inst.isOpen = TRUE;
330
- if (onOpen) {
331
- onOpen.apply((inst.input ? inst.input[0] : null), [inst]);
332
- }
333
- $.data(target, PROP_NAME, inst);
334
- },
335
-
336
- /**
337
- * Close opened selectbox
338
- *
339
- * @param {HTMLElement} target
340
- */
341
- _closeSelectbox: function (target) {
342
- var inst = this._getInst(target);
343
- //if (!inst || !this._state[inst.uid]) {
344
- if (!inst || !inst.isOpen) {
345
- return;
346
- }
347
- var onClose = this._get(inst, 'onClose');
348
- inst.settings.effect === "fade" ? $("#sbOptions_" + inst.uid).fadeOut(inst.settings.speed) : $("#sbOptions_" + inst.uid).slideUp(inst.settings.speed);
349
- $("#sbToggle_" + inst.uid).removeClass(inst.settings.classToggleOpen);
350
- this._state[inst.uid] = FALSE;
351
- inst.isOpen = FALSE;
352
- if (onClose) {
353
- onClose.apply((inst.input ? inst.input[0] : null), [inst]);
354
- }
355
- $.data(target, PROP_NAME, inst);
356
- },
357
-
358
- /**
359
- * Create a new instance object
360
- *
361
- * @param {HTMLElement} target
362
- * @return {Object}
363
- */
364
- _newInst: function(target) {
365
- var id = target[0].id.replace(/([^A-Za-z0-9_-])/g, '\\\\$1');
366
- return {
367
- id: id,
368
- input: target,
369
- uid: Math.floor(Math.random() * 99999999),
370
- isOpen: FALSE,
371
- isDisabled: FALSE,
372
- settings: {}
373
- };
374
- },
375
-
376
- /**
377
- * Retrieve the instance data for the target control.
378
- *
379
- * @param {HTMLElement} target
380
- * @return {Object} - the associated instance data
381
- * @throws error if a jQuery problem getting data
382
- */
383
- _getInst: function(target) {
384
- try {
385
- return $.data(target, PROP_NAME);
386
- }
387
- catch (err) {
388
- throw 'Missing instance data for this selectbox';
389
- }
390
- },
391
-
392
- /**
393
- * Get a setting value, defaulting if necessary
394
- *
395
- * @param {Object} inst
396
- * @param {String} name
397
- * @return {Mixed}
398
- */
399
- _get: function(inst, name) {
400
- return inst.settings[name] !== undefined ? inst.settings[name] : this._defaults[name];
401
- }
402
- });
403
-
404
- /**
405
- * Invoke the selectbox functionality.
406
- *
407
- * @param {Object|String} options
408
- * @return {Object}
409
- */
410
- $.fn.selectbox = function (options) {
411
-
412
- var otherArgs = Array.prototype.slice.call(arguments, 1);
413
- if (typeof options == 'string' && options == 'isDisabled') {
414
- return $.selectbox['_' + options + 'Selectbox'].apply($.selectbox, [this[0]].concat(otherArgs));
415
- }
10
+ var PROP_NAME = 'selectbox',
11
+ FALSE = false,
12
+ TRUE = true;
13
+ /**
14
+ * Selectbox manager.
15
+ * Use the singleton instance of this class, $.selectbox, to interact with the select box.
16
+ * Settings for (groups of) select boxes are maintained in an instance object,
17
+ * allowing multiple different settings on the same page
18
+ */
19
+ function Selectbox() {
20
+ this._state = [];
21
+ this._defaults = { // Global defaults for all the select box instances
22
+ classHolder: "sbHolder",
23
+ classHolderDisabled: "sbHolderDisabled",
24
+ classSelector: "sbSelector",
25
+ classOptions: "sbOptions",
26
+ classGroup: "sbGroup",
27
+ classSub: "sbSub",
28
+ classDisabled: "sbDisabled",
29
+ classToggleOpen: "sbToggleOpen",
30
+ classToggle: "sbToggle",
31
+ classFocus: "sbFocus",
32
+ speed: 200,
33
+ effect: "slide", // "slide" or "fade"
34
+ onChange: null, //Define a callback function when the selectbox is changed
35
+ onOpen: null, //Define a callback function when the selectbox is open
36
+ onClose: null //Define a callback function when the selectbox is closed
37
+ };
38
+ }
39
+
40
+ $.extend(Selectbox.prototype, {
41
+ /**
42
+ * Is the first field in a jQuery collection open as a selectbox
43
+ *
44
+ * @param {Object} target
45
+ * @return {Boolean}
46
+ */
47
+ _isOpenSelectbox: function (target) {
48
+ if (!target) {
49
+ return FALSE;
50
+ }
51
+ var inst = this._getInst(target);
52
+ return inst.isOpen;
53
+ },
54
+ /**
55
+ * Is the first field in a jQuery collection disabled as a selectbox
56
+ *
57
+ * @param {HTMLElement} target
58
+ * @return {Boolean}
59
+ */
60
+ _isDisabledSelectbox: function (target) {
61
+ if (!target) {
62
+ return FALSE;
63
+ }
64
+ var inst = this._getInst(target);
65
+ return inst.isDisabled;
66
+ },
67
+ /**
68
+ * Attach the select box to a jQuery selection.
69
+ *
70
+ * @param {HTMLElement} target
71
+ * @param {Object} settings
72
+ */
73
+ _attachSelectbox: function (target, settings) {
74
+ if (this._getInst(target)) {
75
+ return FALSE;
76
+ }
77
+ var $target = $(target),
78
+ self = this,
79
+ inst = self._newInst($target),
80
+ sbHolder, sbSelector, sbToggle, sbOptions,
81
+ s = FALSE, optGroup = $target.find("optgroup"), opts = $target.find("option"), olen = opts.length;
82
+
83
+ $target.attr("sb", inst.uid);
84
+
85
+ $.extend(inst.settings, self._defaults, settings);
86
+ self._state[inst.uid] = FALSE;
87
+ $target.hide();
88
+
89
+ function closeOthers() {
90
+ var key, sel,
91
+ uid = this.attr("id").split("_")[1];
92
+ for (key in self._state) {
93
+ if (key !== uid) {
94
+ if (self._state.hasOwnProperty(key)) {
95
+ sel = $("select[sb='" + key + "']")[0];
96
+ if (sel) {
97
+ self._closeSelectbox(sel);
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ sbHolder = $("<div>", {
105
+ "id": "sbHolder_" + inst.uid,
106
+ "class": inst.settings.classHolder,
107
+ "tabindex": $target.attr("tabindex")
108
+ });
109
+
110
+ sbSelector = $("<a>", {
111
+ "id": "sbSelector_" + inst.uid,
112
+ "href": "#",
113
+ "class": inst.settings.classSelector,
114
+ "click": function (e) {
115
+ e.preventDefault();
116
+ closeOthers.apply($(this), []);
117
+ var uid = $(this).attr("id").split("_")[1];
118
+ if (self._state[uid]) {
119
+ self._closeSelectbox(target);
120
+ } else {
121
+ self._openSelectbox(target);
122
+ }
123
+ }
124
+ });
125
+
126
+ sbToggle = $("<a>", {
127
+ "id": "sbToggle_" + inst.uid,
128
+ "href": "#",
129
+ "class": inst.settings.classToggle,
130
+ "click": function (e) {
131
+ e.preventDefault();
132
+ closeOthers.apply($(this), []);
133
+ var uid = $(this).attr("id").split("_")[1];
134
+ if (self._state[uid]) {
135
+ self._closeSelectbox(target);
136
+ } else {
137
+ self._openSelectbox(target);
138
+ }
139
+ }
140
+ });
141
+ sbToggle.appendTo(sbHolder);
416
142
 
417
- if (options == 'option' && arguments.length == 2 && typeof arguments[1] == 'string') {
418
- return $.selectbox['_' + options + 'Selectbox'].apply($.selectbox, [this[0]].concat(otherArgs));
419
- }
143
+ sbOptions = $("<ul>", {
144
+ "id": "sbOptions_" + inst.uid,
145
+ "class": inst.settings.classOptions,
146
+ "css": {
147
+ "display": "none"
148
+ }
149
+ });
150
+
151
+ $target.children().each(function(i) {
152
+ var that = $(this), li, config = {};
153
+ if (that.is("option")) {
154
+ getOptions(that);
155
+ } else if (that.is("optgroup")) {
156
+ li = $("<li>");
157
+ $("<span>", {
158
+ "text": that.attr("label")
159
+ }).addClass(inst.settings.classGroup).appendTo(li);
160
+ li.appendTo(sbOptions);
161
+ if (that.is(":disabled")) {
162
+ config.disabled = true;
163
+ }
164
+ config.sub = true;
165
+ getOptions(that.find("option"), config);
166
+ }
167
+ });
168
+
169
+ function getOptions () {
170
+ var sub = arguments[1] && arguments[1].sub ? true : false,
171
+ disabled = arguments[1] && arguments[1].disabled ? true : false;
172
+ arguments[0].each(function (i) {
173
+ var that = $(this),
174
+ li = $("<li>"),
175
+ child;
176
+ if (that.is(":selected")) {
177
+ sbSelector.text(that.text());
178
+ s = TRUE;
179
+ }
180
+ if (i === olen - 1) {
181
+ li.addClass("last");
182
+ }
183
+ if (!that.is(":disabled") && !disabled) {
184
+ child = $("<a>", {
185
+ "href": "#" + that.val(),
186
+ "rel": that.val()
187
+ }).text(that.text()).bind("click.sb", function (e) {
188
+ if (e && e.preventDefault) {
189
+ e.preventDefault();
190
+ }
191
+ var t = sbToggle,
192
+ $this = $(this),
193
+ uid = t.attr("id").split("_")[1];
194
+ self._changeSelectbox(target, $this.attr("rel"), $this.text());
195
+ self._closeSelectbox(target);
196
+ }).bind("mouseover.sb", function () {
197
+ var $this = $(this);
198
+ $this.parent().siblings().find("a").removeClass(inst.settings.classFocus);
199
+ $this.addClass(inst.settings.classFocus);
200
+ }).bind("mouseout.sb", function () {
201
+ $(this).removeClass(inst.settings.classFocus);
202
+ });
203
+ if (sub) {
204
+ child.addClass(inst.settings.classSub);
205
+ }
206
+ if (that.is(":selected")) {
207
+ child.addClass(inst.settings.classFocus);
208
+ }
209
+ child.appendTo(li);
210
+ } else {
211
+ child = $("<span>", {
212
+ "text": that.text()
213
+ }).addClass(inst.settings.classDisabled);
214
+ if (sub) {
215
+ child.addClass(inst.settings.classSub);
216
+ }
217
+ child.appendTo(li);
218
+ }
219
+ li.appendTo(sbOptions);
220
+ });
221
+ }
222
+
223
+ if (!s) {
224
+ sbSelector.text(opts.first().text());
225
+ }
420
226
 
421
- return this.each(function() {
422
- typeof options == 'string' ?
423
- $.selectbox['_' + options + 'Selectbox'].apply($.selectbox, [this].concat(otherArgs)) :
424
- $.selectbox._attachSelectbox(this, options);
425
- });
426
- };
227
+ $.data(target, PROP_NAME, inst);
228
+
229
+ sbHolder.data("uid", inst.uid).bind("keydown.sb", function (e) {
230
+ var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0,
231
+ $this = $(this),
232
+ uid = $this.data("uid"),
233
+ inst = $this.siblings("select[sb='"+uid+"']").data(PROP_NAME),
234
+ trgt = $this.siblings(["select[sb='", uid, "']"].join("")).get(0),
235
+ $f = $this.find("ul").find("a." + inst.settings.classFocus);
236
+ switch (key) {
237
+ case 37: //Arrow Left
238
+ case 38: //Arrow Up
239
+ if ($f.length > 0) {
240
+ var $next;
241
+ $("a", $this).removeClass(inst.settings.classFocus);
242
+ $next = $f.parent().prevAll("li:has(a)").eq(0).find("a");
243
+ if ($next.length > 0) {
244
+ $next.addClass(inst.settings.classFocus).focus();
245
+ $("#sbSelector_" + uid).text($next.text());
246
+ }
247
+ }
248
+ break;
249
+ case 39: //Arrow Right
250
+ case 40: //Arrow Down
251
+ var $next;
252
+ $("a", $this).removeClass(inst.settings.classFocus);
253
+ if ($f.length > 0) {
254
+ $next = $f.parent().nextAll("li:has(a)").eq(0).find("a");
255
+ } else {
256
+ $next = $this.find("ul").find("a").eq(0);
257
+ }
258
+ if ($next.length > 0) {
259
+ $next.addClass(inst.settings.classFocus).focus();
260
+ $("#sbSelector_" + uid).text($next.text());
261
+ }
262
+ break;
263
+ case 13: //Enter
264
+ if ($f.length > 0) {
265
+ self._changeSelectbox(trgt, $f.attr("rel"), $f.text());
266
+ }
267
+ self._closeSelectbox(trgt);
268
+ break;
269
+ case 9: //Tab
270
+ if (trgt) {
271
+ var inst = self._getInst(trgt);
272
+ if (inst/* && inst.isOpen*/) {
273
+ if ($f.length > 0) {
274
+ self._changeSelectbox(trgt, $f.attr("rel"), $f.text());
275
+ }
276
+ self._closeSelectbox(trgt);
277
+ }
278
+ }
279
+ var i = parseInt($this.attr("tabindex"), 10);
280
+ if (!e.shiftKey) {
281
+ i++;
282
+ } else {
283
+ i--;
284
+ }
285
+ $("*[tabindex='" + i + "']").focus();
286
+ break;
287
+ case 27: //Escape
288
+ self._closeSelectbox(trgt);
289
+ break;
290
+ }
291
+ e.stopPropagation();
292
+ return false;
293
+ }).delegate("a", "mouseover", function (e) {
294
+ $(this).addClass(inst.settings.classFocus);
295
+ }).delegate("a", "mouseout", function (e) {
296
+ $(this).removeClass(inst.settings.classFocus);
297
+ });
298
+
299
+ sbSelector.appendTo(sbHolder);
300
+ sbOptions.appendTo(sbHolder);
301
+ sbHolder.insertAfter($target);
302
+
303
+ $("html").live('mousedown', function(e) {
304
+ e.stopPropagation();
305
+ $("select").selectbox('close');
306
+ });
307
+ $([".", inst.settings.classHolder, ", .", inst.settings.classSelector].join("")).mousedown(function(e) {
308
+ e.stopPropagation();
309
+ });
310
+ },
311
+ /**
312
+ * Remove the selectbox functionality completely. This will return the element back to its pre-init state.
313
+ *
314
+ * @param {HTMLElement} target
315
+ */
316
+ _detachSelectbox: function (target) {
317
+ var inst = this._getInst(target);
318
+ if (!inst) {
319
+ return FALSE;
320
+ }
321
+ $("#sbHolder_" + inst.uid).remove();
322
+ $.data(target, PROP_NAME, null);
323
+ $(target).show();
324
+ },
325
+ /**
326
+ * Change selected attribute of the selectbox.
327
+ *
328
+ * @param {HTMLElement} target
329
+ * @param {String} value
330
+ * @param {String} text
331
+ */
332
+ _changeSelectbox: function (target, value, text) {
333
+ var onChange,
334
+ inst = this._getInst(target);
335
+ if (inst) {
336
+ onChange = this._get(inst, 'onChange');
337
+ $("#sbSelector_" + inst.uid).text(text);
338
+ }
339
+ value = value.replace(/\'/g, "\\'");
340
+ $(target).find("option[value='" + value + "']").attr("selected", TRUE);
341
+ if (inst && onChange) {
342
+ onChange.apply((inst.input ? inst.input[0] : null), [value, inst]);
343
+ } else if (inst && inst.input) {
344
+ inst.input.trigger('change');
345
+ }
346
+ },
347
+ /**
348
+ * Enable the selectbox.
349
+ *
350
+ * @param {HTMLElement} target
351
+ */
352
+ _enableSelectbox: function (target) {
353
+ var inst = this._getInst(target);
354
+ if (!inst || !inst.isDisabled) {
355
+ return FALSE;
356
+ }
357
+ $("#sbHolder_" + inst.uid).removeClass(inst.settings.classHolderDisabled);
358
+ inst.isDisabled = FALSE;
359
+ $.data(target, PROP_NAME, inst);
360
+ },
361
+ /**
362
+ * Disable the selectbox.
363
+ *
364
+ * @param {HTMLElement} target
365
+ */
366
+ _disableSelectbox: function (target) {
367
+ var inst = this._getInst(target);
368
+ if (!inst || inst.isDisabled) {
369
+ return FALSE;
370
+ }
371
+ $("#sbHolder_" + inst.uid).addClass(inst.settings.classHolderDisabled);
372
+ inst.isDisabled = TRUE;
373
+ $.data(target, PROP_NAME, inst);
374
+ },
375
+ /**
376
+ * Get or set any selectbox option. If no value is specified, will act as a getter.
377
+ *
378
+ * @param {HTMLElement} target
379
+ * @param {String} name
380
+ * @param {Object} value
381
+ */
382
+ _optionSelectbox: function (target, name, value) {
383
+ var inst = this._getInst(target);
384
+ if (!inst) {
385
+ return FALSE;
386
+ }
387
+ //TODO check name
388
+ inst[name] = value;
389
+ $.data(target, PROP_NAME, inst);
390
+ },
391
+ /**
392
+ * Call up attached selectbox
393
+ *
394
+ * @param {HTMLElement} target
395
+ */
396
+ _openSelectbox: function (target) {
397
+ var inst = this._getInst(target);
398
+ //if (!inst || this._state[inst.uid] || inst.isDisabled) {
399
+ if (!inst || inst.isOpen || inst.isDisabled) {
400
+ return;
401
+ }
402
+ var el = $("#sbOptions_" + inst.uid),
403
+ viewportHeight = parseInt($(window).height(), 10),
404
+ offset = $("#sbHolder_" + inst.uid).offset(),
405
+ scrollTop = $(window).scrollTop(),
406
+ height = el.prev().height(),
407
+ diff = viewportHeight - (offset.top - scrollTop) - height / 2,
408
+ onOpen = this._get(inst, 'onOpen');
409
+ el.css({
410
+ "top": height + "px",
411
+ "maxHeight": (diff - height) + "px"
412
+ });
413
+ inst.settings.effect === "fade" ? el.fadeIn(inst.settings.speed) : el.slideDown(inst.settings.speed);
414
+ $("#sbToggle_" + inst.uid).addClass(inst.settings.classToggleOpen);
415
+ this._state[inst.uid] = TRUE;
416
+ inst.isOpen = TRUE;
417
+ if (onOpen) {
418
+ onOpen.apply((inst.input ? inst.input[0] : null), [inst]);
419
+ }
420
+ $.data(target, PROP_NAME, inst);
421
+ },
422
+ /**
423
+ * Close opened selectbox
424
+ *
425
+ * @param {HTMLElement} target
426
+ */
427
+ _closeSelectbox: function (target) {
428
+ var inst = this._getInst(target);
429
+ //if (!inst || !this._state[inst.uid]) {
430
+ if (!inst || !inst.isOpen) {
431
+ return;
432
+ }
433
+ var onClose = this._get(inst, 'onClose');
434
+ inst.settings.effect === "fade" ? $("#sbOptions_" + inst.uid).fadeOut(inst.settings.speed) : $("#sbOptions_" + inst.uid).slideUp(inst.settings.speed);
435
+ $("#sbToggle_" + inst.uid).removeClass(inst.settings.classToggleOpen);
436
+ this._state[inst.uid] = FALSE;
437
+ inst.isOpen = FALSE;
438
+ if (onClose) {
439
+ onClose.apply((inst.input ? inst.input[0] : null), [inst]);
440
+ }
441
+ $.data(target, PROP_NAME, inst);
442
+ },
443
+ /**
444
+ * Create a new instance object
445
+ *
446
+ * @param {HTMLElement} target
447
+ * @return {Object}
448
+ */
449
+ _newInst: function(target) {
450
+ var id = target[0].id.replace(/([^A-Za-z0-9_-])/g, '\\\\$1');
451
+ return {
452
+ id: id,
453
+ input: target,
454
+ uid: Math.floor(Math.random() * 99999999),
455
+ isOpen: FALSE,
456
+ isDisabled: FALSE,
457
+ settings: {}
458
+ };
459
+ },
460
+ /**
461
+ * Retrieve the instance data for the target control.
462
+ *
463
+ * @param {HTMLElement} target
464
+ * @return {Object} - the associated instance data
465
+ * @throws error if a jQuery problem getting data
466
+ */
467
+ _getInst: function(target) {
468
+ try {
469
+ return $.data(target, PROP_NAME);
470
+ }
471
+ catch (err) {
472
+ throw 'Missing instance data for this selectbox';
473
+ }
474
+ },
475
+ /**
476
+ * Get a setting value, defaulting if necessary
477
+ *
478
+ * @param {Object} inst
479
+ * @param {String} name
480
+ * @return {Mixed}
481
+ */
482
+ _get: function(inst, name) {
483
+ return inst.settings[name] !== undefined ? inst.settings[name] : this._defaults[name];
484
+ }
485
+ });
427
486
 
428
- $.selectbox = new Selectbox(); // singleton instance
429
- $.selectbox.version = "0.1.3";
430
- })(jQuery);
487
+ /**
488
+ * Invoke the selectbox functionality.
489
+ *
490
+ * @param {Object|String} options
491
+ * @return {Object}
492
+ */
493
+ $.fn.selectbox = function (options) {
494
+
495
+ var otherArgs = Array.prototype.slice.call(arguments, 1);
496
+ if (typeof options == 'string' && options == 'isDisabled') {
497
+ return $.selectbox['_' + options + 'Selectbox'].apply($.selectbox, [this[0]].concat(otherArgs));
498
+ }
499
+
500
+ if (options == 'option' && arguments.length == 2 && typeof arguments[1] == 'string') {
501
+ return $.selectbox['_' + options + 'Selectbox'].apply($.selectbox, [this[0]].concat(otherArgs));
502
+ }
503
+
504
+ return this.each(function() {
505
+ typeof options == 'string' ?
506
+ $.selectbox['_' + options + 'Selectbox'].apply($.selectbox, [this].concat(otherArgs)) :
507
+ $.selectbox._attachSelectbox(this, options);
508
+ });
509
+ };
510
+
511
+ $.selectbox = new Selectbox(); // singleton instance
512
+ $.selectbox.version = "0.2";
513
+ })(jQuery);
@@ -1,105 +1,110 @@
1
1
  @CHARSET "UTF-8";
2
2
  .sbHolder{
3
- background-color: #2d2d2d;
4
- border: solid 1px #515151;
5
- font-family: Arial, sans-serif;
6
- font-size: 12px;
7
- font-weight: normal;
8
- height: 30px;
9
- position: relative;
10
- width: 200px;
3
+ background-color: #2d2d2d;
4
+ border: solid 1px #515151;
5
+ font-family: Arial, sans-serif;
6
+ font-size: 12px;
7
+ font-weight: normal;
8
+ height: 30px;
9
+ position: relative;
10
+ width: 200px;
11
+ }
12
+ .sbHolder:focus .sbSelector{
13
+
11
14
  }
12
15
  .sbSelector{
13
- display: block;
14
- height: 30px;
15
- left: 0;
16
- line-height: 30px;
17
- outline: none;
18
- overflow: hidden;
19
- position: absolute;
20
- text-indent: 10px;
21
- top: 0;
22
- width: 170px;
16
+ display: block;
17
+ height: 30px;
18
+ left: 0;
19
+ line-height: 30px;
20
+ outline: none;
21
+ overflow: hidden;
22
+ position: absolute;
23
+ text-indent: 10px;
24
+ top: 0;
25
+ width: 170px;
23
26
  }
24
27
  .sbSelector:link, .sbSelector:visited, .sbSelector:hover{
25
- color: #EBB52D;
26
- outline: none;
27
- text-decoration: none;
28
+ color: #EBB52D;
29
+ outline: none;
30
+ text-decoration: none;
28
31
  }
29
32
  .sbToggle{
30
- background: url(<%= asset_path("select-icons.png") %>) 0 -116px no-repeat;
31
- display: block;
32
- height: 30px;
33
- outline: none;
34
- position: absolute;
35
- right: 0;
36
- top: 0;
37
- width: 30px;
33
+ background: url(<%= asset_path('jquery-selectbox/select-icons.png') %>) 0 -116px no-repeat;
34
+ display: block;
35
+ height: 30px;
36
+ outline: none;
37
+ position: absolute;
38
+ right: 0;
39
+ top: 0;
40
+ width: 30px;
38
41
  }
39
42
  .sbToggle:hover{
40
- background: url(<%= asset_path("select-icons.png") %>) 0 -167px no-repeat;
43
+ background: url(<%= asset_path('jquery-selectbox/select-icons.png') %>) 0 -167px no-repeat;
41
44
  }
42
45
  .sbToggleOpen{
43
- background: url(<%= asset_path("select-icons.png") %>) 0 -16px no-repeat;
46
+ background: url(<%= asset_path('jquery-selectbox/select-icons.png') %>) 0 -16px no-repeat;
44
47
  }
45
48
  .sbToggleOpen:hover{
46
- background: url(<%= asset_path("select-icons.png") %>) 0 -66px no-repeat;
49
+ background: url(<%= asset_path('jquery-selectbox/select-icons.png') %>) 0 -66px no-repeat;
47
50
  }
48
51
  .sbHolderDisabled{
49
- background-color: #3C3C3C;
50
- border: solid 1px #515151;
52
+ background-color: #3C3C3C;
53
+ border: solid 1px #515151;
51
54
  }
52
55
  .sbHolderDisabled .sbHolder{
53
-
56
+
54
57
  }
55
58
  .sbHolderDisabled .sbToggle{
56
-
59
+
57
60
  }
58
61
  .sbOptions{
59
- background-color: #212121;
60
- border: solid 1px #515151;
61
- list-style: none;
62
- left: -1px;
63
- margin: 0;
64
- padding: 0;
65
- position: absolute;
66
- top: 30px;
67
- width: 200px;
68
- z-index: 1;
69
- overflow-y: auto;
62
+ background-color: #212121;
63
+ border: solid 1px #515151;
64
+ list-style: none;
65
+ left: -1px;
66
+ margin: 0;
67
+ padding: 0;
68
+ position: absolute;
69
+ top: 30px;
70
+ width: 200px;
71
+ z-index: 1;
72
+ overflow-y: auto;
70
73
  }
71
74
  .sbOptions li{
72
- padding: 0 7px;
75
+ padding: 0 7px;
73
76
  }
74
77
  .sbOptions a{
75
- border-bottom: dotted 1px #515151;
76
- display: block;
77
- outline: none;
78
- padding: 7px 0 7px 3px;
78
+ border-bottom: dotted 1px #515151;
79
+ display: block;
80
+ outline: none;
81
+ padding: 7px 0 7px 3px;
79
82
  }
80
83
  .sbOptions a:link, .sbOptions a:visited{
81
- color: #ddd;
82
- text-decoration: none;
84
+ color: #ddd;
85
+ text-decoration: none;
83
86
  }
84
- .sbOptions a:hover{
85
- color: #EBB52D;
87
+ .sbOptions a:hover,
88
+ .sbOptions a:focus,
89
+ .sbOptions a.sbFocus{
90
+ color: #EBB52D;
86
91
  }
87
92
  .sbOptions li.last a{
88
- border-bottom: none;
93
+ border-bottom: none;
89
94
  }
90
95
  .sbOptions .sbDisabled{
91
- border-bottom: dotted 1px #515151;
92
- color: #999;
93
- display: block;
94
- padding: 7px 0 7px 3px;
96
+ border-bottom: dotted 1px #515151;
97
+ color: #999;
98
+ display: block;
99
+ padding: 7px 0 7px 3px;
95
100
  }
96
101
  .sbOptions .sbGroup{
97
- border-bottom: dotted 1px #515151;
98
- color: #EBB52D;
99
- display: block;
100
- font-weight: bold;
101
- padding: 7px 0 7px 3px;
102
+ border-bottom: dotted 1px #515151;
103
+ color: #EBB52D;
104
+ display: block;
105
+ font-weight: bold;
106
+ padding: 7px 0 7px 3px;
102
107
  }
103
108
  .sbOptions .sbSub{
104
- padding-left: 17px;
105
- }
109
+ padding-left: 17px;
110
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-selectbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-25 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-01-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: vendorer
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: Custom select box replacement inspired by jQuery UI source
15
31
  email:
16
32
  - subosito@gmail.com
@@ -21,12 +37,13 @@ files:
21
37
  - .gitignore
22
38
  - Gemfile
23
39
  - LICENSE
24
- - README.md
40
+ - README.markdown
25
41
  - Rakefile
42
+ - Vendorfile
26
43
  - jquery-selectbox.gemspec
27
44
  - lib/jquery-selectbox.rb
28
45
  - lib/jquery-selectbox/version.rb
29
- - vendor/assets/images/select-icons.png
46
+ - vendor/assets/images/jquery-selectbox/select-icons.png
30
47
  - vendor/assets/javascripts/jquery.selectbox.js
31
48
  - vendor/assets/stylesheets/jquery.selectbox.css.erb
32
49
  homepage: https://github.com/subosito/jquery-selectbox
@@ -49,9 +66,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
66
  version: '0'
50
67
  requirements: []
51
68
  rubyforge_project:
52
- rubygems_version: 1.8.11
69
+ rubygems_version: 1.8.23
53
70
  signing_key:
54
71
  specification_version: 3
55
72
  summary: jQuery Selectbox plugin. Custom select box replacement inspired by jQuery
56
73
  UI source. Require jQuery 1.4.x or higher
57
74
  test_files: []
75
+ has_rdoc: