UNPKG

2.54 kBJavaScriptView Raw
1/**
2 * @class ApHistorys
3 */
4'use strict';
5
6/** @lends ApHistory */
7
8var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
10function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11
12var ApHistory = function () {
13 function ApHistory(conf) {
14 _classCallCheck(this, ApHistory);
15
16 var s = this;
17 Object.assign(s, conf || {});
18 }
19
20 /**
21 * Push a history.
22 * @param {string} url - History url
23 */
24
25
26 _createClass(ApHistory, [{
27 key: 'push',
28 value: function push(url) {
29 var s = this;
30 var history = s._getHistory();
31 if (!history) {
32 ApHistory.warn('`history` not found.');
33 return;
34 }
35 history.pushState(null, '', url);
36 }
37
38 /**
39 * Register pop handler.
40 * @param {function} handler
41 */
42
43 }, {
44 key: 'onPop',
45 value: function onPop(handler) {
46 var s = this;
47 var window = s._getWindow();
48 if (!window) {
49 ApHistory.warn('`window` not found.');
50 return;
51 }
52 window.addEventListener('popstate', handler);
53 }
54
55 /**
56 * Deregister pop handler.
57 * @param {function} handler
58 */
59
60 }, {
61 key: 'offPop',
62 value: function offPop(handler) {
63 var s = this;
64 var window = s._getWindow();
65 if (!window) {
66 ApHistory.warn('`window` not found.');
67 return;
68 }
69 window.removeEventListener('popstate', handler);
70 }
71 }, {
72 key: '_getWindow',
73 value: function _getWindow() {
74 var s = this;
75 return typeof window === 'undefined' ? null : window;
76 }
77 }, {
78 key: '_getHistory',
79 value: function _getHistory() {
80 var s = this;
81 return typeof history === 'undefined' ? null : history;
82 }
83 }]);
84
85 return ApHistory;
86}();
87
88Object.assign(ApHistory, {
89 /**
90 * Print warning.
91 * @param {string} msg - Warning message.
92 */
93
94 warn: function warn(msg) {
95 console.log('[ApHistory]', msg);
96 }
97});
98
99module.exports = ApHistory;
\No newline at end of file