-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathmodel.js
More file actions
549 lines (478 loc) · 11.2 KB
/
model.js
File metadata and controls
549 lines (478 loc) · 11.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/**
* Main object storing data model and emitting model events
* @constructor
*/
function Model() {
/**
* @member {Group}
* @readonly
*/
this.root = null;
/**
* Base for event emitting
* @member {jQuery}
* @readonly
* @private
*/
this.$ = $(this);
}
$.extend(Model.prototype, /** @lends Model.prototype */ {
/**
* Triggers an event on the model
* @param {string} type
* @returns {$.Event}
*/
trigger: function(type) {
var event = new $.Event(type);
this.$.triggerHandler(event, Array.prototype.slice.call(arguments, 1));
return event;
},
/**
* Attaches an event listener on the model
* @param {string} type
* @param {function} cb
* @returns {Model}
*/
on: function() {
this.$.on.apply(this.$, Array.prototype.slice.call(arguments));
return this;
},
/**
* Removes an event listener from the model
* @param {string} type
* @param {function} [cb]
* @returns {Model}
*/
off: function() {
this.$.off.apply(this.$, Array.prototype.slice.call(arguments));
return this;
},
/**
* Attaches an event listener called once on the model
* @param {string} type
* @param {function} cb
* @returns {Model}
*/
once: function() {
this.$.one.apply(this.$, Array.prototype.slice.call(arguments));
return this;
}
});
/**
* Root abstract object
* @constructor
* @param {Node} [parent]
* @param {jQuery} $el
*/
var Node = function(parent, $el) {
if (!(this instanceof Node)) {
return new Node(parent, $el);
}
Object.defineProperty(this, '__', { value: {} });
$el.data('queryBuilderModel', this);
/**
* @name level
* @member {int}
* @memberof Node
* @instance
* @readonly
*/
this.__.level = 1;
/**
* @name error
* @member {string}
* @memberof Node
* @instance
*/
this.__.error = null;
/**
* @name flags
* @member {object}
* @memberof Node
* @instance
* @readonly
*/
this.__.flags = {};
/**
* @name data
* @member {object}
* @memberof Node
* @instance
*/
this.__.data = undefined;
/**
* @member {jQuery}
* @readonly
*/
this.$el = $el;
/**
* @member {string}
* @readonly
*/
this.id = $el[0].id;
/**
* @member {Model}
* @readonly
*/
this.model = null;
/**
* @member {Group}
* @readonly
*/
this.parent = parent;
};
Utils.defineModelProperties(Node, ['level', 'error', 'data', 'flags']);
Object.defineProperty(Node.prototype, 'parent', {
enumerable: true,
get: function() {
return this.__.parent;
},
set: function(value) {
this.__.parent = value;
this.level = value === null ? 1 : value.level + 1;
this.model = value === null ? null : value.model;
}
});
/**
* Checks if this Node is the root
* @returns {boolean}
*/
Node.prototype.isRoot = function() {
return (this.level === 1);
};
/**
* Returns the node position inside its parent
* @returns {int}
*/
Node.prototype.getPos = function() {
if (this.isRoot()) {
return -1;
}
else {
return this.parent.getNodePos(this);
}
};
/**
* Deletes self
* @fires Model.model:drop
*/
Node.prototype.drop = function() {
var model = this.model;
if (!!this.parent) {
this.parent.removeNode(this);
}
this.$el.removeData('queryBuilderModel');
if (model !== null) {
/**
* After a node of the model has been removed
* @event model:drop
* @memberof Model
* @param {Node} node
*/
model.trigger('drop', this);
}
};
/**
* Moves itself after another Node
* @param {Node} target
* @fires Model.model:move
*/
Node.prototype.moveAfter = function(target) {
if (!this.isRoot()) {
this.move(target.parent, target.getPos() + 1);
}
};
/**
* Moves itself at the beginning of parent or another Group
* @param {Group} [target]
* @fires Model.model:move
*/
Node.prototype.moveAtBegin = function(target) {
if (!this.isRoot()) {
if (target === undefined) {
target = this.parent;
}
this.move(target, 0);
}
};
/**
* Moves itself at the end of parent or another Group
* @param {Group} [target]
* @fires Model.model:move
*/
Node.prototype.moveAtEnd = function(target) {
if (!this.isRoot()) {
if (target === undefined) {
target = this.parent;
}
this.move(target, target.length() === 0 ? 0 : target.length() - 1);
}
};
/**
* Moves itself at specific position of Group
* @param {Group} target
* @param {int} index
* @fires Model.model:move
*/
Node.prototype.move = function(target, index) {
if (!this.isRoot()) {
if (typeof target === 'number') {
index = target;
target = this.parent;
}
this.parent.removeNode(this);
target.insertNode(this, index, false);
if (this.model !== null) {
/**
* After a node of the model has been moved
* @event model:move
* @memberof Model
* @param {Node} node
* @param {Node} target
* @param {int} index
*/
this.model.trigger('move', this, target, index);
}
}
};
/**
* Group object
* @constructor
* @extends Node
* @param {Group} [parent]
* @param {jQuery} $el
*/
var Group = function(parent, $el) {
if (!(this instanceof Group)) {
return new Group(parent, $el);
}
Node.call(this, parent, $el);
/**
* @member {object[]}
* @readonly
*/
this.rules = [];
/**
* @name condition
* @member {string}
* @memberof Group
* @instance
*/
this.__.condition = null;
};
Group.prototype = Object.create(Node.prototype);
Group.prototype.constructor = Group;
Utils.defineModelProperties(Group, ['condition']);
/**
* Removes group's content
*/
Group.prototype.empty = function() {
this.each('reverse', function(rule) {
rule.drop();
}, function(group) {
group.drop();
});
};
/**
* Deletes self
*/
Group.prototype.drop = function() {
this.empty();
Node.prototype.drop.call(this);
};
/**
* Returns the number of children
* @returns {int}
*/
Group.prototype.length = function() {
return this.rules.length;
};
/**
* Adds a Node at specified index
* @param {Node} node
* @param {int} [index=end]
* @param {boolean} [trigger=false] - fire 'add' event
* @returns {Node} the inserted node
* @fires Model.model:add
*/
Group.prototype.insertNode = function(node, index, trigger) {
if (index === undefined) {
index = this.length();
}
this.rules.splice(index, 0, node);
node.parent = this;
if (trigger && this.model !== null) {
/**
* After a node of the model has been added
* @event model:add
* @memberof Model
* @param {Node} parent
* @param {Node} node
* @param {int} index
*/
this.model.trigger('add', this, node, index);
}
return node;
};
/**
* Adds a new Group at specified index
* @param {jQuery} $el
* @param {int} [index=end]
* @returns {Group}
* @fires Model.model:add
*/
Group.prototype.addGroup = function($el, index) {
return this.insertNode(new Group(this, $el), index, true);
};
/**
* Adds a new Rule at specified index
* @param {jQuery} $el
* @param {int} [index=end]
* @returns {Rule}
* @fires Model.model:add
*/
Group.prototype.addRule = function($el, index) {
return this.insertNode(new Rule(this, $el), index, true);
};
/**
* Deletes a specific Node
* @param {Node} node
*/
Group.prototype.removeNode = function(node) {
var index = this.getNodePos(node);
if (index !== -1) {
node.parent = null;
this.rules.splice(index, 1);
}
};
/**
* Returns the position of a child Node
* @param {Node} node
* @returns {int}
*/
Group.prototype.getNodePos = function(node) {
return this.rules.indexOf(node);
};
/**
* @callback Model#GroupIteratee
* @param {Node} node
* @returns {boolean} stop the iteration
*/
/**
* Iterate over all Nodes
* @param {boolean} [reverse=false] - iterate in reverse order, required if you delete nodes
* @param {Model#GroupIteratee} cbRule - callback for Rules (can be `null` but not omitted)
* @param {Model#GroupIteratee} [cbGroup] - callback for Groups
* @param {object} [context] - context for callbacks
* @returns {boolean} if the iteration has been stopped by a callback
*/
Group.prototype.each = function(reverse, cbRule, cbGroup, context) {
if (typeof reverse !== 'boolean' && typeof reverse !== 'string') {
context = cbGroup;
cbGroup = cbRule;
cbRule = reverse;
reverse = false;
}
context = context === undefined ? null : context;
var i = reverse ? this.rules.length - 1 : 0;
var l = reverse ? 0 : this.rules.length - 1;
var c = reverse ? -1 : 1;
var next = function() {
return reverse ? i >= l : i <= l;
};
var stop = false;
for (; next(); i += c) {
if (this.rules[i] instanceof Group) {
if (!!cbGroup) {
stop = cbGroup.call(context, this.rules[i]) === false;
}
}
else if (!!cbRule) {
stop = cbRule.call(context, this.rules[i]) === false;
}
if (stop) {
break;
}
}
return !stop;
};
/**
* Checks if the group contains a particular Node
* @param {Node} node
* @param {boolean} [recursive=false]
* @returns {boolean}
*/
Group.prototype.contains = function(node, recursive) {
if (this.getNodePos(node) !== -1) {
return true;
}
else if (!recursive) {
return false;
}
else {
// the loop will return with false as soon as the Node is found
return !this.each(function() {
return true;
}, function(group) {
return !group.contains(node, true);
});
}
};
/**
* Rule object
* @constructor
* @extends Node
* @param {Group} parent
* @param {jQuery} $el
*/
var Rule = function(parent, $el) {
if (!(this instanceof Rule)) {
return new Rule(parent, $el);
}
Node.call(this, parent, $el);
this._updating_value = false;
this._updating_input = false;
/**
* @name filter
* @member {QueryBuilder.Filter}
* @memberof Rule
* @instance
*/
this.__.filter = null;
/**
* @name operator
* @member {QueryBuilder.Operator}
* @memberof Rule
* @instance
*/
this.__.operator = null;
/**
* @name value
* @member {*}
* @memberof Rule
* @instance
*/
this.__.value = undefined;
};
Rule.prototype = Object.create(Node.prototype);
Rule.prototype.constructor = Rule;
Utils.defineModelProperties(Rule, ['filter', 'operator', 'value']);
/**
* Checks if this Node is the root
* @returns {boolean} always false
*/
Rule.prototype.isRoot = function() {
return false;
};
/**
* @member {function}
* @memberof QueryBuilder
* @see Group
*/
QueryBuilder.Group = Group;
/**
* @member {function}
* @memberof QueryBuilder
* @see Rule
*/
QueryBuilder.Rule = Rule;