-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbootstrap-magic.coffee
284 lines (229 loc) · 8.79 KB
/
bootstrap-magic.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
flattenedMagic = {}
flattenedMagicValues = {}
for group in bootstrap_magic_variables
for item in group.data
flattenedMagic[item._id] = item
flattenedMagicValues[item._id] = item.value
###
# EXPORTS
###
@BootstrapMagic =
dictionary :
overrides : new ReactiveDict()
defaults : new ReactiveDict()
currentCategory : new ReactiveVar()
currentSubCategory : new ReactiveVar()
searchTerms : new ReactiveVar('')
searchFilters: new ReactiveDict()
on : (eventName, callback) ->
@[eventName] = callback
off : (eventName) ->
delete @[eventName]
setOne: (dictionaryName, key, val) ->
@dictionary[dictionaryName].set key, val
obj = {}
obj[key] = val
@change obj if @change
setMany: (dictionaryName, obj) ->
for key,val of obj
@dictionary[dictionaryName].set key, val
setOverride : (key, val) -> @setOne 'overrides', key, val
setOverrides : (obj) ->
# clear the previous overrides
for key,val of @dictionary['overrides'].keys
@dictionary['overrides'].set key, null
# override wth passed obj
@setMany 'overrides', obj
setDefault : (key, val) -> @setOne 'defaults', key, val
setDefaults : (obj) ->
# clear the previous defaults
for key,val of @dictionary['defaults'].keys
@dictionary['defaults'].set key, null
# set the botostrap defautls
@setMany 'defaults', flattenedMagicValues
# override with user defaults
@setMany 'defaults', obj
###
# UI
###
# default starting script: load hard-coded defaults
# this function can be overriden by other packages for integrations
BootstrapMagic.on 'start', -> @setDefaults {}
Template._bootstrap_magic.onCreated ->
BootstrapMagic.start() if BootstrapMagic.start
Template._bootstrap_magic.onRendered ->
BootstrapMagic.dictionary.currentCategory.set bootstrap_magic_variables[0].category
BootstrapMagic.dictionary.currentSubCategory.set bootstrap_magic_variables[0]._id
# this recursive function takes an object with {_id: bootstrapKey} format
# it will return all 'computed' values, if it's value relates to another bootstrap variable
mapVariableOverrides = (obj) ->
obj.isOverride = false
obj.isReference = false
myVal = BootstrapMagic.dictionary.overrides.get(obj._id)
if myVal
obj.isOverride = true
else
myVal = BootstrapMagic.dictionary.defaults.get(obj._id)
if myVal
obj.value = myVal
# get the refernece recursively
if obj.value?.indexOf('@') > -1
obj.isReference = true
obj.parentVar = obj.value
obj.reference = mapVariableOverrides {_id: obj.value}
obj.reference.value?= '?'
if obj.reference.reference
obj.reference = obj.reference.reference
return obj
getCurrentCategory = ->
myCat = BootstrapMagic.dictionary.currentCategory.get()
subCats = _.where bootstrap_magic_variables, category: myCat
# https://github.com/TAPevents/tap-i18n/issues/100
return _.sortBy subCats, (cat) -> TAPi18n.__ cat._id
getCurrentVariables = ->
subCatId = BootstrapMagic.dictionary.currentSubCategory.get()
return _.find bootstrap_magic_variables, (group) -> group._id is subCatId
showSearchResults = -> # only show the search results if there are 3 characters or more, or if you're filtering
BootstrapMagic.dictionary.searchTerms.get().length >= 3 or BootstrapMagic.dictionary.searchFilters.get('overrides')
getSearchResults = ->
query = BootstrapMagic.dictionary.searchTerms.get()
searchResults = {search: true}
if BootstrapMagic.dictionary.searchFilters.get('overrides')
searchResults.data = _.filter flattenedMagic, (obj) -> obj._id.indexOf(query) > -1 && obj.isOverride is true
else
searchResults.data = _.filter flattenedMagic, (obj) -> obj._id.indexOf(query) > -1
return searchResults
Template.bootstrap_magic_input_number.helpers
'unitRange' : ->
myUnit = getUnitFromContext.apply @
ranges = _.clone(unitRanges[myUnit] || {})
ranges.unit = myUnit
ranges.current = parseInt(@reference?.value || @value)
if ranges.current > ranges.max
ranges.max = ranges.current
return ranges
camelToSnake = (str) -> str.replace(/\W+/g, '_').replace(/([a-z\d])([A-Z])/g, '$1-$2')
spaceToHyphen = (str) -> str.replace(/\s/g, '-')
Template._bootstrap_magic.helpers
"categories" : _.map _.groupBy(bootstrap_magic_variables, 'category'), (obj) -> _id: obj[0].category
"subCategories" : getCurrentCategory
"isSelectedCat" : -> @_id is BootstrapMagic.dictionary.currentCategory.get()
"currentVars" : -> if showSearchResults() then getSearchResults() else getCurrentVariables()
"mappedVariables" : -> _.map @data, mapVariableOverrides
"isSelectedSubCat" : -> @_id is BootstrapMagic.dictionary.currentSubCategory.get()
"previewTmpl" : -> Template["bootstrap_magic_preview_#{camelToSnake @_id}"] || null
"inputTmpl" : -> Template["bootstrap_magic_input_#{@type}"] || null
"filteringOverrides" : -> BootstrapMagic.dictionary.searchFilters.get('overrides')
Template._bootstrap_magic.events
'keyup .search-input' : (e) ->
BootstrapMagic.dictionary.searchTerms.set spaceToHyphen e.currentTarget.value
'click .search-filter' :->
BootstrapMagic.dictionary.searchFilters.set 'overrides', !BootstrapMagic.dictionary.searchFilters.get('overrides')
'change .bootstrap-magic-input' : (e) ->
$input = $(e.currentTarget)
BootstrapMagic.setOverride @_id, $input.val() || undefined
'click .main-menu a' : ->
BootstrapMagic.dictionary.currentCategory.set @_id
BootstrapMagic.dictionary.currentSubCategory.set getCurrentCategory()[0]._id # set subcategory to the first child
'click .sub-menu a' : ->
BootstrapMagic.dictionary.currentSubCategory.set @_id
###
# Colorpicker Create/Destroy
###
Template.bootstrap_magic_input_color.onRendered ->
self = @
$thisColorPicker = $(@firstNode)
# init colorpicker
$thisColorPicker.colorpicker
horizontal: true
.on 'showPicker', ->
@startVal = $('input', @).val()
.on 'hidePicker', ->
$input = $('input',@)
@endVal = $input.val()
if @startVal isnt @endVal
@startVal = @endVal
$input.trigger 'change'
@picker = $thisColorPicker.data('colorpicker').picker
# auto update color if value changes
@autorun ->
mappedVar = mapVariableOverrides({_id: self.data._id})
thisColor = mappedVar.reference?.value || mappedVar.value
# don't update the value of the input box
oldInputVal = $('input', $thisColorPicker).val()
$thisColorPicker.colorpicker('setValue', thisColor).colorpicker('update', true)
$('input', $thisColorPicker).val oldInputVal
Template.bootstrap_magic_input_color.onDestroyed ->
@picker.remove()
$(@firstNode).colorpicker('destroy')
###
# Bootstrap Popovers, Tooltips & EZ-Modal
###
# Informed variables popover
Template.bootstrap_magic_input.helpers
'myChildren' : ->
items = _.map flattenedMagic, mapVariableOverrides
return _.filter items, (obj) => obj.value?.indexOf(@._id) >- 1
Template.bootstrap_magic_input.onRendered ->
$popoverLabel = @$('.popover-label')
$popoverLabel.popover
placement: 'right'
trigger: 'manual'
html: true
animation: true
container: $popoverLabel
template: """
<div class="popover popover-list" role="tooltip">
<div class="arrow"></div>
<h3 class="popover-title"></h3>
<ul class="popover-content list-group"></ul>
<button class='btn btn-default btn-xs pull-right mar-xs popover-pin' href='#' role='button'>
<i class='glyphicon glyphicon-pushpin'></i>
</button>
</div>
"""
.hover ->
unless $popoverLabel.hasClass 'pinned'
$popoverLabel
.addClass 'popover-active'
.popover 'show'
.find('.popover-pin').off('click').click ->
$popoverLabel.toggleClass 'pinned'
, ->
unless $popoverLabel.hasClass 'pinned'
$popoverLabel
.removeClass 'popover-active'
.popover 'hide'
Template.bootstrap_magic_input.onDestroyed ->
@$('[data-toggle="popover"]').popover('destroy')
unitRanges =
'px' :
min: 0
max: 40
step: 1
'%' :
min: 0
max: 100
step: 5
getUnitFromContext = ->
matchVal = @reference?.value || @value
for unit, val of unitRanges
if matchVal.indexOf(unit) > -1
return unit
Template.bootstrap_magic_input_number.helpers
'unitRange' : ->
myUnit = getUnitFromContext.apply @
ranges = _.clone(unitRanges[myUnit] || {})
ranges.unit = myUnit
ranges.current = parseInt(@reference?.value || @value)
if ranges.current > ranges.max
ranges.max = ranges.current
return ranges
Template.bootstrap_magic_input_number.events
"input input[type='range']" : (e, tmpl) ->
$("input[type='text']", tmpl.firstNode).val "#{e.currentTarget.value}#{@unit}"
"change input[type='range']" : (e, tmpl) ->
$(".bootstrap-magic-input", tmpl.firstNode).trigger 'change'
# Template examples
Template.bootstrap_magic_preview_tooltips.onRendered ->
@$('[data-toggle="tooltip"]').tooltip()