1- import Model from '../Model.mjs' ;
1+ import Model from '../Model.mjs' ;
2+ import NeoArray from '../../util/Array.mjs' ;
23
34/**
45 * Abstract base class for all grid related selection models
@@ -12,7 +13,13 @@ class BaseModel extends Model {
1213 * @member {String} className='Neo.selection.grid.BaseModel'
1314 * @protected
1415 */
15- className : 'Neo.selection.grid.BaseModel'
16+ className : 'Neo.selection.grid.BaseModel' ,
17+ /**
18+ * Storing the record ids
19+ * @member {Number[]|String[]} selectedRows=[]
20+ * @protected
21+ */
22+ selectedRows : [ ]
1623 }
1724
1825 /**
@@ -23,6 +30,53 @@ class BaseModel extends Model {
2330 return this . view . parent . columns . items . map ( column => column . dataField )
2431 }
2532
33+ /**
34+ * @param {Boolean } [silent=false] true to prevent a vdom update
35+ */
36+ deselectAllRows ( silent = false ) {
37+ let me = this ,
38+ items = [ ...me . selectedRows ] ,
39+ { view} = me ;
40+
41+ if ( items . length ) {
42+ items . forEach ( item => {
43+ me . deselectRow ( item , true )
44+ } ) ;
45+
46+ if ( ! silent && items . length > 0 ) {
47+ view . update ( )
48+ }
49+
50+ me . fire ( 'selectionChange' , {
51+ selection : me . selectedRows
52+ } )
53+ } else if ( ! silent ) {
54+ me . fire ( 'noChange' )
55+ }
56+ }
57+
58+ /**
59+ * @param {Number|String } recordId
60+ * @param {Boolean } [silent=false]
61+ */
62+ deselectRow ( recordId , silent = false ) {
63+ let me = this ,
64+ { view} = me ,
65+ { store} = view ,
66+ record = store . get ( recordId ) ,
67+ rowId = view . getRowId ( store . indexOf ( record ) ) ,
68+ node = view . getVdomChild ( rowId ) ;
69+
70+ if ( node ) {
71+ node . cls = NeoArray . remove ( node . cls || [ ] , me . selectedCls ) ;
72+ delete node [ 'aria-selected' ]
73+ }
74+
75+ me . selectedRows = [ recordId ] ;
76+
77+ ! silent && view . update ( )
78+ }
79+
2680 /**
2781 * Get the record for a given event path
2882 * @param {Object[] } path
@@ -61,6 +115,48 @@ class BaseModel extends Model {
61115
62116 return false
63117 }
118+
119+ /**
120+ * @param {Number|String } recordId
121+ * @returns {Boolean } true in case the row is selected
122+ */
123+ isSelectedRow ( recordId ) {
124+ return this . selectedRows . includes ( recordId )
125+ }
126+
127+ /**
128+ * @param {Number|String } recordId
129+ * @param {Boolean } [silent=false]
130+ */
131+ selectRow ( recordId , silent = false ) {
132+ let me = this ,
133+ { view} = me ,
134+ { store} = view ,
135+ record = store . get ( recordId ) ,
136+ rowId = view . getRowId ( store . indexOf ( record ) ) ,
137+ node = view . getVdomChild ( rowId ) ;
138+
139+ if ( me . singleSelect ) {
140+ me . deselectAllRows ( true )
141+ }
142+
143+ if ( node ) {
144+ node . cls = NeoArray . add ( node . cls || [ ] , me . selectedCls ) ;
145+ node [ 'aria-selected' ] = true
146+ }
147+
148+ me . selectedRows = [ recordId ] ;
149+
150+ ! silent && view . update ( )
151+ }
152+
153+ /**
154+ * @param {Number|String } recordId
155+ * @param {Boolean } [silent=false]
156+ */
157+ toggleRowSelection ( recordId , silent = false ) {
158+ this [ this . isSelectedRow ( recordId ) ? 'deselectRow' : 'selectRow' ] ( recordId , silent )
159+ }
64160}
65161
66162export default Neo . setupClass ( BaseModel ) ;
0 commit comments