NRuby
104 строки · 2.7 Кб
1/*
2* Navigation allows movement using the arrow keys through the search results.
3*
4* When using this library you will need to set scrollIntoView to the
5* appropriate function for your layout. Use scrollInWindow if the container
6* is not scrollable and scrollInElement if the container is a separate
7* scrolling region.
8*/
9Navigation = new function() {10this.initNavigation = function() {11var _this = this;12
13document.addEventListener('keydown', function(e) {14_this.onkeydown(e);15});16
17this.navigationActive = true;18}19
20this.setNavigationActive = function(state) {21this.navigationActive = state;22}23
24this.onkeydown = function(e) {25if (!this.navigationActive) return;26switch(e.keyCode) {27case 37: //Event.KEY_LEFT:28if (this.moveLeft()) e.preventDefault();29break;30case 38: //Event.KEY_UP:31if (e.keyCode == 38 || e.ctrlKey) {32if (this.moveUp()) e.preventDefault();33}34break;35case 39: //Event.KEY_RIGHT:36if (this.moveRight()) e.preventDefault();37break;38case 40: //Event.KEY_DOWN:39if (e.keyCode == 40 || e.ctrlKey) {40if (this.moveDown()) e.preventDefault();41}42break;43case 13: //Event.KEY_RETURN:44if (this.current) e.preventDefault();45this.select(this.current);46break;47}48if (e.ctrlKey && e.shiftKey) this.select(this.current);49}50
51this.moveRight = function() {52}53
54this.moveLeft = function() {55}56
57this.move = function(isDown) {58}59
60this.moveUp = function() {61return this.move(false);62}63
64this.moveDown = function() {65return this.move(true);66}67
68/*69* Scrolls to the given element in the scrollable element view.
70*/
71this.scrollInElement = function(element, view) {72var offset, viewHeight, viewScroll, height;73offset = element.offsetTop;74height = element.offsetHeight;75viewHeight = view.offsetHeight;76viewScroll = view.scrollTop;77
78if (offset - viewScroll + height > viewHeight) {79view.scrollTop = offset - viewHeight + height;80}81if (offset < viewScroll) {82view.scrollTop = offset;83}84}85
86/*87* Scrolls to the given element in the window. The second argument is
88* ignored
89*/
90this.scrollInWindow = function(element, ignored) {91var offset, viewHeight, viewScroll, height;92offset = element.offsetTop;93height = element.offsetHeight;94viewHeight = window.innerHeight;95viewScroll = window.scrollY;96
97if (offset - viewScroll + height > viewHeight) {98window.scrollTo(window.scrollX, offset - viewHeight + height);99}100if (offset < viewScroll) {101window.scrollTo(window.scrollX, offset);102}103}104}
105
106