-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSimpleTable.js
More file actions
1030 lines (926 loc) · 41.2 KB
/
SimpleTable.js
File metadata and controls
1030 lines (926 loc) · 41.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////
// Copyright © 2014 Esri. All Rights Reserved.
//
// Licensed under the Apache License Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////
define(['dojo/_base/declare',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',
'dojo/Evented',
'dojo/_base/lang',
'dojo/_base/html',
'dojo/_base/array',
'dojo/on',
'dojo/query',
'jimu/utils'
],
function (declare, _WidgetBase, _TemplatedMixin, Evented, lang, html, array, on, query,
jimuUtils) {
return declare([_WidgetBase, _TemplatedMixin, Evented], {
baseClass: 'jimu-simple-table',
declaredClass: 'jimu.dijit.SimpleTable',
templateString: '<div>' +
'<div class="head-section" data-dojo-attach-point="headDiv"></div>' +
'<div class="body-section" data-dojo-attach-point="bodyDiv">' +
'<div class="table-div" data-dojo-attach-point="tableDiv">' +
'<table class="table" cellspacing="0" onselectstart="return false;">' +
'<colgroup></colgroup>' +
'<thead class="simple-table-thead simple-table-title"></thead>' +
'<tbody class="simple-table-tbody"></tbody>' +
'</table>' +
'</div>' +
'</div>' +
'</div>',
_name: null,
_rowIndex: 0,
_rowHeight: 30,
_headHeight: 36,
REPEATING_ERROR: "REPEATING_ERROR",
_classSimpleTableRow: 'simple-table-row',
_classFirstSimpleTableRow: 'first-simple-table-row',
_classLastSimpleTableRow: 'last-simple-table-row',
_classJimuStateDisabled: 'jimu-state-disabled',
_classRowUpDiv: 'row-up-div',
_classRowDownDiv: 'row-down-div',
//options:
autoHeight: true, //if true, automatically calculate the height
selectable: false,
fields: null,
/*
//fieldInfo's attributes:
//name:field name
//title:field title
//type:text radio checkbox actions empty extension
//class:class name of th and td
//width:width of the field column, auto is default.
//hidden:default false.If true,the field column will be hidden.
//if text
//editable:the text can be edited if true.
//unique:field value is unique in the column.
//if actions
//actions:['up','down','edit','delete']
//if extension
//create //function
//setValue //function
//getValue //function
*/
//public methods:
//clear
//clearEmptyRows
//addEmptyRow
//addRows
//addRow
//deleteRow
//editRow
//selectRow
//getRows
//getSelectedRow
//getSelectedRowData
//getData
//getRowData
//getRowDataArrayByFieldValue
//events:
//row-click
//row-dblclick
//row-select
//rows-clear
//row-add
//row-edit
//row-delete
//row-up
//row-down
//actions-edit
//css classes:
//simple-table-title
//simple-table-row
//simple-table-field
//simple-table-cell
postMixInProperties: function () {
this.nls = window.jimuNls.simpleTable;
},
postCreate: function () {
this.inherited(arguments);
this._initSelf();
},
startup: function () {
this.inherited(arguments);
this._updateUI();
},
_initSelf: function () {
this._initAttachPoints();
this.own(
jimuUtils.bindClickAndDblclickEvents(this.table,
lang.hitch(this, function (evt) {
var target = evt.target || evt.srcElement;
var tr = jimuUtils.getAncestorDom(target, function (dom) {
return html.hasClass(dom, 'simple-table-row') && html.hasClass(dom, 'not-empty');
}, this.tbody);
if (tr) {
this.selectRow(tr);
this._onClickRow(tr);
}
}), lang.hitch(this, function (evt) {
var target = evt.target || evt.srcElement;
var tr = jimuUtils.getAncestorDom(target, function (dom) {
return html.hasClass(dom, 'simple-table-row') && html.hasClass(dom, 'not-empty');
}, this.tbody);
if (tr) {
this.selectRow(tr);
this._onDblClickRow(tr);
}
}))
);
var num = Math.random().toString();
this._name = 'jimu_table_' + num.slice(2, num.length);
this.thead = query('thead', this.domNode)[0];
this.tbody = query('tbody', this.domNode)[0];
if (this.fields && this.fields.length > 0) {
var tr = html.create('tr', {}, this.thead);
array.forEach(this.fields, lang.hitch(this, function (item) {
var width = 'auto';
if (item.type === 'actions') {
item.name = 'actions';
}
if (item.hidden) {
width = 1;
} else if (item.width !== undefined && item.width !== null) {
width = item.width;
} else if (item.type === 'actions') {
if (!item.name) {
item.width = this._calculateActionsWidth(item) + 20;
width = item.width;
}
}
html.create('col', {
width: width
}, this.colgroup);
var th = html.create('th', {
innerHTML: item.title,
title: item.title
}, tr);
html.addClass(th, 'simple-table-field');
if (item.hidden) {
html.addClass(th, 'hidden-column');
}
if (item['class']) {
html.addClass(th, item['class']);
}
html.addClass(th, item.name);
}));
//clone thead
html.empty(this.headDiv);
var tableDiv = lang.clone(this.tableDiv);
html.place(tableDiv, this.headDiv);
//this.addEmptyRow();
} else {
this.fields = null;
}
},
_initAttachPoints: function () {
this.table = query('table', this.bodyDiv)[0];
this.colgroup = query('colgroup', this.bodyDiv)[0];
this.head = query('thead', this.bodyDiv)[0];
this.tbody = query('tbody', this.bodyDiv)[0];
},
clear: function () {
var trs = this._getNotEmptyRows();
html.empty(this.tbody);
array.forEach(trs, lang.hitch(this, function (tr) {
this._onDeleteRow(tr);
}));
//this.addEmptyRow();
this._updateUI();
this._rowIndex = 0;
this._onClearRows(trs);
},
// clearEmptyRows: function() {
// var trs = this._getEmptyRows();
// array.forEach(trs, lang.hitch(this, function(tr) {
// html.destroy(tr);
// }));
// this._updateUI();
// },
// addEmptyRow: function() {
// if (!this.fields) {
// return;
// }
// this.clearEmptyRows();
// var length = this.fields.length;
// var tr = html.create('tr', {
// 'class': 'simple-table-row empty'
// }, this.tbody);
// for (var i = 0; i < length; i++) {
// html.create('td', {
// 'class': 'simple-table-cell empty-td'
// }, tr);
// }
// this._updateRowClassName();
// },
addRows: function (rowsData) {
var results = [];
if (this.fields && rowsData && rowsData.length > 0) {
array.forEach(rowsData, lang.hitch(this, function (item) {
results.push(this.addRow(item, true));
}));
}
this._updateUI();
return results;
},
//example:{name1:value1,name2:value2...}
addRow: function (rowData, /* optional */ dontUpdateUI) {
this._rowIndex++;
var result = {
success: false,
tr: null,
errorCode: null,
errorMessage: null,
repeatFields: null
};
if (!this.fields || (typeof rowData !== 'object')) {
return result;
}
var uniqueFieldMetas = array.filter(this.fields, lang.hitch(this, function (item) {
return item.type === 'text' && item.unique === true;
}));
var repeatFieldMetas = array.filter(uniqueFieldMetas, lang.hitch(this, function (item) {
var sameValueRows = this.getRowDataArrayByFieldValue(item.name, rowData[item.name]);
return sameValueRows.length > 0;
}));
if (repeatFieldMetas.length > 0) {
result.errorCode = this.REPEATING_ERROR;
result.errorMessage = "repeating data";
result.repeatFields = repeatFieldMetas;
return result;
}
// this.clearEmptyRows();
var tr = html.create("tr", {
'class': "simple-table-row not-empty"
}, this.tbody);
var rowId = 'row' + this._rowIndex;
html.setAttr(tr, 'rowId', rowId);
array.forEach(this.fields, lang.hitch(this, function (fieldMeta) {
var fieldData = rowData[fieldMeta.name];
var type = fieldMeta.type;
var td = null;
if (type === 'actions') {
td = this._createActionsTd(tr, fieldMeta);
} else {
if (type === "text") {
td = this._createTextTd(tr, fieldMeta, fieldData);
} else if (type === "radio") {
td = this._createRadioTd(tr, fieldMeta, fieldData);
} else if (type === 'checkbox') {
td = this._createCheckboxTd(tr, fieldMeta, fieldData);
} else if (type === "empty") {
td = this._createEmptyTd(tr, fieldMeta);
} else if (type === "extension") {
td = this._createExtensionTd(tr, fieldMeta, fieldData);
}
if (fieldMeta.hidden) {
html.addClass(td, 'hidden-column');
}
}
}));
// attach item as attribute of tr so can retrieve later
tr.item = rowData;
if (!dontUpdateUI) {
this._updateUI();
}
result.success = true;
result.tr = tr;
result.errorMessage = null;
this._onAddRow(tr);
return result;
},
deleteRow: function (tr) {
if (tr) {
html.destroy(tr);
this._updateUI();
this._onDeleteRow(tr);
// var trs = this._getAllRows();
// if(trs.length === 0){
// this.addEmptyRow();
// }
}
},
selectRow: function (tr) {
if (this.selectable) {
var trs = query('.simple-table-row', this.tbody);
trs.removeClass('jimu-state-active');
html.addClass(tr, 'jimu-state-active');
this._onSelectRow(tr);
}
},
_updateUI: function () {
this._updateRowClassName();
this._updateHeight();
},
_updateHeight: function () {
if (this.autoHeight) {
var rows = this.getRows();
var trCount = rows.length > 0 ? rows.length : 1;
// var count = trCount + 1;
var height = this._headHeight + this._rowHeight * trCount + 1;
html.setStyle(this.domNode, 'height', height + 'px');
// this.bodyDiv.style.overflowY = 'hidden';
}
},
_updateRowClassName: function () {
var originalFirtTr = query('.' + this._classFirstSimpleTableRow, this.tbody)[0];
if (originalFirtTr) {
var originalFirstUpDiv = query('.' + this._classRowUpDiv, originalFirtTr)[0];
if (originalFirstUpDiv) {
html.removeClass(originalFirstUpDiv, this._classJimuStateDisabled);
}
}
var originalLastTr = query('.' + this._classLastSimpleTableRow, this.tbody)[0];
if (originalLastTr) {
var originalLastDownDiv = query('.' + this._classRowDownDiv, originalLastTr)[0];
if (originalLastDownDiv) {
html.removeClass(originalLastDownDiv, this._classJimuStateDisabled);
}
}
var trs = query('.' + this._classSimpleTableRow, this.tbody);
trs.removeClass('odd');
trs.removeClass('even');
trs.removeClass(this._classFirstSimpleTableRow);
trs.removeClass(this._classLastSimpleTableRow);
array.forEach(trs, lang.hitch(this, function (tr, index) {
if (index % 2 === 0) {
html.addClass(tr, 'odd');
} else {
html.addClass(tr, 'even');
}
}));
if (trs.length > 0) {
var firstTr = trs[0];
html.addClass(firstTr, this._classFirstSimpleTableRow);
var firstUpDiv = query('.' + this._classRowUpDiv, firstTr)[0];
if (firstUpDiv) {
html.addClass(firstUpDiv, this._classJimuStateDisabled);
}
var lastTr = trs[trs.length - 1];
html.addClass(lastTr, this._classLastSimpleTableRow);
var lastDownDiv = query('.' + this._classRowDownDiv, lastTr)[0];
if (lastDownDiv) {
html.addClass(lastDownDiv, this._classJimuStateDisabled);
}
}
},
_createTextTd: function (tr, fieldMeta, fieldData) {
var td = null;
if (fieldMeta.editable) {
td = this._createEditableTextTd(tr, fieldMeta, fieldData);
} else {
td = this._createNormalTextTd(tr, fieldMeta, fieldData);
}
return td;
},
_createNormalTextTd: function (tr, fieldMeta, fieldData) {
var strTd = '<td class="simple-table-cell normal-text-td">' +
'<div class="normal-text-div"></div></td>';
var td = html.toDom(strTd);
html.addClass(td, fieldMeta.name);
var textDiv = query('div', td)[0];
textDiv.innerHTML = fieldData || "";
textDiv.title = fieldData || "";
if (fieldMeta['class']) {
html.addClass(td, fieldMeta['class']);
}
html.place(td, tr);
return td;
},
_createEditableTextTd: function (tr, fieldMeta, fieldData) {
var tdStr = '<td class="editable-text-td ' + fieldMeta.name + '">' +
'<div class="editable-div">' +
'</div><input class="editable-input" type="text" style="display:none;" /></td>';
var td = html.toDom(tdStr);
html.addClass(td, 'simple-table-cell');
html.place(td, tr);
if (fieldMeta['class']) {
html.addClass(td, fieldMeta['class']);
}
var editableDiv = query('div', td)[0];
var editableInput = query('input', td)[0];
editableDiv.innerHTML = fieldData || "";
if (editableDiv.innerHTML !== "") {
editableDiv.title = editableDiv.innerHTML;
}
editableInput.value = editableDiv.innerHTML;
/*
this.own(on(editableDiv, 'dblclick', lang.hitch(this, function (event) {
event.stopPropagation();
// do not enable edit mode on double click
editableInput.value = editableDiv.innerHTML;
html.setStyle(editableDiv, 'display', 'none');
html.setStyle(editableInput, 'display', 'inline');
editableInput.focus();
})));
*/
// trigger blur on enter key
this.own(on(editableInput, 'keypress', lang.hitch(this, function (e) {
if (e.keyCode === dojo.keys.ENTER) {
editableInput.blur();
}
})));
this.own(on(editableInput, 'blur', lang.hitch(this, function () {
editableInput.value = lang.trim(editableInput.value);
var oldValue = editableDiv.innerHTML;
var newValue = editableInput.value;
if (newValue !== '') {
if (fieldMeta.unique) {
var sameValueRows = this.getRowDataArrayByFieldValue(fieldMeta.name, newValue, tr);
if (sameValueRows.length > 0) {
editableInput.value = oldValue;
} else {
editableDiv.innerHTML = newValue;
}
} else {
editableDiv.innerHTML = newValue;
}
} else {
editableInput.value = oldValue;
}
// update item
tr.item[fieldMeta.name] = editableDiv.innerHTML;
html.setStyle(editableInput, 'display', 'none');
html.setStyle(editableDiv, 'display', 'block');
this._onEditRow(tr);
})));
return td;
},
_createRadioTd: function (tr, fieldMeta, fieldData) {
var tdStr = '<td class="radio-td ' + fieldMeta.name + '"><input type="radio" /></td>';
var td = html.toDom(tdStr);
html.addClass(td, 'simple-table-cell');
html.place(td, tr);
if (fieldMeta['class']) {
html.addClass(td, fieldMeta['class']);
}
var radio = query('input', td)[0];
if (fieldMeta.radio && fieldMeta.radio === "row") {
radio.name = this._name + this._rowIndex;
} else {
radio.name = this._name + fieldMeta.name;
}
radio.checked = fieldData === true;
return td;
},
_createCheckboxTd: function (tr, fieldMeta, fieldData) {
var tdStr = '<td class="checkbox-td ' + fieldMeta.name + '"><input type="checkbox" /></td>';
var td = html.toDom(tdStr);
html.addClass(td, 'simple-table-cell');
html.place(td, tr);
if (fieldMeta['class']) {
html.addClass(td, fieldMeta['class']);
}
var checkbox = query('input', td)[0];
checkbox.checked = fieldData === true;
return td;
},
_createActionsTd: function (tr, fieldMeta) {
var tdStr = '<td class="actions-td">' +
'<div class="action-item-parent jimu-float-leading"></div></td>';
var td = html.toDom(tdStr);
html.addClass(td, 'simple-table-cell');
var actionItemParent = query(".action-item-parent", td)[0];
html.place(td, tr);
if (fieldMeta['class']) {
html.addClass(td, fieldMeta['class']);
}
array.forEach(fieldMeta.actions, lang.hitch(this, function (item) {
if (item === 'up') {
var moveupDiv = html.create('div', {
'class': 'action-item jimu-float-leading row-up-div jimu-icon jimu-icon-up'
}, actionItemParent);
moveupDiv.title = this.nls.moveUp;
this.own(on(moveupDiv, 'click', lang.hitch(this, function (event) {
event.stopPropagation();
if (!this.onBeforeRowUp(tr)) {
return;
}
var trs = query('.simple-table-row', this.tbody);
var index = array.indexOf(trs, tr);
if (index > 0) {
var newIndex = index - 1;
var trRef = trs[newIndex];
if (trRef) {
html.place(tr, trRef, 'before');
this._updateUI();
this.emit('row-up', tr);
}
}
})));
} else if (item === 'down') {
var movedownDiv = html.create('div', {
'class': 'action-item jimu-float-leading row-down-div jimu-icon jimu-icon-down'
}, actionItemParent);
movedownDiv.title = this.nls.moveDown;
this.own(on(movedownDiv, 'click', lang.hitch(this, function (event) {
event.stopPropagation();
if (!this.onBeforeRowDown(tr)) {
return;
}
var trs = query('.simple-table-row', this.tbody);
var index = array.indexOf(trs, tr);
if (index < trs.length - 1) {
var newIndex = index + 1;
var trRef = trs[newIndex];
if (trRef) {
html.place(tr, trRef, 'after');
this._updateUI();
this.emit('row-down', tr);
}
}
})));
} else if (item === 'load') {
var loadDiv = html.create('div', {
'class': 'action-item jimu-float-leading row-load-div jimu-icon jimu-icon-load'
}, actionItemParent);
loadDiv.title = "Load Map";
this.own(on(loadDiv, 'click', lang.hitch(this, function (event) {
event.stopPropagation();
this._onActionsLoad(tr);
})));
} else if (item === 'download') {
var loadDiv = html.create('div', {
'class': 'action-item jimu-float-leading row-load-div jimu-icon jimu-icon-download'
}, actionItemParent);
loadDiv.title = "Download Map";
this.own(on(loadDiv, 'click', lang.hitch(this, function (event) {
event.stopPropagation();
this._onActionsDownload(tr);
})));
} else if (item === 'edit') {
var editDiv = html.create('div', {
'class': 'action-item jimu-float-leading row-edit-div jimu-icon jimu-icon-edit'
}, actionItemParent);
editDiv.title = this.nls.edit;
this.own(on(editDiv, 'click', lang.hitch(this, function (event) {
event.stopPropagation();
if (!this.onBeforeRowEdit(tr)) {
return;
}
this._onActionsEdit(tr);
})));
} else if (item === 'delete') {
var deleteDiv = html.create('div', {
'class': 'action-item jimu-float-leading row-delete-div jimu-icon jimu-icon-delete'
}, actionItemParent);
deleteDiv.title = this.nls.deleteRow;
this.own(on(deleteDiv, 'click', lang.hitch(this, function (event) {
event.stopPropagation();
if (!this.onBeforeRowDelete(tr)) {
return;
}
this.deleteRow(tr);
})));
}
}));
var width = this._calculateActionsWidth(fieldMeta) + 'px';
html.setStyle(actionItemParent, 'width', width);
return td;
},
_calculateActionsWidth: function (fieldMeta) {
var items = array.map(fieldMeta.actions, function (item) {
return item === 'load' || item === 'up' || item === 'down' || item === 'edit' || item === 'delete' || item === 'download';
});
return items.length * 30;
},
_createEmptyTd: function (tr, fieldMeta) {
var td = html.create('td', {
'class': fieldMeta.name
}, tr);
html.addClass(td, 'simple-table-cell');
html.addClass(td, 'empty-text-td');
if (fieldMeta['class']) {
html.addClass(td, fieldMeta['class']);
}
return td;
},
_createExtensionTd: function (tr, fieldMeta, fieldData) {
var td = html.create('td', {
'class': fieldMeta.name
}, tr);
html.addClass(td, 'simple-table-cell');
html.addClass(td, 'extension-td');
if (fieldMeta['class']) {
html.addClass(td, fieldMeta['class']);
}
if (fieldMeta.create && typeof fieldMeta.create === 'function') {
fieldMeta.create(td);
}
if (fieldMeta.setValue && typeof fieldMeta.setValue === 'function') {
fieldMeta.setValue(td, fieldData);
}
return td;
},
editRow: function (tr, rowData) {
var result = {
success: false,
tr: null,
errorCode: null,
errorMessage: null,
repeatFields: null
};
if (!this.fields || (typeof rowData !== 'object')) {
return result;
}
if (!html.isDescendant(tr, this.tbody)) {
return result;
}
var allFieldMetas = lang.mixin([], this.fields);
var uniqueFieldMetas = array.filter(allFieldMetas, lang.hitch(this, function (item) {
return item.type === 'text' && item.unique === true;
}));
var repeatFieldMetas = array.filter(uniqueFieldMetas, lang.hitch(this, function (item) {
var sameValueRows = this.getRowDataArrayByFieldValue(item.name, rowData[item.name], tr);
return sameValueRows.length > 0;
}));
if (repeatFieldMetas.length > 0) {
result.errorCode = this.REPEATING_ERROR;
result.errorMessage = "repeating data";
result.repeatFields = repeatFieldMetas;
return result;
}
var tds = query('.simple-table-cell', tr);
array.forEach(this.fields, lang.hitch(this, function (fieldMeta, idx) {
if (!rowData.hasOwnProperty(fieldMeta.name)) {
return;
}
var td = tds[idx];
var fieldData = rowData[fieldMeta.name];
var type = fieldMeta.type;
if (type === 'text') {
if (fieldMeta.editable) {
this._editEditableText(td, fieldMeta, fieldData);
} else {
this._editNormalText(td, fieldMeta, fieldData);
}
} else if (type === 'radio') {
this._editRadio(td, fieldMeta, fieldData);
} else if (type === 'checkbox') {
this._editCheckbox(td, fieldMeta, fieldData);
} else if (type === 'extension') {
this._editExtension(td, fieldMeta, fieldData);
}
}));
result.success = true;
result.tr = tr;
result.errorMessage = null;
this._onEditRow(tr);
return result;
},
_editNormalText: function (td, fieldMeta, fieldData) {
/*jshint unused: false*/
var normalTextDiv = query('div', td)[0];
normalTextDiv.innerHTML = fieldData || "";
normalTextDiv.title = normalTextDiv.innerHTML;
},
_editEditableText: function (td, fieldMeta, fieldData) {
/*jshint unused: false*/
var editableDiv = query('div', td)[0];
editableDiv.innerHTML = fieldData || "";
var editableInput = query('input', td)[0];
editableInput.value = editableDiv.innerHTML;
},
_editRadio: function (td, fieldMeta, fieldData) {
/*jshint unused: false*/
var radio = query('input', td)[0];
radio.checked = fieldData === true;
},
_editCheckbox: function (td, fieldMeta, fieldData) {
/*jshint unused: false*/
var checkbox = query('input', td)[0];
checkbox.checked = fieldData === true;
},
_editExtension: function (td, fieldMeta, fieldData) {
if (fieldMeta.setValue && typeof fieldMeta.setValue === 'function') {
fieldMeta.setValue(td, fieldData);
}
},
_getAllRows: function () {
return query('.simple-table-row', this.tbody);
},
_getNotEmptyRows: function () {
var trs = this._getAllRows();
return array.filter(trs, lang.hitch(this, function (tr) {
return !html.hasClass(tr, 'empty');
}));
},
_getEmptyRows: function () {
var trs = this._getAllRows();
return array.filter(trs, lang.hitch(this, function (tr) {
return html.hasClass(tr, 'empty');
}));
},
getRows: function () {
return this._getNotEmptyRows();
},
getSelectedRow: function () {
var result = null;
var trs = query('.simple-table-row', this.tbody);
var filterTrs = array.filter(trs, lang.hitch(this, function (tr) {
return !html.hasClass(tr, 'empty') && html.hasClass(tr, 'jimu-state-active');
}));
if (filterTrs.length > 0) {
result = filterTrs[0];
}
return result;
},
getSelectedRowData: function () {
var result = null;
var tr = this.getSelectedRow();
if (tr) {
result = this._getRowDataByTr(tr);
}
return result;
},
getData: function ( /*optional*/ ignoredTr) {
var trs = this._getNotEmptyRows();
if (ignoredTr) {
trs = array.filter(trs, lang.hitch(this, function (tr) {
return tr !== ignoredTr;
}));
}
var result = array.map(trs, lang.hitch(this, function (tr) {
return this._getRowDataByTr(tr);
}));
return result;
},
getRowData: function (tr) {
return this._getRowDataByTr(tr);
},
_getRowDataByTr: function (tr) {
var rowData = null;
if (tr) {
rowData = {};
} else {
return null;
}
array.forEach(this.fields, lang.hitch(this, function (fieldMeta) {
var type = fieldMeta.type;
if (type === 'actions') {
return;
}
var name = fieldMeta.name;
rowData[name] = null;
var td = query('.' + name, tr)[0];
if (td) {
if (type === 'text') {
if (fieldMeta.editable) {
var editableDiv = query('div', td)[0];
rowData[name] = editableDiv.innerHTML;
} else {
var normalTextDiv = query('div', td)[0];
rowData[name] = normalTextDiv.innerHTML;
}
} else if (type === 'radio') {
var radio = query('input', td)[0];
rowData[name] = radio.checked;
} else if (type === 'checkbox') {
var checkbox = query('input', td)[0];
rowData[name] = checkbox.checked;
} else if (type === 'extension') {
if (fieldMeta.getValue && typeof fieldMeta.getValue === 'function') {
rowData[name] = fieldMeta.getValue(td, fieldMeta);
}
}
}
}));
return rowData;
},
getRowDataArrayByFieldValue: function (fieldName, fieldValue, /*optional*/ ignoredTr) {
var result = [];
if (!this.fields) {
return [];
}
var validField = array.some(this.fields, lang.hitch(this, function (item) {
return item.name === fieldName;
}));
if (!validField) {
return [];
}
var rows = this.getData(ignoredTr);
result = array.filter(rows, lang.hitch(this, function (row) {
/* jshint eqeqeq: false*/
return row[fieldName] == fieldValue;
}));
return result;
},
/**
* return all the data items from the table
*/
getItems: function () {
var trs = [],
result = [];
trs = this._getNotEmptyRows();
var result = array.map(trs, lang.hitch(this, function (tr) {
return tr.item;
}));
return result;
},
/**
* set edit mode on all cells in given row - except for action cell
* @param {TableRow} tr table row dom node
*/
_setEditModeOnRow: function (tr) {
var tds = query('.simple-table-cell', tr);
array.forEach(tds, lang.hitch(this, function (td, idx) {
if (html.hasClass(td, "actions")) {
// skip actions
return;
}
this._setEditModeOnCell(td);
}));
},
/**
* enable Edit Mode on the given cell
* @param {TableCell} td table cell dom node
*/
_setEditModeOnCell: function (td) {
var editableDiv = query('div', td)[0];
var editableInput = query('input', td)[0];
editableInput.value = editableDiv.innerHTML;
html.setStyle(editableDiv, 'display', 'none');
html.setStyle(editableInput, 'display', 'inline');
editableInput.focus();
},
_onClickRow: function (tr) {
this.emit('row-click', tr);
},
_onDblClickRow: function (tr) {
var args = {
element: tr,
item: tr.item
};
this.emit('row-dblclick', args);
},
_onSelectRow: function (tr) {
this.emit('row-select', tr);
},
_onAddRow: function (tr) {
this.emit('row-add', tr);
},
_onEditRow: function (tr) {
this.emit('row-edit', tr);
},
_onDeleteRow: function (tr) {
var args = {
element: tr,
item: tr.item
};
this.emit('row-delete', args);
},
_onEnterRow: function (tr) {
this.emit('row-enter', tr);
},
_onClearRows: function (trs) {
this.emit('rows-clear', trs);
},
_onActionsEdit: function (tr) {
this.emit('actions-edit', tr);
this._setEditModeOnRow(tr);
},
_onActionsLoad: function (tr) {
var args = {
element: tr,
item: tr.item
};
this.emit('actions-load', args);
},