-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.form-toggle.coffee
89 lines (69 loc) · 3.02 KB
/
jquery.form-toggle.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
jQuery ($) ->
$.fn.formToggle = (options) ->
settings = $.extend
dataAttribute: "toggle"
reverse: false
# You can pass arguments to show and hide here.
# Useful for sending callbacks if you need to do anything besides showing and hiding.
checkbox: 400
radio: null
select: null
# These are called when the page is loaded.
# You probably don't want to have a visual effect here,
# but you might need the callback.
checkboxInit: null
radioInit: null
selectInit: null
# used to process the value to create a selector
parameterize: (string) ->
$.trim(string).toLowerCase().replace(/\W/g, '-')
, options
methods =
showOrHideTarget: (selected, target, args) ->
if (selected and not settings.reverse) or (settings.reverse and not selected)
target.show args
else
target.hide args
targetForController: (controller, value = null) ->
selector = controller.data(settings.dataAttribute)
selectorPrefix = controller.data("#{settings.dataAttribute}-prefix")
selectorSuffix = controller.data("#{settings.dataAttribute}-suffix")
unless selector?
value ||= controller.val()
value = settings.parameterize(value)
selector = selectorPrefix + value
selector += selectorSuffix if selectorSuffix?
$(selector)
handleCheckedState: (controller, target, args) ->
selected = $(controller).is(':checked')
methods.showOrHideTarget selected, target, args
handleSelect: (select, args) ->
select.find("option").each (index) ->
option = $(this)
target = methods.targetForController(select, option.val())
selected = option.val() == $(select).val()
methods.showOrHideTarget selected, target, args
element_selector = "[data-#{settings.dataAttribute}], [data-#{settings.dataAttribute}-prefix]"
# set initial state
$(element_selector).each (index, controller) ->
controller = $(controller)
if controller.is('select')
methods.handleSelect controller, settings.selectInit
else
target = methods.targetForController(controller)
setting = settings.checkboxInit if controller.is(':checkbox')
setting = settings.radioInit if controller.is(':radio')
methods.handleCheckedState controller, target, setting
$(document).on 'change', element_selector, (event) ->
controller = $(this)
if controller.is('select')
methods.handleSelect controller, settings.select
if controller.is(':checkbox')
target = methods.targetForController(controller)
methods.handleCheckedState controller, target, settings.checkbox
if controller.is(':radio')
group = controller.attr("name")
$(":radio[name='#{group}']").each (index, radio) ->
radio = $(radio)
target = methods.targetForController(radio)
methods.handleCheckedState radio, target, settings.radio