Guest User

Untitled

a guest
Jul 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. (function() {
  2. const NAMESPACE = 'mfp.xrea.jp';
  3. if (!(NAMESPACE in window))
  4. window[NAMESPACE] = {};
  5. if (window[NAMESPACE].Pager !== undefined)
  6. return;
  7.  
  8. const Pager = function(namePrefix) {
  9. this._namePrefix = namePrefix;
  10. this._callbacks = [];
  11. this._numbers = [];
  12. };
  13.  
  14. Pager.prototype = {
  15. _namePrefix: null,
  16. _callbacks: null,
  17. addCallback: function(fun) {
  18. if (typeof fun !== 'function')
  19. return;
  20. if (this._callbacks.indexOf(fun) === -1)
  21. this._callbacks.push(fun);
  22. },
  23. removeCallback: function(fun) {
  24. let idx = this._callbacks.indexOf(fun);
  25. if (idx !== -1)
  26. this._callbacks.splice(idx, 1);
  27. },
  28. _callCallbacks: function() {
  29. let arg = arguments;
  30. this._callbacks.forEach(function(f) f.apply(null, arg));
  31. },
  32. _length: NaN, _current: NaN,
  33. _numbers: null,
  34. _createLink: function(text, href) {
  35. let link = document.createElement('a');
  36. link.href = '#' + href;
  37. link.className = this._namePrefix + 'page-link';
  38. link.textContent = text;
  39. return link;
  40. },
  41. update: function(length) {
  42. this._length = length;
  43. this.element.textContent = '';
  44. {
  45. let prev = this._createLink('\xab', 'prev');
  46. prev.classList.add(this._namePrefix + 'prev-page');
  47. this.element.appendChild(prev);
  48. }
  49. this._numbers = [];
  50. for (let i = 0; i < length; i++) {
  51. let link = this._createLink(i+1, i);
  52. this._numbers.push(link);
  53. this.element.appendChild(link);
  54. }
  55. {
  56. let next = this._createLink('\xbb', 'next');
  57. next.classList.add(this._namePrefix + 'next-page');
  58. this.element.appendChild(next);
  59. }
  60. },
  61. goTo: function(page) {
  62. if (page < 0) page = 0;
  63. if (page >= this._length) page = this._length - 1;
  64. let prefix = this._namePrefix;
  65. let length = this._length;
  66. this._numbers.forEach(function(e, i) {
  67. if (i === page)
  68. e.classList.add(prefix + 'current-page');
  69. else
  70. e.classList.remove(prefix + 'current-page');
  71. });
  72. this._current = page;
  73. },
  74. goNext: function() this.goTo(this._current + 1),
  75. goPrev: function() this.goTo(this._current - 1),
  76. handleEvent: function(e) {
  77. let { type, target } = e;
  78. if (type !== 'click' || target.nodeName !== 'A')
  79. return;
  80. e.preventDefault();
  81. let addr = target.getAttribute('href').slice(1);
  82. switch (addr) {
  83. case 'prev': this.goPrev(); break;
  84. case 'next': this.goNext(); break;
  85. default: this.goTo(parseInt(addr, 10)); break;
  86. }
  87. this._callCallbacks(this._current);
  88. },
  89. _element: null, get element() {
  90. if (this._element !== null) return this._element;
  91. this._element = document.createElement('span');
  92. this._element.addEventListener('click', this, false);
  93. return this._element;
  94. }
  95. };
  96.  
  97. window[NAMESPACE].Pager = Pager;
  98. })();
Add Comment
Please, Sign In to add comment