rasti 2.0.0-alpha.9 → 2.0.1
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 +51 -13
- package/dist/rasti.js +4 -4
- package/dist/rasti.min.js +1 -1
- package/es/Component.js +72 -59
- package/es/Emitter.js +2 -2
- package/es/Model.js +12 -11
- package/es/View.js +13 -10
- package/lib/Component.js +72 -59
- package/lib/Emitter.js +2 -2
- package/lib/Model.js +12 -11
- package/lib/View.js +13 -10
- package/package.json +34 -34
- package/src/Component.js +75 -59
- package/src/Emitter.js +2 -2
- package/src/Model.js +9 -7
- package/src/View.js +11 -7
package/src/Component.js
CHANGED
|
@@ -47,9 +47,9 @@ const evalExpression = (expression, context, ...args) =>
|
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* Components are a special kind of `View` that is designed to be easily composable,
|
|
50
|
-
* making it simple to add child views and build complex user interfaces
|
|
50
|
+
* making it simple to add child views and build complex user interfaces.
|
|
51
51
|
* Unlike views, which are render-agnostic, components have a specific set of rendering
|
|
52
|
-
* guidelines that allow for a more declarative development style
|
|
52
|
+
* guidelines that allow for a more declarative development style.
|
|
53
53
|
* Components are defined with the `create` static method, which takes a tagged template.
|
|
54
54
|
* @module
|
|
55
55
|
* @extends Rasti.View
|
|
@@ -74,25 +74,19 @@ const evalExpression = (expression, context, ...args) =>
|
|
|
74
74
|
*/
|
|
75
75
|
export default class Component extends View {
|
|
76
76
|
constructor(options = {}) {
|
|
77
|
-
super(
|
|
77
|
+
super(...arguments);
|
|
78
78
|
// Extend "this" with options, mapping componentOptions keys.
|
|
79
79
|
Object.keys(options).forEach(key => {
|
|
80
80
|
if (componentOptions[key]) this[key] = options[key];
|
|
81
81
|
});
|
|
82
82
|
// Store options by default.
|
|
83
83
|
this.options = options;
|
|
84
|
-
// Ensure id.
|
|
85
|
-
this.id = this.attributes.id ?
|
|
86
|
-
// If id is provided, evaluate it.
|
|
87
|
-
evalExpression(this.attributes.id, this, this) :
|
|
88
|
-
// Generate a unique id and set it as id attribute.
|
|
89
|
-
Component.ID_TEMPLATE(this.uid);
|
|
90
84
|
// Bind onChange to this to be used as listener.
|
|
91
85
|
// Store bound version, so it can be removed on onDestroy method.
|
|
92
86
|
this.onChange = this.onChange.bind(this);
|
|
93
87
|
// Listen to model changes and call onChange.
|
|
94
|
-
if (this.model) this.model.on('change', this.onChange);
|
|
95
|
-
if (this.state) this.state.on('change', this.onChange);
|
|
88
|
+
if (this.model && this.model.on) this.model.on('change', this.onChange);
|
|
89
|
+
if (this.state && this.state.on) this.state.on('change', this.onChange);
|
|
96
90
|
// Call life cycle method.
|
|
97
91
|
this.onCreate.apply(this, arguments);
|
|
98
92
|
}
|
|
@@ -103,7 +97,18 @@ export default class Component extends View {
|
|
|
103
97
|
*/
|
|
104
98
|
ensureElement() {
|
|
105
99
|
// If el is provided, delegate events.
|
|
106
|
-
if (this.el)
|
|
100
|
+
if (this.el) {
|
|
101
|
+
this.delegateEvents();
|
|
102
|
+
this.id = this.el.id;
|
|
103
|
+
}
|
|
104
|
+
// Ensure id.
|
|
105
|
+
if (!this.id) {
|
|
106
|
+
this.id = this.attributes && this.attributes.id ?
|
|
107
|
+
// If id is provided, evaluate it.
|
|
108
|
+
evalExpression(this.attributes.id, this, this) :
|
|
109
|
+
// Generate a unique id and set it as id attribute.
|
|
110
|
+
Component.ID_TEMPLATE(this.uid);
|
|
111
|
+
}
|
|
107
112
|
}
|
|
108
113
|
|
|
109
114
|
/*
|
|
@@ -117,33 +122,32 @@ export default class Component extends View {
|
|
|
117
122
|
* Eval attributes expressions.
|
|
118
123
|
*/
|
|
119
124
|
getAttributes() {
|
|
120
|
-
const add = {};
|
|
125
|
+
const add = { id : this.id };
|
|
121
126
|
const remove = {};
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
add[key] = value;
|
|
139
|
-
attrs.push(`${key}="${value}"`);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
127
|
+
const html = [`id="${this.id}"`];
|
|
128
|
+
|
|
129
|
+
if (this.attributes) {
|
|
130
|
+
Object.keys(this.attributes).forEach(key => {
|
|
131
|
+
if (key === 'id') return;
|
|
132
|
+
// Evaluate attribute value.
|
|
133
|
+
let value = evalExpression(this.attributes[key], this, this);
|
|
134
|
+
|
|
135
|
+
// Transform bool attribute values
|
|
136
|
+
if (value === false) {
|
|
137
|
+
remove[key] = true;
|
|
138
|
+
} else if (value === true) {
|
|
139
|
+
add[key] = '';
|
|
140
|
+
html.push(key);
|
|
141
|
+
} else {
|
|
142
|
+
if (value === null || typeof value === 'undefined') value = '';
|
|
142
143
|
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
add[key] = value;
|
|
145
|
+
html.push(`${key}="${value}"`);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
145
149
|
|
|
146
|
-
return { add, remove, html :
|
|
150
|
+
return { add, remove, html : html.join(' ') };
|
|
147
151
|
}
|
|
148
152
|
|
|
149
153
|
/*
|
|
@@ -176,10 +180,12 @@ export default class Component extends View {
|
|
|
176
180
|
destroy() {
|
|
177
181
|
super.destroy.apply(this, arguments);
|
|
178
182
|
// Stop listening to `change`.
|
|
179
|
-
|
|
180
|
-
if (this.
|
|
181
|
-
|
|
183
|
+
if (this.model && this.model.off) this.model.off('change', this.onChange);
|
|
184
|
+
if (this.state && this.state.off) this.state.off('change', this.onChange);
|
|
185
|
+
// Set destroyed flag to prevent a last render after destroyed.
|
|
182
186
|
this.destroyed = true;
|
|
187
|
+
// Return `this` for chaining.
|
|
188
|
+
return this;
|
|
183
189
|
}
|
|
184
190
|
|
|
185
191
|
/**
|
|
@@ -195,8 +201,8 @@ export default class Component extends View {
|
|
|
195
201
|
* Maybe comparing new attributes with previous ones and calling
|
|
196
202
|
* render when needed. Or doing some dom transformation.
|
|
197
203
|
* @param model {Rasti.Model} The model that emitted the event.
|
|
198
|
-
* @param
|
|
199
|
-
* @param
|
|
204
|
+
* @param changed {object} Object containing keys and values that has changed.
|
|
205
|
+
* @param [...args] {any} Any extra arguments passed to set method.
|
|
200
206
|
*/
|
|
201
207
|
onChange() {
|
|
202
208
|
this.render();
|
|
@@ -243,12 +249,13 @@ export default class Component extends View {
|
|
|
243
249
|
}, '');
|
|
244
250
|
|
|
245
251
|
})
|
|
246
|
-
// Replace `attribute="true"` with `attribute`
|
|
247
|
-
.replace(
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
+
// Replace `attribute="true"` with `attribute` and `attribute="false"` with empty string.
|
|
253
|
+
.replace(
|
|
254
|
+
new RegExp(`([\\w|data-]+)=(["'])?(${Component.TRUE_PLACEHOLDER}|${Component.FALSE_PLACEHOLDER})\\2`, 'g'),
|
|
255
|
+
(match, attribute, quote, placeholder) => placeholder === Component.TRUE_PLACEHOLDER ? attribute : ''
|
|
256
|
+
)
|
|
257
|
+
// Replace rest of true or false expressions with empty string.
|
|
258
|
+
.replace(new RegExp(`${Component.TRUE_PLACEHOLDER}|${Component.FALSE_PLACEHOLDER}`, 'g'), '');
|
|
252
259
|
}
|
|
253
260
|
|
|
254
261
|
/*
|
|
@@ -257,15 +264,21 @@ export default class Component extends View {
|
|
|
257
264
|
toString() {
|
|
258
265
|
// Normally there won't be any children, but if there are, destroy them.
|
|
259
266
|
this.destroyChildren();
|
|
260
|
-
//
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
267
|
+
// Get tag name.
|
|
268
|
+
const tag = this.tag || 'div';
|
|
269
|
+
// Get attributes.
|
|
270
|
+
const attributes = this.getAttributes().html;
|
|
271
|
+
// Replace expressions of inner template.
|
|
272
|
+
const close = this.template && this.template.inner;
|
|
273
|
+
const inner = close &&
|
|
274
|
+
this.replaceExpressions(this.template.inner, (component) => {
|
|
275
|
+
// Add child component.
|
|
276
|
+
return this.addChild(component);
|
|
277
|
+
});
|
|
265
278
|
// Generate outer template.
|
|
266
|
-
return
|
|
267
|
-
`<${
|
|
268
|
-
`<${
|
|
279
|
+
return close ?
|
|
280
|
+
`<${tag} ${attributes}>${inner}</${tag}>` :
|
|
281
|
+
`<${tag} ${attributes} />`;
|
|
269
282
|
}
|
|
270
283
|
|
|
271
284
|
/*
|
|
@@ -290,7 +303,7 @@ export default class Component extends View {
|
|
|
290
303
|
this.el.setAttribute(key, attributes.add[key]);
|
|
291
304
|
});
|
|
292
305
|
// Check for `template.inner` to see if view has innerHTML.
|
|
293
|
-
if (this.template.inner) {
|
|
306
|
+
if (this.template && this.template.inner) {
|
|
294
307
|
const previousChildren = this.children;
|
|
295
308
|
|
|
296
309
|
this.children = [];
|
|
@@ -308,8 +321,10 @@ export default class Component extends View {
|
|
|
308
321
|
);
|
|
309
322
|
|
|
310
323
|
if (found) {
|
|
324
|
+
const tag = found.el.tagName.toLowerCase();
|
|
325
|
+
const id = found.el.id;
|
|
311
326
|
// If child already exists, replace it html by its root element.
|
|
312
|
-
out = `<${
|
|
327
|
+
out = `<${tag} id="${id}"></${tag}>`;
|
|
313
328
|
// Add child to recycled children.
|
|
314
329
|
recycledChildren.push(found);
|
|
315
330
|
// Destroy new child component. Use recycled one instead.
|
|
@@ -374,7 +389,7 @@ export default class Component extends View {
|
|
|
374
389
|
* @param {boolean} hydrate If true, the view will use existing html.
|
|
375
390
|
* @return {Rasti.Component}
|
|
376
391
|
*/
|
|
377
|
-
static mount(options
|
|
392
|
+
static mount(options, el, hydrate) {
|
|
378
393
|
// Instantiate view.
|
|
379
394
|
const view = new this(options);
|
|
380
395
|
// If `el` is passed, mount component.
|
|
@@ -393,6 +408,7 @@ export default class Component extends View {
|
|
|
393
408
|
// Return view instance.
|
|
394
409
|
return view;
|
|
395
410
|
}
|
|
411
|
+
|
|
396
412
|
/**
|
|
397
413
|
* Takes a tagged template containing an HTML string,
|
|
398
414
|
* and returns a new `Component` class.
|
|
@@ -427,7 +443,7 @@ export default class Component extends View {
|
|
|
427
443
|
// Create output text for main template.
|
|
428
444
|
const main = parts.join('').trim().replace(/\n/g, '');
|
|
429
445
|
// Extract outer tag, attributes and inner html.
|
|
430
|
-
const result = main.match(/^<([a-z]+)(.*?)>(.*)<\/\1>$/) || main.match(/^<([a-z]+)(.*?)\/>$/);
|
|
446
|
+
const result = main.match(/^<([a-z]+[1-6]?)(.*?)>(.*)<\/\1>$/) || main.match(/^<([a-z]+)(.*?)\/>$/);
|
|
431
447
|
// Parse attributes from html text into an object.
|
|
432
448
|
let attributes = extractAttributes(result[2]);
|
|
433
449
|
// Events to be delegated.
|
package/src/Emitter.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `Emitter` is a class that provides an easy way to implement the observer pattern
|
|
3
|
-
* in your applications
|
|
4
|
-
* It can be extended to create new classes that have the ability to emit and bind custom named events
|
|
3
|
+
* in your applications.
|
|
4
|
+
* It can be extended to create new classes that have the ability to emit and bind custom named events.
|
|
5
5
|
* Emitter is used by `Model` and `View` classes, which inherit from it to implement
|
|
6
6
|
* event-driven functionality.
|
|
7
7
|
*
|
package/src/Model.js
CHANGED
|
@@ -3,10 +3,10 @@ import Emitter from './Emitter.js';
|
|
|
3
3
|
* - Orchestrates data and business logic.
|
|
4
4
|
* - Emits events when data changes.
|
|
5
5
|
*
|
|
6
|
-
* A `Model` manages an internal table of data attributes and triggers change events when any of its data is modified
|
|
6
|
+
* A `Model` manages an internal table of data attributes and triggers change events when any of its data is modified.
|
|
7
7
|
* Models may handle syncing data with a persistence layer. To design your models, create atomic, reusable objects
|
|
8
|
-
* that contain all the necessary functions for manipulating their specific data
|
|
9
|
-
* Models should be easily passed throughout your app and used anywhere the corresponding data is needed
|
|
8
|
+
* that contain all the necessary functions for manipulating their specific data.
|
|
9
|
+
* Models should be easily passed throughout your app and used anywhere the corresponding data is needed.
|
|
10
10
|
* Rasti models stores its attributes in `this.attributes`, which is extended from `this.defaults` and the
|
|
11
11
|
* constructor `attrs` parameter. For every attribute, a getter is generated to retrieve the model property
|
|
12
12
|
* from `this.attributes`, and a setter is created to set the model property in `this.attributes` and emit `change`
|
|
@@ -90,11 +90,13 @@ export default class Model extends Emitter {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
* Set an attribute into `this.attributes`.
|
|
94
|
-
* Emit `change` and `change:attribute` if a value change.
|
|
93
|
+
* Set an attribute into `this.attributes`.
|
|
94
|
+
* Emit `change` and `change:attribute` if a value change.
|
|
95
95
|
* Could be called in two forms, `this.set('key', value)` and
|
|
96
|
-
* `this.set({ key : value })`.
|
|
97
|
-
* This method is called internally by generated setters.
|
|
96
|
+
* `this.set({ key : value })`.
|
|
97
|
+
* This method is called internally by generated setters.
|
|
98
|
+
* The `change` event listener will receive the model instance, an object containing the changed attributes, and the rest of the arguments passed to `set` method.
|
|
99
|
+
* The `change:attribute` event listener will receive the model instance, the new attribute value, and the rest of the arguments passed to `set` method.
|
|
98
100
|
* @param {string} key Attribute key or object containing keys/values.
|
|
99
101
|
* @param [value] Attribute value.
|
|
100
102
|
* @return {this} This model.
|
package/src/View.js
CHANGED
|
@@ -17,10 +17,10 @@ const viewOptions = {
|
|
|
17
17
|
* - Sends captured input to the model.
|
|
18
18
|
*
|
|
19
19
|
* A `View` is an atomic unit of the user interface that can render the data from a specific model or multiple models.
|
|
20
|
-
* However, views can also be independent and have no associated data
|
|
20
|
+
* However, views can also be independent and have no associated data.
|
|
21
21
|
* Models must be unaware of views. Views, on the other hand, may render model data and listen to the change events
|
|
22
|
-
* emitted by the models to re-render themselves based on changes
|
|
23
|
-
* Each `View` has a root element, `this.el`, which is used for event delegation
|
|
22
|
+
* emitted by the models to re-render themselves based on changes.
|
|
23
|
+
* Each `View` has a root element, `this.el`, which is used for event delegation.
|
|
24
24
|
* All element lookups are scoped to this element, and any rendering or DOM manipulations should be done inside it.
|
|
25
25
|
* If `this.el` is not present, an element will be created using `this.tag` (defaulting to div) and `this.attributes`.
|
|
26
26
|
* @module
|
|
@@ -104,6 +104,7 @@ export default class View extends Emitter {
|
|
|
104
104
|
/**
|
|
105
105
|
* Destroy the view.
|
|
106
106
|
* Destroy children views if any, undelegate events, stop listening to events, call `onDestroy` lifecycle method.
|
|
107
|
+
* @return {Rasti.View} Return `this` for chaining.
|
|
107
108
|
*/
|
|
108
109
|
destroy() {
|
|
109
110
|
// Call destroy on children.
|
|
@@ -180,18 +181,21 @@ export default class View extends Emitter {
|
|
|
180
181
|
|
|
181
182
|
/**
|
|
182
183
|
* Remove `this.el` from DOM.
|
|
184
|
+
* @return {Rasti.View} Return `this` for chaining.
|
|
183
185
|
*/
|
|
184
186
|
removeElement() {
|
|
185
187
|
this.el.parentNode.removeChild(this.el);
|
|
188
|
+
// Return `this` for chaining.
|
|
189
|
+
return this;
|
|
186
190
|
}
|
|
187
191
|
|
|
188
192
|
/**
|
|
189
|
-
* Provide declarative listeners for DOM events within a view. If an events hash is not passed directly, uses `this.events` as the source
|
|
193
|
+
* Provide declarative listeners for DOM events within a view. If an events hash is not passed directly, uses `this.events` as the source.
|
|
190
194
|
* Events are written in the format `{'event selector' : 'listener'}`. The listener may be either the name of a method on the view, or a direct function body.
|
|
191
|
-
* Omitting the selector causes the event to be bound to the view's root element (`this.el`)
|
|
195
|
+
* Omitting the selector causes the event to be bound to the view's root element (`this.el`).
|
|
192
196
|
* By default, `delegateEvents` is called within the View's constructor,
|
|
193
|
-
* so if you have a simple events hash, all of your DOM events will always already be connected, and you will never have to call this function yourself.
|
|
194
|
-
* All attached listeners are bound to the view automatically, so when the listeners are invoked, `this` continues to refer to the view object
|
|
197
|
+
* so if you have a simple events hash, all of your DOM events will always already be connected, and you will never have to call this function yourself.
|
|
198
|
+
* All attached listeners are bound to the view automatically, so when the listeners are invoked, `this` continues to refer to the view object.
|
|
195
199
|
* When `delegateEvents` is run again, perhaps with a different events hash, all listeners are removed and delegated afresh.
|
|
196
200
|
* @param {object} [events] Object in the format `{'event selector' : 'listener'}`. Used to bind delegated event listeners to root element.
|
|
197
201
|
* @return {Rasti.View} Return `this` for chaining.
|