1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {
|
7 | isArray,
|
8 | isNumber
|
9 | } from 'tinper-sparrow/src/util';
|
10 | import {
|
11 | utilFunObj
|
12 | } from './util';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const setAllRowsSelect = function() {
|
21 | var indices = new Array(this.rows().length)
|
22 | for (var i = 0; i < indices.length; i++) {
|
23 | indices[i] = i
|
24 | }
|
25 | this.setRowsSelect(indices);
|
26 | this.allSelected(true);
|
27 | this.trigger(DataTable.ON_ROW_ALLSELECT, {})
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | const setRowSelect = function(index) {
|
38 | if (index instanceof Row) {
|
39 | index = this.getIndexByRowId(index.rowId)
|
40 | }
|
41 | this.setRowsSelect([index])
|
42 | this.setRowFocus(this.getSelectedIndex())
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const setRowsSelect = function(indices) {
|
53 | indices = indices || -1;
|
54 | if (indices == -1) {
|
55 | this.setAllRowsUnSelect({
|
56 | quiet: true
|
57 | })
|
58 | return;
|
59 | }
|
60 | indices = utilFunObj._formatToIndicesArray(this, indices);
|
61 | var sIns = this.selectedIndices();
|
62 | if (isArray(indices) && isArray(sIns) && indices.join() == sIns.join()) {
|
63 |
|
64 | return;
|
65 | }
|
66 |
|
67 | if (isArray(indices)) {
|
68 | var rowNum = this.rows().length
|
69 | for (var i = 0; i < indices.length; i++) {
|
70 | if (indices[i] < 0 || indices[i] >= rowNum)
|
71 | indices.splice(i, 1);
|
72 | }
|
73 | }
|
74 |
|
75 | this.setAllRowsUnSelect({
|
76 | quiet: true
|
77 | });
|
78 | try {
|
79 | this.selectedIndices(indices);
|
80 | } catch (e) {
|
81 |
|
82 | }
|
83 | this.updatePageSelect();
|
84 | var rowIds = this.getRowIdsByIndices(indices);
|
85 | this.currentRowChange(-this.currentRowChange());
|
86 | this.trigger(DataTable.ON_ROW_SELECT, {
|
87 | indices: indices,
|
88 | rowIds: rowIds
|
89 | })
|
90 | this.updateCurrIndex();
|
91 |
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | const addRowSelect = function(index) {
|
103 | if (index instanceof Row) {
|
104 | index = this.getIndexByRowId(index.rowId)
|
105 | }
|
106 | this.addRowsSelect([index])
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | const addRowsSelect = function(indices) {
|
117 | indices = utilFunObj._formatToIndicesArray(this, indices)
|
118 | var selectedIndices = this.selectedIndices().slice()
|
119 | var needTrigger = false;
|
120 | for (var i = 0; i < indices.length; i++) {
|
121 | var ind = indices[i],
|
122 | toAdd = true
|
123 | for (var j = 0; j < selectedIndices.length; j++) {
|
124 | if (selectedIndices[j] == ind) {
|
125 | toAdd = false
|
126 | }
|
127 | }
|
128 |
|
129 | if (toAdd && indices[i] > -1) {
|
130 | needTrigger = true
|
131 | selectedIndices.push(indices[i])
|
132 | }
|
133 | }
|
134 | this.selectedIndices(selectedIndices)
|
135 | this.updatePageSelect();
|
136 | var rowIds = this.getRowIdsByIndices(selectedIndices)
|
137 | if (needTrigger) {
|
138 | this.trigger(DataTable.ON_ROW_SELECT, {
|
139 | indices: selectedIndices,
|
140 | rowIds: rowIds
|
141 | })
|
142 | }
|
143 | this.updateCurrIndex();
|
144 |
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | const setAllRowsUnSelect = function(options) {
|
157 | this.selectedIndices([])
|
158 | this.updatePageSelect();
|
159 | if (!(options && options.quiet)) {
|
160 | this.trigger(DataTable.ON_ROW_ALLUNSELECT)
|
161 | }
|
162 | this.updateCurrIndex();
|
163 | this.allSelected(false);
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | const setRowUnSelect = function(index) {
|
174 | if (index instanceof Row) {
|
175 | index = this.getIndexByRowId(index.rowId)
|
176 | }
|
177 | this.setRowsUnSelect([index])
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | const setRowsUnSelect = function(indices) {
|
188 | indices = utilFunObj._formatToIndicesArray(this, indices)
|
189 | var selectedIndices = this.selectedIndices().slice()
|
190 |
|
191 |
|
192 | if (selectedIndices.indexOf(indices[0]) == -1) return;
|
193 |
|
194 | for (var i = 0; i < indices.length; i++) {
|
195 | var index = indices[i]
|
196 | var pos = selectedIndices.indexOf(index)
|
197 | if (pos != -1)
|
198 | selectedIndices.splice(pos, 1)
|
199 | }
|
200 | this.selectedIndices(selectedIndices)
|
201 | this.updatePageSelect();
|
202 | var rowIds = this.getRowIdsByIndices(indices)
|
203 | this.trigger(DataTable.ON_ROW_UNSELECT, {
|
204 | indices: indices,
|
205 | rowIds: rowIds
|
206 | })
|
207 | this.updateCurrIndex();
|
208 | this.allSelected(false);
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | const toggleAllSelect = function() {
|
216 | if (this.allSelected()) {
|
217 | this.setAllRowsUnSelect();
|
218 | } else {
|
219 | this.setAllRowsSelect();
|
220 | }
|
221 |
|
222 | };
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | const updateSelectedIndices = function(index, type, num) {
|
233 | if (!isNumber(num)) {
|
234 | num = 1
|
235 | }
|
236 | var selectedIndices = this.selectedIndices().slice()
|
237 | if (selectedIndices == null || selectedIndices.length == 0)
|
238 | return
|
239 | for (var i = 0, count = selectedIndices.length; i < count; i++) {
|
240 | if (type == '+') {
|
241 | if (selectedIndices[i] >= index)
|
242 | selectedIndices[i] = parseInt(selectedIndices[i]) + num
|
243 | } else if (type == '-') {
|
244 | if (selectedIndices[i] >= index && selectedIndices[i] <= index + num - 1) {
|
245 | selectedIndices.splice(i, 1)
|
246 | } else if (selectedIndices[i] > index + num - 1)
|
247 | selectedIndices[i] = selectedIndices[i] - num
|
248 | }
|
249 | }
|
250 | this.selectedIndices(selectedIndices)
|
251 | this.updatePageSelect();
|
252 | }
|
253 | export const rowSelectFunObj = {
|
254 | setAllRowsSelect: setAllRowsSelect,
|
255 | setRowSelect: setRowSelect,
|
256 | setRowsSelect: setRowsSelect,
|
257 | addRowSelect: addRowSelect,
|
258 | addRowsSelect: addRowsSelect,
|
259 | setAllRowsUnSelect: setAllRowsUnSelect,
|
260 | setRowUnSelect: setRowUnSelect,
|
261 | setRowsUnSelect: setRowsUnSelect,
|
262 | toggleAllSelect: toggleAllSelect,
|
263 | updateSelectedIndices: updateSelectedIndices
|
264 | }
|