-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new controllers and enhance functionality of controllers
Introduce `device-motion` and `touch` controllers, enhance existing controllers with improved event dispatching and throttling, and add support for new configuration options like monochrome icons. Refactor code for better scalability, providing utilities like `expandIcons` and abstracting specific controller features.
- Loading branch information
Showing
23 changed files
with
280 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
'use strict'; | ||
|
||
import { getComponent } from '@symfony/ux-live-component'; | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
component = null; | ||
async initialize() { | ||
try { | ||
this.component = await getComponent(this.element); | ||
} catch (e) { | ||
} | ||
} | ||
|
||
dispatchEvent = (name, payload) => { | ||
this.dispatch(name, { detail: payload }); | ||
if (payload === undefined) { | ||
payload = {}; | ||
} | ||
this.dispatch('pwa:'+name, { detail: payload, bubbles: true }); | ||
if (this.component) { | ||
this.component.emit('pwa:'+name, payload); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
import AbstractController from './abstract_controller.js'; | ||
|
||
/* stimulusFetch: 'lazy' */ | ||
export default class extends AbstractController { | ||
static values = { | ||
throttle: { type: Number, default: 1000 }, | ||
}; | ||
|
||
connect() { | ||
const throttle = (func, limit) => { | ||
let inThrottle; | ||
return function() { | ||
const context = this; | ||
if (!inThrottle) { | ||
func.apply(context, arguments); | ||
inThrottle = true; | ||
setTimeout(() => (inThrottle = false), limit); | ||
} | ||
}; | ||
}; | ||
|
||
const dispatchMotionEvent = (event) => { | ||
this.dispatchEvent('device:motion', { | ||
acceleration: event.acceleration, | ||
accelerationIncludingGravity: event.accelerationIncludingGravity, | ||
rotationRate: event.rotationRate, | ||
interval: event.interval, | ||
}); | ||
}; | ||
|
||
const throttledDispatch = throttle(dispatchMotionEvent.bind(this), this.throttleValue); | ||
window.addEventListener('devicemotion', throttledDispatch, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
import AbstractController from './abstract_controller.js'; | ||
|
||
export default class extends AbstractController { | ||
ongoingTouches = []; | ||
|
||
connect() { | ||
this.element.addEventListener("touchstart", this.onTouchStart); | ||
this.element.addEventListener("touchmove", this.onTouchMove); | ||
this.element.addEventListener("touchcancel", this.onTouchCancel); | ||
this.element.addEventListener("touchend", this.onTouchEnd); | ||
} | ||
|
||
copyTouch = ({ identifier, clientX, clientY, pageX, pageY, radiusX, radiusY, screenX, screenY, force, rotationAngle})=> { | ||
return { | ||
identifier, clientX, clientY, pageX, pageY, radiusX, radiusY, screenX, screenY, force, rotationAngle, | ||
top: this.element.offsetTop, | ||
left: this.element.offsetLeft, | ||
}; | ||
} | ||
|
||
onTouchEnd = (event) => { | ||
event.preventDefault(); | ||
const {changedTouches} = event; | ||
for (let i = 0; i < changedTouches.length; i++) { | ||
const idx = this.ongoingTouchIndexById(changedTouches[i].identifier); | ||
this.ongoingTouches.splice(idx, 1); | ||
this.dispatchEvent('touch:ended', { touch: changedTouches[i], bubbles: true }); | ||
} | ||
this.dispatchEvent('touch:updated', { touches: this.ongoingTouches, bubbles: true }); | ||
} | ||
|
||
onTouchCancel = (event) => { | ||
event.preventDefault(); | ||
const {changedTouches} = event; | ||
for (let i = 0; i < changedTouches.length; i++) { | ||
const idx = this.ongoingTouchIndexById(changedTouches[i].identifier); | ||
this.ongoingTouches.splice(idx, 1); | ||
this.dispatchEvent('touch:cancelled', { touch: changedTouches[i], bubbles: true }); | ||
} | ||
this.dispatchEvent('touch:updated', { touches: this.ongoingTouches, bubbles: true }); | ||
} | ||
|
||
onTouchMove = (event) => { | ||
event.preventDefault(); | ||
const {changedTouches} = event; | ||
for (let i = 0; i < changedTouches.length; i++) { | ||
const idx = this.ongoingTouchIndexById(changedTouches[i].identifier); | ||
this.ongoingTouches.splice(idx, 1, this.copyTouch(changedTouches[i])) | ||
this.dispatchEvent('touch:moved', { touch: changedTouches[i], bubbles: true }); | ||
} | ||
this.dispatchEvent('touch:updated', { touches: this.ongoingTouches, bubbles: true }); | ||
} | ||
|
||
onTouchStart = (event) => { | ||
event.preventDefault(); | ||
const {changedTouches} = event; | ||
for (let i = 0; i < changedTouches.length; i++) { | ||
this.ongoingTouches.push(this.copyTouch(changedTouches[i])); | ||
this.dispatchEvent('touch:started', { touch: changedTouches[i], bubbles: true }); | ||
} | ||
this.dispatchEvent('touch:updated', { touches: this.ongoingTouches, bubbles: true }); | ||
} | ||
|
||
ongoingTouchIndexById = (idToFind) => { | ||
for (let i = 0; i < this.ongoingTouches.length; i++) { | ||
const id = this.ongoingTouches[i].identifier; | ||
|
||
if (id === idToFind) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.