@converse/skeletor 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -25
- package/dist/skeletor.js +10413 -6875
- package/dist/skeletor.js.map +1 -1
- package/dist/skeletor.min.js +74 -3
- package/dist/skeletor.min.js.map +1 -1
- package/package.json +23 -12
- package/src/collection.js +540 -395
- package/src/drivers/sessionStorage.js +226 -232
- package/src/element.js +132 -82
- package/src/eventemitter.js +187 -0
- package/src/helpers.js +148 -114
- package/src/{main.js → index.js} +21 -17
- package/src/listening.js +81 -0
- package/src/model.js +340 -240
- package/src/storage.js +223 -241
- package/src/types/collection.d.ts +366 -0
- package/src/types/collection.d.ts.map +1 -0
- package/src/types/drivers/sessionStorage.d.ts +37 -0
- package/src/types/drivers/sessionStorage.d.ts.map +1 -0
- package/src/types/element.d.ts +115 -0
- package/src/types/element.d.ts.map +1 -0
- package/src/types/eventemitter.d.ts +93 -0
- package/src/types/eventemitter.d.ts.map +1 -0
- package/src/types/helpers.d.ts +62 -0
- package/src/types/helpers.d.ts.map +1 -0
- package/src/types/index.d.ts +19 -0
- package/src/types/index.d.ts.map +1 -0
- package/src/types/listening.d.ts +40 -0
- package/src/types/listening.d.ts.map +1 -0
- package/src/types/model.d.ts +230 -0
- package/src/types/model.d.ts.map +1 -0
- package/src/types/storage.d.ts +35 -0
- package/src/types/storage.d.ts.map +1 -0
- package/src/types/utils/events.d.ts +28 -0
- package/src/types/utils/events.d.ts.map +1 -0
- package/src/utils/events.js +174 -0
- package/src/drivers/indexeddb-getitems.js~ +0 -1116
- package/src/drivers/indexeddb.js~ +0 -1115
- package/src/events.js +0 -328
- package/src/history.js +0 -301
- package/src/overview.js +0 -191
- package/src/router.js +0 -119
- package/src/storage.js~ +0 -269
- package/src/view.js +0 -267
package/src/events.js
DELETED
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
// Backbone.js 1.4.0
|
|
2
|
-
// (c) 2010-2019 Jeremy Ashkenas and DocumentCloud
|
|
3
|
-
// Backbone may be freely distributed under the MIT license.
|
|
4
|
-
|
|
5
|
-
// Events
|
|
6
|
-
// ------
|
|
7
|
-
|
|
8
|
-
// A module that can be mixed in to *any object* in order to provide it with
|
|
9
|
-
// a custom event channel. You may bind a callback to an event with `on` or
|
|
10
|
-
// remove with `off`; `trigger`-ing an event fires all callbacks in
|
|
11
|
-
// succession.
|
|
12
|
-
//
|
|
13
|
-
// let object = {};
|
|
14
|
-
// extend(object, Backbone.Events);
|
|
15
|
-
// object.on('expand', function(){ alert('expanded'); });
|
|
16
|
-
// object.trigger('expand');
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
import isEmpty from 'lodash-es/isEmpty.js';
|
|
20
|
-
import keys from 'lodash-es/keys.js';
|
|
21
|
-
import once from 'lodash-es/once.js';
|
|
22
|
-
import uniqueId from 'lodash-es/uniqueId.js';
|
|
23
|
-
|
|
24
|
-
export const Events = {};
|
|
25
|
-
|
|
26
|
-
// Regular expression used to split event strings.
|
|
27
|
-
const eventSplitter = /\s+/;
|
|
28
|
-
|
|
29
|
-
// A private global variable to share between listeners and listenees.
|
|
30
|
-
let _listening;
|
|
31
|
-
|
|
32
|
-
// Iterates over the standard `event, callback` (as well as the fancy multiple
|
|
33
|
-
// space-separated events `"change blur", callback` and jQuery-style event
|
|
34
|
-
// maps `{event: callback}`).
|
|
35
|
-
const eventsApi = function(iteratee, events, name, callback, opts) {
|
|
36
|
-
let i = 0, names;
|
|
37
|
-
if (name && typeof name === 'object') {
|
|
38
|
-
// Handle event maps.
|
|
39
|
-
if (callback !== undefined && 'context' in opts && opts.context === undefined) opts.context = callback;
|
|
40
|
-
for (names = keys(name); i < names.length ; i++) {
|
|
41
|
-
events = eventsApi(iteratee, events, names[i], name[names[i]], opts);
|
|
42
|
-
}
|
|
43
|
-
} else if (name && eventSplitter.test(name)) {
|
|
44
|
-
// Handle space-separated event names by delegating them individually.
|
|
45
|
-
for (names = name.split(eventSplitter); i < names.length; i++) {
|
|
46
|
-
events = iteratee(events, names[i], callback, opts);
|
|
47
|
-
}
|
|
48
|
-
} else {
|
|
49
|
-
// Finally, standard events.
|
|
50
|
-
events = iteratee(events, name, callback, opts);
|
|
51
|
-
}
|
|
52
|
-
return events;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// Bind an event to a `callback` function. Passing `"all"` will bind
|
|
56
|
-
// the callback to all events fired.
|
|
57
|
-
Events.on = function(name, callback, context) {
|
|
58
|
-
this._events = eventsApi(onApi, this._events || {}, name, callback, {
|
|
59
|
-
context: context,
|
|
60
|
-
ctx: this,
|
|
61
|
-
listening: _listening
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
if (_listening) {
|
|
65
|
-
const listeners = this._listeners || (this._listeners = {});
|
|
66
|
-
listeners[_listening.id] = _listening;
|
|
67
|
-
// Allow the listening to use a counter, instead of tracking
|
|
68
|
-
// callbacks for library interop
|
|
69
|
-
_listening.interop = false;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return this;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// Inversion-of-control versions of `on`. Tell *this* object to listen to
|
|
76
|
-
// an event in another object... keeping track of what it's listening to
|
|
77
|
-
// for easier unbinding later.
|
|
78
|
-
Events.listenTo = function(obj, name, callback) {
|
|
79
|
-
if (!obj) return this;
|
|
80
|
-
const id = obj._listenId || (obj._listenId = uniqueId('l'));
|
|
81
|
-
const listeningTo = this._listeningTo || (this._listeningTo = {});
|
|
82
|
-
let listening = _listening = listeningTo[id];
|
|
83
|
-
|
|
84
|
-
// This object is not listening to any other events on `obj` yet.
|
|
85
|
-
// Setup the necessary references to track the listening callbacks.
|
|
86
|
-
if (!listening) {
|
|
87
|
-
this._listenId || (this._listenId = uniqueId('l'));
|
|
88
|
-
listening = _listening = listeningTo[id] = new Listening(this, obj);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Bind callbacks on obj.
|
|
92
|
-
const error = tryCatchOn(obj, name, callback, this);
|
|
93
|
-
_listening = undefined;
|
|
94
|
-
|
|
95
|
-
if (error) throw error;
|
|
96
|
-
// If the target obj is not Backbone.Events, track events manually.
|
|
97
|
-
if (listening.interop) listening.on(name, callback);
|
|
98
|
-
|
|
99
|
-
return this;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
// The reducing API that adds a callback to the `events` object.
|
|
103
|
-
const onApi = function(events, name, callback, options) {
|
|
104
|
-
if (callback) {
|
|
105
|
-
const handlers = events[name] || (events[name] = []);
|
|
106
|
-
const context = options.context, ctx = options.ctx, listening = options.listening;
|
|
107
|
-
if (listening) listening.count++;
|
|
108
|
-
|
|
109
|
-
handlers.push({callback: callback, context: context, ctx: context || ctx, listening: listening});
|
|
110
|
-
}
|
|
111
|
-
return events;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// An try-catch guarded #on function, to prevent poisoning the global
|
|
115
|
-
// `_listening` variable.
|
|
116
|
-
const tryCatchOn = function(obj, name, callback, context) {
|
|
117
|
-
try {
|
|
118
|
-
obj.on(name, callback, context);
|
|
119
|
-
} catch (e) {
|
|
120
|
-
return e;
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// Remove one or many callbacks. If `context` is null, removes all
|
|
125
|
-
// callbacks with that function. If `callback` is null, removes all
|
|
126
|
-
// callbacks for the event. If `name` is null, removes all bound
|
|
127
|
-
// callbacks for all events.
|
|
128
|
-
Events.off = function(name, callback, context) {
|
|
129
|
-
if (!this._events) return this;
|
|
130
|
-
this._events = eventsApi(offApi, this._events, name, callback, {
|
|
131
|
-
context: context,
|
|
132
|
-
listeners: this._listeners
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
return this;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// Tell this object to stop listening to either specific events ... or
|
|
139
|
-
// to every object it's currently listening to.
|
|
140
|
-
Events.stopListening = function(obj, name, callback) {
|
|
141
|
-
const listeningTo = this._listeningTo;
|
|
142
|
-
if (!listeningTo) return this;
|
|
143
|
-
|
|
144
|
-
const ids = obj ? [obj._listenId] : keys(listeningTo);
|
|
145
|
-
for (let i = 0; i < ids.length; i++) {
|
|
146
|
-
const listening = listeningTo[ids[i]];
|
|
147
|
-
|
|
148
|
-
// If listening doesn't exist, this object is not currently
|
|
149
|
-
// listening to obj. Break out early.
|
|
150
|
-
if (!listening) break;
|
|
151
|
-
|
|
152
|
-
listening.obj.off(name, callback, this);
|
|
153
|
-
if (listening.interop) listening.off(name, callback);
|
|
154
|
-
}
|
|
155
|
-
if (isEmpty(listeningTo)) this._listeningTo = undefined;
|
|
156
|
-
|
|
157
|
-
return this;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
// The reducing API that removes a callback from the `events` object.
|
|
161
|
-
const offApi = function(events, name, callback, options) {
|
|
162
|
-
if (!events) return;
|
|
163
|
-
|
|
164
|
-
const context = options.context, listeners = options.listeners;
|
|
165
|
-
let i = 0, names;
|
|
166
|
-
|
|
167
|
-
// Delete all event listeners and "drop" events.
|
|
168
|
-
if (!name && !context && !callback) {
|
|
169
|
-
for (names = keys(listeners); i < names.length; i++) {
|
|
170
|
-
listeners[names[i]].cleanup();
|
|
171
|
-
}
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
names = name ? [name] : keys(events);
|
|
176
|
-
for (; i < names.length; i++) {
|
|
177
|
-
name = names[i];
|
|
178
|
-
const handlers = events[name];
|
|
179
|
-
|
|
180
|
-
// Bail out if there are no events stored.
|
|
181
|
-
if (!handlers) {
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Find any remaining events.
|
|
186
|
-
const remaining = [];
|
|
187
|
-
for (let j = 0; j < handlers.length; j++) {
|
|
188
|
-
const handler = handlers[j];
|
|
189
|
-
if (
|
|
190
|
-
callback && callback !== handler.callback &&
|
|
191
|
-
callback !== handler.callback._callback ||
|
|
192
|
-
context && context !== handler.context
|
|
193
|
-
) {
|
|
194
|
-
remaining.push(handler);
|
|
195
|
-
} else {
|
|
196
|
-
const listening = handler.listening;
|
|
197
|
-
if (listening) listening.off(name, callback);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Replace events if there are any remaining. Otherwise, clean up.
|
|
202
|
-
if (remaining.length) {
|
|
203
|
-
events[name] = remaining;
|
|
204
|
-
} else {
|
|
205
|
-
delete events[name];
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return events;
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
// Bind an event to only be triggered a single time. After the first time
|
|
213
|
-
// the callback is invoked, its listener will be removed. If multiple events
|
|
214
|
-
// are passed in using the space-separated syntax, the handler will fire
|
|
215
|
-
// once for each event, not once for a combination of all events.
|
|
216
|
-
Events.once = function(name, callback, context) {
|
|
217
|
-
// Map the event into a `{event: once}` object.
|
|
218
|
-
const events = eventsApi(onceMap, {}, name, callback, this.off.bind(this));
|
|
219
|
-
if (typeof name === 'string' && (context === null || context === undefined)) callback = undefined;
|
|
220
|
-
return this.on(events, callback, context);
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
// Inversion-of-control versions of `once`.
|
|
224
|
-
Events.listenToOnce = function(obj, name, callback) {
|
|
225
|
-
// Map the event into a `{event: once}` object.
|
|
226
|
-
const events = eventsApi(onceMap, {}, name, callback, this.stopListening.bind(this, obj));
|
|
227
|
-
return this.listenTo(obj, events);
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
// Reduces the event callbacks into a map of `{event: onceWrapper}`.
|
|
231
|
-
// `offer` unbinds the `onceWrapper` after it has been called.
|
|
232
|
-
const onceMap = function(map, name, callback, offer) {
|
|
233
|
-
if (callback) {
|
|
234
|
-
const _once = map[name] = once(function() {
|
|
235
|
-
offer(name, _once);
|
|
236
|
-
callback.apply(this, arguments);
|
|
237
|
-
});
|
|
238
|
-
_once._callback = callback;
|
|
239
|
-
}
|
|
240
|
-
return map;
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
// Trigger one or many events, firing all bound callbacks. Callbacks are
|
|
244
|
-
// passed the same arguments as `trigger` is, apart from the event name
|
|
245
|
-
// (unless you're listening on `"all"`, which will cause your callback to
|
|
246
|
-
// receive the true name of the event as the first argument).
|
|
247
|
-
Events.trigger = function(name) {
|
|
248
|
-
if (!this._events) return this;
|
|
249
|
-
|
|
250
|
-
const length = Math.max(0, arguments.length - 1);
|
|
251
|
-
const args = Array(length);
|
|
252
|
-
for (let i = 0; i < length; i++) args[i] = arguments[i + 1];
|
|
253
|
-
|
|
254
|
-
eventsApi(triggerApi, this._events, name, undefined, args);
|
|
255
|
-
return this;
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
// Handles triggering the appropriate event callbacks.
|
|
259
|
-
const triggerApi = function(objEvents, name, callback, args) {
|
|
260
|
-
if (objEvents) {
|
|
261
|
-
const events = objEvents[name];
|
|
262
|
-
let allEvents = objEvents.all;
|
|
263
|
-
if (events && allEvents) allEvents = allEvents.slice();
|
|
264
|
-
if (events) triggerEvents(events, args);
|
|
265
|
-
if (allEvents) triggerEvents(allEvents, [name].concat(args));
|
|
266
|
-
}
|
|
267
|
-
return objEvents;
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
// A difficult-to-believe, but optimized internal dispatch function for
|
|
271
|
-
// triggering events. Tries to keep the usual cases speedy (most internal
|
|
272
|
-
// Backbone events have 3 arguments).
|
|
273
|
-
const triggerEvents = function(events, args) {
|
|
274
|
-
let ev, i = -1;
|
|
275
|
-
const l = events.length,
|
|
276
|
-
a1 = args[0],
|
|
277
|
-
a2 = args[1],
|
|
278
|
-
a3 = args[2];
|
|
279
|
-
switch (args.length) {
|
|
280
|
-
case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return;
|
|
281
|
-
case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return;
|
|
282
|
-
case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return;
|
|
283
|
-
case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return;
|
|
284
|
-
default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); return;
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
// A listening class that tracks and cleans up memory bindings
|
|
289
|
-
// when all callbacks have been offed.
|
|
290
|
-
const Listening = function(listener, obj) {
|
|
291
|
-
this.id = listener._listenId;
|
|
292
|
-
this.listener = listener;
|
|
293
|
-
this.obj = obj;
|
|
294
|
-
this.interop = true;
|
|
295
|
-
this.count = 0;
|
|
296
|
-
this._events = undefined;
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
Listening.prototype.on = Events.on;
|
|
300
|
-
|
|
301
|
-
// Offs a callback (or several).
|
|
302
|
-
// Uses an optimized counter if the listenee uses Backbone.Events.
|
|
303
|
-
// Otherwise, falls back to manual tracking to support events
|
|
304
|
-
// library interop.
|
|
305
|
-
Listening.prototype.off = function(name, callback) {
|
|
306
|
-
let cleanup;
|
|
307
|
-
if (this.interop) {
|
|
308
|
-
this._events = eventsApi(offApi, this._events, name, callback, {
|
|
309
|
-
context: undefined,
|
|
310
|
-
listeners: undefined
|
|
311
|
-
});
|
|
312
|
-
cleanup = !this._events;
|
|
313
|
-
} else {
|
|
314
|
-
this.count--;
|
|
315
|
-
cleanup = this.count === 0;
|
|
316
|
-
}
|
|
317
|
-
if (cleanup) this.cleanup();
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
// Cleans up memory bindings between the listener and the listenee.
|
|
321
|
-
Listening.prototype.cleanup = function() {
|
|
322
|
-
delete this.listener._listeningTo[this.obj._listenId];
|
|
323
|
-
if (!this.interop) delete this.obj._listeners[this.id];
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
// Aliases for backwards compatibility.
|
|
327
|
-
Events.bind = Events.on;
|
|
328
|
-
Events.unbind = Events.off;
|
package/src/history.js
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
// Backbone.js 1.4.0
|
|
2
|
-
// (c) 2010-2019 Jeremy Ashkenas and DocumentCloud
|
|
3
|
-
// Backbone may be freely distributed under the MIT license.
|
|
4
|
-
|
|
5
|
-
import extend from 'lodash-es/extend.js';
|
|
6
|
-
import some from 'lodash-es/some.js';
|
|
7
|
-
import { Events } from './events.js';
|
|
8
|
-
import { inherits } from './helpers.js';
|
|
9
|
-
|
|
10
|
-
// History
|
|
11
|
-
// -------
|
|
12
|
-
|
|
13
|
-
// Handles cross-browser history management, based on either
|
|
14
|
-
// [pushState](http://diveintohtml5.info/history.html) and real URLs, or
|
|
15
|
-
// [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange)
|
|
16
|
-
// and URL fragments. If the browser supports neither (old IE, natch),
|
|
17
|
-
// falls back to polling.
|
|
18
|
-
const History = function() {
|
|
19
|
-
this.handlers = [];
|
|
20
|
-
this.checkUrl = this.checkUrl.bind(this);
|
|
21
|
-
|
|
22
|
-
// Ensure that `History` can be used outside of the browser.
|
|
23
|
-
if (typeof window !== 'undefined') {
|
|
24
|
-
this.location = window.location;
|
|
25
|
-
this.history = window.history;
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
History.extend = inherits;
|
|
30
|
-
|
|
31
|
-
// Cached regex for stripping a leading hash/slash and trailing space.
|
|
32
|
-
const routeStripper = /^[#\/]|\s+$/g;
|
|
33
|
-
// Cached regex for stripping leading and trailing slashes.
|
|
34
|
-
const rootStripper = /^\/+|\/+$/g;
|
|
35
|
-
// Cached regex for stripping urls of hash.
|
|
36
|
-
const pathStripper = /#.*$/;
|
|
37
|
-
|
|
38
|
-
// Has the history handling already been started?
|
|
39
|
-
History.started = false;
|
|
40
|
-
|
|
41
|
-
// Set up all inheritable **History** properties and methods.
|
|
42
|
-
Object.assign(History.prototype, Events, {
|
|
43
|
-
|
|
44
|
-
// The default interval to poll for hash changes, if necessary, is
|
|
45
|
-
// twenty times a second.
|
|
46
|
-
interval: 50,
|
|
47
|
-
|
|
48
|
-
// Are we at the app root?
|
|
49
|
-
atRoot: function() {
|
|
50
|
-
const path = this.location.pathname.replace(/[^\/]$/, '$&/');
|
|
51
|
-
return path === this.root && !this.getSearch();
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
// Does the pathname match the root?
|
|
55
|
-
matchRoot: function() {
|
|
56
|
-
const path = this.decodeFragment(this.location.pathname);
|
|
57
|
-
const rootPath = path.slice(0, this.root.length - 1) + '/';
|
|
58
|
-
return rootPath === this.root;
|
|
59
|
-
},
|
|
60
|
-
|
|
61
|
-
// Unicode characters in `location.pathname` are percent encoded so they're
|
|
62
|
-
// decoded for comparison. `%25` should not be decoded since it may be part
|
|
63
|
-
// of an encoded parameter.
|
|
64
|
-
decodeFragment: function(fragment) {
|
|
65
|
-
return decodeURI(fragment.replace(/%25/g, '%2525'));
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
// In IE6, the hash fragment and search params are incorrect if the
|
|
69
|
-
// fragment contains `?`.
|
|
70
|
-
getSearch: function() {
|
|
71
|
-
const match = this.location.href.replace(/#.*/, '').match(/\?.+/);
|
|
72
|
-
return match ? match[0] : '';
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
// Gets the true hash value. Cannot use location.hash directly due to bug
|
|
76
|
-
// in Firefox where location.hash will always be decoded.
|
|
77
|
-
getHash: function(window) {
|
|
78
|
-
const match = (window || this).location.href.match(/#(.*)$/);
|
|
79
|
-
return match ? match[1] : '';
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
// Get the pathname and search params, without the root.
|
|
83
|
-
getPath: function() {
|
|
84
|
-
const path = this.decodeFragment(
|
|
85
|
-
this.location.pathname + this.getSearch()
|
|
86
|
-
).slice(this.root.length - 1);
|
|
87
|
-
return path.charAt(0) === '/' ? path.slice(1) : path;
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
// Get the cross-browser normalized URL fragment from the path or hash.
|
|
91
|
-
getFragment: function(fragment) {
|
|
92
|
-
if (fragment == null) {
|
|
93
|
-
if (this._usePushState || !this._wantsHashChange) {
|
|
94
|
-
fragment = this.getPath();
|
|
95
|
-
} else {
|
|
96
|
-
fragment = this.getHash();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return fragment.replace(routeStripper, '');
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
// Start the hash change handling, returning `true` if the current URL matches
|
|
103
|
-
// an existing route, and `false` otherwise.
|
|
104
|
-
start: function(options) {
|
|
105
|
-
if (History.started) throw new Error('history has already been started');
|
|
106
|
-
History.started = true;
|
|
107
|
-
|
|
108
|
-
// Figure out the initial configuration. Do we need an iframe?
|
|
109
|
-
// Is pushState desired ... is it available?
|
|
110
|
-
this.options = extend({root: '/'}, this.options, options);
|
|
111
|
-
this.root = this.options.root;
|
|
112
|
-
this._wantsHashChange = this.options.hashChange !== false;
|
|
113
|
-
this._hasHashChange = 'onhashchange' in window && (document.documentMode === undefined|| document.documentMode > 7);
|
|
114
|
-
this._useHashChange = this._wantsHashChange && this._hasHashChange;
|
|
115
|
-
this._wantsPushState = !!this.options.pushState;
|
|
116
|
-
this._hasPushState = !!(this.history && this.history.pushState);
|
|
117
|
-
this._usePushState = this._wantsPushState && this._hasPushState;
|
|
118
|
-
this.fragment = this.getFragment();
|
|
119
|
-
|
|
120
|
-
// Normalize root to always include a leading and trailing slash.
|
|
121
|
-
this.root = ('/' + this.root + '/').replace(rootStripper, '/');
|
|
122
|
-
|
|
123
|
-
// Transition from hashChange to pushState or vice versa if both are
|
|
124
|
-
// requested.
|
|
125
|
-
if (this._wantsHashChange && this._wantsPushState) {
|
|
126
|
-
|
|
127
|
-
// If we've started off with a route from a `pushState`-enabled
|
|
128
|
-
// browser, but we're currently in a browser that doesn't support it...
|
|
129
|
-
if (!this._hasPushState && !this.atRoot()) {
|
|
130
|
-
const rootPath = this.root.slice(0, -1) || '/';
|
|
131
|
-
this.location.replace(rootPath + '#' + this.getPath());
|
|
132
|
-
// Return immediately as browser will do redirect to new url
|
|
133
|
-
return true;
|
|
134
|
-
|
|
135
|
-
// Or if we've started out with a hash-based route, but we're currently
|
|
136
|
-
// in a browser where it could be `pushState`-based instead...
|
|
137
|
-
} else if (this._hasPushState && this.atRoot()) {
|
|
138
|
-
this.navigate(this.getHash(), {replace: true});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Proxy an iframe to handle location events if the browser doesn't
|
|
144
|
-
// support the `hashchange` event, HTML5 history, or the user wants
|
|
145
|
-
// `hashChange` but not `pushState`.
|
|
146
|
-
if (!this._hasHashChange && this._wantsHashChange && !this._usePushState) {
|
|
147
|
-
this.iframe = document.createElement('iframe');
|
|
148
|
-
this.iframe.src = 'javascript:0';
|
|
149
|
-
this.iframe.style.display = 'none';
|
|
150
|
-
this.iframe.tabIndex = -1;
|
|
151
|
-
const body = document.body;
|
|
152
|
-
// Using `appendChild` will throw on IE < 9 if the document is not ready.
|
|
153
|
-
const iWindow = body.insertBefore(this.iframe, body.firstChild).contentWindow;
|
|
154
|
-
iWindow.document.open();
|
|
155
|
-
iWindow.document.close();
|
|
156
|
-
iWindow.location.hash = '#' + this.fragment;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Depending on whether we're using pushState or hashes, and whether
|
|
160
|
-
// 'onhashchange' is supported, determine how we check the URL state.
|
|
161
|
-
if (this._usePushState) {
|
|
162
|
-
addEventListener('popstate', this.checkUrl, false);
|
|
163
|
-
} else if (this._useHashChange && !this.iframe) {
|
|
164
|
-
addEventListener('hashchange', this.checkUrl, false);
|
|
165
|
-
} else if (this._wantsHashChange) {
|
|
166
|
-
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (!this.options.silent) return this.loadUrl();
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
// Disable history, perhaps temporarily. Not useful in a real app,
|
|
173
|
-
// but possibly useful for unit testing Routers.
|
|
174
|
-
stop: function() {
|
|
175
|
-
// Remove window listeners.
|
|
176
|
-
if (this._usePushState) {
|
|
177
|
-
removeEventListener('popstate', this.checkUrl, false);
|
|
178
|
-
} else if (this._useHashChange && !this.iframe) {
|
|
179
|
-
removeEventListener('hashchange', this.checkUrl, false);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// Clean up the iframe if necessary.
|
|
183
|
-
if (this.iframe) {
|
|
184
|
-
document.body.removeChild(this.iframe);
|
|
185
|
-
this.iframe = null;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Some environments will throw when clearing an undefined interval.
|
|
189
|
-
if (this._checkUrlInterval) clearInterval(this._checkUrlInterval);
|
|
190
|
-
History.started = false;
|
|
191
|
-
},
|
|
192
|
-
|
|
193
|
-
// Add a route to be tested when the fragment changes. Routes added later
|
|
194
|
-
// may override previous routes.
|
|
195
|
-
route: function(route, callback) {
|
|
196
|
-
this.handlers.unshift({route: route, callback: callback});
|
|
197
|
-
},
|
|
198
|
-
|
|
199
|
-
// Checks the current URL to see if it has changed, and if it has,
|
|
200
|
-
// calls `loadUrl`, normalizing across the hidden iframe.
|
|
201
|
-
checkUrl: function(e) {
|
|
202
|
-
let current = this.getFragment();
|
|
203
|
-
|
|
204
|
-
// If the user pressed the back button, the iframe's hash will have
|
|
205
|
-
// changed and we should use that for comparison.
|
|
206
|
-
if (current === this.fragment && this.iframe) {
|
|
207
|
-
current = this.getHash(this.iframe.contentWindow);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (current === this.fragment) return false;
|
|
211
|
-
if (this.iframe) this.navigate(current);
|
|
212
|
-
this.loadUrl();
|
|
213
|
-
},
|
|
214
|
-
|
|
215
|
-
// Attempt to load the current URL fragment. If a route succeeds with a
|
|
216
|
-
// match, returns `true`. If no defined routes matches the fragment,
|
|
217
|
-
// returns `false`.
|
|
218
|
-
loadUrl: function(fragment) {
|
|
219
|
-
// If the root doesn't match, no routes can match either.
|
|
220
|
-
if (!this.matchRoot()) return false;
|
|
221
|
-
fragment = this.fragment = this.getFragment(fragment);
|
|
222
|
-
return some(this.handlers, function(handler) {
|
|
223
|
-
if (handler.route.test(fragment)) {
|
|
224
|
-
handler.callback(fragment);
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
},
|
|
229
|
-
|
|
230
|
-
// Save a fragment into the hash history, or replace the URL state if the
|
|
231
|
-
// 'replace' option is passed. You are responsible for properly URL-encoding
|
|
232
|
-
// the fragment in advance.
|
|
233
|
-
//
|
|
234
|
-
// The options object can contain `trigger: true` if you wish to have the
|
|
235
|
-
// route callback be fired (not usually desirable), or `replace: true`, if
|
|
236
|
-
// you wish to modify the current URL without adding an entry to the history.
|
|
237
|
-
navigate: function(fragment, options) {
|
|
238
|
-
if (!History.started) return false;
|
|
239
|
-
if (!options || options === true) options = {trigger: !!options};
|
|
240
|
-
|
|
241
|
-
// Normalize the fragment.
|
|
242
|
-
fragment = this.getFragment(fragment || '');
|
|
243
|
-
|
|
244
|
-
// Don't include a trailing slash on the root.
|
|
245
|
-
let rootPath = this.root;
|
|
246
|
-
if (fragment === '' || fragment.charAt(0) === '?') {
|
|
247
|
-
rootPath = rootPath.slice(0, -1) || '/';
|
|
248
|
-
}
|
|
249
|
-
const url = rootPath + fragment;
|
|
250
|
-
|
|
251
|
-
// Strip the fragment of the query and hash for matching.
|
|
252
|
-
fragment = fragment.replace(pathStripper, '');
|
|
253
|
-
|
|
254
|
-
// Decode for matching.
|
|
255
|
-
const decodedFragment = this.decodeFragment(fragment);
|
|
256
|
-
|
|
257
|
-
if (this.fragment === decodedFragment) return;
|
|
258
|
-
this.fragment = decodedFragment;
|
|
259
|
-
|
|
260
|
-
// If pushState is available, we use it to set the fragment as a real URL.
|
|
261
|
-
if (this._usePushState) {
|
|
262
|
-
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
|
|
263
|
-
|
|
264
|
-
// If hash changes haven't been explicitly disabled, update the hash
|
|
265
|
-
// fragment to store history.
|
|
266
|
-
} else if (this._wantsHashChange) {
|
|
267
|
-
this._updateHash(this.location, fragment, options.replace);
|
|
268
|
-
if (this.iframe && fragment !== this.getHash(this.iframe.contentWindow)) {
|
|
269
|
-
const iWindow = this.iframe.contentWindow;
|
|
270
|
-
|
|
271
|
-
// Opening and closing the iframe tricks IE7 and earlier to push a
|
|
272
|
-
// history entry on hash-tag change. When replace is true, we don't
|
|
273
|
-
// want this.
|
|
274
|
-
if (!options.replace) {
|
|
275
|
-
iWindow.document.open();
|
|
276
|
-
iWindow.document.close();
|
|
277
|
-
}
|
|
278
|
-
this._updateHash(iWindow.location, fragment, options.replace);
|
|
279
|
-
}
|
|
280
|
-
// If you've told us that you explicitly don't want fallback hashchange-
|
|
281
|
-
// based history, then `navigate` becomes a page refresh.
|
|
282
|
-
} else {
|
|
283
|
-
return this.location.assign(url);
|
|
284
|
-
}
|
|
285
|
-
if (options.trigger) return this.loadUrl(fragment);
|
|
286
|
-
},
|
|
287
|
-
|
|
288
|
-
// Update the hash location, either replacing the current entry, or adding
|
|
289
|
-
// a new one to the browser history.
|
|
290
|
-
_updateHash: function(location, fragment, replace) {
|
|
291
|
-
if (replace) {
|
|
292
|
-
const href = location.href.replace(/(javascript:|#).*$/, '');
|
|
293
|
-
location.replace(href + '#' + fragment);
|
|
294
|
-
} else {
|
|
295
|
-
// Some browsers require that `hash` contains a leading #.
|
|
296
|
-
location.hash = '#' + fragment;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
export default History;
|