-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathCourse.js
More file actions
199 lines (161 loc) · 4.27 KB
/
Course.js
File metadata and controls
199 lines (161 loc) · 4.27 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
/**
* Single Course View
*
* @since 3.13.0
* @version 3.16.0
*/
define( [
'Views/SectionList',
'Views/_Detachable',
'Views/_Editable',
'Views/_Shiftable',
'Views/_Trashable'
], function(
SectionListView,
Detachable,
Editable,
Shiftable,
Trashable
) {
return Backbone.View.extend( _.defaults( {
/**
* Get default attributes for the html wrapper element
*
* @return obj
* @since 3.13.0
* @version 3.13.0
*/
attributes: function() {
return {
'data-id': this.model.id,
};
},
/**
* HTML element selector
*
* @type {String}
*/
el: '#llms-builder-main',
/**
* Wrapper Tag name
*
* @type {String}
*/
tagName: 'div',
/**
* Get the underscore template
*
* @type {[type]}
*/
template: wp.template( 'llms-course-template' ),
/**
* Initialization callback func (renders the element on screen)
*
* @return void
* @since 3.13.0
* @version 3.13.0
*/
initialize: function() {
var self = this;
// this.listenTo( this.model, 'sync', this.render );
this.render();
this.sectionListView = new SectionListView( {
collection: this.model.get( 'sections' ),
} );
this.sectionListView.render();
// drag and drop start
this.sectionListView.on( 'sortStart', this.sectionListView.sortable_start );
// drag and drop stop
this.sectionListView.on( 'sortStop', this.sectionListView.sortable_stop );
// selection changes
this.sectionListView.on( 'selectionChanged', this.active_section_change );
// "select" a section when it's added to the course
this.listenTo( this.model.get( 'sections' ), 'add', this.on_section_add );
Backbone.pubSub.on( 'section-toggle', this.on_section_toggle, this );
Backbone.pubSub.on( 'section-select', this.on_section_select, this );
Backbone.pubSub.on( 'expand-section', this.expand_section, this );
Backbone.pubSub.on( 'lesson-selected', this.active_lesson_change, this );
},
/**
* Events
* @type {Object}
* @version [version]
*/
events: _.defaults( {
'click .new-section': 'add_new_section',
}, Detachable.events, Editable.events, Trashable.events ),
/**
* Compiles the template and renders the view
*
* @return self (for chaining)
* @since 3.13.0
* @version 3.13.0
*/
render: function() {
this.$el.html( this.template( this.model ) );
return this;
},
active_lesson_change: function( model ) {
// set parent section to be active
var section = this.model.get( 'sections' ).get( model.get( 'parent_section' ) );
this.sectionListView.setSelectedModel( section );
},
/**
* When a section "selection" changes in the list
* Update each section model so we can figure out which one is selected from other views
*
* @param array current array of selected models
* @param array previous array of previously selected models
* @return void
* @since 3.16.0
* @version 3.16.0
*/
active_section_change: function( current, previous ) {
_.each( current, function( model ) {
model.set( '_selected', true );
} );
_.each( previous, function( model ) {
model.set( '_selected', false );
} );
},
/**
* "Selects" the new section when it's added to the course
*
* @param obj model Section model that's just been added
* @return void
* @since 3.16.0
* @version 3.16.0
*/
on_section_add: function( model ) {
this.sectionListView.setSelectedModel( model );
},
/**
* When expanding/collapsing sections
* if collapsing, unselect, if expanding, select
*
* @param obj model toggled section
* @return void
* @since 3.16.0
* @version 3.16.0
*/
on_section_toggle: function( model ) {
var selected = model.get( '_expanded' ) ? [ model ] : [];
this.sectionListView.setSelectedModels( selected );
},
/**
* When doing things like adding a lesson, seelct the section.
*
* @param obj model toggled section
* @return void
* @since [version]
* @version [version]
*/
on_section_select: function( model ) {
this.sectionListView.setSelectedModel( model );
},
add_new_section: function( event ) {
event.preventDefault();
Backbone.pubSub.trigger( 'add-new-section' );
},
}, Editable ) );
} );