1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {
|
7 | isEmptyObject
|
8 | } from 'tinper-sparrow/src/util';
|
9 |
|
10 |
|
11 | const setRows = function(rows, options) {
|
12 | var insertRows = [],
|
13 | _id;
|
14 | for (var i = 0; i < rows.length; i++) {
|
15 | var r = rows[i]
|
16 | _id = r.rowId || r.id;
|
17 | if (!_id)
|
18 | _id = Row.getRandomRowId()
|
19 | if (r.status == Row.STATUS.DELETE) {
|
20 | this.removeRowByRowId(_id)
|
21 | } else {
|
22 | var row = this.getRowByRowId(_id)
|
23 | if (row) {
|
24 | row.updateRow(r);
|
25 | if (!isEmptyObject(r.data)) {
|
26 | this.trigger(DataTable.ON_UPDATE, {
|
27 | index: i,
|
28 | rows: [row]
|
29 | })
|
30 | if (row == this.getCurrentRow()) {
|
31 | this.currentRowChange(-this.currentRowChange())
|
32 | row.currentRowChange(-row.currentRowChange())
|
33 | this.trigger(DataTable.ON_CURRENT_UPDATE, {
|
34 | index: i,
|
35 | rows: [row]
|
36 | })
|
37 | } else {
|
38 | row.currentRowChange(-row.currentRowChange())
|
39 | }
|
40 | }
|
41 |
|
42 | } else {
|
43 | row = new Row({
|
44 | parent: this,
|
45 | id: _id
|
46 | })
|
47 | row.setData(rows[i], null, options)
|
48 | insertRows.push(row)
|
49 | }
|
50 |
|
51 | if (r.status) {
|
52 | row.setStatus(r.status);
|
53 | }
|
54 | }
|
55 | }
|
56 | if (insertRows.length > 0)
|
57 | this.addRows(insertRows)
|
58 | return insertRows;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | const addRow = function(row) {
|
75 | this.insertRow(this.rows().length, row)
|
76 | this.resetDelRowEnd();
|
77 | }
|
78 |
|
79 | const resetDelRowEnd = function() {
|
80 | for (var i = 0; i < this.rows().length; i++) {
|
81 | var row = this.rows()[i];
|
82 | if (row.status == Row.STATUS.DELETE || row.status == Row.STATUS.FALSE_DELETE) {
|
83 | this.rows().splice(i, 1)
|
84 | this.rows().push(row);
|
85 | }
|
86 | }
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | const addRows = function(rows) {
|
107 | this.insertRows(this.rows().length, rows)
|
108 | this.resetDelRowEnd();
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | const insertRow = function(index, row) {
|
125 | if (!row) {
|
126 | row = new Row({
|
127 | parent: this
|
128 | })
|
129 | }
|
130 | this.insertRows(index, [row])
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | const insertRows = function(index, rows) {
|
151 | var args = [index, 0]
|
152 | for (var i = 0; i < rows.length; i++) {
|
153 | args.push(rows[i]);
|
154 | }
|
155 | this.rows.splice.apply(this.rows, args);
|
156 |
|
157 | this.updateSelectedIndices(index, '+', rows.length)
|
158 | this.updateFocusIndex(index, '+', rows.length)
|
159 | this.updatePageAll();
|
160 | this.trigger(DataTable.ON_INSERT, {
|
161 | index: index,
|
162 | rows: rows
|
163 | })
|
164 | if (this.ns) {
|
165 | if (this.root.valueChange[this.ns])
|
166 | this.root.valueChange[this.ns](-this.root.valueChange[this.ns]());
|
167 | }
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | const createEmptyRow = function(options) {
|
178 | var r = new Row({
|
179 | parent: this
|
180 | })
|
181 | this.addRow(r)
|
182 | var unSelect = options ? options.unSelect : false;
|
183 | if (!unSelect) {
|
184 | if (!this.getCurrentRow())
|
185 | this.setRowSelect(r);
|
186 | }
|
187 | return r
|
188 | }
|
189 |
|
190 | export const rowFunObj = {
|
191 | setRows: setRows,
|
192 | addRow: addRow,
|
193 | addRows: addRows,
|
194 | insertRow: insertRow,
|
195 | insertRows: insertRows,
|
196 | createEmptyRow: createEmptyRow,
|
197 | resetDelRowEnd: resetDelRowEnd
|
198 | }
|