From 0d8f1f88793d2893f31b7b6c1be3b7ca6306a6d4 Mon Sep 17 00:00:00 2001 From: Tomi Virkki Date: Thu, 13 Oct 2016 16:20:06 +0300 Subject: [PATCH] Sorting helpers feature --- test/array-data-source.html | 6 - vaadin-grid-array-data-source-behavior.html | 27 +++- vaadin-grid-cell-click-behavior.html | 38 +++++ vaadin-grid-data-source-behavior.html | 37 +++-- vaadin-grid-sort-behavior.html | 161 ++++++++++++++++++++ vaadin-grid-table-cell.html | 38 ++--- vaadin-grid.html | 4 +- 7 files changed, 258 insertions(+), 53 deletions(-) create mode 100644 vaadin-grid-cell-click-behavior.html create mode 100644 vaadin-grid-sort-behavior.html diff --git a/test/array-data-source.html b/test/array-data-source.html index 6c270f18d..121057f63 100644 --- a/test/array-data-source.html +++ b/test/array-data-source.html @@ -91,12 +91,6 @@ expect(grid.size).to.equal(0); }); - it('should handle string values', function() { - grid.items = 'FOO'; - expect(grid.size).to.equal(3); - expect(getCell(0, 0).item).to.equal('F'); - }); - it('should set array data source', function() { expect(grid.dataSource).to.equal(grid._arrayDataSource); }); diff --git a/vaadin-grid-array-data-source-behavior.html b/vaadin-grid-array-data-source-behavior.html index 2342993ac..38271e685 100644 --- a/vaadin-grid-array-data-source-behavior.html +++ b/vaadin-grid-array-data-source-behavior.html @@ -26,11 +26,36 @@ }, _arrayDataSource: function(opts, cb) { - var items = this.items || []; + var items = (this.items || []).slice(0); + items.sort(this._multiSort.bind(this)); + var start = opts.page * opts.pageSize; var end = start + opts.pageSize; var slice = items.slice(start, end); cb(slice, items.length); + }, + + _multiSort: function(a, b) { + return (this._sorters || []).map(function(sort) { + if (sort.direction === 'asc') { + return this._compare(Polymer.Base.get(sort.path, a), Polymer.Base.get(sort.path, b)); + } else if (sort.direction === 'desc') { + return this._compare(Polymer.Base.get(sort.path, b), Polymer.Base.get(sort.path, a)); + } + return 0; + }, this).reduce(function firstNonZeroValue(p, n) { + return p ? p : n; + }, 0); + }, + + _compare: function(a, b) { + if (a < b) { + return -1; + } + if (a > b) { + return 1; + } + return 0; } }; diff --git a/vaadin-grid-cell-click-behavior.html b/vaadin-grid-cell-click-behavior.html new file mode 100644 index 000000000..5d5f0e1f6 --- /dev/null +++ b/vaadin-grid-cell-click-behavior.html @@ -0,0 +1,38 @@ + diff --git a/vaadin-grid-data-source-behavior.html b/vaadin-grid-data-source-behavior.html index 3bf3f83f4..da212287f 100644 --- a/vaadin-grid-data-source-behavior.html +++ b/vaadin-grid-data-source-behavior.html @@ -162,23 +162,25 @@ var firstVisiblePage = this._getPageForIndex(this.$.scroller.firstVisibleIndex + this.$.scroller._vidxOffset); return [firstVisiblePage].concat( - this.$.scroller._physicalItems - .filter(function(row) { return row.index; }) - .map(function(row) { - return this._getPageForIndex(row.index); - }.bind(this))) - .reduce(function(prev, curr) { - if (prev.indexOf(curr) === -1) { - prev.push(curr); - } + this.$.scroller._physicalItems + .filter(function(row) { + return row.index; + }) + .map(function(row) { + return this._getPageForIndex(row.index); + }.bind(this))) + .reduce(function(prev, curr) { + if (prev.indexOf(curr) === -1) { + prev.push(curr); + } - return prev; - }, []); + return prev; + }, []); }, _updateItems: function(page, items) { - for(var i=0;i + + + + + + + diff --git a/vaadin-grid-table-cell.html b/vaadin-grid-table-cell.html index 22ab18ad7..92b14b6b6 100644 --- a/vaadin-grid-table-cell.html +++ b/vaadin-grid-table-cell.html @@ -1,3 +1,5 @@ + + @@ -223,38 +225,16 @@ behaviors: [ Polymer.Templatizer, - vaadinGridTableCellBehavior + vaadinGridTableCellBehavior, + vaadin.elements.grid.CellClickBehavior ], - listeners: { - 'click': '_onClick' - }, - - _isFocusable: function(target) { - if (Polymer.Settings.useNativeShadow) { - // https://nemisj.com/focusable/ - // tabIndex is not reliable in IE. - return target.tabIndex >= 0; - } else { - // unreliable with Shadow, document.activeElement doesn't go inside - // the shadow root. - // Also, atleast iOS doesn't seem to focus links or buttons. - var focusableElements = ['A', 'BUTTON']; - return target.contains(Polymer.dom(document.activeElement).node) || focusableElements.indexOf(target.tagName) > -1; - } - }, - - // we need to listen to click instead of tap because on mobile safari, the - // document.activeElement has not been updated (focus has not been shifted) - // yet at the point when tap event is being executed. - _onClick: function(e) { - // Prevent item action if cell itself is not focused. - if (!this._isFocusable(Polymer.dom(e).localTarget)) { - this.fire('cell-click', { - model: this.instance - }); - } + _cellClick: function() { + this.fire('cell-click', { + model: this.instance + }); } + }); Polymer({ diff --git a/vaadin-grid.html b/vaadin-grid.html index 8bbfb81d9..18b65dd89 100644 --- a/vaadin-grid.html +++ b/vaadin-grid.html @@ -5,6 +5,7 @@ +