@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.
Binary file
@@ -1,316 +0,0 @@
1
- // Copyright 2014 Mozilla
2
- // Copyright 2015 Thodoris Greasidis
3
- // Copyright 2018 JC Brand
4
- //
5
- // Licensed under the Apache License, Version 2.0 (the "License");
6
- // you may not use this file except in compliance with the License.
7
- // You may obtain a copy of the License at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // Unless required by applicable law or agreed to in writing, software
12
- // distributed under the License is distributed on an "AS IS" BASIS,
13
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- // See the License for the specific language governing permissions and
15
- // limitations under the License.
16
-
17
- import executeCallback from 'localforage/src/utils/executeCallback';
18
- import getCallback from 'localforage/src/utils/getCallback';
19
- import normalizeKey from 'localforage/src/utils/normalizeKey';
20
- import serializer from 'localforage/src/utils/serializer';
21
-
22
- const serialize = serializer["serialize"];
23
- const deserialize = serializer["deserialize"];
24
-
25
-
26
- function isSessionStorageValid () {
27
- // If the app is running inside a Google Chrome packaged webapp, or some
28
- // other context where sessionStorage isn't available, we don't use
29
- // sessionStorage. This feature detection is preferred over the old
30
- // `if (window.chrome && window.chrome.runtime)` code.
31
- // See: https://github.com/mozilla/localForage/issues/68
32
- try {
33
- // If sessionStorage isn't available, we get outta here!
34
- // This should be inside a try catch
35
- if (sessionStorage && ('setItem' in sessionStorage)) {
36
- return true;
37
- }
38
- } catch (e) {
39
- console.log(e);
40
- }
41
- return false;
42
- }
43
-
44
- function _getKeyPrefix(options, defaultConfig) {
45
- let keyPrefix = options.name + '/';
46
-
47
- if (options.storeName !== defaultConfig.storeName) {
48
- keyPrefix += options.storeName + '/';
49
- }
50
- return keyPrefix;
51
- }
52
-
53
- const dbInfo = {
54
- 'serializer': {
55
- 'serialize': serialize,
56
- 'deserialize': deserialize
57
- }
58
- };
59
-
60
- function _initStorage(options) {
61
- dbInfo.keyPrefix = _getKeyPrefix(options, this._defaultConfig);
62
- if (options) {
63
- for (const i in options) { // eslint-disable-line guard-for-in
64
- dbInfo[i] = options[i];
65
- }
66
- }
67
- }
68
-
69
- // Remove all keys from the datastore, effectively destroying all data in
70
- // the app's key/value store!
71
- function clear(callback) {
72
- const promise = this.ready().then(function() {
73
- const keyPrefix = dbInfo.keyPrefix;
74
-
75
- for (let i = sessionStorage.length - 1; i >= 0; i--) {
76
- const key = sessionStorage.key(i);
77
-
78
- if (key.indexOf(keyPrefix) === 0) {
79
- sessionStorage.removeItem(key);
80
- }
81
- }
82
- });
83
-
84
- executeCallback(promise, callback);
85
- return promise;
86
- }
87
-
88
- // Retrieve an item from the store. Unlike the original async_storage
89
- // library in Gaia, we don't modify return values at all. If a key's value
90
- // is `undefined`, we pass that value to the callback function.
91
- function getItem(key, callback) {
92
- key = normalizeKey(key);
93
-
94
- const promise = this.ready().then(function() {
95
- let result = sessionStorage.getItem(dbInfo.keyPrefix + key);
96
- // If a result was found, parse it from the serialized
97
- // string into a JS object. If result isn't truthy, the key
98
- // is likely undefined and we'll pass it straight to the
99
- // callback.
100
- if (result) {
101
- result = dbInfo.serializer.deserialize(result);
102
- }
103
- return result;
104
- });
105
- executeCallback(promise, callback);
106
- return promise;
107
- }
108
-
109
- // Iterate over all items in the store.
110
- function iterate(iterator, callback) {
111
- const self = this;
112
-
113
- const promise = self.ready().then(function() {
114
- const keyPrefix = dbInfo.keyPrefix;
115
- const keyPrefixLength = keyPrefix.length;
116
- const length = sessionStorage.length;
117
-
118
- // We use a dedicated iterator instead of the `i` variable below
119
- // so other keys we fetch in sessionStorage aren't counted in
120
- // the `iterationNumber` argument passed to the `iterate()`
121
- // callback.
122
- //
123
- // See: github.com/mozilla/localForage/pull/435#discussion_r38061530
124
- let iterationNumber = 1;
125
-
126
- for (let i = 0; i < length; i++) {
127
- const key = sessionStorage.key(i);
128
- if (key.indexOf(keyPrefix) !== 0) {
129
- continue;
130
- }
131
- let value = sessionStorage.getItem(key);
132
-
133
- // If a result was found, parse it from the serialized
134
- // string into a JS object. If result isn't truthy, the
135
- // key is likely undefined and we'll pass it straight
136
- // to the iterator.
137
- if (value) {
138
- value = dbInfo.serializer.deserialize(value);
139
- }
140
-
141
- value = iterator(
142
- value,
143
- key.substring(keyPrefixLength),
144
- iterationNumber++
145
- );
146
-
147
- if (value !== void 0) { // eslint-disable-line no-void
148
- return value;
149
- }
150
- }
151
- });
152
-
153
- executeCallback(promise, callback);
154
- return promise;
155
- }
156
-
157
- // Same as sessionStorage's key() method, except takes a callback.
158
- function key(n, callback) {
159
- const self = this;
160
- const promise = self.ready().then(function() {
161
- let result;
162
- try {
163
- result = sessionStorage.key(n);
164
- } catch (error) {
165
- result = null;
166
- }
167
-
168
- // Remove the prefix from the key, if a key is found.
169
- if (result) {
170
- result = result.substring(dbInfo.keyPrefix.length);
171
- }
172
-
173
- return result;
174
- });
175
-
176
- executeCallback(promise, callback);
177
- return promise;
178
- }
179
-
180
- function keys(callback) {
181
- const self = this;
182
- const promise = self.ready().then(function() {
183
- const length = sessionStorage.length;
184
- const keys = [];
185
-
186
- for (let i = 0; i < length; i++) {
187
- const itemKey = sessionStorage.key(i);
188
- if (itemKey.indexOf(dbInfo.keyPrefix) === 0) {
189
- keys.push(itemKey.substring(dbInfo.keyPrefix.length));
190
- }
191
- }
192
- return keys;
193
- });
194
-
195
- executeCallback(promise, callback);
196
- return promise;
197
- }
198
-
199
- // Supply the number of keys in the datastore to the callback function.
200
- function length(callback) {
201
- const self = this;
202
- const promise = self.keys().then(function(keys) {
203
- return keys.length;
204
- });
205
-
206
- executeCallback(promise, callback);
207
- return promise;
208
- }
209
-
210
- // Remove an item from the store, nice and simple.
211
- function removeItem(key, callback) {
212
- key = normalizeKey(key);
213
- const promise = this.ready().then(function() {
214
- sessionStorage.removeItem(dbInfo.keyPrefix + key);
215
- });
216
- executeCallback(promise, callback);
217
- return promise;
218
- }
219
-
220
- // Set a key's value and run an optional callback once the value is set.
221
- // Unlike Gaia's implementation, the callback function is passed the value,
222
- // in case you want to operate on that value only after you're sure it
223
- // saved, or something like that.
224
- function setItem(key, value, callback) {
225
- key = normalizeKey(key);
226
-
227
- const promise = this.ready().then(function() {
228
- // Convert undefined values to null.
229
- // https://github.com/mozilla/localForage/pull/42
230
- if (value === undefined) {
231
- value = null;
232
- }
233
-
234
- // Save the original value to pass to the callback.
235
- const originalValue = value;
236
-
237
- return new Promise(function(resolve, reject) {
238
- dbInfo.serializer.serialize(value, function(value, error) {
239
- if (error) {
240
- reject(error);
241
- } else {
242
- try {
243
- sessionStorage.setItem(dbInfo.keyPrefix + key, value);
244
- resolve(originalValue);
245
- } catch (e) {
246
- // sessionStorage capacity exceeded.
247
- // TODO: Make this a specific error/event.
248
- if (
249
- e.name === 'QuotaExceededError' ||
250
- e.name === 'NS_ERROR_DOM_QUOTA_REACHED'
251
- ) {
252
- reject(e);
253
- }
254
- reject(e);
255
- }
256
- }
257
- });
258
- });
259
- });
260
-
261
- executeCallback(promise, callback);
262
- return promise;
263
- }
264
-
265
- function dropInstance(options, callback) {
266
- callback = getCallback.apply(this, arguments);
267
-
268
- options = (typeof options !== 'function' && options) || {};
269
- if (!options.name) {
270
- const currentConfig = this.config();
271
- options.name = options.name || currentConfig.name;
272
- options.storeName = options.storeName || currentConfig.storeName;
273
- }
274
-
275
- const self = this;
276
- let promise;
277
- if (!options.name) {
278
- promise = Promise.reject(new Error('Invalid arguments'));
279
- } else {
280
- promise = new Promise(function(resolve) {
281
- if (!options.storeName) {
282
- resolve(`${options.name}/`);
283
- } else {
284
- resolve(_getKeyPrefix(options, self._defaultConfig));
285
- }
286
- }).then(function(keyPrefix) {
287
- for (var i = sessionStorage.length - 1; i >= 0; i--) {
288
- var key = sessionStorage.key(i);
289
-
290
- if (key.indexOf(keyPrefix) === 0) {
291
- sessionStorage.removeItem(key);
292
- }
293
- }
294
- });
295
- }
296
-
297
- executeCallback(promise, callback);
298
- return promise;
299
- }
300
-
301
- const sessionStorageWrapper = {
302
- _driver: 'sessionStorageWrapper',
303
- _initStorage: _initStorage,
304
- _support: isSessionStorageValid(),
305
- iterate: iterate,
306
- getItem: getItem,
307
- setItem: setItem,
308
- removeItem: removeItem,
309
- clear: clear,
310
- length: length,
311
- key: key,
312
- keys: keys,
313
- dropInstance: dropInstance
314
- };
315
-
316
- export default sessionStorageWrapper;
@@ -1,4 +0,0 @@
1
-
2
-
3
- export class ElementOverview extends ElementView {
4
- }
package/src/element.js~ DELETED
@@ -1,188 +0,0 @@
1
- import extend from "lodash-es/extend.js";
2
- import isElement from "lodash-es/isElement.js";
3
- import isFunction from "lodash-es/isFunction.js";
4
- import pick from "lodash-es/pick.js";
5
- import result from "lodash-es/result.js";
6
- import uniqueId from "lodash-es/uniqueId.js";
7
- import { Events } from './events.js';
8
- import { inherits, NotImplementedError } from './helpers.js';
9
- import { render } from 'lit-html';
10
-
11
-
12
- const paddedLt = /^\s*</;
13
-
14
- // Caches a local reference to `Element.prototype` for faster access.
15
- const ElementProto = (typeof Element !== 'undefined' && Element.prototype) || {};
16
-
17
- // Cached regex to split keys for `delegate`.
18
- const delegateEventSplitter = /^(\S+)\s*(.*)$/;
19
-
20
- // List of view options to be set as properties.
21
- const viewOptions = ['model', 'collection', 'events'];
22
-
23
-
24
- export class ElementView extends HTMLElement {
25
-
26
- events = {}
27
-
28
- constructor(options) {
29
- super();
30
- // Creating a View creates its initial element outside of the DOM,
31
- // if an existing element is not provided...
32
- this.cid = uniqueId('view');
33
- this._domEvents = [];
34
- extend(this, pick(options, viewOptions));
35
- }
36
-
37
- createRenderRoot () {
38
- // Render without the shadow DOM
39
- return this;
40
- }
41
-
42
- connectedCallback () {
43
- if (!this._initialized) {
44
- this.preinitialize.apply(this, arguments);
45
- this.initialize.apply(this, arguments);
46
- this._initialized = true;
47
- }
48
- this.delegateEvents();
49
- }
50
-
51
- disconnectedCallback () {
52
- this.undelegateEvents();
53
- this.stopListening();
54
- }
55
-
56
- // preinitialize is an empty function by default. You can override it with a function
57
- // or object. preinitialize will run before any instantiation logic is run in the View
58
- preinitialize () { // eslint-disable-line class-methods-use-this
59
- }
60
-
61
- // Initialize is an empty function by default. Override it with your own
62
- // initialization logic.
63
- initialize() {} // eslint-disable-line class-methods-use-this
64
-
65
- // **render** is the core function that your view should override, in order
66
- // to populate its element (`this.el`), with the appropriate HTML. The
67
- // convention is for **render** to always return `this`.
68
- render() {
69
- isFunction(this.beforeRender) && this.beforeRender();
70
- isFunction(this.toHTML) && render(this.toHTML(), this);
71
- isFunction(this.afterRender) && this.afterRender();
72
- return this;
73
- }
74
-
75
- // Set callbacks, where `this.events` is a hash of
76
- //
77
- // *{"event selector": "callback"}*
78
- //
79
- // {
80
- // 'mousedown .title': 'edit',
81
- // 'click .button': 'save',
82
- // 'click .open': function(e) { ... }
83
- // }
84
- //
85
- // pairs. Callbacks will be bound to the view, with `this` set properly.
86
- // Uses event delegation for efficiency.
87
- // Omitting the selector binds the event to `this.el`.
88
- delegateEvents() {
89
- if (!this.events) {
90
- return this;
91
- }
92
- this.undelegateEvents();
93
- for (const key in this.events) {
94
- let method = this.events[key];
95
- if (!isFunction(method)) method = this[method];
96
- if (!method) continue;
97
- const match = key.match(delegateEventSplitter);
98
- this.delegate(match[1], match[2], method.bind(this));
99
- }
100
- return this;
101
- }
102
-
103
- // Make a event delegation handler for the given `eventName` and `selector`
104
- // and attach it to `this.el`.
105
- // If selector is empty, the listener will be bound to `this.el`. If not, a
106
- // new handler that will recursively traverse up the event target's DOM
107
- // hierarchy looking for a node that matches the selector. If one is found,
108
- // the event's `delegateTarget` property is set to it and the return the
109
- // result of calling bound `listener` with the parameters given to the
110
- // handler.
111
- delegate(eventName, selector, listener) {
112
- const root = this;
113
- if (!root) {
114
- return this;
115
- }
116
- if (typeof selector === 'function') {
117
- listener = selector;
118
- selector = null;
119
- }
120
- // Given that `focus` and `blur` events do not bubble, do not delegate these events
121
- if (['focus', 'blur'].indexOf(eventName) !== -1) {
122
- const els = this.querySelectorAll(selector);
123
- for (let i = 0, len = els.length; i < len; i++) {
124
- const item = els[i];
125
- item.addEventListener(eventName, listener, false);
126
- this._domEvents.push({el: item, eventName: eventName, handler: listener});
127
- }
128
- return listener;
129
- }
130
-
131
- const handler = selector ? function (e) {
132
- let node = e.target || e.srcElement;
133
- for (; node && node != root; node = node.parentNode) {
134
- if (node.matches(selector)) {
135
- e.delegateTarget = node;
136
- listener(e);
137
- }
138
- }
139
- } : listener;
140
-
141
- this.addEventListener(eventName, handler, false);
142
- this._domEvents.push({el: this, eventName: eventName, handler: handler, listener: listener, selector: selector});
143
- return this;
144
- }
145
-
146
- // Clears all callbacks previously bound to the view by `delegateEvents`.
147
- // You usually don't need to use this, but may wish to if you have multiple
148
- // Backbone views attached to the same DOM element.
149
- undelegateEvents() {
150
- if (this) {
151
- for (let i = 0, len = this._domEvents.length; i < len; i++) {
152
- const item = this._domEvents[i];
153
- item.el.removeEventListener(item.eventName, item.handler, false);
154
- }
155
- this._domEvents.length = 0;
156
- }
157
- return this;
158
- }
159
-
160
- // A finer-grained `undelegateEvents` for removing a single delegated event.
161
- // `selector` and `listener` are both optional.
162
- undelegate(eventName, selector, listener) {
163
- if (typeof selector === 'function') {
164
- listener = selector;
165
- selector = null;
166
- }
167
- if (this) {
168
- const handlers = this._domEvents.slice();
169
- let i = handlers.length;
170
- while (i--) {
171
- const item = handlers[i];
172
- const match = item.eventName === eventName &&
173
- (listener ? item.listener === listener : true) &&
174
- (selector ? item.selector === selector : true);
175
-
176
- if (!match) {
177
- continue;
178
- }
179
- item.el.removeEventListener(item.eventName, item.handler, false);
180
- this._domEvents.splice(i, 1);
181
- }
182
- }
183
- return this;
184
- }
185
- }
186
-
187
- // Set up all inheritable **View** properties and methods.
188
- Object.assign(ElementView.prototype, Events);