forked from GetmeUK/ContentTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignition.coffee
More file actions
161 lines (127 loc) · 4.41 KB
/
Copy pathignition.coffee
File metadata and controls
161 lines (127 loc) · 4.41 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
class ContentTools.IgnitionUI extends ContentTools.WidgetUI
# To control editing of content (starting/stopping) a ignition switch is
# provided, the switch has 3 states:
#
# - ready - Displays an edit option
# - editing - Displays confirm and cancel options
# - busy - Displays a busy status animation
constructor: () ->
super()
# The state to return to once when the component state reverts from a
# a busy state.
@_revertToState = 'ready'
# The state of the switch
@_state = 'ready'
# Methods
busy: (busy) ->
# Set the widget to busy (or revert if from a busy state to its previous
# state.
if @dispatchEvent(@createEvent('busy', {busy: busy}))
# If the widget is already busy do nothing
if busy == (@_state == 'busy')
return
if busy
@_revertToState = @_state
@state('busy')
else
@state(@_revertToState)
cancel: () ->
# Dispatch the cancel event against the switch and set its state to
# ready.
if @dispatchEvent(@createEvent('cancel'))
@state('ready')
confirm: () ->
# Dispatch the confirm event against the switch set its state to
# ready.
if @dispatchEvent(@createEvent('confirm'))
@state('ready')
edit: () ->
# Dispatch the edit event against the switch and set its state to
# editing.
if @dispatchEvent(@createEvent('edit'))
@state('editing')
mount: () ->
# Mount the component to the DOM
super()
# Base widget component
@_domElement = @constructor.createDiv([
'ct-widget',
'ct-ignition',
'ct-ignition--ready'
])
@parent().domElement().appendChild(@_domElement)
# Edit button
@_domEdit = @constructor.createDiv([
'ct-ignition__button',
'ct-ignition__button--edit'
])
@_domElement.appendChild(@_domEdit)
# Confirm button
@_domConfirm = @constructor.createDiv([
'ct-ignition__button',
'ct-ignition__button--confirm'
])
@_domElement.appendChild(@_domConfirm)
# Cancel button
@_domCancel = @constructor.createDiv([
'ct-ignition__button',
'ct-ignition__button--cancel'
])
@_domElement.appendChild(@_domCancel)
# Busy
@_domBusy = @constructor.createDiv([
'ct-ignition__button',
'ct-ignition__button--busy'
])
@_domElement.appendChild(@_domBusy)
# Add events
@_addDOMEventListeners()
state: (state) ->
# Get/Set the state of the ignition switch. State must be one of the
# following values:
#
# - busy
# - editing
# - ready
#
if state == undefined
return @_state
# If the state hasn't changed do nothing
if @_state == state
return
if not @dispatchEvent(@createEvent('statechange', {state: state}))
return
# Modify the state of the switch
@_state = state
# Remove existing state modifier classes
@removeCSSClass('ct-ignition--busy')
@removeCSSClass('ct-ignition--editing')
@removeCSSClass('ct-ignition--ready')
# Apply the new state modifier class
if @_state is 'busy'
@addCSSClass('ct-ignition--busy')
else if @_state is 'editing'
@addCSSClass('ct-ignition--editing')
else if @_state is 'ready'
@addCSSClass('ct-ignition--ready')
unmount: () ->
# Unmount the widget from the DOM
super()
@_domEdit = null
@_domConfirm = null
@_domCancel = null
# Private methods
_addDOMEventListeners: () ->
# Add all DOM event bindings for the component in this method
# Start editing
@_domEdit.addEventListener 'click', (ev) =>
ev.preventDefault()
@edit()
# Stop editing - Confirm changes
@_domConfirm.addEventListener 'click', (ev) =>
ev.preventDefault()
@confirm()
# Stop editing - Cancel changes
@_domCancel.addEventListener 'click', (ev) =>
ev.preventDefault()
@cancel()