Advertisement
Guest User

Untitled

a guest
May 4th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. var extend = function(obj){
  2. var others = [].slice.call(arguments,1);
  3. others.forEach(function(other){
  4. for(var j in other) obj[j] = other[j];
  5. });
  6. return obj;
  7. }
  8.  
  9. var Node = function(value){
  10. this.value = value;
  11. this.next = null;
  12. this.prev = null;
  13. this.page = parseInt(value.getAttribute("page"), 10);
  14. };
  15. Node.prototype = extend(Node.prototype, {});
  16.  
  17. var List = function(){
  18. this.length = 0;
  19. };
  20. List.prototype = extend(List.prototype, {
  21. push : function(value) {
  22. var node = new Node(value);
  23.  
  24. if(!(this.head || this.tail)){
  25. this.head = this.tail = node;
  26. this.head.next = this.head.prev = this.head;
  27. } else {
  28. node.prev = this.tail;
  29. this.tail.next = node;
  30. this.tail = node;
  31. this.tail.next = this.head;
  32. this.head.prev = this.tail;
  33. }
  34.  
  35. this.length++;
  36. }
  37. });
  38.  
  39. var list = new List();
  40. var current = null;
  41. var elems = [];
  42.  
  43. var show = function(node) { node.style.display = "block"; };
  44. var hide = function(node) { node.style.display = "none"; };
  45. // sets current by side effect
  46. var next = function(){
  47. elems.forEach(hide);
  48. current = current.next;
  49. show(current.value);
  50. };
  51. // sets current by side effect
  52. var prev = function(){
  53. elems.forEach(hide);
  54. current = current.prev;
  55. show(current.value);
  56. };
  57.  
  58. var keyPress = function(e){
  59. if(current){
  60. if([32, 39].indexOf(e.keyCode) > -1) {
  61. next();
  62. window.location.hash = current.page;
  63. } else if([37, 8].indexOf(e.keyCode) > -1) {
  64. prev();
  65. window.location.hash = current.page;
  66. }
  67. }
  68. };
  69.  
  70. var parseHash = function(n) {
  71. return parseInt(window.location.hash.replace(/#/,""), 10)
  72. }
  73.  
  74. // sets current by side effect
  75. var go = function(){
  76. elems = [].slice.call(document.getElementsByTagName("div"));
  77. elems.forEach(function(el, idx) {
  78. el.setAttribute("page", idx + 1);
  79. list.push(el);
  80. });
  81. current = list.head;
  82.  
  83. if (window.location.hash && parseHash(window.location.hash) > 1) {
  84. for (var i = 1; i < parseHash(window.location.hash); i++) {
  85. next();
  86. }
  87. } else {
  88. show(current.value);
  89. }
  90.  
  91. document.addEventListener("keydown", keyPress, false);
  92. //document.addEventListener("click", next, false);
  93. };
  94.  
  95. document.addEventListener("DOMContentLoaded", go, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement