-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.selectbox.coffee
524 lines (468 loc) · 16.6 KB
/
jquery.selectbox.coffee
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!
# * jQuery Selectbox plugin 0.3
# *
# * Copyright 2011-2012, Dimitar Ivanov (http://www.bulgaria-web-developers.com/projects/javascript/selectbox/)
# * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
# *
# * A fork by Danil Valov for some simple modifications. Based off v.0.2
# *
# * Date: Tue Jul 09 18:19:36 2014 +0700
#
(($) ->
###*
Selectbox manager.
Use the singleton instance of this class, $.selectbox, to interact with the select box.
Settings for (groups of) select boxes are maintained in an instance object,
allowing multiple different settings on the same page
###
Selectbox = ->
@_state = []
@_defaults = # Global defaults for all the select box instances
classHolder: "selectbox__holder"
classHolderDisabled: "selectbox__holder--disabled"
classSelector: "selectbox__selector"
classOptions: "selectbox__options"
classOptionsList: "selectbox__options-list"
classOptionsItem: "selectbox__options-item"
classOptionSelected: "selectbox__options-item--selected"
classGroup: "selectbox__group"
classSub: "selectbox__sub"
classDisabled: "selectbox--disabled"
classSelectOpen: "selectbox--open"
classToggle: "selectbox__toggle"
classFocus: "selectbox--focus"
classDropUp: "selectbox__holder--drop-up"
minWidth: 50
maxWidth: 500
maxHeight: 300
padding: 30
speed: 200
effect: "slide" # "slide" or "fade"
onChange: null #Define a callback function when the selectbox is changed
onOpen: null #Define a callback function when the selectbox is open
onClose: null #Define a callback function when the selectbox is closed
return
PROP_NAME = "selectbox"
$.extend Selectbox::,
###*
Is the first field in a jQuery collection open as a selectbox
@param {Object} target
@return {Boolean}
###
_isOpenSelectbox: (target = false) ->
inst = @_getInst(target)
inst.isOpen
###*
Is the first field in a jQuery collection disabled as a selectbox
@param {HTMLElement} target
@return {Boolean}
###
_isDisabledSelectbox: (target = false) ->
inst = @_getInst(target)
inst.isDisabled
###*
Attach the select box to a jQuery selection.
@param {HTMLElement} target
@param {Object} settings
###
_attachSelectbox: (target, settings) ->
closeOthers = ->
key = undefined
sel = undefined
uid = @attr("id").split("_")[1]
for key of self._state
if key isnt uid
if self._state.hasOwnProperty(key)
sel = $("select[sb='" + key + "']")[0]
self._closeSelectbox sel if sel
return
getOptions = ->
sub = (if arguments[1] and arguments[1].sub then true else false)
disabled = (if arguments[1] and arguments[1].disabled then true else false)
arguments[0].each (i) ->
that = $(this)
li = $("<li>",
class: inst.settings.classOptionsItem
)
child = undefined
if that.is(":selected")
sbSelector.text that.text()
li.addClass inst.settings.classOptionSelected
s = true
li.addClass "last" if i is olen - 1
if not that.is(":disabled") and not disabled
child = $("<a>",
href: "#" + that.val()
rel: that.val()
title: that.text()
).text(that.text()).bind("click.sb", (e) ->
e.preventDefault() if e and e.preventDefault
t = sbToggle
$this = $(this)
uid = t.attr("id").split("_")[1]
self._changeSelectbox target, $this.attr("rel"), $this.text(), $this.parent().index()
self._closeSelectbox target
return
).bind("mouseover.sb", ->
$this = $(this)
$this.parent().siblings().find("a").removeClass inst.settings.classFocus
$this.addClass inst.settings.classFocus
return
).bind("mouseout.sb", ->
$(this).removeClass inst.settings.classFocus
return
)
child.addClass inst.settings.classSub if sub
child.addClass inst.settings.classFocus if that.is(":selected")
child.appendTo li
else
child = $("<span>",
text: that.text()
).addClass(inst.settings.classDisabled)
child.addClass inst.settings.classSub if sub
child.appendTo li
li.appendTo sbOptions
return
return
return false if @_getInst(target)
$target = $(target)
self = this
inst = self._newInst($target)
sbHolder = undefined
sbSelector = undefined
sbToggle = undefined
sbOptionsContainer = undefined
sbOptions = undefined
s = false
optGroup = $target.find("optgroup")
opts = $target.find("option")
olen = opts.length
$target.attr "sb", inst.uid
$.extend inst.settings, self._defaults, settings, $target.data("options")
self._state[inst.uid] = false
$target.hide()
sbHolderWidth = $target.outerWidth()
sbHolderWidth = (if inst.settings.maxWidth < sbHolderWidth then inst.settings.maxWidth else sbHolderWidth)
sbHolderWidth = (if inst.settings.minWidth > sbHolderWidth then inst.settings.minWidth else sbHolderWidth)
sbHolder = $("<div>",
id: "sbHolder_" + inst.uid
class: inst.settings.classHolder
css:
width: sbHolderWidth
tabindex: $target.attr("tabindex")
)
sbSelector = $("<a>",
id: "sbSelector_" + inst.uid
href: "#"
class: inst.settings.classSelector
click: (e) ->
e.preventDefault()
if $target.children().length
closeOthers.apply $(this), []
uid = $(this).attr("id").split("_")[1]
if self._state[uid]
self._closeSelectbox target
else
self._openSelectbox target
return
)
sbToggle = $("<a>",
id: "sbToggle_" + inst.uid
href: "#"
class: inst.settings.classToggle
click: (e) ->
e.preventDefault()
closeOthers.apply $(this), []
uid = $(this).attr("id").split("_")[1]
if self._state[uid]
self._closeSelectbox target
else
self._openSelectbox target
return
)
sbToggle.appendTo sbHolder
sbOptionsContainer = $("<div>",
id: "sbOptions_" + inst.uid
class: inst.settings.classOptions
css:
display: "none"
)
sbOptions = $("<ul>",
class: inst.settings.classOptionsList
)
$target.children().each (i) ->
that = $(this)
li = undefined
config = {}
if that.is("option")
getOptions that
else if that.is("optgroup")
li = $("<li>",
class: inst.settings.classOptionsItem
)
$("<span>",
text: that.attr("label")
).addClass(inst.settings.classGroup).appendTo li
li.appendTo sbOptions
config.disabled = true if that.is(":disabled")
config.sub = true
getOptions that.find("option"), config
return
sbSelector.text opts.first().text() unless s
$.data target, PROP_NAME, inst
#Arrow Left
#Arrow Up
#Arrow Right
#Arrow Down
#Enter
#Tab
# && inst.isOpen
#Escape
sbHolder.data("uid", inst.uid).bind("keydown.sb", (e) ->
key = (if e.charCode then e.charCode else (if e.keyCode then e.keyCode else 0))
$this = $(this)
uid = $this.data("uid")
inst = $this.siblings("select[sb='" + uid + "']").data(PROP_NAME)
trgt = $this.siblings([
"select[sb='"
uid
"']"
].join("")).get(0)
$f = $this.find("ul").find("a." + inst.settings.classFocus)
switch key
when 37, 38
if $f.length > 0
$next = undefined
$("a", $this).removeClass inst.settings.classFocus
$next = $f.parent().prevAll("li:has(a)").eq(0).find("a")
if $next.length > 0
$next.addClass(inst.settings.classFocus).focus()
$("#sbSelector_" + uid).text $next.text()
when 39, 40
$next = undefined
$("a", $this).removeClass inst.settings.classFocus
if $f.length > 0
$next = $f.parent().nextAll("li:has(a)").eq(0).find("a")
else
$next = $this.find("ul").find("a").eq(0)
if $next.length > 0
$next.addClass(inst.settings.classFocus).focus()
$("#sbSelector_" + uid).text $next.text()
when 13
self._changeSelectbox trgt, $f.attr("rel"), $f.text(), $f.parent().index() if $f.length > 0
self._closeSelectbox trgt
when 9
if trgt
inst = self._getInst(trgt)
if inst
self._changeSelectbox trgt, $f.attr("rel"), $f.text(), $f.parent().index() if $f.length > 0
self._closeSelectbox trgt
i = parseInt($this.attr("tabindex"), 10)
unless e.shiftKey
i++
else
i--
$("*[tabindex='" + i + "']").focus()
when 27
self._closeSelectbox trgt
e.stopPropagation()
false
).delegate("a", "mouseover", (e) ->
$(this).addClass inst.settings.classFocus
return
).delegate "a", "mouseout", (e) ->
$(this).removeClass inst.settings.classFocus
return
sbSelector.appendTo sbHolder
sbOptions.appendTo sbOptionsContainer
sbOptionsContainer.appendTo sbHolder
sbHolder.insertAfter $target
$("html").live "mousedown", (e) ->
e.stopPropagation()
$("select").selectbox "close"
return
$([
"."
inst.settings.classHolder
", ."
inst.settings.classSelector
].join("")).mousedown (e) ->
e.stopPropagation()
return
return
###*
Remove the selectbox functionality completely. This will return the element back to its pre-init state.
@param {HTMLElement} target
###
_detachSelectbox: (target) ->
inst = @_getInst(target)
return false unless inst
$("#sbHolder_" + inst.uid).remove()
$.data target, PROP_NAME, null
$(target).show()
return
###*
Change selected attribute of the selectbox.
@param {HTMLElement} target
@param {String} value
@param {String} text
@param {Number} index
###
_changeSelectbox: (target, value, text, index) ->
onChange = undefined
inst = @_getInst(target)
if inst
onChange = @_get(inst, "onChange")
$("#sbSelector_" + inst.uid).text text
$sbOptions = $("#sbOptions_" + inst.uid)
$sbOptions.find("." + inst.settings.classOptionSelected).removeClass inst.settings.classOptionSelected
$sbOptions.find("li").eq(index).addClass inst.settings.classOptionSelected
value = value.replace(/\'/g, "\\'")
$(target).find("option[value='" + value + "']").attr "selected", true
if inst and onChange
onChange.apply ((if inst.input then inst.input[0] else null)), [
value
inst
]
else inst.input.trigger "change" if inst and inst.input
return
###*
Enable the selectbox.
@param {HTMLElement} target
###
_enableSelectbox: (target) ->
inst = @_getInst(target)
return false if not inst or not inst.isDisabled
$("#sbHolder_" + inst.uid).removeClass inst.settings.classHolderDisabled
inst.isDisabled = false
$.data target, PROP_NAME, inst
return
###*
Disable the selectbox.
@param {HTMLElement} target
###
_disableSelectbox: (target) ->
inst = @_getInst(target)
return false if not inst or inst.isDisabled
$("#sbHolder_" + inst.uid).addClass inst.settings.classHolderDisabled
inst.isDisabled = true
$.data target, PROP_NAME, inst
return
###*
Get or set any selectbox option. If no value is specified, will act as a getter.
@param {HTMLElement} target
@param {String} name
@param {Object} value
###
_optionSelectbox: (target, name, value) ->
inst = @_getInst(target)
return false unless inst
#TODO check name
inst[name] = value
$.data target, PROP_NAME, inst
return
###*
Call up attached selectbox
@param {HTMLElement} target
###
_openSelectbox: (target) ->
inst = @_getInst(target)
#if (!inst || this._state[inst.uid] || inst.isDisabled) {
return if not inst or inst.isOpen or inst.isDisabled
el = $("#sbOptions_" + inst.uid)
holder = $("#sbHolder_" + inst.uid)
viewportHeight = parseInt($(document).height(), 10)
offset = holder.offset()
height = el.prev().outerHeight()
diff = viewportHeight - offset.top - height / 2
onOpen = @_get(inst, "onOpen")
maxHeight = undefined
if diff > viewportHeight / 2
maxHeight = (diff - height - (inst.settings.padding * 2))
maxHeight = (if maxHeight > inst.settings.maxHeight then inst.settings.maxHeight else maxHeight)
el.css
top: height + "px"
bottom: "auto"
maxHeight: maxHeight + "px"
else
holder.addClass inst.settings.classDropUp
maxHeight = (offset.top - height - (inst.settings.padding * 2))
maxHeight = (if maxHeight > inst.settings.maxHeight then inst.settings.maxHeight else maxHeight)
el.css
top: "auto"
bottom: height + "px"
maxHeight: maxHeight + "px"
(if inst.settings.effect is "fade" then el.fadeIn(inst.settings.speed) else el.slideDown(inst.settings.speed))
holder.addClass inst.settings.classSelectOpen
@_state[inst.uid] = true
inst.isOpen = true
onOpen.apply ((if inst.input then inst.input[0] else null)), [inst] if onOpen
$.data target, PROP_NAME, inst
return
###*
Close opened selectbox
@param {HTMLElement} target
###
_closeSelectbox: (target) ->
inst = @_getInst(target)
#if (!inst || !this._state[inst.uid]) {
return if not inst or not inst.isOpen
onClose = @_get(inst, "onClose")
sbOptions = $("#sbOptions_" + inst.uid)
(if inst.settings.effect is "fade" then sbOptions.fadeOut(inst.settings.speed) else sbOptions.slideUp(inst.settings.speed))
$holder = $("#sbHolder_" + inst.uid);
$holder.removeClass inst.settings.classSelectOpen
$holder.removeClass inst.settings.classDropUp
@_state[inst.uid] = false
inst.isOpen = false
onClose.apply ((if inst.input then inst.input[0] else null)), [inst] if onClose
$.data target, PROP_NAME, inst
return
###*
Create a new instance object
@param {HTMLElement} target
@return {Object}
###
_newInst: (target) ->
id = target[0].id.replace(/([^A-Za-z0-9_-])/g, "\\\\$1")
id: id
input: target
uid: Math.floor(Math.random() * 99999999)
isOpen: false
isDisabled: false
settings: {}
###*
Retrieve the instance data for the target control.
@param {HTMLElement} target
@return {Object} - the associated instance data
@throws error if a jQuery problem getting data
###
_getInst: (target) ->
try
return $.data(target, PROP_NAME)
catch err
throw "Missing instance data for this selectbox"
return
###*
Get a setting value, defaulting if necessary
@param {Object} inst
@param {String} name
###
_get: (inst, name) ->
(if inst.settings[name] isnt `undefined` then inst.settings[name] else @_defaults[name])
###*
Invoke the selectbox functionality.
@param {Object|String} options
@return {Object}
###
$.fn.selectbox = (options) ->
otherArgs = Array::slice.call(arguments, 1)
return $.selectbox["_" + options + "Selectbox"].apply($.selectbox, [this[0]].concat(otherArgs)) if typeof options is "string" and options is "isDisabled"
return $.selectbox["_" + options + "Selectbox"].apply($.selectbox, [this[0]].concat(otherArgs)) if options is "option" and arguments.length is 2 and typeof arguments[1] is "string"
@each ->
(if typeof options is "string" then $.selectbox["_" + options + "Selectbox"].apply($.selectbox, [this].concat(otherArgs)) else $.selectbox._attachSelectbox(this, options))
return
$.selectbox = new Selectbox() # singleton instance
$.selectbox.version = "0.3"
return
) jQuery