forked from GetmeUK/ContentTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.coffee
More file actions
262 lines (186 loc) · 7.6 KB
/
Copy pathui.coffee
File metadata and controls
262 lines (186 loc) · 7.6 KB
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
class ContentTools.ComponentUI
# All UI compontents inherit from the CompontentUI class which provides base
# functionality and a common API.
constructor: () ->
# Events are supported using the native DOM event system. We create a
# DOM element that's never attached to the DOM but allows us to plugin
# to the native event system.
@_eventBinderDOM = document.createElement('div')
# Event bindings for the component
@_bindings = {}
# The component's parent
@_parent = null
# The component's children
@_children = []
# The DOM element associated with this component
@_domElement = null
# Read-only methods
children: () ->
# Return the list of children for the component
return @_children.slice()
domElement: () ->
# Return the mounted DOM element for the component
return @_domElement
isMounted: () ->
# Return true if the component is mounted to the DOM
return @_domElement != null
parent: () ->
# Return the parent of the component
return @_parent
# Methods
attach: (component, index) ->
# Attach a component as a child of this component
if component.parent()
component.parent().detach(component)
component._parent = this
if index != undefined
@_children.splice(index, 0, component)
else
@_children.push(component)
addCSSClass: (className) ->
# Add a CSS class to the DOM element
unless @isMounted()
return
ContentEdit.addCSSClass(@_domElement, className)
detatch: (component) ->
# Detach a child component from this component
# Find the component to detatch (if not found return)
componentIndex = @_children.indexOf(component)
if componentIndex == -1
return
# Remove the component from the components children
@_children.splice(componentIndex, 1)
mount: () ->
# Mount the component to the DOM
removeCSSClass: (className) ->
# Remove a CSS class from the DOM element
unless @isMounted()
return
ContentEdit.removeCSSClass(@_domElement, className)
unmount: () ->
# Unmount the component from the DOM
unless @isMounted()
return
@_removeDOMEventListeners()
if @_domElement.parentNode
@_domElement.parentNode.removeChild(@_domElement)
@_domElement = null
# Event methods
addEventListener: (eventName, callback) ->
# Add an event listener for the UI component
# Check a list has been set for the specified event
if @_bindings[eventName] == undefined
@_bindings[eventName] = []
# Add the callback to list for the event
@_bindings[eventName].push(callback)
return
createEvent: (eventName, detail) ->
# Create an event
return new ContentTools.Event(eventName, detail)
dispatchEvent: (ev) ->
# Dispatch an event against the UI compontent
# Check we have callbacks to trigger for the event
unless @_bindings[ev.name()]
return not ev.defaultPrevented()
# Call each function bound to the event
for callback in @_bindings[ev.name()]
if ev.propagationStopped()
break
if not callback
continue
callback.call(this, ev)
return not ev.defaultPrevented()
removeEventListener: (eventName, callback) ->
# Remove a previously registered event listener for the UI component
# If no eventName is specified remove all events
unless eventName
@_bindings = {}
return
# If no callback is specified remove all callbacks for the event
unless callback
@_bindings[eventName] = undefined
return
# Check if any callbacks are bound to this event
unless @_bindings[eventName]
return
# Remove the callback from the event
for suspect, i in @_bindings[eventName]
if suspect is callback
@_bindings[eventName].splice(i, 1)
# Private methods
_addDOMEventListeners: () ->
# Add all event bindings for the DOM element in this method
_removeDOMEventListeners: () ->
# Remove all event bindings for the DOM element in this method
@createDiv: (classNames, attributes, content) ->
# All UI components are constructed entirely from one or more nested
# <div>s, this class method provides a shortcut for creating a <div>
# including the initial CSS class names, attributes and content.
# Create the element
domElement = document.createElement('div')
# Add the specified CSS classes
if classNames and classNames.length > 0
domElement.setAttribute('class', classNames.join(' '))
# Add the specified attributes
if attributes
for name, value of attributes
domElement.setAttribute(name, value)
if content
domElement.innerHTML = content
return domElement
class ContentTools.WidgetUI extends ContentTools.ComponentUI
# The widget class provides a base class for components that render at the
# root of the application.
attach: (component, index) ->
# Attach a component as a child of this component
super(component, index)
if not @isMounted()
component.mount()
detatch: (component) ->
# Detach a child component from this component
super(component)
if @isMounted()
component.unmount()
show: () ->
# Show the widget
if not @isMounted()
@mount()
# We delay adding the --active modifier to ensure any CSS transition is
# activated.
fadeIn = () =>
@addCSSClass('ct-widget--active')
setTimeout(fadeIn, 100)
hide: () ->
# Hide the widget
# Removing the --active modifier will attempt to trigger an CSS
# transition to fade out the widget. Once the transition to 0 opacity
# is complete we unmount it.
@removeCSSClass('ct-widget--active')
monitorForHidden = () =>
# If there's no support for `getComputedStyle` then we fallback to
# unmounting the widget immediately.
unless window.getComputedStyle
@unmount()
return
# If the widget is now hidden we unmount it
if parseFloat(window.getComputedStyle(@_domElement).opacity) < 0.01
@unmount()
else
setTimeout(monitorForHidden, 250)
if @isMounted()
setTimeout(monitorForHidden, 250)
class ContentTools.AnchoredComponentUI extends ContentTools.ComponentUI
# Anchored components are mounted against a specified DOM element at a
# specified anchor. Remounting an anchored component requires that the
# parent perform the re-mount.
#
# The benefit of anchored components is they are light weight and can be
# rendered into different a specific DOM element by the parent, for example
# tools within the toolbox are anchored components.
mount: (domParent, before=null) ->
# Mount the component to the DOM (mount should be called by inheriting
# classes after they've created their DOM element using `super`.
# Mount the element
domParent.insertBefore(@_domElement, before)
# Add interaction handlers
@_addDOMEventListeners()