forked from garrett/magicmockup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicmockup.coffee
199 lines (138 loc) · 4.65 KB
/
magicmockup.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
$ = @jQuery
@magicmockup = do ->
$doc = $(@document)
layers = {}
filter = {}
defaultLayer = ''
# Convenience function to grab attributes from the Inkscape namespace
_getInk = (el, attr) ->
inkNS = 'http://www.inkscape.org/namespaces/inkscape'
el.getAttributeNS(inkNS, attr)
# Add each layer to the layer object (if it contains a an Inkscape label)
_initLayers = ($layers = $('g')) ->
$layers.each ->
group = _getInk(@, 'groupmode')
label = _getInk(@, 'label')
if group is 'layer'
layers[label] = @
defaultLayer = label if $(@).is(':visible')
return
# Find all filters and store in the filter object
_findFilters = ->
$doc.find('filter').each ->
label = _getInk(@, 'label')
filter[label] = @id
# Do the heavy lifting
# (right now, there's only "next" for switching pages; more to come)
_dispatch = (context, [command, val]) ->
act =
load: (url) ->
url = url.shift()
window.location = url || val
next: (location) ->
location = location.shift()
if location.match /#/
# if "#" is added, then load the new page
act.load(location)
else
# Hide the current visible layer
$(context).parents('g').not('[style=display:none]').last().hide()
# Show the specified layer
$(layers[location]).show?()
location = '' if location is defaultLayer
window.location.hash = location
show: (show_layers) ->
for layer in show_layers
$(layers[layer]).show()
hide: (hide_layers) ->
for layer in hide_layers
$(layers[layer]).hide()
toggle: (toggle_layers) ->
for layer in toggle_layers
$(layers[layer]).toggle()
fadeOut: (params) ->
if params? and params.length > 0
# Capture parameters with defaults
layer = params[0]
time = params[1] ? 1
easing = params[2] ? 'linear'
# Convert time from seconds to milliseconds
time = parseInt(time) * 1000
$(layers[layer])
.attr('opacity', 1)
.animate svgOpacity: 0.0, time, easing, () ->
# Reset opacity but hide
$(this).hide().attr 'opacity', 1
params = val?.split ','
act[command]?(params)
# Return the description for an element
_getDescription = (el) ->
$(el).children('desc').text()
# If there's inline JS, strip it (and provide warnings)
_stripInlineJS = ->
$onclick = $('[onclick]')
return unless $onclick.length
# Warn about inline JS (if console.warn is available)
if console and console.warn
console.group? 'Warning: inline JavaScript found (and deactivated)'
$onclick.each -> console.warn @id, ':', @onclick
console.groupEnd?()
# Strip the inline JS
$onclick.each -> @onclick = undefined
return
# Return the URL fragment
_getHash = ->
window.location.hash.substr(1)
# Hide all layers
_hideLayers = ->
for name, layer of layers
$(layer).hide()
# Make a layer visible
_showLayer = (layer) ->
if typeof layer isnt 'string'
layer = _getHash()
# Make sure the layer exists
return unless layers[layer] or layer is ''
_hideLayers()
_dispatch @, ['next', layer or defaultLayer]
# If a hash is specified, view the appropriate layer
_setInitialPage = ->
layer = _getHash()
if layer
_showLayer layer
# Handle clicks on items with instructions
_handleClick = (e) ->
actions = _getDescription(e.currentTarget)
# Skip if there's no description
return unless actions
for action in actions.split /([\s\n]+)/
_dispatch @, action.split /\=/
return
# Change the cursor for interactive elements
_handleHover = (e) ->
$this = $(this)
isHovered = e.type is "mouseenter"
# Skip if there's no description
return unless _getDescription(e.currentTarget)
# Alter hover CSS if there's a hover filter
if filter.hover
hover = if isHovered then "url(##{filter.hover})" else "none"
$this.css filter: hover
# Skip if already hoverable
return if $this.data('hoverable')
# We're handling the hoverable state now
$this.data('hoverable', true).css(cursor: 'pointer')
return
# Run on page load
init = (loadEvent) ->
_initLayers()
_setInitialPage()
_findFilters()
_stripInlineJS()
$(window).bind 'hashchange', _showLayer
$doc.delegate 'g'
click : _handleClick
hover : _handleHover
{init} # Public exports
# Hack to attach the init to <svg/> for an unobtrusive SVG onload
$('svg').attr onload: 'magicmockup.init()'