UNPKG

2.76 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable rowFocus
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-08 09:59:01
5 */
6import {
7 isNumber
8} from 'tinper-sparrow/src/util';
9
10/**
11 * 设置焦点行
12 * @memberof DataTable
13 * @param {number|u.Row} index 行对象或者行index
14 * @param {boolean} [quiet] 如果为true则不触发事件,否则触发事件
15 * @param {boolean} [force] 如果为true当index行与已focus的行相等时,仍然触发事件,否则不触发事件
16 * @example
17 * datatable.setRowFocus(1) // 设置第二行为焦点行
18 * datatable.setRowFocus(1,true) // 设置第二行为焦点行,不触发事件
19 * datatable.setRowFocus(1,false,true) // 设置第二行为焦点行,如果当前焦点行为第二行,仍旧触发事件
20 */
21const setRowFocus = function(index, quiet, force) {
22 var rowId = null
23 if (index instanceof Row) {
24 index = this.getIndexByRowId(index.rowId)
25 rowId = index.rowId
26 }
27 if (index === -1 || (index === this.focusIndex() && !force)) {
28 return;
29 }
30 this.focusIndex(index)
31 if (quiet) {
32 return;
33 }
34 this.currentRowChange(-this.currentRowChange())
35 if (!rowId) {
36 rowId = this.getRow(index).rowId
37 }
38 this.trigger(DataTable.ON_ROW_FOCUS, {
39 index: index,
40 rowId: rowId
41 })
42 this.updateCurrIndex();
43}
44
45
46/**
47 * 焦点行反选
48 * @memberof DataTable
49 * @example
50 * datatable.setRowUnFocus()
51 */
52const setRowUnFocus = function() {
53 this.currentRowChange(-this.currentRowChange())
54 var indx = this.focusIndex(),
55 rowId = null;
56 if (indx !== -1) {
57 rowId = this.getRow(indx).rowId
58 }
59 this.trigger(DataTable.ON_ROW_UNFOCUS, {
60 index: indx,
61 rowId: rowId
62 })
63 this.focusIndex(-1)
64 this.updateCurrIndex();
65}
66
67/***
68 * 数据行发生改变时更新focusindex
69 * @memberof DataTable
70 * @param {number} opIndex 发生改变的数据行位置
71 * @param {string} opType +表示新增行,-表示减少行
72 * @param {number} num 新增/减少的行数
73 *
74 */
75const updateFocusIndex = function(opIndex, opType, num) {
76 if (!isNumber(num)) {
77 num = 1
78 }
79 if (opIndex <= this.focusIndex() && this.focusIndex() != -1) {
80 if (opType === '+') {
81 this.focusIndex(this.focusIndex() + num)
82 } else if (opType === '-') {
83 if (this.focusIndex() >= opIndex && this.focusIndex() <= opIndex + num - 1) {
84 this.focusIndex(-1)
85 } else if (this.focusIndex() > opIndex + num - 1) {
86 this.focusIndex(this.focusIndex() - num)
87 }
88 }
89 }
90}
91
92export const rowFocusFunObj = {
93 setRowFocus: setRowFocus,
94 setRowUnFocus: setRowUnFocus,
95 updateFocusIndex: updateFocusIndex
96}