33 * Copyright (c) 2021 Vaadin Ltd.
44 * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55 */
6- import { Base } from '@polymer/polymer/polymer-legacy .js' ;
6+ import { createArrayDataProvider } from './array-data-provider .js' ;
77
88/**
99 * @polymerMixin
@@ -22,152 +22,54 @@ export const ArrayDataProviderMixin = (superClass) =>
2222 }
2323
2424 static get observers ( ) {
25- return [ '_itemsChanged( items, items.*, isAttached )' ] ;
25+ return [ '__dataProviderOrItemsChanged(dataProvider, items, isAttached, items.*, _filters, _sorters )' ] ;
2626 }
2727
2828 /** @private */
29- _itemsChanged ( items , splices , isAttached ) {
30- if ( ! isAttached ) {
31- return ;
32- }
33- if ( ! Array . isArray ( items ) ) {
34- if ( this . dataProvider === this . _arrayDataProvider ) {
35- this . dataProvider = undefined ;
36- this . size = 0 ;
37- }
38- } else {
39- this . size = items . length ;
40- this . dataProvider = this . dataProvider || this . _arrayDataProvider ;
41- this . clearCache ( ) ;
42- this . _ensureFirstPageLoaded ( ) ;
43- }
44- }
45-
46- /**
47- * @param {GridDataProviderParams } opts
48- * @param {GridDataProviderCallback } cb
49- * @protected
50- */
51- _arrayDataProvider ( opts , cb ) {
52- let items = ( Array . isArray ( this . items ) ? this . items : [ ] ) . slice ( 0 ) ;
53-
54- if ( this . _filters && this . _checkPaths ( this . _filters , 'filtering' , items ) ) {
55- items = this . _filter ( items ) ;
56- }
57-
58- this . size = items . length ;
59-
60- if ( opts . sortOrders . length && this . _checkPaths ( this . _sorters , 'sorting' , items ) ) {
61- items = items . sort ( this . _multiSort . bind ( this ) ) ;
62- }
63-
64- const start = opts . page * opts . pageSize ;
65- const end = start + opts . pageSize ;
66- const slice = items . slice ( start , end ) ;
67- cb ( slice , items . length ) ;
29+ __setArrayDataProvider ( items ) {
30+ const arrayDataProvider = createArrayDataProvider ( this . items , { } ) ;
31+ arrayDataProvider . __items = items ;
32+ this . setProperties ( {
33+ _arrayDataProvider : arrayDataProvider ,
34+ size : items . length ,
35+ dataProvider : arrayDataProvider
36+ } ) ;
6837 }
6938
70- /**
71- * Check array of filters/sorters for paths validity, console.warn invalid items
72- * @param {!Array<!GridFilter | !GridSorter> } arrayToCheck The array of filters/sorters to check
73- * @param {string } action The name of action to include in warning (filtering, sorting)
74- * @param {!Array<!GridItem> } items
75- * @protected
76- */
77- _checkPaths ( arrayToCheck , action , items ) {
78- if ( ! items . length ) {
79- return false ;
39+ /** @private */
40+ __dataProviderOrItemsChanged ( dataProvider , items , isAttached ) {
41+ if ( ! isAttached ) {
42+ return ;
8043 }
8144
82- let result = true ;
83-
84- for ( let i in arrayToCheck ) {
85- const path = arrayToCheck [ i ] . path ;
86-
87- // skip simple paths
88- if ( ! path || path . indexOf ( '.' ) === - 1 ) {
89- continue ;
90- }
91-
92- const parentProperty = path . replace ( / \. [ ^ . ] * $ / , '' ) ; // a.b.c -> a.b
93- if ( Base . get ( parentProperty , items [ 0 ] ) === undefined ) {
94- console . warn ( `Path "${ path } " used for ${ action } does not exist in all of the items, ${ action } is disabled.` ) ;
95- result = false ;
45+ if ( this . _arrayDataProvider ) {
46+ // Has an items array data provider beforehand
47+
48+ if ( dataProvider !== this . _arrayDataProvider ) {
49+ // A custom data provider was set externally
50+ this . setProperties ( {
51+ _arrayDataProvider : undefined ,
52+ items : undefined
53+ } ) ;
54+ } else if ( ! items ) {
55+ // The items array was unset
56+ this . setProperties ( {
57+ _arrayDataProvider : undefined ,
58+ dataProvider : undefined ,
59+ size : 0
60+ } ) ;
61+ this . clearCache ( ) ;
62+ } else if ( this . _arrayDataProvider . __items === items ) {
63+ // The items array was modified
64+ this . clearCache ( ) ;
65+ this . size = this . _effectiveSize ;
66+ } else {
67+ // The items array was replaced
68+ this . __setArrayDataProvider ( items ) ;
9669 }
70+ } else if ( items ) {
71+ // There was no array data provider before items was set
72+ this . __setArrayDataProvider ( items ) ;
9773 }
98-
99- return result ;
100- }
101-
102- /**
103- * @param {unknown } a
104- * @param {unknown } b
105- * @return {number }
106- * @protected
107- */
108- _multiSort ( a , b ) {
109- return this . _sorters
110- . map ( ( sort ) => {
111- if ( sort . direction === 'asc' ) {
112- return this . _compare ( Base . get ( sort . path , a ) , Base . get ( sort . path , b ) ) ;
113- } else if ( sort . direction === 'desc' ) {
114- return this . _compare ( Base . get ( sort . path , b ) , Base . get ( sort . path , a ) ) ;
115- }
116- return 0 ;
117- } )
118- . reduce ( ( p , n ) => {
119- return p ? p : n ;
120- } , 0 ) ;
121- }
122-
123- /**
124- * @param {unknown } value
125- * @return {string }
126- * @protected
127- */
128- _normalizeEmptyValue ( value ) {
129- if ( [ undefined , null ] . indexOf ( value ) >= 0 ) {
130- return '' ;
131- } else if ( isNaN ( value ) ) {
132- return value . toString ( ) ;
133- } else {
134- return value ;
135- }
136- }
137-
138- /**
139- * @param {unknown } a
140- * @param {unknown } b
141- * @return {number }
142- * @protected
143- */
144- _compare ( a , b ) {
145- a = this . _normalizeEmptyValue ( a ) ;
146- b = this . _normalizeEmptyValue ( b ) ;
147-
148- if ( a < b ) {
149- return - 1 ;
150- }
151- if ( a > b ) {
152- return 1 ;
153- }
154- return 0 ;
155- }
156-
157- /**
158- * @param {!Array<!GridItem> } items
159- * @return {!Array<!GridItem> }
160- * @protected
161- */
162- _filter ( items ) {
163- return items . filter ( ( item ) => {
164- return (
165- this . _filters . filter ( ( filter ) => {
166- const value = this . _normalizeEmptyValue ( Base . get ( filter . path , item ) ) ;
167- const filterValueLowercase = this . _normalizeEmptyValue ( filter . value ) . toString ( ) . toLowerCase ( ) ;
168- return value . toString ( ) . toLowerCase ( ) . indexOf ( filterValueLowercase ) === - 1 ;
169- } ) . length === 0
170- ) ;
171- } ) ;
17274 }
17375 } ;
0 commit comments