/
renderer.js
962 lines (819 loc) · 29.9 KB
/
renderer.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
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
/**
* @fileoverview Rendering 모델
* @author NHN. FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
var _ = require('underscore');
var snippet = require('tui-code-snippet');
var Model = require('../base/model');
var Row = require('./row');
var RowList = require('./rowList');
var renderStateMap = require('../common/constMap').renderState;
var CELL_BORDER_WIDTH = require('../common/constMap').dimension.CELL_BORDER_WIDTH;
var DATA_LENGTH_FOR_LOADING = 1000;
/**
* View 에서 Rendering 시 사용할 객체
* @module model/renderer
* @extends module:base/model
* @param {Object} attrs - Attributes
* @param {Object} options - Options
* @ignore
*/
var Renderer = Model.extend(/** @lends module:model/renderer.prototype */{
initialize: function(attrs, options) {
var rowListOptions;
var partialLside, partialRside;
_.assign(this, {
dataModel: options.dataModel,
columnModel: options.columnModel,
focusModel: options.focusModel,
dimensionModel: options.dimensionModel,
coordRowModel: options.coordRowModel,
coordColumnModel: options.coordColumnModel
});
rowListOptions = {
dataModel: this.dataModel,
columnModel: this.columnModel,
focusModel: this.focusModel
};
partialLside = new RowList([], rowListOptions);
partialRside = new RowList([], rowListOptions);
this.set({
lside: [],
rside: [],
partialLside: partialLside,
partialRside: partialRside
});
this.listenTo(this.columnModel, 'columnModelChange change', this._onColumnModelChange)
.listenTo(this.dataModel, 'reset', this._initializeScrollValues)
.listenTo(this.dataModel, 'sort reset', this._onDataModelChange)
.listenTo(this.dataModel, 'deleteRange', this._onRangeDataModelChange)
.listenTo(this.dataModel, 'add', this._onAddDataModelChange)
.listenTo(this.dataModel, 'remove', this._onRemoveDataModelChange)
.listenTo(this.dataModel, 'beforeReset', this._onBeforeResetData)
.listenTo(this.dataModel, 'expanded ', this._onExpanded)
.listenTo(this.dataModel, 'collapsed ', this._onCollapsed)
.listenTo(this.focusModel, 'change:editingAddress', this._onEditingAddressChange)
.listenTo(partialLside, 'valueChange', this._executeRelation)
.listenTo(partialRside, 'valueChange', this._executeRelation)
.listenTo(this.coordRowModel, 'reset', this._onChangeRowHeights)
.listenTo(this.dimensionModel, 'columnWidthChanged', this.finishEditing)
.listenTo(this.dimensionModel, 'change:width', this._updateMaxScrollLeft)
.listenTo(this.dimensionModel, 'change:totalRowHeight change:scrollBarSize change:bodyHeight',
this._updateMaxScrollTop);
if (this.get('showDummyRows')) {
this.listenTo(this.dimensionModel, 'change:bodyHeight change:totalRowHeight', this._resetDummyRowCount);
this.on('change:dummyRowCount', this._resetDummyRows);
}
this.on('change', this._onChangeIndex, this);
this._onChangeLayoutBound = _.bind(this._onChangeLayout, this);
},
defaults: {
top: 0,
bottom: 0,
scrollTop: 0,
scrollLeft: 0,
maxScrollLeft: 0,
maxScrollTop: 0,
startIndex: -1,
endIndex: -1,
startNumber: 1,
lside: null,
rside: null,
partialLside: null,
partialRside: null,
showDummyRows: false,
dummyRowCount: 0,
// text that will be shown if no data to render (custom value set by user)
emptyMessage: null,
// constMap.renderState
state: renderStateMap.DONE
},
/**
* Event handler for 'expanded' event on dataModel using tree
* @param {object} ev - Event object
* @private
*/
_onExpanded: function(ev) {
var rowKeys = ev.descendantRowKeys;
var dataModel = this.dataModel;
var columnNamesMap = this._getColumnNamesOfEachSide();
var height, viewData, rowNum, viewModel, index, row;
_.each(rowKeys, function(rowKey) {
index = dataModel.indexOfRowKey(rowKey);
row = dataModel.at(index);
height = this.coordRowModel.getHeightAt(index);
_.each(['lside', 'rside'], function(attrName) {
rowNum = index + 1;
viewData = this._createViewDataFromDataModel(
row, columnNamesMap[attrName], height, rowNum);
viewModel = this._createRowModel(viewData, true);
this.get(attrName)[index] = viewModel;
}, this);
}, this);
this._setRenderingRange();
this.refresh({
type: 'add',
dataListChanged: true
});
},
/**
* Event handler for 'collapsed' event on dataModel using tree
* @param {object} ev - Event object
* @private
*/
_onCollapsed: function(ev) {
var rowKeys = ev.descendantRowKeys;
_.each(rowKeys, function(rowKey) {
var index = this.dataModel.indexOfRowKey(rowKey);
_.each(['lside', 'rside'], function(attrName) {
delete this.get(attrName)[index];
}, this);
}, this);
this._setRenderingRange();
this.refresh({
type: 'deleteRange',
dataListChanged: true
});
},
/**
* Event handler for change:scrollTop and change:scrollLeft.
* @private
*/
_onChangeLayout: function() {
this.focusModel.finishEditing();
this.focusModel.focusClipboard();
},
/**
* Event handler for changing startIndex or endIndex.
* @param {Object} model - Renderer model fired event
* @private
*/
_onChangeIndex: function(model) {
var changedData = model.changed;
var changedStartIndex = _.has(changedData, 'startIndex');
var changedEndIndex = _.has(changedData, 'endIndex');
if (changedStartIndex || changedEndIndex) {
this.refresh();
}
},
/**
* Event handler for 'reset' event on coordRowModel
* @private
*/
_onChangeRowHeights: function() {
var lside = this.get('partialLside');
var rside = this.get('partialRside');
var i = 0;
var len = lside.length;
var rowKey, height;
for (; i < len; i += 1) {
rowKey = lside.at(i).get('rowKey');
height = this.coordRowModel.getHeight(rowKey);
lside.at(i).set('height', height);
rside.at(i).set('height', height);
}
},
/**
* Event handler for 'change:width' event on Dimension.
* @private
*/
_updateMaxScrollLeft: function() {
var dimension = this.dimensionModel;
var maxScrollLeft = this.coordColumnModel.getFrameWidth('R') - dimension.get('rsideWidth') +
dimension.getScrollYWidth();
this.set('maxScrollLeft', maxScrollLeft);
},
/**
* Event handler to reset 'maxScrollTop' attribute.
* @private
*/
_updateMaxScrollTop: function() {
var dimension = this.dimensionModel;
var maxScrollTop = dimension.get('totalRowHeight') - dimension.get('bodyHeight') +
dimension.getScrollXHeight();
this.set('maxScrollTop', maxScrollTop);
},
/**
* Event handler for 'beforeReset' event on dataModel
* @param {number} dataLength - the length of data
* @private
*/
_onBeforeResetData: function(dataLength) {
if (dataLength > DATA_LENGTH_FOR_LOADING) {
this.set('state', renderStateMap.LOADING);
}
},
/**
* Event handler for 'change:editingAddress' event on focusModel
* @param {module:model/focus} focusModel - focus model
* @param {{rowKey: Number, columnName: String}} address - address
* @private
*/
_onEditingAddressChange: function(focusModel, address) {
var target = address;
var editing = true;
var self = this;
if (!address) {
target = focusModel.previous('editingAddress');
editing = false;
}
this._updateCellData(target.rowKey, target.columnName, {
editing: editing
});
this._triggerEditingStateChanged(target.rowKey, target.columnName);
// defered call to prevent 'change:scrollLeft' or 'change:scrollTop' event
// triggered by module:view/layout/body._onScroll()
// when module:model/focus.scrollToFocus() method is called.
_.defer(function() {
self._toggleChangeLayoutEventHandlers(editing);
});
},
/**
* Toggle event handler for change:scrollTop and change:scrollLeft event.
* @param {Boolean} editing - whether currently editing
* @private
*/
_toggleChangeLayoutEventHandlers: function(editing) {
var renderEventName = 'change:scrollTop change:scrollLeft';
var dimensionEventName = 'columnWidthChanged';
if (editing) {
this.listenToOnce(this.dimensionModel, dimensionEventName, this._onChangeLayoutBound);
this.once(renderEventName, this._onChangeLayoutBound);
} else {
this.stopListening(this.dimensionModel, dimensionEventName, this._onChangeLayoutBound);
this.off(renderEventName, this._onChangeLayoutBound);
}
},
/**
* Triggers the 'editingStateChanged' event if the cell data identified by
* given row key and column name has the useViewMode:true option.
* @param {String} rowKey - row key
* @param {String} columnName - column name
* @private
*/
_triggerEditingStateChanged: function(rowKey, columnName) {
var cellData = this.getCellData(rowKey, columnName);
if (snippet.pick(cellData, 'columnModel', 'editOptions', 'useViewMode') !== false &&
cellData.convertedHTML === null) {
this.trigger('editingStateChanged', cellData);
}
},
/**
* Updates the view-data of the cell identified by given rowKey and columnName.
* @param {(String|Number)} rowKey - row key
* @param {String} columnName - column name
* @param {Object} cellData - cell data
* @private
*/
_updateCellData: function(rowKey, columnName, cellData) {
var rowModel = this._getRowModel(rowKey, columnName);
if (rowModel) {
rowModel.setCell(columnName, cellData);
}
},
/**
* Update data of tree-cell
* @param {number} rowKey - row key
* @private
*/
_updateTreeCellData: function(rowKey) {
var columnName = this.columnModel.getTreeColumnName();
var rowModel = this._getRowModel(rowKey, columnName);
if (rowModel) {
rowModel.setCell(columnName, {
hasChildren: this.dataModel.get(rowKey).hasTreeChildren()
});
}
},
/**
* Initializes own properties.
* (called by module:addon/net)
*/
initializeVariables: function() {
this.set({
top: 0,
scrollTop: 0,
scrollLeft: 0,
startNumber: 1
});
},
/**
* Initializes values of the scroll
* @private
*/
_initializeScrollValues: function() {
this.set({
scrollTop: 0,
scrollLeft: 0
});
},
/**
* 열고정 영역 또는 열고정이 아닌 영역에 대한 Render Collection 을 반환한다.
* @param {String} [whichSide='R'] 어느 영역인지 여부. 'L|R' 중에 하나의 값을 넘긴다.
* @returns {Object} collection 해당 영역의 랜더 데이터 콜랙션
*/
getCollection: function(whichSide) {
var attrName = this._getPartialWhichSideType(whichSide);
return this.get(attrName);
},
/**
* Get string of partial which side type
* @param {string} whichSide - Type of which side (L|R)
* @returns {string} String of appened prefix value 'partial'
* @private
*/
_getPartialWhichSideType: function(whichSide) {
return snippet.isString(whichSide) ? 'partial' + whichSide + 'side' : 'partialRside';
},
/**
* Event handler for regenerating left and right side frames when the Data.ColumnModel is changed
* @private
*/
_onColumnModelChange: function() {
var scrollLeftBeforeChange = this.get('scrollLeft');
var scrollTopBeforeChange = this.get('scrollTop');
this.set({
scrollLeft: 0,
scrollTop: 0
}, {silent: true});
this._resetViewModelList();
this._setRenderingRange(true);
this.refresh({
columnModelChanged: true
});
this._updateMaxScrollLeft();
this.set({
scrollLeft: scrollLeftBeforeChange,
scrollTop: scrollTopBeforeChange
});
},
/**
* Event handler for changing data list
* @private
*/
_onDataModelChange: function() {
this._resetViewModelList();
this._setRenderingRange(true);
this.refresh({
type: 'reset',
dataListChanged: true
});
},
/**
* Event handler for adding data list
* @param {array} rowList - List of added item
* @param {object} options - Info of added item
* @private
*/
_onAddDataModelChange: function(rowList, options) {
var columnNamesMap = this._getColumnNamesOfEachSide();
var at = options.at;
var height, viewData, rowNum;
var viewModel;
_.each(rowList, function(row, index) {
height = this.coordRowModel.getHeightAt(index);
_.each(['lside', 'rside'], function(attrName) {
rowNum = at + index + 1;
viewData = this._createViewDataFromDataModel(
row, columnNamesMap[attrName], height, rowNum);
viewModel = this._createRowModel(viewData, true);
this.get(attrName).splice(at + index, 0, viewModel);
}, this);
}, this);
this._updateTreeCellData(options.parentRowKey);
this._setRenderingRange(true);
this.refresh({
type: 'add',
dataListChanged: true
});
if (options.focus) {
this.focusModel.focusAt(options.at, 0);
}
},
/**
* Event handler for removing data list
* @param {number|string} rowKey - rowKey of the removed row
* @param {number} removedIndex - Index of the removed row
* @param {Array.<number>} [descendantRowKeys] - All descendants key of the removed when using tree
* @param {number} [parentRowKey] - Parent key of the removed row when using tree
* @private
*/
_onRemoveDataModelChange: function(rowKey, removedIndex, descendantRowKeys, parentRowKey) {
var removedRowsCnt = descendantRowKeys ? descendantRowKeys.length : 1;
_.each(['lside', 'rside'], function(attrName) {
this.get(attrName).splice(removedIndex, removedRowsCnt);
}, this);
this._updateTreeCellData(parentRowKey);
this._setRenderingRange(true);
this.refresh({
dataListChanged: true
});
},
/**
* Event handler for deleting cell data
* @param {GridEvent} ev - event object when "delRange" event is fired
* @private
*/
_onRangeDataModelChange: function(ev) {
var columnModel = this.columnModel;
var rowKeys = ev.rowKeys;
var columnNames = ev.columnNames;
this._setRenderingRange(true);
_.each(['partialLside', 'partialRside'], function(attrName) {
_.each(this.get(attrName).models, function(model) {
var rowKey = model.get('rowKey');
var changedRow = _.contains(rowKeys, rowKey);
if (changedRow) {
_.each(columnNames, function(columnName) {
if (columnModel.getColumnModel(columnName).editOptions) {
this._updateCellData(rowKey, columnName, {
value: '',
formattedValue: ''
});
}
}, this);
}
}, this);
}, this);
this.refresh({
type: 'deleteRange',
dataListChanged: true
});
},
/**
* Resets dummy rows and trigger 'dataListChanged' event.
* @private
*/
_resetDummyRows: function() {
this._clearDummyRows();
this._fillDummyRows();
this.trigger('rowListChanged');
},
/**
* Set index-range to render
* @param {boolean} silent - whether set attributes silently
* @private
*/
_setRenderingRange: function(silent) {
var dataLength = this.dataModel.length;
this.set({
startIndex: dataLength ? 0 : -1,
endIndex: dataLength - 1
}, {
silent: silent
});
},
/**
* Returns the new data object for rendering based on rowDataModel and specified column names.
* @param {Object} rowDataModel - Instance of module:model/data/row
* @param {Array.<String>} columnNames - Column names
* @param {Number} height - the height of the row
* @param {Number} rowNum - Row number
* @returns {Object} - view data object
* @private
*/
_createViewDataFromDataModel: function(rowDataModel, columnNames, height, rowNum) {
var viewData = {
rowNum: rowNum,
height: height,
rowKey: rowDataModel.get('rowKey'),
_extraData: rowDataModel.get('_extraData')
};
_.each(columnNames, function(columnName) {
var value = rowDataModel.get(columnName);
if (columnName === '_number' && !_.isNumber(value)) {
value = rowNum;
}
viewData[columnName] = value;
});
return viewData;
},
/**
* Returns the object that contains two array of column names splitted by frozenCount.
* @returns {{lside: Array, rside: Array}} - Column names map
* @private
*/
_getColumnNamesOfEachSide: function() {
var frozenCount = this.columnModel.getVisibleFrozenCount(true);
var columnModels = this.columnModel.getVisibleColumns(null, true);
var columnNames = _.pluck(columnModels, 'name');
return {
lside: columnNames.slice(0, frozenCount),
rside: columnNames.slice(frozenCount)
};
},
/**
* Add view model list by range
* @param {number} startIndex - Index of start row
* @param {number} endIndex - Index of end row
* @private
*/
_addViewModelListWithRange: function(startIndex, endIndex) {
var dataModel = this.dataModel;
var columnNamesMap = this._getColumnNamesOfEachSide();
var index, row, height;
if (startIndex < 0 || endIndex < 0) {
return;
}
for (index = startIndex; index < endIndex + 1; index += 1) {
row = dataModel.at(index);
height = this.coordRowModel.getHeightAt(index);
if (dataModel.isVisibleRow(row.get('rowKey'))) {
this._addViewModelList(row, columnNamesMap, height, index);
}
}
},
/**
* Add view model list on each side
* @param {object} rowDataModel - Data model of row
* @param {object} columnNamesMap - Map of column names
* @param {number} height - Height of row
* @param {number} index - Index of row
* @private
*/
_addViewModelList: function(rowDataModel, columnNamesMap, height, index) {
_.each(['lside', 'rside'], function(attrName) {
var viewData;
if (!this.get(attrName)[index]) {
viewData = this._createViewDataFromDataModel(
rowDataModel, columnNamesMap[attrName], height, index + 1);
this.get(attrName)[index] = this._createRowModel(viewData, true);
}
}, this);
},
/**
* Update the row number
* @param {number} startIndex - Start index
* @param {number} endIndex - End index
* @private
*/
_updateRowNumber: function(startIndex, endIndex) {
var collection = this.get('lside');
var index = startIndex;
var currentModel, rowNum, newRowNum;
for (; index <= endIndex; index += 1) {
currentModel = collection[index];
newRowNum = index + 1;
if (currentModel) {
rowNum = currentModel.get('rowNum');
newRowNum = index + this.get('startNumber');
if (rowNum !== newRowNum) {
currentModel.set({
rowNum: newRowNum
}, {
silent: true
});
currentModel.setCell('_number', {
formattedValue: newRowNum,
value: newRowNum
});
}
}
}
},
/**
* Reset partial view model list
* @param {number} startIndex - Index of start row
* @param {number} endIndex - Index of end row
* @private
*/
_resetPartialViewModelList: function(startIndex, endIndex) {
var originalWhichSide, partialWhichSide;
var viewModelList, partialViewModelList;
_.each(['L', 'R'], function(whichSide) {
partialViewModelList = [];
originalWhichSide = whichSide.toLowerCase() + 'side';
partialWhichSide = this._getPartialWhichSideType(whichSide);
viewModelList = this.get(originalWhichSide);
partialViewModelList = this._getPartialViewModelList(viewModelList, startIndex, endIndex);
this.get(partialWhichSide).reset(partialViewModelList);
}, this);
},
/**
* Get partial view model list
* @param {Array.<obejct>} viewModelList - List of view model
* @param {number} startIndex - Index of start row
* @param {number} endIndex - Index of end row
* @returns {Array.<module:model/data/row>}>} List of partial view model
* @private
*/
_getPartialViewModelList: function(viewModelList, startIndex, endIndex) {
var index = startIndex;
var len = endIndex + 1;
var partialViewModelList = [];
var viewModel;
for (; index < len; index += 1) {
viewModel = viewModelList[index];
if (viewModel && this.dataModel.isVisibleRow(viewModel.get('rowKey'))) {
partialViewModelList.push(viewModel);
}
}
return partialViewModelList;
},
/**
* Returns the count of rows (except dummy rows) in view model list
* @returns {Number} Count of rows
* @private
*/
_getActualRowCount: function() {
return this.get('endIndex') - this.get('startIndex') + 1;
},
/**
* Removes all dummy rows in the view model list.
* @private
*/
_clearDummyRows: function() {
var dataRowCount = this.get('endIndex') - this.get('startIndex') + 1;
_.each(['lside', 'rside'], function(attrName) {
var rowList = this.get(attrName);
while (rowList.length > dataRowCount) {
rowList.pop();
}
}, this);
},
/**
* Calculate required count of dummy rows and set the 'dummyRowCount' attribute.
* @param {boolean} silent - whether sets the dummyRowCount silently
* @private
*/
_resetDummyRowCount: function() {
var dimensionModel = this.dimensionModel;
var totalRowHeight = dimensionModel.get('totalRowHeight');
var rowHeight = dimensionModel.get('rowHeight') + CELL_BORDER_WIDTH;
var bodyHeight = dimensionModel.get('bodyHeight') - dimensionModel.getScrollXHeight();
var dummyRowCount = 0;
if (totalRowHeight < bodyHeight) {
dummyRowCount = Math.ceil((bodyHeight - totalRowHeight) / rowHeight);
}
this.set('dummyRowCount', dummyRowCount);
},
/**
* fills the empty area with dummy rows.
* @private
*/
_fillDummyRows: function() {
var dummyRowCount = this.get('dummyRowCount');
var rowNum, rowHeight;
if (dummyRowCount) {
rowNum = this.get('startNumber') + this.get('endIndex') + 1;
rowHeight = this.dimensionModel.get('rowHeight');
_.times(dummyRowCount, function() {
_.each(['partialLside', 'partialRside'], function(listName) {
this.get(listName).push(this._createRowModel({
height: rowHeight,
rowNum: rowNum
}));
}, this);
rowNum += 1;
}, this);
}
},
/* eslint-disable complexity */
/**
* Refreshes the rendering range and the list of view models, and triggers events.
* @param {object} options - options
* @param {boolean} [options.columnModelChanged] - The boolean value whether columnModel has changed
* @param {boolean} [options.dataListChanged] - The boolean value whether dataModel has changed
* @param {string} [options.type] - Event type (reset|add|remove)
*/
refresh: function(options) {
var columnModelChanged = !!options && options.columnModelChanged;
var dataListChanged = !!options && options.dataListChanged;
var eventType = !!options && options.type;
var startIndex, endIndex, i;
startIndex = this.get('startIndex');
endIndex = this.get('endIndex');
if (eventType !== 'add' && eventType !== 'deleteRange') {
this._addViewModelListWithRange(startIndex, endIndex);
}
if (eventType !== 'deleteRange') {
this._updateRowNumber(startIndex, endIndex);
}
this._resetPartialViewModelList(startIndex, endIndex);
this._fillDummyRows();
if (startIndex >= 0 && endIndex >= 0) {
for (i = startIndex; i <= endIndex; i += 1) {
this._executeRelation(i);
}
}
if (columnModelChanged) {
this.trigger('columnModelChanged');
} else {
this.trigger('rowListChanged', dataListChanged);
if (dataListChanged) {
this.coordRowModel.syncWithDom();
}
}
this._refreshState();
},
/* eslint-enable complexity */
/**
* Set state value based on the DataModel.length
* @private
*/
_refreshState: function() {
if (this.dataModel.length) {
this.set('state', renderStateMap.DONE);
} else {
this.set('state', renderStateMap.EMPTY);
}
},
/**
* columnName 으로 lside 와 rside rendering collection 중 하나를 반환한다.
* @param {String} columnName 컬럼명
* @returns {Collection} 컬럼명에 해당하는 영역의 콜랙션
* @private
*/
_getCollectionByColumnName: function(columnName) {
var lside = this.get('partialLside');
var collection;
if (lside.at(0) && lside.at(0).get(columnName)) {
collection = lside;
} else {
collection = this.get('partialRside');
}
return collection;
},
/**
* Returns the specified row model.
* @param {(Number|String)} rowKey - row key
* @param {String} columnName - column name
* @returns {module:model/row}
* @private
*/
_getRowModel: function(rowKey, columnName) {
var collection = this._getCollectionByColumnName(columnName);
return collection.get(rowKey);
},
/**
* 셀 데이터를 반환한다.
* @param {number} rowKey 데이터의 키값
* @param {String} columnName 컬럼명
* @returns {object} cellData 셀 데이터
* @example
{
rowKey: rowKey,
columnName: columnName,
value: value,
rowSpan: rowSpanData.count,
isMainRow: rowSpanData.isMainRow,
mainRowKey: rowSpanData.mainRowKey,
editable: editable,
disabled: disabled,
listItems: [],
className: row.getClassNameList(columnName).join(' '),
changed: [] //names of changed properties
}
*/
getCellData: function(rowKey, columnName) {
var row = this._getRowModel(rowKey, columnName);
var cellData = null;
if (row) {
cellData = row.get(columnName);
}
return cellData;
},
/**
* Executes the relation of the row identified by rowIndex
* @param {Number} rowIndex - Row index
* @private
*/
_executeRelation: function(rowIndex) {
var row = this.dataModel.at(rowIndex);
var renderIdx = rowIndex - this.get('startIndex');
var rowModel, relationResult;
relationResult = row.executeRelationCallbacksAll();
_.each(relationResult, function(changes, columnName) {
rowModel = this._getCollectionByColumnName(columnName).at(renderIdx);
if (rowModel) {
rowModel.setCell(columnName, changes);
}
}, this);
},
/**
* Create row model
* @param {object} attrs - Attributes to create
* @param {boolean} parse - Whether calling parse or not
* @returns {object} Row model
* @private
*/
_createRowModel: function(attrs, parse) {
return new Row(attrs, {
parse: parse,
dataModel: this.dataModel,
columnModel: this.columnModel,
focusModel: this.focusModel
});
},
/**
* Reset view models when value of columModel or dataModel is changed
* @private
*/
_resetViewModelList: function() {
_.each(['lside', 'rside'], function(attrName) {
this.set(attrName, new Array(this.dataModel.length));
}, this);
}
});
module.exports = Renderer;