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
131 lines (102 loc) · 3.75 KB
/
Copy pathignition.coffee
File metadata and controls
131 lines (102 loc) · 3.75 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
class ContentTools.IgnitionUI extends ContentTools.WidgetUI
# To control editing of content (starting/stopping) a ignition switch is
# provided, the switch has 3 states:
constructor: () ->
super()
# Flag indicating if the ignition is currently busy (cannot be
# interacted with).
@_busy = false
# Methods
busy: (busy) ->
# Get/Set the busy state of the ignition switch. The busy state is
# useful when you want to prevent the user from attempting to start the
# editor, for example during a save request.
if busy is undefined
return @_busy
# Check the ignition isn't already busy
if @_busy is busy
return
# Change the state of the switch
@_busy = busy
if busy
@addCSSClass('ct-ignition--busy')
else
@removeCSSClass('ct-ignition--busy')
changeState: (state) ->
# Change the state of the ignition without triggering an event, (useful
# when you wish to revert to a previous state).
if state is 'editing'
@addCSSClass('ct-ignition--editing')
@removeCSSClass('ct-ignition--ready')
else if state is 'ready'
@removeCSSClass('ct-ignition--editing')
@addCSSClass('ct-ignition--ready')
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()
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()
# Change the state of the switch
@addCSSClass('ct-ignition--editing')
@removeCSSClass('ct-ignition--ready')
# Trigger the start event
@trigger('start')
# Stop editing - Confirm changes
@_domConfirm.addEventListener 'click', (ev) =>
ev.preventDefault()
# Change the state of the switch
@removeCSSClass('ct-ignition--editing')
@addCSSClass('ct-ignition--ready')
# Trigger the start event
@trigger('stop', true)
# Stop editing - Cancel changes
@_domCancel.addEventListener 'click', (ev) =>
ev.preventDefault()
# Change the state of the switch
@removeCSSClass('ct-ignition--editing')
@addCSSClass('ct-ignition--ready')
# Trigger the start event
@trigger('stop', false)