-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructors.js
581 lines (526 loc) · 16.6 KB
/
constructors.js
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
// -
// --
// --- This is the main prototype
// --
// -
function CustomMethods(obj) {
for (var attr in obj) {
if (!obj.hasOwnProperty(attr)) {
continue;
}
this[attr] = obj[attr];
switch (attr) {
case 'trgt': {
this.divObj.setAttribute('target', this.trgt);
break;
}
case 'hrf': {
this.divObj.setAttribute('href', this.hrf);
break;
}
case 'clss': {
this.divObj.setAttribute('class', this.clss);
break;
}
case 'style': {
this.divObj.setAttribute('style', this.style);
break;
}
case 'identity': {
this.divObj.setAttribute('id', this.identity);
this.divObj.setAttribute('name', this.identity);
break;
}
case 'txt': {
this.divObj.innerText = this.txt;
break;
}
case 'clck': {
this.divObj.addEventListener('click', this.clck);
break;
}
case 'chnge': {
this.divObj.addEventListener('change', this.chnge);
break;
}
case 'forInp': {
this.divObj.setAttribute('for', this.forInp);
break;
}
default: { continue; }
}
}
if (this.appendTo) {
this.appendSelf(this.appendTo);
this.setParent(this.appendTo);
}
}
CustomMethods.prototype.chngID = function(ID) {
this.identity = ID;
this.divObj.setAttribute('id', this.identity);
};
CustomMethods.prototype.setStyle = function(styleObj) {
var styl = '';
for (var attr in styleObj) {
if (!styleObj.hasOwnProperty(attr)) {
continue;
}
styl += attr + ': ' + styleObj[attr] + ';';
}
this.divObj.setAttribute('style', styl);
};
CustomMethods.prototype.changeClass = function(newClass) {
this.clss = newClass;
this.divObj.setAttribute('class', newClass);
};
CustomMethods.prototype.append = function(variable) {
if (variable instanceof HTMLElement) this.divObj.appendChild(variable);
else this.divObj.appendChild(variable.divObj);
};
CustomMethods.prototype.remove = function(variable) {
this.divObj.removeChild(variable.divObj);
};
CustomMethods.prototype.setAttr = function(attr, val, obj) {
if (typeof attr === 'object') {
for (var key in attr) {
if (!attr.hasOwnProperty(key)) continue;
this[key] = attr[key];
this.divObj.setAttribute(key, attr[key]);
}
} else {
this[attr] = val;
if (!obj) this.divObj.setAttribute(attr, val);
}
};
CustomMethods.prototype.removeAttr = function(attr, obj) {
this[attr] = null;
if (!obj) this.divObj.removeAttribute(attr);
};
CustomMethods.prototype.val = function(value) {
if (value) {
this.divObj.value = value;
this.value = value;
} else if (this.divObj.type === 'number') {
return parseFloat(this.divObj.value);
} else {
return this.divObj.value;
}
};
CustomMethods.prototype.appendSelf = function(target) {
if (target instanceof HTMLElement) {
target.appendChild(this.divObj);
} else {
target.append(this);
}
};
CustomMethods.prototype.removeSelf = function() {
if (this.divObj.parentNode) {
this.divObj.parentNode.removeChild(this.divObj);
} else {
console.log('The element below is not on the DOM: ');
console.log(this.divObj);
}
};
CustomMethods.prototype.css = function(cssChanges) {
if (!cssChanges) {
this.divObj.removeAttribute('style');
return;
}
for (var key in cssChanges) {
if (!cssChanges.hasOwnProperty(key)) continue;
this.divObj.style[key] = cssChanges[key];
}
};
CustomMethods.prototype.setParent = function(target) {
if (typeof target === 'object') {
this.parentObj = target;
this.divObj.parentObj = target;
} else {
throw new TypeError('The target is not an object. You can set references with setAttr()');
}
};
CustomMethods.prototype.addClass = function(clss) {
this.divObj.className += ' ' + clss;
};
function KYM_Button(obj) {
this.divObj = document.createElement('button');
CustomMethods.call(this, obj);
if (this.type) this.divObj.setAttribute('type', this.type);
}
KYM_Button.prototype = Object.create(CustomMethods.prototype);
KYM_Button.prototype.constructor = KYM_Button;
function DivElem(obj) {
if (obj.type) {
this.divObj = document.createElement(obj.type);
} else {
this.divObj = document.createElement('div');
}
CustomMethods.call(this, obj);
}
DivElem.prototype = Object.create(CustomMethods.prototype);
DivElem.prototype.val = function(txt) {
if (txt) {
this.divObj.innerText = txt;
} else {
return this.divObj.innerText;
}
};
DivElem.prototype.constructor = DivElem;
function LinkElem(obj) {
this.hrf = '#';
this.divObj = document.createElement('a');
CustomMethods.call(this, obj);
}
LinkElem.prototype = Object.create(CustomMethods.prototype);
LinkElem.prototype.constructor = LinkElem;
function JSelection(obj) {
for (var attr in obj) {
if (!obj.hasOwnProperty(attr)) {
continue;
}
this[attr] = obj[attr];
}
this.divObj = document.createElement('select');
// if (this.identity) this.divObj.setAttribute('id', this.identity);
// if (this.identity) this.divObj.setAttribute('name', this.identity);
// if (this.clss) this.divObj.setAttribute('class', this.clss);
// if (this.style) this.divObj.setAttribute('style', this.style);
// if (this.type) this.divObj.setAttribute('type', this.type);
// if (this.chnge) this.divObj.addEventListener('change', this.chnge);
CustomMethods.call(this, obj);
var i = 0;
for (i = 0;i < this.options.length;i++) {
if (this.options[i].val) {
var option = document.createElement('option');
option.setAttribute('value', this.options[i].val);
var optTxt = document.createTextNode(this.options[i].txt);
option.appendChild(optTxt);
this.divObj.appendChild(option);
if (this.options[i].sel) {
option.setAttribute('selected', true);
}
} else if (this.options[i].sel) {
option = document.createElement('option');
option.setAttribute('value', this.options[i].txt);
optTxt = document.createTextNode(this.options[i].txt);
option.appendChild(optTxt);
this.divObj.appendChild(option);
if (this.options[i].sel) {
option.setAttribute('selected', true);
}
} else {
option = document.createElement('option');
option.setAttribute('value', this.options[i]);
optTxt = document.createTextNode(this.options[i]);
option.appendChild(optTxt);
this.divObj.appendChild(option);
}
}
if (this.value) {
this.divObj.value = this.value;
}
if (this.appendTo) {
this.appendSelf(this.appendTo);
this.setParent(this.appendTo);
}
}
JSelection.prototype = Object.create(CustomMethods.prototype);
JSelection.prototype.removeOption = function(index) {
this.divObj.removeChild(this.divObj.childNodes[index]);
};
JSelection.prototype.getTxt = function() {
var selVal = this.val();
for (var i = 0; i < this.options.length; i++) {
if (this.options[i].val === selVal) {
return this.options[i].txt;
}
}
};
JSelection.prototype.constructor = JSelection;
function NumInput(obj) {
this.divObj = document.createElement('input');
this.divObj.setAttribute('type', 'number');
CustomMethods.call(this, obj);
if (this.identity) {
this.divObj.setAttribute('id', this.identity);
this.divObj.setAttribute('name', this.identity);
}
if (this.min) this.divObj.setAttribute('min', parseFloat(this.min));
if (this.max) this.divObj.setAttribute('max', parseFloat(this.max));
if (this.step) this.divObj.setAttribute('step', this.step);
if (this.clss) this.divObj.setAttribute('class', this.clss);
if (this.chnge) this.divObj.addEventListener('change', this.chnge);
if (this.style) this.divObj.setAttribute('style', this.style);
if (this.placeholder) this.divObj.setAttribute('placeholder', this.placeholder);
if (this.appendTo) {
this.appendSelf(this.appendTo);
this.setParent(this.appendTo);
}
}
NumInput.prototype = Object.create(CustomMethods.prototype);
NumInput.prototype.constructor = NumInput;
function TxtInput(obj) {
for (var attr in obj) {
if (!obj.hasOwnProperty(attr)) {
continue;
}
this[attr] = obj[attr];
}
this.divObj = document.createElement('input');
this.divObj.setAttribute('type', 'text');
if (this.identity) {
this.divObj.setAttribute('id', this.identity);
this.divObj.setAttribute('name', this.identity);
}
if (this.clss) this.divObj.setAttribute('class', this.clss);
if (this.chnge) this.divObj.addEventListener('change', this.chnge);
if (this.placeholder) this.divObj.setAttribute('placeholder', this.placeholder);
if (this.value) this.divObj.setAttribute('value', this.value);
if (this.appendTo) {
this.appendSelf(this.appendTo);
this.setParent(this.appendTo);
}
}
TxtInput.prototype = Object.create(CustomMethods.prototype);
NumInput.prototype.constructor = TxtInput;
function KYM_Text(obj) {
for (var attr in obj) {
if (!obj.hasOwnProperty(attr)) {
continue;
}
this[attr] = obj[attr];
}
this.divObj = document.createTextNode(this.txt);
if (this.appendTo) {
this.appendSelf(this.appendTo);
}
}
KYM_Text.prototype = Object.create(CustomMethods.prototype);
KYM_Text.prototype.val = function(txt) {
var parent = this.divObj.parentNode;
var tmp = document.createTextNode(txt);
parent.replaceChild(tmp, this.divObj);
this.divObj = tmp;
};
KYM_Text.prototype.constructor = KYM_Text;
function Label(obj) {
this.divObj = document.createElement('label');
CustomMethods.call(this, obj);
}
Label.prototype = Object.create(CustomMethods.prototype);
Label.prototype.constructor = Label;
// Object should include the following parameters:
// - clss
// - title
// - bodyContent
// - footerContent
function Panel(obj) {
this.clss = 'default';
for (var attr in obj) {
if (!obj.hasOwnProperty(attr)) {
continue;
}
this[attr] = obj[attr];
}
this.thePanel = new DivElem({clss: 'panel panel-' + this.clss});
this.divObj = this.thePanel.divObj;
this.heading = new DivElem({appendTo: this.thePanel, clss: 'panel-heading'});
if (this.panelBody != false) this.panelBody = new DivElem({appendTo: this.thePanel, clss: 'panel-body'});
this.footer = new DivElem({appendTo: this.thePanel, clss: 'panel-footer'});
if (this.title) {
var h4 = new DivElem({appendTo: this.heading, type: 'h4', clss: 'panel-title'});
var title = new KYM_Text({appendTo: h4, txt: this.title});
}
if (this.bodyContent) {
if (typeof this.bodyContent === 'string') {
var bodyContent = new KYM_Text({appendTo: this.panelBody, txt: this.bodyContent});
}
if (typeof this.bodyContent === 'object') {
this.bodyContent.appendSelf(this.panelBody);
}
}
if (this.footerContent) {
if (typeof this.footerContent === 'string') {
var footerContent = new KYM_Text({appendTo: this.footer, txt: this.footerContent});
}
if (typeof this.footerContent === 'object') {
this.footerContent.appendSelf(this.footer);
}
}
if (this.appendTo) {
this.appendSelf(this.appendTo);
this.setParent(this.appendTo);
}
}
Panel.prototype = Object.create(CustomMethods.prototype);
Panel.prototype.constructor = Panel;
function Table(obj) {
this.clss = 'table table-condensed';
for (var attr in obj) {
if (!obj.hasOwnProperty(attr)) {
continue;
}
this[attr] = obj[attr];
}
this.divObj = document.createElement('table');
this.head = document.createElement('thead');
this.head.setAttribute('class', 'text-centered');
this.body = document.createElement('tbody');
this.caption = document.createElement('caption');
this.divObj.appendChild(this.head);
this.divObj.appendChild(this.body);
this.divObj.setAttribute('class', this.clss);
if (this.captionTxt) {
this.captionTxt = new KYM_Text({appendTo: this.caption, txt: this.captionTxt});
this.divObj.appendChild(this.caption);
}
if (this.heading) {
var tr = document.createElement('tr');
for (var i = 0; i < this.heading.length; i++) {
var th = document.createElement('th');
var txt = document.createTextNode(this.heading[i]);
th.appendChild(txt);
tr.appendChild(th);
}
this.head.appendChild(tr);
}
if (this.row) {
for (i = 0; i < this.row.length; i++) {
tr = document.createElement('tr');
for (var j = 0; j < this.row[i].length; j++) {
var td = document.createElement('td');
if (this.row[i][j] instanceof HTMLElement) {
td.appendChild(this.row[i][j]);
}
else if (typeof this.row[i][j] === 'string') {
this.row[i][j] = new KYM_Text({txt: this.row[i][j], appendTo: td});
}
else if (typeof this.row[i][j] === 'number') {
this.row[i][j] = new KYM_Text({txt: this.row[i][j], appendTo: td});
}
else if (typeof this.row[i][j] === 'object') {
td = document.createElement('td');
td.appendChild(this.row[i][j].divObj);
}
tr.appendChild(td);
}
this.body.appendChild(tr);
}
}
if (this.appendTo) {
this.appendSelf(this.appendTo);
this.setParent(this.appendTo);
}
}
Table.prototype = Object.create(CustomMethods.prototype);
Table.prototype.appendRow = function(rowArray, index) {
if (!index) {
this.row.push(rowArray);
var tr = document.createElement('tr');
for (var i = 0; i < rowArray.length; i++) {
var td = document.createElement('td');
if (rowArray[i] instanceof HTMLElement) {
td.appendChild(rowArray[i]);
} else if (typeof rowArray[i] === 'string' || typeof rowArray[i] === 'number') {
this.row[this.row.length - 1][i] = new KYM_Text({txt: rowArray[i], appendTo: td});
} else if (typeof rowArray[i] === 'object') {
td.appendChild(rowArray[i].divObj);
}
tr.appendChild(td);
}
this.body.appendChild(tr);
}
};
Table.prototype.appendHeading = function(headArray) {
if (this.heading) this.heading.push(headArray);
if (!this.heading) this.heading = headArray;
var tr = document.createElement('tr');
headArray.forEach(function(hd) {
var th = document.createElement('th');
if (hd instanceof HTMLElement) {
th.appendChild(hd);
} else if (typeof hd === 'string' || typeof hd === 'number') {
var txt = document.createTextNode(hd);
th.appendChild(txt);
} else if (typeof hd === 'object') {
th.appendChild(hd.divObj);
}
tr.appendChild(th);
});
this.head.appendChild(tr);
};
Table.prototype.changeCell = function(theRow, column, newThing) {
theRow = parseInt(theRow, 10);
column = parseInt(column, 10);
var theCell = this.divObj.tBodies[0].rows[theRow].children[column];
while (theCell.firstChild) theCell.removeChild(theCell.firstChild);
theCell.appendChild(newThing.divObj);
this.row[theRow][column] = newThing;
};
Table.prototype.cellAttribute = function(theRow, column, attribute, val) {
theRow = parseInt(theRow, 10);
column = parseInt(column, 10);
var theCell = this.divObj.tBodies[0].rows[theRow].children[column];
theCell.setAttribute(attribute, val);
};
Table.prototype.colAttribute = function(col, attr, val) {
var column = parseInt(col, 10);
for (var i = 0; i < this.row.length; i++) {
var theCell = this.divObj.tBodies[0].rows[i].children[column];
theCell.setAttribute(attr, val);
}
};
Table.prototype.constructor = Table;
function cleanOut(element, del) {
if (element instanceof HTMLElement) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
} else {
while (element.divObj.firstChild) {
element.divObj.removeChild(element.divObj.firstChild);
}
}
}
function Space(target) {
this.divObj = document.createTextNode(' ');
this.appendSelf(target);
}
Space.prototype = Object.create(CustomMethods.prototype);
Space.prototype.constructor = Space;
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
};
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseFloat((t2 - t1) / (24 * 3600 * 1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseFloat((t2 - t1) / (24 * 3600 * 1000));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M + 12 * d2Y) - (d1M + 12 * d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear() - d1.getFullYear();
}
};
function formatDate(date) {
var dt = new Date(date),
month = '' + (dt.getMonth() + 1),
day = '' + dt.getDate(),
year = dt.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}