@converse/skeletor 0.0.5 → 0.0.6

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/src/events.js~ DELETED
@@ -1,334 +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
- /**
25
- * Custom error for indicating timeouts
26
- * @namespace _converse
27
- */
28
- export class NotImplementedError extends Error {}
29
-
30
- export const Events = {};
31
-
32
- // Regular expression used to split event strings.
33
- const eventSplitter = /\s+/;
34
-
35
- // A private global variable to share between listeners and listenees.
36
- let _listening;
37
-
38
- // Iterates over the standard `event, callback` (as well as the fancy multiple
39
- // space-separated events `"change blur", callback` and jQuery-style event
40
- // maps `{event: callback}`).
41
- const eventsApi = function(iteratee, events, name, callback, opts) {
42
- let i = 0, names;
43
- if (name && typeof name === 'object') {
44
- // Handle event maps.
45
- if (callback !== undefined && 'context' in opts && opts.context === undefined) opts.context = callback;
46
- for (names = keys(name); i < names.length ; i++) {
47
- events = eventsApi(iteratee, events, names[i], name[names[i]], opts);
48
- }
49
- } else if (name && eventSplitter.test(name)) {
50
- // Handle space-separated event names by delegating them individually.
51
- for (names = name.split(eventSplitter); i < names.length; i++) {
52
- events = iteratee(events, names[i], callback, opts);
53
- }
54
- } else {
55
- // Finally, standard events.
56
- events = iteratee(events, name, callback, opts);
57
- }
58
- return events;
59
- };
60
-
61
- // Bind an event to a `callback` function. Passing `"all"` will bind
62
- // the callback to all events fired.
63
- Events.on = function(name, callback, context) {
64
- this._events = eventsApi(onApi, this._events || {}, name, callback, {
65
- context: context,
66
- ctx: this,
67
- listening: _listening
68
- });
69
-
70
- if (_listening) {
71
- const listeners = this._listeners || (this._listeners = {});
72
- listeners[_listening.id] = _listening;
73
- // Allow the listening to use a counter, instead of tracking
74
- // callbacks for library interop
75
- _listening.interop = false;
76
- }
77
-
78
- return this;
79
- };
80
-
81
- // Inversion-of-control versions of `on`. Tell *this* object to listen to
82
- // an event in another object... keeping track of what it's listening to
83
- // for easier unbinding later.
84
- Events.listenTo = function(obj, name, callback) {
85
- if (!obj) return this;
86
- const id = obj._listenId || (obj._listenId = uniqueId('l'));
87
- const listeningTo = this._listeningTo || (this._listeningTo = {});
88
- let listening = _listening = listeningTo[id];
89
-
90
- // This object is not listening to any other events on `obj` yet.
91
- // Setup the necessary references to track the listening callbacks.
92
- if (!listening) {
93
- this._listenId || (this._listenId = uniqueId('l'));
94
- listening = _listening = listeningTo[id] = new Listening(this, obj);
95
- }
96
-
97
- // Bind callbacks on obj.
98
- const error = tryCatchOn(obj, name, callback, this);
99
- _listening = undefined;
100
-
101
- if (error) throw error;
102
- // If the target obj is not Backbone.Events, track events manually.
103
- if (listening.interop) listening.on(name, callback);
104
-
105
- return this;
106
- };
107
-
108
- // The reducing API that adds a callback to the `events` object.
109
- const onApi = function(events, name, callback, options) {
110
- if (callback) {
111
- const handlers = events[name] || (events[name] = []);
112
- const context = options.context, ctx = options.ctx, listening = options.listening;
113
- if (listening) listening.count++;
114
-
115
- handlers.push({callback: callback, context: context, ctx: context || ctx, listening: listening});
116
- }
117
- return events;
118
- };
119
-
120
- // An try-catch guarded #on function, to prevent poisoning the global
121
- // `_listening` variable.
122
- const tryCatchOn = function(obj, name, callback, context) {
123
- try {
124
- obj.on(name, callback, context);
125
- } catch (e) {
126
- return e;
127
- }
128
- };
129
-
130
- // Remove one or many callbacks. If `context` is null, removes all
131
- // callbacks with that function. If `callback` is null, removes all
132
- // callbacks for the event. If `name` is null, removes all bound
133
- // callbacks for all events.
134
- Events.off = function(name, callback, context) {
135
- if (!this._events) return this;
136
- this._events = eventsApi(offApi, this._events, name, callback, {
137
- context: context,
138
- listeners: this._listeners
139
- });
140
-
141
- return this;
142
- };
143
-
144
- // Tell this object to stop listening to either specific events ... or
145
- // to every object it's currently listening to.
146
- Events.stopListening = function(obj, name, callback) {
147
- const listeningTo = this._listeningTo;
148
- if (!listeningTo) return this;
149
-
150
- const ids = obj ? [obj._listenId] : keys(listeningTo);
151
- for (let i = 0; i < ids.length; i++) {
152
- const listening = listeningTo[ids[i]];
153
-
154
- // If listening doesn't exist, this object is not currently
155
- // listening to obj. Break out early.
156
- if (!listening) break;
157
-
158
- listening.obj.off(name, callback, this);
159
- if (listening.interop) listening.off(name, callback);
160
- }
161
- if (isEmpty(listeningTo)) this._listeningTo = undefined;
162
-
163
- return this;
164
- };
165
-
166
- // The reducing API that removes a callback from the `events` object.
167
- const offApi = function(events, name, callback, options) {
168
- if (!events) return;
169
-
170
- const context = options.context, listeners = options.listeners;
171
- let i = 0, names;
172
-
173
- // Delete all event listeners and "drop" events.
174
- if (!name && !context && !callback) {
175
- for (names = keys(listeners); i < names.length; i++) {
176
- listeners[names[i]].cleanup();
177
- }
178
- return;
179
- }
180
-
181
- names = name ? [name] : keys(events);
182
- for (; i < names.length; i++) {
183
- name = names[i];
184
- const handlers = events[name];
185
-
186
- // Bail out if there are no events stored.
187
- if (!handlers) {
188
- break;
189
- }
190
-
191
- // Find any remaining events.
192
- const remaining = [];
193
- for (let j = 0; j < handlers.length; j++) {
194
- const handler = handlers[j];
195
- if (
196
- callback && callback !== handler.callback &&
197
- callback !== handler.callback._callback ||
198
- context && context !== handler.context
199
- ) {
200
- remaining.push(handler);
201
- } else {
202
- const listening = handler.listening;
203
- if (listening) listening.off(name, callback);
204
- }
205
- }
206
-
207
- // Replace events if there are any remaining. Otherwise, clean up.
208
- if (remaining.length) {
209
- events[name] = remaining;
210
- } else {
211
- delete events[name];
212
- }
213
- }
214
-
215
- return events;
216
- };
217
-
218
- // Bind an event to only be triggered a single time. After the first time
219
- // the callback is invoked, its listener will be removed. If multiple events
220
- // are passed in using the space-separated syntax, the handler will fire
221
- // once for each event, not once for a combination of all events.
222
- Events.once = function(name, callback, context) {
223
- // Map the event into a `{event: once}` object.
224
- const events = eventsApi(onceMap, {}, name, callback, this.off.bind(this));
225
- if (typeof name === 'string' && (context === null || context === undefined)) callback = undefined;
226
- return this.on(events, callback, context);
227
- };
228
-
229
- // Inversion-of-control versions of `once`.
230
- Events.listenToOnce = function(obj, name, callback) {
231
- // Map the event into a `{event: once}` object.
232
- const events = eventsApi(onceMap, {}, name, callback, this.stopListening.bind(this, obj));
233
- return this.listenTo(obj, events);
234
- };
235
-
236
- // Reduces the event callbacks into a map of `{event: onceWrapper}`.
237
- // `offer` unbinds the `onceWrapper` after it has been called.
238
- const onceMap = function(map, name, callback, offer) {
239
- if (callback) {
240
- const _once = map[name] = once(function() {
241
- offer(name, _once);
242
- callback.apply(this, arguments);
243
- });
244
- _once._callback = callback;
245
- }
246
- return map;
247
- };
248
-
249
- // Trigger one or many events, firing all bound callbacks. Callbacks are
250
- // passed the same arguments as `trigger` is, apart from the event name
251
- // (unless you're listening on `"all"`, which will cause your callback to
252
- // receive the true name of the event as the first argument).
253
- Events.trigger = function(name) {
254
- if (!this._events) return this;
255
-
256
- const length = Math.max(0, arguments.length - 1);
257
- const args = Array(length);
258
- for (let i = 0; i < length; i++) args[i] = arguments[i + 1];
259
-
260
- eventsApi(triggerApi, this._events, name, undefined, args);
261
- return this;
262
- };
263
-
264
- // Handles triggering the appropriate event callbacks.
265
- const triggerApi = function(objEvents, name, callback, args) {
266
- if (objEvents) {
267
- const events = objEvents[name];
268
- let allEvents = objEvents.all;
269
- if (events && allEvents) allEvents = allEvents.slice();
270
- if (events) triggerEvents(events, args);
271
- if (allEvents) triggerEvents(allEvents, [name].concat(args));
272
- }
273
- return objEvents;
274
- };
275
-
276
- // A difficult-to-believe, but optimized internal dispatch function for
277
- // triggering events. Tries to keep the usual cases speedy (most internal
278
- // Backbone events have 3 arguments).
279
- const triggerEvents = function(events, args) {
280
- let ev, i = -1;
281
- const l = events.length,
282
- a1 = args[0],
283
- a2 = args[1],
284
- a3 = args[2];
285
- switch (args.length) {
286
- case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return;
287
- case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return;
288
- case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return;
289
- case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return;
290
- default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); return;
291
- }
292
- };
293
-
294
- // A listening class that tracks and cleans up memory bindings
295
- // when all callbacks have been offed.
296
- const Listening = function(listener, obj) {
297
- this.id = listener._listenId;
298
- this.listener = listener;
299
- this.obj = obj;
300
- this.interop = true;
301
- this.count = 0;
302
- this._events = undefined;
303
- };
304
-
305
- Listening.prototype.on = Events.on;
306
-
307
- // Offs a callback (or several).
308
- // Uses an optimized counter if the listenee uses Backbone.Events.
309
- // Otherwise, falls back to manual tracking to support events
310
- // library interop.
311
- Listening.prototype.off = function(name, callback) {
312
- let cleanup;
313
- if (this.interop) {
314
- this._events = eventsApi(offApi, this._events, name, callback, {
315
- context: undefined,
316
- listeners: undefined
317
- });
318
- cleanup = !this._events;
319
- } else {
320
- this.count--;
321
- cleanup = this.count === 0;
322
- }
323
- if (cleanup) this.cleanup();
324
- };
325
-
326
- // Cleans up memory bindings between the listener and the listenee.
327
- Listening.prototype.cleanup = function() {
328
- delete this.listener._listeningTo[this.obj._listenId];
329
- if (!this.interop) delete this.obj._listeners[this.id];
330
- };
331
-
332
- // Aliases for backwards compatibility.
333
- Events.bind = Events.on;
334
- Events.unbind = Events.off;
package/src/helpers.js~ DELETED
@@ -1,151 +0,0 @@
1
- // (c) 2010-2019 Jeremy Ashkenas and DocumentCloud
2
-
3
- import create from 'lodash-es/create.js';
4
- import extend from 'lodash-es/extend.js';
5
- import has from 'lodash-es/has.js';
6
- import result from 'lodash-es/result.js';
7
-
8
- // Helpers
9
- // -------
10
-
11
- // Helper function to correctly set up the prototype chain for subclasses.
12
- // Similar to `goog.inherits`, but uses a hash of prototype properties and
13
- // class properties to be extended.
14
- //
15
- export function inherits(protoProps, staticProps) {
16
- const parent = this;
17
- let child;
18
-
19
- // The constructor function for the new subclass is either defined by you
20
- // (the "constructor" property in your `extend` definition), or defaulted
21
- // by us to simply call the parent constructor.
22
- if (protoProps && has(protoProps, 'constructor')) {
23
- child = protoProps.constructor;
24
- } else {
25
- child = function(){ return parent.apply(this, arguments); };
26
- }
27
-
28
- // Add static properties to the constructor function, if supplied.
29
- extend(child, parent, staticProps);
30
-
31
- // Set the prototype chain to inherit from `parent`, without calling
32
- // `parent`'s constructor function and add the prototype properties.
33
- child.prototype = create(parent.prototype, protoProps);
34
- child.prototype.constructor = child;
35
-
36
- // Set a convenience property in case the parent's prototype is needed
37
- // later.
38
- child.__super__ = parent.prototype;
39
-
40
- return child;
41
- }
42
-
43
-
44
- export function getResolveablePromise () {
45
- const wrapper = {
46
- isResolved: false,
47
- isPending: true,
48
- isRejected: false
49
- };
50
- const promise = new Promise((resolve, reject) => {
51
- wrapper.resolve = resolve;
52
- wrapper.reject = reject;
53
- })
54
- Object.assign(promise, wrapper);
55
- promise.then(
56
- function (v) {
57
- promise.isResolved = true;
58
- promise.isPending = false;
59
- promise.isRejected = false;
60
- return v;
61
- },
62
- function (e) {
63
- promise.isResolved = false;
64
- promise.isPending = false;
65
- promise.isRejected = true;
66
- throw (e);
67
- }
68
- );
69
- return promise;
70
- }
71
-
72
-
73
- // Throw an error when a URL is needed, and none is supplied.
74
- export function urlError() {
75
- throw new Error('A "url" property or function must be specified');
76
- }
77
-
78
- // Wrap an optional error callback with a fallback error event.
79
- export function wrapError(model, options) {
80
- const error = options.error;
81
- options.error = function(resp) {
82
- if (error) error.call(options.context, model, resp, options);
83
- model.trigger('error', model, resp, options);
84
- };
85
- }
86
-
87
- // Map from CRUD to HTTP for our default `sync` implementation.
88
- const methodMap = {
89
- create: 'POST',
90
- update: 'PUT',
91
- patch: 'PATCH',
92
- delete: 'DELETE',
93
- read: 'GET'
94
- };
95
-
96
- export function getSyncMethod(model) {
97
- const store = result(model, 'browserStorage') || result(model.collection, 'browserStorage');
98
- return store ? store.sync() : sync;
99
- }
100
-
101
- // sync
102
- // ----
103
-
104
- // Override this function to change the manner in which Backbone persists
105
- // models to the server. You will be passed the type of request, and the
106
- // model in question. By default, makes a RESTful Ajax request
107
- // to the model's `url()`. Some possible customizations could be:
108
- //
109
- // * Use `setTimeout` to batch rapid-fire updates into a single request.
110
- // * Send up the models as XML instead of JSON.
111
- // * Persist models via WebSockets instead of Ajax.
112
- //
113
- export function sync(method, model, options={}) {
114
- const type = methodMap[method];
115
-
116
- // Default JSON-request options.
117
- const params = {type: type, dataType: 'json'};
118
-
119
- // Ensure that we have a URL.
120
- if (!options.url) {
121
- params.url = result(model, 'url') || urlError();
122
- }
123
-
124
- // Ensure that we have the appropriate request data.
125
- if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
126
- params.contentType = 'application/json';
127
- params.data = JSON.stringify(options.attrs || model.toJSON(options));
128
- }
129
-
130
- // Don't process data on a non-GET request.
131
- if (params.type !== 'GET') {
132
- params.processData = false;
133
- }
134
-
135
- // Pass along `textStatus` and `errorThrown` from jQuery.
136
- const error = options.error;
137
- options.error = function(xhr, textStatus, errorThrown) {
138
- options.textStatus = textStatus;
139
- options.errorThrown = errorThrown;
140
- if (error) error.call(options.context, xhr, textStatus, errorThrown);
141
- };
142
-
143
- // Make the request, allowing the user to override any Ajax options.
144
- const xhr = options.xhr = ajax(extend(params, options));
145
- model.trigger('request', model, xhr, options);
146
- return xhr;
147
- }
148
-
149
- export function ajax() {
150
- return fetch.apply(this, arguments);
151
- }