UNPKG

1.98 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable rowFocus
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-08 09:59:01
5 */
6import {isNumber} from 'tinper-sparrow/src/util';
7
8/**
9 * 设置焦点行
10 * @param {Object} index 行对象或者行index
11 * @param quiet 不触发事件
12 * @param force 当index行与已focus的行相等时,仍然触发事件
13 */
14const setRowFocus = function (index, quiet, force) {
15 var rowId = null
16 if (index instanceof Row) {
17 index = this.getIndexByRowId(index.rowId)
18 rowId = index.rowId
19 }
20 if (index === -1 || (index === this.focusIndex() && !force)) {
21 return;
22 }
23 this.focusIndex(index)
24 if (quiet) {
25 return;
26 }
27 this.currentRowChange(-this.currentRowChange())
28 if (!rowId) {
29 rowId = this.getRow(index).rowId
30 }
31 this.trigger(DataTable.ON_ROW_FOCUS, {
32 index: index,
33 rowId: rowId
34 })
35 this.updateCurrIndex();
36}
37
38
39/**
40 * 焦点行反选
41 */
42const setRowUnFocus = function () {
43 this.currentRowChange(-this.currentRowChange())
44 var indx = this.focusIndex(), rowId = null;
45 if (indx !== -1) {
46 rowId = this.getRow(indx).rowId
47 }
48 this.trigger(DataTable.ON_ROW_UNFOCUS, {
49 index: indx,
50 rowId: rowId
51 })
52 this.focusIndex(-1)
53 this.updateCurrIndex();
54}
55
56
57const updateFocusIndex = function (opIndex, opType, num) {
58 if (!isNumber(num)) {
59 num = 1
60 }
61 if (opIndex <= this.focusIndex() && this.focusIndex() != -1) {
62 if (opType === '+') {
63 this.focusIndex(this.focusIndex() + num)
64 } else if (opType === '-') {
65 if (this.focusIndex() >= opIndex && this.focusIndex() <= opIndex + num - 1) {
66 this.focusIndex(this.focusIndex() - 1)
67 } else if (this.focusIndex() > opIndex + num - 1) {
68 this.focusIndex(this.focusIndex() - num)
69 }
70 }
71 }
72}
73
74export {
75 setRowFocus,
76 setRowUnFocus,
77 updateFocusIndex
78}