turbo-web 4.3.0 → 4.4.0

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 CHANGED
@@ -84,7 +84,10 @@ JavaScript object tree that represents the UI.
84
84
  ### [CORE] Feature 4: Components
85
85
  **Encapsulation & Reusability** principles: making UI building blocks with better combined hierarchical tree structure.
86
86
 
87
+ // Events accept additional modificator ```.stop .prevent``` to stop event bubbling and prevent default browser behaviour.
88
+
87
89
  ```component.js // building scheme: h(tagOrComponent, { attributes, class, style, on: events }, [children])```
90
+
88
91
  ### Feature 5: Client-Side Router (#hash-based)
89
92
  Allows for the **SPA design** (Single Page Application) with route guards (fn: checkNavigation) and a common "catch-all route" (cases: route not found). Based on a hash part of the URL, implemented with regex pattern matching by simulating URL path, parameters and query as part of the hash fragment itself.
90
93
 
@@ -190,4 +193,10 @@ onMounted() {
190
193
  onUnmounted() {
191
194
  if (this.unsubscribe) this.unsubscribe();
192
195
  },
196
+ ```
197
+ ### [6] Event bubbling & default browser behaviour override
198
+ It is possible to prevent both by passing additional modificators with functions.
199
+ ```
200
+ on: { 'click.stop': () => this.addLog('Button with .stop Clicked') }
201
+ on: { 'click.prevent': () => this.addLog('Link with .prevent Clicked') }
193
202
  ```
package/dist/turbo.js CHANGED
@@ -194,8 +194,15 @@ function hSlot(name = 'default', children = []) {
194
194
  return { type: DOM_TYPES.SLOT, name: name, children: children }
195
195
  }
196
196
 
197
- function addEventListener(eventName, handler, el, hostComponent = null) {
198
- function boundHandler() {
197
+ function addEventListener(eventNameWithModifiers, handler, el, hostComponent = null) {
198
+ const [eventName, ...modifiers] = eventNameWithModifiers.split('.');
199
+ function boundHandler(event) {
200
+ if (modifiers.includes('stop')) {
201
+ event.stopPropagation();
202
+ }
203
+ if (modifiers.includes('prevent')) {
204
+ event.preventDefault();
205
+ }
199
206
  hostComponent
200
207
  ? handler.apply(hostComponent, arguments)
201
208
  : handler(...arguments);
@@ -212,7 +219,8 @@ function addEventListeners(listeners = {}, el, hostComponent = null ) {
212
219
  return addedListeners
213
220
  }
214
221
  function removeEventListeners(listeners = {}, el) {
215
- Object.entries(listeners).forEach(([eventName, handler]) => {
222
+ Object.entries(listeners).forEach(([eventNameWithModifiers, handler]) => {
223
+ const eventName = eventNameWithModifiers.split('.')[0];
216
224
  el.removeEventListener(eventName, handler);
217
225
  });
218
226
  }
@@ -893,14 +901,14 @@ function patchStyles(el, oldStyle = {}, newStyle = {}) {
893
901
  }
894
902
  function patchEvents(el, oldListeners = {}, oldEvents = {}, newEvents = {}, hostComponent) {
895
903
  const { removed, added, updated } = objectsDiff(oldEvents, newEvents);
896
- for (const eventName of removed.concat(updated)) {
897
- el.removeEventListener(eventName, oldListeners[eventName]);
904
+ for (const eventNameWithModifiers of removed.concat(updated)) {
905
+ const eventName = eventNameWithModifiers.split('.')[0];
906
+ el.removeEventListener(eventName, oldListeners[eventNameWithModifiers]);
898
907
  }
899
908
  const addedListeners = {};
900
- for (const eventName of added.concat(updated)) {
901
- const listener =
902
- addEventListener(eventName, newEvents[eventName], el, hostComponent);
903
- addedListeners[eventName] = listener;
909
+ for (const eventNameWithModifiers of added.concat(updated)) {
910
+ const listener = addEventListener(eventNameWithModifiers, newEvents[eventNameWithModifiers], el, hostComponent);
911
+ addedListeners[eventNameWithModifiers] = listener;
904
912
  }
905
913
  return addedListeners
906
914
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turbo-web",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "main": "dist/turbo.js",
5
5
  "files": [
6
6
  "dist",