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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596 | 1
9
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
| var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) Eif (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
(function (factory) {
Eif (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports", './DisplayObjectContainer', '../event/BaseEvent', '../util/TemplateFactory', '../util/ComponentFactory', '../plugin/jquery.eventListener'], factory);
}
})(function (require, exports) {
var DisplayObjectContainer_1 = require('./DisplayObjectContainer');
var BaseEvent_1 = require('../event/BaseEvent');
var TemplateFactory_1 = require('../util/TemplateFactory');
var ComponentFactory_1 = require('../util/ComponentFactory');
var jquery_eventListener_1 = require('../plugin/jquery.eventListener');
/**
* The {{#crossLink "DOMElement"}}{{/crossLink}} class is the base view class for all objects that can be placed into the HTML DOM.
*
* @class DOMElement
* @param type [any=null] Either a jQuery object or JavaScript template string reference you want to use as the view. Check out the examples below.
* @param params [any=null] Any data you would like to pass into the jQuery element or template that is being created.
* @extends DisplayObjectContainer
* @module StructureJS
* @submodule view
* @requires Extend
* @requires DisplayObjectContainer
* @requires BaseEvent
* @requires TemplateFactory
* @requires ComponentFactory
* @requires jQuery
* @constructor
* @author Robert S. (www.codeBelt.com)
* @example
* // Example: Using DOMElement without extending it.
* let aLink = new DOMElement('a', {text: 'Google', href: 'http://www.google.com', 'class': 'externalLink'});
* this.addChild(aLink);
*
* // Example: A view passing in a jQuery object.
* let view = new CustomView($('.selector'));
* this.addChild(view);
*
* // Example: A view extending DOMElement while passing in a jQuery object.
* class ClassName extends DOMElement {
*
* constructor($element) {
* super($element);
* }
*
* create() {
* super.create();
*
* // Create and add your child objects to this parent class.
* }
*
* onEnabled() {
* // Enable the child objects and add any event listeners.
* }
*
* onDisabled() {
* // Disable the child objects and remove any event listeners.
* }
*
* layout() {
* // Layout or update the child objects in this parent class.
* }
*
* destroy() {
* this.disable();
*
* // Destroy the child objects and references in this parent class to prevent memory leaks.
*
* super.destroy();
* }
*
* }
*
* // Example: A view extending DOMElement with a precompiled JavaScript template reference passed in.
* class ClassName extends DOMElement {
*
* constructor() {
* _super();
* }
*
* create() {
* super.create('templates/home/homeTemplate', {data: 'some data'});
*
* // Create and add your child objects to this parent class.
* }
*
* onEnabled() {
* // Enable the child objects and add any event listeners.
* }
*
* onDisabled() {
* // Disable the child objects and remove any event listeners.
* }
*
* layout() {
* // Layout or update the child objects in this parent class.
* }
*
* destroy() {
* this.disable();
*
* // Destroy the child objects and references in this parent class to prepare for garbage collection.
*
* super.destroy();
* }
*
* }
*/
var DOMElement = (function (_super) {
__extends(DOMElement, _super);
function DOMElement(type, params) {
Eif (type === void 0) { type = null; }
Eif (params === void 0) { params = null; }
_super.call(this);
/**
* Tracks number of times an element's width has been checked
* in order to determine if the element has been added
* to the DOM.
*
* @property checkCount
* @type {number}
* @public
*/
this.checkCount = 0;
/**
* A cached reference to the DOM Element
*
* @property element
* @type {HTMLElement}
* @default null
* @public
*/
this.element = null;
/**
* A cached reference to the jQuery DOM element
*
* @property $element
* @type {JQuery}
* @default null
* @public
*/
this.$element = null;
/**
* If a jQuery object was passed into the constructor this will be set as true and
* this class will not try to add the view to the DOM since it already exists.
*
* @property _isReference
* @type {boolean}
* @protected
*/
this._isReference = false;
/**
* Holds onto the value passed into the constructor.
*
* @property _type
* @type {string}
* @default null
* @protected
*/
this._type = null;
/**
* Holds onto the value passed into the constructor.
*
* @property _params
* @type {any}
* @default null
* @protected
*/
this._params = null;
Iif (type instanceof jquery_eventListener_1.default) {
this.$element = type;
this.element = this.$element[0];
this._isReference = true;
}
else Iif (type) {
this._type = type;
this._params = params;
}
}
/**
* The create function is intended to provide a consistent place for the creation and adding
* of children to the view. It will automatically be called the first time that the view is added
* to another DisplayObjectContainer. It is critical that all subclasses call the super for this function in
* their overridden methods.
*
* This method gets called once when the child view is added to another view. If the child view is removed
* and added to another view the create method will not be called again.
*
* @method create
* @param type [string=div] The HTML tag you want to create or the id/class selector of the template or the pre-compiled path to a template.
* @param params [any=null] Any data you would like to pass into the jQuery element or template that is being created.
* @returns {any} Returns an instance of itself.
* @public
* @chainable
* @example
* // EXAMPLE 1: By default your view class will be a div element:
* create() {
* super.create();
*
* this._childInstance = new DOMElement();
* this.addChild(this._childInstance);
* }
*
* // EXAMPLE 2: But lets say you wanted the view to be a ul element:
* create() {
* super.create('ul');
* }
*
* // Then you could nest other elements inside this base view/element.
* create() {
* super.create('ul', {id: 'myId', 'class': 'myClass anotherClass'});
*
* let li = new DOMElement('li', {text: 'Robert is cool'});
* this.addChild(li);
* }
*
* // EXAMPLE 3: So that's cool but what if you wanted a block of html to be your view. Let's say you had the below
* // inline Handlebar template in your html file.
* <script id="todoTemplate" type="text/template">
* <div id="htmlTemplate" class="js-todo">
* <div id="input-wrapper">
* <input type="text" class="list-input" placeholder="{{ data.text }}">
* <input type="button" class="list-item-submit" value="Add">
* </div>
* </div>
* </script>
*
* // You would just pass in the id or class selector of the template which in this case is "#todoTemplate".
* // There is a second optional argument where you can pass data for the Handlebar template to use.
* create() {
* super.create('#todoTemplate', { data: this.viewData });
*
* }
*
* // EXAMPLE 4: Or maybe you're using grunt-contrib-handlebars, or similar, to precompile hbs templates
* create() {
* super.create('templates/HomeTemplate', {data: "some data"});
*
* }
*/
DOMElement.prototype.create = function (type, params) {
if (type === void 0) { type = 'div'; }
if (params === void 0) { params = null; }
// Use the data passed into the constructor first else use the arguments from create.
type = this._type || type;
params = this._params || params;
if (this.isCreated === true) {
throw new Error('[' + this.getQualifiedClassName() + '] You cannot call the create method manually. It is only called once automatically during the view lifecycle and should only be called once.');
}
if (this.$element == null) {
var html_1 = TemplateFactory_1.default.create(type, params);
if (html_1) {
this.$element = jquery_eventListener_1.default(html_1);
}
else {
this.$element = jquery_eventListener_1.default("<" + type + "/>", params);
}
}
this.element = this.$element[0];
this.width = this.$element.width();
this.height = this.$element.height();
this.setSize(this.width, this.height);
return this;
};
/**
* @overridden DisplayObjectContainer.addChild
* @method addChild
* @param child {DOMElement} The DOMElement instance to add as a child of this object instance.
* @returns {any} Returns an instance of itself.
* @chainable
* @example
* this.addChild(domElementInstance);
*/
DOMElement.prototype.addChild = function (child) {
if (this.$element == null) {
throw new Error('[' + this.getQualifiedClassName() + '] You cannot use the addChild method if the parent object is not added to the DOM.');
}
_super.prototype.addChild.call(this, child);
// If an empty jQuery object is passed into the constructor then don't run the code below.
if (child._isReference === true && child.$element.length === 0) {
return this;
}
if (child.isCreated === false) {
child.create(); // Render the item before adding to the DOM
child.isCreated = true;
}
// If the child object is not a reference of a jQuery object in the DOM then append it.
if (child._isReference === false) {
this.$element.append(child.$element);
}
this._onAddedToDom(child);
return this;
};
/**
* Adds the sjsId to the DOM element so we can know what what Class object the HTMLElement belongs too.
*
* @method _addClientSideId
* @param child {DOMElement} The DOMElement instance to add the sjsId too.
* @protected
*/
DOMElement.prototype._addClientSideId = function (child) {
var type = child.$element.attr('data-sjs-type');
var id = child.$element.attr('data-sjs-id');
Eif (type === void 0) {
// Make them array's so the join method will work.
type = [child.getQualifiedClassName()];
id = [child.sjsId];
}
else {
// Split them so we can push/add the new values.
type = type.split(',');
id = id.split(',');
type.push(child.getQualifiedClassName());
id.push(child.sjsId);
}
// Updated list of id's and types
child.$element.attr('data-sjs-id', id.join(','));
child.$element.attr('data-sjs-type', type.join(','));
};
/**
* Removes the sjsId and class type from the HTMLElement.
*
* @method _removeClientSideId
* @param child {DOMElement} The DOMElement instance to add the sjsId too.
* @protected
* @return {boolean}
*/
DOMElement.prototype._removeClientSideId = function (child) {
var type = child.$element.attr('data-sjs-type');
var id = child.$element.attr('data-sjs-id');
// Split them so we can remove the child sjsId and type.
var typeList = type.split(',');
var idList = id.split(',').map(Number); // Convert each item into a number.
var index = idList.indexOf(child.sjsId);
if (index > -1) {
// Remove the id and type from the array.
typeList.splice(index, 1);
idList.splice(index, 1);
// Updated list of id's and types
child.$element.attr('data-sjs-type', typeList.join(','));
child.$element.attr('data-sjs-id', idList.join(','));
}
return idList.length === 0;
};
/**
* Called when the child object is added to the DOM.
* The method will call {{#crossLink "DOMElement/layout:method"}}{{/crossLink}} and dispatch the BaseEvent.ADDED_TO_STAGE event.
*
* @method _onAddedToDom
* @protected
*/
DOMElement.prototype._onAddedToDom = function (child) {
var _this = this;
child.checkCount++;
if (child.$element.width() === 0 && child.checkCount < 5) {
setTimeout(function () {
_this._onAddedToDom(child);
}, 100);
return;
}
this._addClientSideId(child);
child.width = child.$element.width();
child.height = child.$element.height();
child.setSize(child.width, child.height);
child.enable();
child.layout();
child.dispatchEvent(new BaseEvent_1.default(BaseEvent_1.default.ADDED_TO_STAGE));
};
/**
* @overridden DisplayObjectContainer.addChildAt
*/
DOMElement.prototype.addChildAt = function (child, index) {
var children = this.$element.children();
var length = children.length;
// If an empty jQuery object is passed into the constructor then don't run the code below.
if (child._isReference === true && child.$element.length === 0) {
return this;
}
if (index < 0 || index >= length) {
// If the index passed in is less than 0 and greater than the total number of children then place the item at the end.
this.addChild(child);
}
else {
// Else get the child in the children array by the index passed in and place the item before that child.
if (child.isCreated === false) {
child.create(); // Render the item before adding to the DOM
child.isCreated = true;
}
// Adds the child at a specific index but also will remove the child from another parent object if one exists.
if (child.parent) {
child.parent.removeChild(child, false);
}
this.children.splice(index, 0, child);
this.numChildren = this.children.length;
child.parent = this;
// Adds the child before any child already added in the DOM.
jquery_eventListener_1.default(children.get(index)).before(child.$element);
this._onAddedToDom(child);
}
return this;
};
/**
* @overridden DisplayObjectContainer.swapChildren
*/
DOMElement.prototype.swapChildren = function (child1, child2) {
var child1Index = child1.$element.index();
var child2Index = child2.$element.index();
this.addChildAt(child1, child2Index);
this.addChildAt(child2, child1Index);
return this;
};
/**
* @overridden DisplayObjectContainer.getChildAt
*/
DOMElement.prototype.getChildAt = function (index) {
return _super.prototype.getChildAt.call(this, index);
};
/**
* Returns a DOMElement object with the first found DOM element by the passed in selector.
*
* @method getChild
* @param selector {string} DOM id name, DOM class name or a DOM tag name.
* @returns {DOMElement}
* @public
*/
DOMElement.prototype.getChild = function (selector) {
// Get the first match from the selector passed in.
var jQueryElement = this.$element.find(selector).first();
if (jQueryElement.length === 0) {
throw new TypeError('[' + this.getQualifiedClassName() + '] getChild(' + selector + ') Cannot find DOM $element');
}
// Check to see if the element has a sjsId value and is a child of this parent object.
var sjsId = parseInt(jQueryElement.attr('data-sjs-id'));
var domElement = this.getChildByCid(sjsId);
// Creates a DOMElement from the jQueryElement.
if (domElement == null) {
// Create a new DOMElement and assign the jQuery element to it.
domElement = new DOMElement();
domElement.$element = jQueryElement;
this._addClientSideId(domElement);
domElement.element = jQueryElement[0];
domElement.isCreated = true;
// Added to the super addChild method because we don't need to append the element to the DOM.
// At this point it already exists and we are just getting a reference to the DOM element.
_super.prototype.addChild.call(this, domElement);
}
return domElement;
};
/**
* Gets all the HTML elements children of this object.
*
* @method getChildren
* @param [selector] {string} You can pass in any type of jQuery selector. If there is no selector passed in it will get all the children of this parent element.
* @returns {Array.<DOMElement>} Returns a list of DOMElement's. It will grab all children HTML DOM elements of this object and will create a DOMElement for each DOM child.
* If the 'data-sjs-id' property exists is on an HTML element a DOMElement will not be created for that element because it will be assumed it already exists as a DOMElement.
* @public
*/
DOMElement.prototype.getChildren = function (selector) {
if (selector === void 0) { selector = ''; }
//TODO: Make sure the index of the children added is the same as the what is in the actual DOM.
var $child;
var domElement;
var $list = this.$element.children(selector);
var listLength = $list.length;
for (var i_1 = 0; i_1 < listLength; i_1++) {
$child = $list.eq(i_1);
// If the jQuery element already has sjsId data property then it must be an existing DisplayObjectContainer (DOMElement) in the children array.
if ($child.attr('data-sjs-id') === void 0) {
domElement = new DOMElement();
domElement.$element = $child;
this._addClientSideId(domElement);
domElement.element = $child.get(0);
domElement.isCreated = true;
// Added to the super addChild method because we don't need to append the element to the DOM.
// At this point it already exists and we are just getting a reference to the DOM element.
_super.prototype.addChild.call(this, domElement);
}
}
return this.children;
};
/**
* Removes the specified child object instance from the child list of the parent object instance.
* The parent property of the removed child is set to null and the object is garbage collected if there are no other references
* to the child. The index positions of any objects above the child in the parent object are decreased by 1.
*
* @method removeChild
* @param child {DOMElement} The DisplayObjectContainer instance to remove.
* @returns {any} Returns an instance of itself.
* @override
* @public
* @chainable
*/
DOMElement.prototype.removeChild = function (child, destroy) {
if (destroy === void 0) { destroy = true; }
var remove = this._removeClientSideId(child);
child.disable();
// Checks if destroy was called before removeChild so it doesn't error.
if (remove === true && child.$element != null) {
child.$element.unbind();
child.$element.remove();
}
if (destroy === true) {
child.destroy();
}
_super.prototype.removeChild.call(this, child);
return this;
};
/**
* Removes the child display object instance that exists at the specified index.
*
* @method removeChildAt
* @param index {int} The index position of the child object.
* @public
* @chainable
*/
DOMElement.prototype.removeChildAt = function (index, destroy) {
if (destroy === void 0) { destroy = true; }
this.removeChild(this.getChildAt(index), destroy);
return this;
};
/**
* Removes all child object instances from the child list of the parent object instance.
* The parent property of the removed children is set to null and the objects are garbage collected if no other
* references to the children exist.
*
* @method removeChildren
* @returns {DOMElement} Returns an instance of itself.
* @override
* @public
* @chainable
*/
DOMElement.prototype.removeChildren = function (destroy) {
if (destroy === void 0) { destroy = true; }
while (this.children.length > 0) {
this.removeChild(this.children.pop(), destroy);
}
this.$element.empty();
return this;
};
/**
* @overridden DisplayObjectContainer.destroy
*/
DOMElement.prototype.destroy = function () {
// Note: we can't just call destroy to remove the HTMLElement because there could be other views managing the same HTMLElement.
/*if (this.$element != null) {
this.$element.unbind();
this.$element.remove();
}*/
_super.prototype.destroy.call(this);
};
/**
* A way to instantiate view classes by found html selectors.
*
* Example: It will find all children elements of the {{#crossLink "DOMElement/$element:property"}}{{/crossLink}} property with the 'js-shareEmail' selector.
* If any selectors are found the EmailShareComponent class will be instantiated and pass the found jQuery element into the contructor.
*
* @method createComponents
* @param componentList (Array.<{ selector: string; component: DOMElement }>
* @return {Array.<DOMElement>} Returns all the items created from this createComponents method.
* @public
* @chainable
* @example
* create() {
* super.create();
*
* this.createComponents([
* {selector: '.js-shareEmail', component: EmailShareComponent},
* {selector: '.js-pagination', component: PaginationComponent},
* {selector: '.js-carousel', component: CarouselComponent}
* ]);
* }
*/
DOMElement.prototype.createComponents = function (componentList) {
var list;
var createdChildren = [];
var length = componentList.length;
var obj;
for (var i_2 = 0; i_2 < length; i_2++) {
obj = componentList[i_2];
list = ComponentFactory_1.default.create(this.$element.find(obj.selector), obj.component, this);
createdChildren = createdChildren.concat(list);
}
return createdChildren;
};
return DOMElement;
})(DisplayObjectContainer_1.default);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = DOMElement;
});
|