-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathLesson.js
More file actions
263 lines (217 loc) · 5.57 KB
/
Lesson.js
File metadata and controls
263 lines (217 loc) · 5.57 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
263
/**
* Single Lesson View
* @since 3.16.0
* @version 3.27.0
*/
define( [
'Views/_Detachable',
'Views/_Editable',
'Views/_Shiftable',
'Views/_Trashable'
], function(
Detachable,
Editable,
Shiftable,
Trashable
) {
return Backbone.View.extend( _.defaults( {
/**
* Get default attributes for the html wrapper element
* @return obj
* @since 3.16.0
* @version 3.16.0
*/
attributes: function() {
return {
'data-id': this.model.id,
'data-section-id': this.model.get( 'parent_section' ),
};
},
/**
* HTML class names
* @type {String}
*/
className: 'llms-builder-item llms-lesson',
/**
* Events
* @type {Object}
* @since 3.16.0
* @version 3.16.12
*/
events: _.defaults( {
'click .edit-lesson': 'open_lesson_editor',
'click .llms-headline': 'open_lesson_editor',
'click .edit-quiz': 'open_quiz_editor',
'click .edit-assignment': 'open_assignment_editor',
'click .section-prev': 'section_prev',
'click .section-next': 'section_next',
'click .shift-up--lesson': 'shift_up',
'click .shift-down--lesson': 'shift_down',
}, Detachable.events, Editable.events, Trashable.events ),
/**
* HTML element wrapper ID attribute
* @return string
* @since 3.16.0
* @version 3.16.0
*/
id: function() {
return 'llms-lesson-' + this.model.id;
},
/**
* Wrapper Tag name
* @type {String}
*/
tagName: 'li',
/**
* Get the underscore template
* @type {[type]}
*/
template: wp.template( 'llms-lesson-template' ),
/**
* Initialization callback func (renders the element on screen)
* @return void
* @since 3.14.1
* @version 3.14.1
*/
initialize: function() {
this.render();
this.listenTo( this.model, 'change', this.render );
Backbone.pubSub.on( 'lesson-selected', this.on_select, this );
Backbone.pubSub.on( 'new-lesson-added', this.on_select, this );
},
/**
* Compiles the template and renders the view
* @return self (for chaining)
* @since 3.16.0
* @version 3.16.0
*/
render: function() {
this.$el.html( this.template( this.model ) );
this.maybe_hide_shiftable_buttons();
if ( this.model.get( '_selected' ) ) {
this.$el.addClass( 'selected' );
} else {
this.$el.removeClass( 'selected' );
}
return this;
},
/**
* Click event for the assignment editor action icon
* Opens sidebar to the assignment editor tab
* @param obj event JS Event obj.
* @return void
* @since 3.17.0
* @version 3.27.0
*/
open_assignment_editor: function( event ) {
if ( event ) {
event.preventDefault();
}
Backbone.pubSub.trigger( 'lesson-selected', this.model, 'assignment' );
this.model.set( '_selected', true );
this.set_hash( 'assignment' );
},
/**
* Click event for lesson settings action icon
* Opens sidebar to the lesson editor tab
* @param obj event JS Event obj.
* @return void
* @since 3.16.0
* @version 3.27.0
*/
open_lesson_editor: function( event ) {
if ( event ) {
event.preventDefault();
}
Backbone.pubSub.trigger( 'lesson-selected', this.model, 'lesson' );
this.model.set( '_selected', true );
this.set_hash( false );
},
/**
* Click event for the quiz editor action icon
* Opens sidebar to the quiz editor tab
* @param obj event JS Event obj.
* @return void
* @since 3.16.0
* @version 3.27.0
*/
open_quiz_editor: function( event ) {
if ( event ) {
event.preventDefault();
}
Backbone.pubSub.trigger( 'lesson-selected', this.model, 'quiz' );
this.model.set( '_selected', true );
this.set_hash( 'quiz' );
},
/**
* When a lesson is selected mark it as selected in the hidden prop
* Allows views to re-render and reflect current state properly
* @param obj model lesson model that's been selected
* @return void
* @since 3.16.0
* @version 3.16.0
*/
on_select: function( model ) {
if ( this.model.id !== model.id ) {
this.model.set( '_selected', false );
}
},
/**
* Click event for the "Next Section" button
* @param obj event js event obj
* @return void
* @since 3.16.11
* @version 3.16.11
*/
section_next: function( event ) {
event.preventDefault();
this._move_to_section( 'next' );
},
/**
* Click event for the "Previous Section" button
* @param obj event js event obj
* @return void
* @since 3.16.11
* @version 3.16.11
*/
section_prev: function( event ) {
event.preventDefault();
this._move_to_section( 'prev' );
},
/**
* Adds a hash for deep linking to a specific lesson tab
* @param string subtab subtab [quiz|assignment]
* @return void
* @since 3.27.0
* @version 3.27.0
*/
set_hash: function( subtab ) {
var hash = 'lesson:' + this.model.get( 'id' );
if ( subtab ) {
hash += ':' + subtab;
}
window.location.hash = hash;
},
/**
* Move the lesson into a new section
* @param string direction direction [prev|next]
* @return void
* @since 3.16.11
* @version 3.16.11
*/
_move_to_section: function( direction ) {
var from_coll = this.model.collection,
to_section;
if ( 'next' === direction ) {
to_section = from_coll.parent.get_next();
} else if ( 'prev' === direction ) {
to_section = from_coll.parent.get_prev();
}
if ( to_section ) {
from_coll.remove( this.model );
to_section.add_lesson( this.model );
to_section.set( '_expanded', true );
}
},
}, Detachable, Editable, Shiftable, Trashable ) );
} );