Fix screen calculation in menus
Change-Id: I676caa4b4c288774d962164fcb85cb2d0e8c0842
diff --git a/dev/js/spec/menuSpec.js b/dev/js/spec/menuSpec.js
index a60d7ab..ba534de 100644
--- a/dev/js/spec/menuSpec.js
+++ b/dev/js/spec/menuSpec.js
@@ -1380,6 +1380,80 @@
expect(menu.shownItem(2).lcField()).toEqual(' veröffentlichungsdatum');
});
+ it('should be view upable and downable (1)', function () {
+ var menu = KorAP.OwnMenu.create(demolonglist);
+ menu.limit(7);
+
+ // Choose the final value
+ expect(menu.show(1)).toBe(true);
+
+ expect(menu.shownItem(0).active()).toBe(false);
+ expect(menu.shownItem(0).lcField()).toEqual(' titel');
+ expect(menu.shownItem(1).active()).toBe(true);
+ expect(menu.shownItem(2).active()).toBe(false);
+ expect(menu.shownItem(2).lcField()).toEqual(' veröffentlichungsdatum');
+ expect(menu.shownItem(6).active()).toBe(false);
+ expect(menu.shownItem(7)).toBe(undefined);
+
+ // Doesn't change anything
+ menu.viewUp();
+
+ expect(menu.shownItem(0).active()).toBe(false);
+ expect(menu.shownItem(0).lcField()).toEqual(' titel');
+ expect(menu.shownItem(1).active()).toBe(true);
+
+ menu.viewDown();
+
+ expect(menu.shownItem(0).active()).toBe(true);
+ expect(menu.shownItem(0).lcField()).toEqual(' untertitel');
+ expect(menu.shownItem(1).active()).toBe(false);
+
+ menu.viewDown();
+
+ expect(menu.shownItem(0).active()).toBe(false);
+ expect(menu.shownItem(0).lcField()).toEqual(' veröffentlichungsdatum');
+ expect(menu.shownItem(1).active()).toBe(false);
+
+ // No effect anymore
+ menu.viewDown();
+
+ expect(menu.shownItem(0).active()).toBe(false);
+ expect(menu.shownItem(0).lcField()).toEqual(' veröffentlichungsdatum');
+ expect(menu.shownItem(1).active()).toBe(false);
+ });
+
+ it('should be view upable and downable (2)', function () {
+
+ // List is longer than limit
+ var menu = KorAP.OwnMenu.create(demolist);
+ menu.limit(7);
+
+ // Choose the final value
+ expect(menu.show(1)).toBe(true);
+
+ expect(menu.shownItem(0).active()).toBe(false);
+ expect(menu.shownItem(0).lcField()).toEqual(' titel');
+ expect(menu.shownItem(1).active()).toBe(true);
+ expect(menu.shownItem(2).active()).toBe(false);
+ expect(menu.shownItem(2).lcField()).toEqual(' veröffentlichungsdatum');
+ expect(menu.shownItem(4).active()).toBe(false);
+ expect(menu.shownItem(5)).toBe(undefined);
+
+ // Doesn't change anything
+ menu.viewUp();
+
+ expect(menu.shownItem(0).active()).toBe(false);
+ expect(menu.shownItem(0).lcField()).toEqual(' titel');
+ expect(menu.shownItem(1).active()).toBe(true);
+
+ menu.viewDown();
+
+ expect(menu.shownItem(0).active()).toBe(false);
+ expect(menu.shownItem(0).lcField()).toEqual(' titel');
+ expect(menu.shownItem(1).active()).toBe(true);
+ });
+
+
it('should scroll to a chosen value (1)', function () {
var menu = KorAP.OwnMenu.create(demolist);
menu.limit(3);
diff --git a/dev/js/src/menu.js b/dev/js/src/menu.js
index 1f9d849..1fb1ffd 100644
--- a/dev/js/src/menu.js
+++ b/dev/js/src/menu.js
@@ -338,13 +338,18 @@
}
else if (e.type === "touchmove") {
var t = e.touches[0];
+
+ // TODO:
+ // Instead of using 26px, choose the item height
+ // or use the menu height // shownItems
+
// s.movetoRel(t.clientY - this._initTouch);
if ((this._lastTouch + 26) < t.clientY) {
- this.screen(this.offset - 1);
+ this.viewDown();
this._lastTouch = t.clientY;
}
else if ((this._lastTouch - 26) > t.clientY) {
- this.screen(this.offset + 1);
+ this.viewUp();
this._lastTouch = t.clientY;
}
e.halt();
@@ -425,19 +430,32 @@
* in the viewport.
*/
screen : function (nr) {
+
+ // Normalize negative values
if (nr < 0) {
nr = 0
}
+
+ // The shown list already shows everything
+ else if (this.liveLength() < this.limit()) {
+ return false;
+ }
+
+ // Move relatively to the next screen
else if (nr > (this.liveLength() - this.limit())) {
nr = (this.liveLength() - this.limit());
};
+ // no change
if (this.offset === nr)
- return;
+ return false;
this._showItems(nr);
+
+ return true;
},
+
/**
* Get the associated dom element.
*/
@@ -677,7 +695,7 @@
return this._list.length;
},
-
+
/**
* Make the next item in the filtered menu active
*/
@@ -816,6 +834,21 @@
},
+ /**
+ * Move the view one item up
+ */
+ viewUp : function () {
+ this.screen(this.offset - 1);
+ },
+
+
+ /**
+ * Move the view one item down
+ */
+ viewDown : function () {
+ this.screen(this.offset + 1);
+ },
+
// Unmark all items
_unmark : function () {
for (var i in this._list) {