-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItemsClass.js
More file actions
269 lines (260 loc) · 8.2 KB
/
ItemsClass.js
File metadata and controls
269 lines (260 loc) · 8.2 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
const util = require('util');
const canvas = require('./canvas');
const parse = require('./parse');
/**
* An abstract class which acts as a container for the different types of items
* @public @prop {array} ids - list of ids starting at courseId going to the leaf id
* @private @abstract @prop {Class} childClass - the class used for the children
*/
module.exports = class Items extends Array {
/**
* Not really sure what this line does,
* but it makes things not mess up as badly when doing slice and such
* ( doing slice on this class returns just the array of sub items without this class wrapping it )
*/
static get[Symbol.species]() {
return Array;
}
/**
* Coerce to Array when console.logging
*/
[util.inspect.custom](depth, options) {
return this.constructor.name + ' ' + util.inspect(Array.from(this), options);
}
/**
* Attaches the delete event listener, so that we can remove it from the list if it
* get removed independently
* @param {Item} item
*/
_attachListeners(item) {
item.on('delete', item => {
var foundIndex = this.findIndex(n => n.getId() == item.getId());
if (foundIndex != -1) {
this.splice(foundIndex, 1);
}
});
}
/**
* Constructs an instance of the child class
* @private
* @param {number} id
* @return {Item}
*/
_constructItem(id) {
if (!this.childClass) {
throw new TypeError('Classes extending the Items class needs childClass defined');
}
var item = new this.childClass(this.ids, id);
this._attachListeners(item);
return item;
}
/**
* Creates an instance of the child class and assigns it the data passed
* @private
* @param {Object} data
* @return {Item}
*/
_classify(data) {
var item = this._constructItem();
item.setData(data);
return item;
}
/**
* Need to add this function so that functions in the api don't screw things up
* @private
* @param {array} data - array of items to add
*/
setData(data) {
data.forEach(datum => {
var item = this._classify(datum);
var existing = this.find(n => n.getId() == item.getId());
if (existing) {
existing.setData(datum);
} else {
this.push(item);
}
});
}
/**
* Any of the childern have changed
*/
hasChanged() {
return this.some(item => {
var [thisChanged, childrenChanged] = item.getChanged();
return thisChanged || childrenChanged;
});
}
/** @return {string} - the class name */
getType() {
return this.constructor.name;
}
/** @return {[string]} - list of parent ids [ to match the Item.getIds() ] */
getIds() {
return this.ids;
}
/**
* Returns a flat list of all children's children
*/
getFlattened(depth = 1) {
return this.reduce((arr, child) => arr.concat(child.getFlattened(depth)), []);
}
/**
* Updates all of the items
* @async
* @param {Function} [callback] - If not specified, returns a promise
*/
async update(alternateBody = undefined, callback = undefined) {
if (callback) {
return util.callbackify(this.update.bind(this))(...arguments);
}
await Promise.all(this.map(item => item.update(alternateBody)));
}
/**
* Creates an Item
* @async
* @param {Object} data - The properties used to create the item
* @param {Function} [callback] - If not specified, returns a promise
*/
async create(data = {}, callback = undefined) {
if (callback) {
return util.callbackify(this.create.bind(this))(...arguments);
}
var item = this._constructItem();
data = parse(data);
data = data[item._post] || data;
item.setData(data);
await item.create();
this.push(item);
return item;
}
/**
* Retrieves all of the items
* @async
* @param {function} [callback] - If not specified, returns a promise
*/
async get(query = undefined, callback = undefined) {
if (typeof query == 'function') {
callback = query;
query = undefined;
}
if (callback) {
return util.callbackify(this.get.bind(this))(...arguments);
}
var data = await canvas(this._constructItem().getPath(false), query);
data.forEach(datum => {
var item = this._classify(datum);
var existing = this.find(n => n.getId() == item.getId());
if (existing) {
existing.setData(datum);
} else {
this.push(item);
}
});
return this;
}
/**
* Retrieves all of the items with their sub items
* @async
* @param {function} [callback] - If not specified, returns a promise
*/
async getComplete(query = undefined, callback = undefined) {
if (typeof query == 'function') {
callback = query;
query = undefined;
}
if (callback) {
return util.callbackify(this.getComplete.bind(this))(...arguments);
}
await this.get(query);
await Promise.all(this.map(item => item.getSub()));
return this;
}
/**
* Retrieves a single item from canvas
* @async
* @param {number} id - The id of the item to get
* @param {function} [callback] - If not specified, returns a promise
*/
async getOne(id, query = undefined, callback = undefined) {
// Backwards compatability
if (typeof query == 'boolean') {
if (query == true) {
return this.getOneComplete(id, callback);
}
query = undefined;
} else if (typeof query == 'function') {
callback = query;
query = undefined;
}
if (callback) {
return util.callbackify(this.getOne.bind(this))(...arguments);
}
var existing = this.find(n => n.getId() == id);
if (existing) {
await existing.get(query);
return existing;
} else {
var item = this._constructItem(id);
await item.get(query);
this.push(item);
return item;
}
}
async getOneComplete(id, query = undefined, callback = undefined) {
if (typeof query == 'function') {
callback = query;
query = undefined;
}
if (callback) {
return util.callbackify(this.getOneComplete.bind(this))(...arguments);
}
var item = await this.getOne(id, query);
await item.getSub();
return item;
}
/**
* Removes an item from canvas, and from the local list
* @async
* @param {number} id - The id of the item to delete
* @param {function} [callback] If not specified, returns a promise
*/
async delete(id, query = undefined, callback = undefined) {
if (typeof query == 'function') {
callback = query;
query = undefined;
}
if (callback) {
return util.callbackify(this.delete.bind(this))(...arguments);
}
var foundIndex = this.findIndex(n => n.getId() == id);
if (foundIndex != -1) {
await this[foundIndex].delete(query);
// Might already be removed because of the event listeners
foundIndex = this.findIndex(n => n.getId() == id);
if (foundIndex != -1) {
this.splice(foundIndex, 1);
}
} else {
var temp = this._constructItem(id);
await temp.delete(query);
}
}
/* Backwards Compatability Aliases */
async getAll(includeSub = false, callback = undefined) {
if (typeof includeSub == 'function') {
callback = includeSub;
includeSub = false;
}
if (callback) {
return util.callbackify(this.getAll.bind(this))(...arguments);
}
if (includeSub) {
return this.getComplete();
} else {
return this.get();
}
}
async updateAll(callback) {
return this.update(callback);
}
};