From 182599f6638fbbfd2a9a787466a6ba8209ea77bf Mon Sep 17 00:00:00 2001 From: Joan Date: Mon, 30 Jul 2018 12:12:41 +0200 Subject: [PATCH] Fixed scroll on itemDIary to line with alertLevel 1 #433 --- client/core/src/directives/repeat-last.js | 4 +- client/item/src/diary/index.js | 49 ++++++++++++++++------- client/item/src/diary/index.spec.js | 22 ++++++++-- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/client/core/src/directives/repeat-last.js b/client/core/src/directives/repeat-last.js index 894666cae..504fa5253 100644 --- a/client/core/src/directives/repeat-last.js +++ b/client/core/src/directives/repeat-last.js @@ -13,7 +13,9 @@ export function directive($parse) { link: function($scope, $element, $attrs) { if ($scope.$last && $attrs.onLast) { let fn = $parse($attrs.onLast); - fn($scope); + setTimeout(() => { + fn($scope); + }); } } }; diff --git a/client/item/src/diary/index.js b/client/item/src/diary/index.js index 79af25b13..15a012291 100644 --- a/client/item/src/diary/index.js +++ b/client/item/src/diary/index.js @@ -25,17 +25,6 @@ class Controller { return this._item; } - get alertLevelIndex() { - let lines = this.$scope.model.data; - for (let i = 0; i < lines.length; i++) { - let isFutureDate = new Date(lines[i].date) > new Date(); - let isGenreOut = lines[i].alertLevel != 0; - - if (!isFutureDate && !isGenreOut) - return i; - } - } - onChange(value) { if (!value) return; @@ -63,15 +52,47 @@ class Controller { } } + get freeLineIndex() { + let lines = this.$scope.model.data; + for (let i = 0; i < lines.length; i++) { + let isFutureDate = new Date(lines[i].date) > new Date(); + let isGenreOut = lines[i].alertLevel != 0; + + if (!isFutureDate && !isGenreOut) + return i; + } + } + + get onPreparationLineIndex() { + let lines = this.$scope.model.data; + + for (let i = this.freeLineIndex; i > 0; i--) { + let line = lines[i]; + + if (line.alertLevel == 1 && line.isPicked || line.alertLevel > 1) { + return i; + } + } + } + scrollToActive() { let body = this.$window.document.body; - let lineIndex = this.alertLevelIndex; + let lineIndex = this.onPreparationLineIndex; let lines = body.querySelector('vn-tbody').children; if (!lineIndex || !lines.length) return; - lines[lineIndex].scrollIntoView(); - lines[lineIndex - 1].querySelector('.balance').classList.add('counter'); + let onPreparationLine = lines[lineIndex]; + + let balance = onPreparationLine.querySelector('.balance'); + balance.classList.add('counter'); + balance.title = 'Visible quantity'; + + let headerOffset = body.querySelector('header').getBoundingClientRect(); + let headerHeight = headerOffset.height; + let offsetTop = onPreparationLine.offsetTop - headerHeight; + + body.querySelector('ui-view').scrollTop = offsetTop; } /** diff --git a/client/item/src/diary/index.spec.js b/client/item/src/diary/index.spec.js index 303a36083..ecece4f86 100644 --- a/client/item/src/diary/index.spec.js +++ b/client/item/src/diary/index.spec.js @@ -1,6 +1,6 @@ import './index.js'; -describe('Item', () => { +fdescribe('Item', () => { describe('Component vnItemDiary', () => { let $componentController; let $scope; @@ -38,19 +38,33 @@ describe('Item', () => { }); }); - describe('alertLevelIndex()', () => { - it(`should call alertLevelIndex() and return an index from line with alertLevel 0 and current date`, () => { + describe('freeLineIndex()', () => { + it(`should call freeLineIndex() and return an index from line with alertLevel 0 and current date`, () => { controller.$scope.model = {data: [ {name: 'My item 1', alertLevel: 3, date: '2018-05-02'}, {name: 'My item 2', alertLevel: 1, date: '2018-05-03'}, {name: 'My item 3', alertLevel: 0, date: new Date()}] }; - let result = controller.alertLevelIndex; + let result = controller.freeLineIndex; expect(result).toEqual(2); }); }); + describe('onPreparationLineIndex()', () => { + it(`should call onPreparationLineIndex() and return an index from line with alertLevel 1 and isPicked true`, () => { + controller.$scope.model = {data: [ + {name: 'My item 1', alertLevel: 3, isPicked: true, date: '2018-05-02'}, + {name: 'My item 3', alertLevel: 1, isPicked: true, date: '2018-05-03'}, + {name: 'My item 4', alertLevel: 1, isPicked: false, date: '2018-05-03'}, + {name: 'My item 5', alertLevel: 0, isPicked: false, date: new Date()}] + }; + let result = controller.onPreparationLineIndex; + + expect(result).toEqual(1); + }); + }); + describe('set item()', () => { it(`should call filterBuilder()`, () => { spyOn(controller, 'filterBuilder');