-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItemClass.js
More file actions
339 lines (331 loc) · 10.5 KB
/
ItemClass.js
File metadata and controls
339 lines (331 loc) · 10.5 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const util = require('util');
const canvas = require('./canvas');
const parse = require('./parse');
/**
* An abstract class for the different types of items to inherit from
* @private @prop {number} _course - the id of the course
* @private @prop {number} _id - the id of the item
* @private @prop {string} _original - the original values from canvas stringified
* @private @abstract @prop {array} _subs - the property names of the sub items
* @private @abstract @prop {string} _post - the name of the property to wrap the data in if any
* @private @abstract @prop {string} _path - the path used to create the url
* @private @abstract @prop {string} _html - the property name to access the html of the object
* @private @abstract @prop {string} _title - the property name to access the title of the object
*/
module.exports = class Item {
/**
* Attach event listener, don't really know when you would use
* these so they aren't documented. I only needed them so that
* the Parent Items knows if it got deleted independently
* @param {string} event
* @param {function} func
*/
on(event, func) {
if (typeof event != 'string') {
throw TypeError('Event label needs to be a string');
}
if (typeof func != 'function') {
throw TypeError('Event handler needs to be a function');
}
this._listeners[event] = this._listeners[event] || [];
this._listeners[event].push(func);
}
/**
* Called when I'm sending an event
* @private
* @param {string} event
*/
send(event) {
if (typeof event != 'string') {
throw TypeError('Event label needs to be a string');
}
if (this._listeners[event]) {
this._listeners[event].forEach(func => func(this));
}
}
/**
* Set the data of the item
* - purges the old data
* - sets the _original
* @private
* @param {Object} data
*/
setData(data) {
// Purge all of the old data
for (var prop in this) {
if (this.hasOwnProperty(prop) && Object.getOwnPropertyDescriptor(this, prop).configurable) {
delete this[prop];
}
}
// Save the data
Object.assign(this, data);
if (data[this._idProp] != undefined) {
this._id = data[this._idProp];
this.getSubs().forEach(sub => {
if (sub.ids[sub.ids.length - 1] != this._id) {
sub.ids.push(this._id);
}
});
}
this._original = JSON.stringify(this);
}
/**
* Creates the post body, wraping it if _post is specified
* @private
* @return {Object} - postbody
*/
getPostbody() {
var top = {};
var postbody = this._post ? top[this._post] = {} : top;
Object.assign(postbody, this);
this._subs.forEach(key => {
delete postbody[key];
});
return top;
}
/**
* Creates the url with the internal ids, dosen't specify last id if asked
* @private
* @param {boolean} includeId
* @return {string} - path
*/
getPath(individual = true) {
if (!this._path) {
throw new TypeError('Class extending the Item class doesn\'t have path defined');
}
let path = this._path,
includeCourse = true;
if (typeof this._path == 'function') {
path = path(this._parents, individual);
}
if (Array.isArray(path)) {
[path, includeCourse = true] = path;
}
return [
'/api/v1',
includeCourse && 'courses/' + this._parents[0],
path.replace(/^\/|\/$/g, ''),
individual && String(this._id)
].filter(n => n).join('/');
}
/** @return {string} - item's html */
getHtml() {
if (this._html) {
return this[this._html] || '';
} else {
return undefined;
}
}
/** @param {string} - item's html */
setHtml(val) {
if (!this._html) {
throw new TypeError('Class extending the Item class doesn\'t have a html property defined');
}
this[this._html] = val;
}
/** @return {string} - item's title */
getTitle() {
if (this._title) {
return this[this._title] || '';
} else {
return undefined;
}
}
/** @param {string} - item's title */
setTitle(val) {
if (!this._title) {
throw new TypeError('Class extending the Item class doesn\'t have a title property defined');
}
this[this._title] = val;
}
/** @return {string} - item's Url */
getUrl() {
if (typeof this._url == 'function') {
return this._url(this.getIds());
} else if (this._url.includes('http')) {
return this._url;
} else {
return this[this._url];
}
}
/** @return {string} - name of this class */
getType() {
return this.constructor.name;
}
/** @return {string} - item's id */
getId() {
return this._id;
}
/** @return {[string]} - Parent's ids plus item's id */
getIds() {
return this._parents.concat([this._id]);
}
/** @return {[string]} - Parent's ids */
getParentIds() {
return this._parents;
}
/** @return {[Items]} - array of subs */
getSubs() {
return this._subs.map(key => this[key]);
}
/** @return {string} */
getType() {
return this.constructor.name;
}
/**
* @private
* @return {object} - this object with out it's children
*/
toJSON() {
var temp = Object.assign({}, this);
this._subs.forEach(key => delete temp[key]);
return temp;
}
/**
* Returns whether this or its children have changed
* [ convience method to match the Items class ]
* @return {Boolean}
*/
hasChanged() {
return this.getChanged().some(n => n);
}
/**
* Checks to see if this item's properties have changed since the last setData
* @private
* @return {Array[thisChanged,childrenChanged]}
*/
getChanged() {
return [
this._original != undefined && JSON.stringify(this) != this._original,
this.getSubs().some(sub => sub.hasChanged()),
];
}
/**
* Returns a flat list of all children and their children
*/
getFlattened(depth = 1) {
return this.getSubs().reduce((arr, children) => arr.concat(children.getFlattened(depth - 1)), depth <= 0 ? [this] : []);
}
/**
* Retrieves all of the sub items, and their sub items
* @private
*/
async getSub() {
await Promise.all(this.getSubs().map(sub => sub.getComplete()));
// reset the has changed
this._original = JSON.stringify(this);
}
/**
* Retrieves this item's data from canvas
* @async
* @param {function} [callback] If not specified, returns a promise
* @return {Item} this
*/
async get(query = undefined, callback = undefined) {
// Backwards compatability
if (typeof query == 'boolean') {
if (query == true) {
return this.getComplete(callback);
}
query = undefined;
} else if (typeof query == 'function') {
callback = query;
query = undefined;
}
// Fancy boilerplate to recursivly callbackify if there is a callback
if (callback) {
return util.callbackify(this.get.bind(this))(...arguments);
}
var data = await canvas(this.getPath(), query);
this.setData(data);
this.send('get');
return this;
}
/**
* Retrieves this item from canvas and all of it's sub items
* @async
* @param {function} [callback] If not specified, returns a promise
* @return {Item} this
*/
async getComplete(callback = undefined) {
if (callback) {
return util.callbackify(this.getComplete.bind(this))(...arguments);
}
await this.get();
await this.getSub();
this.send('getComplete');
return this;
}
/**
* Posts this item's data to canvas
* @async
* @param {function} [callback] If not specified, returns a promise
*/
async update(alternateBody = undefined, callback = undefined) {
if (typeof alternateBody == 'function') {
callback = alternateBody;
alternateBody = undefined;
}
if (callback) {
return util.callbackify(this.update.bind(this))(...arguments);
}
// If they specify an alternate body then always post
// regaurdless of whether things have actually changed
if (alternateBody) {
alternateBody = parse(alternateBody);
if (this._post && !alternateBody[this._post]) {
alternateBody = {
[this._post]: alternateBody
};
}
var data = await canvas.put(this.getPath(), alternateBody);
this.setData(data);
} else {
// If nothing has changed then don't bother
var [thisChanged, childrenChanged] = this.getChanged();
if (thisChanged) {
var data = await canvas.put(this.getPath(), this.getPostbody());
this.setData(data);
}
if (childrenChanged) {
// Update all of the children as well
await Promise.all(this.getSubs().map(sub => sub.update()));
}
}
this.send('update');
}
/**
* Deletes the item from canvas
* @async
* @param {function} [callback] If not specified, returns a promise
*/
async delete(query = undefined, callback = undefined) {
if (typeof query == 'function') {
callback = query;
query = undefined;
}
if (callback) {
return util.callbackify(this.delete.bind(this))(...arguments);
}
var data = await canvas.delete(this.getPath(), query);
this.setData(data);
this.send('delete');
}
/**
* Creates the item in canvas, with whatever properties it contains
* @async
* @private - Use Items.create instead
*/
async create() {
var data = await canvas.post(this.getPath(false), this.getPostbody());
this.setData(data);
// update our children so they know their parent's id
this.getSubs().forEach(sub => {
if (sub.ids[sub.ids.length - 1] != this._id) {
sub.ids.push(this._id);
}
});
this.send('create');
return this;
}
};