stimulus_reflex
Advanced tools
Comparing version 1.1.1 to 2.0.0
{ | ||
"name": "stimulus_reflex", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"main": "./stimulus_reflex.js", | ||
@@ -16,3 +16,4 @@ "scripts": { | ||
"actioncable": ">= 5.2", | ||
"cable_ready": ">= 4.0.3" | ||
"cable_ready": ">= 4.0.3", | ||
"stimulus": ">= 1.1.1" | ||
}, | ||
@@ -19,0 +20,0 @@ "devDependencies": { |
import ActionCable from 'actioncable'; | ||
import CableReady from 'cable_ready'; | ||
import StimulusReflexController from './stimulus_reflex_controller'; | ||
@@ -63,18 +64,47 @@ const app = window.App || {}; | ||
export default { | ||
// | ||
// Registers a Stimulus controller and extends it with StimulusReflex behavior | ||
// The room can be specified via a data attribute on the Stimulus controller element i.e. data-room="12345" | ||
// | ||
// controller - the Stimulus controller | ||
// options - optional configuration | ||
// * renderDelay - amount of time to delay before mutating the DOM (adds latency but reduces jitter) | ||
// | ||
register: (controller, options = {}) => { | ||
const channel = 'StimulusReflex::Channel'; | ||
const room = controller.element.dataset.room || ''; | ||
controller.StimulusReflex = { ...options, channel, room }; | ||
createSubscription(controller); | ||
extend(controller); | ||
}, | ||
// Sets up implicit declarative reflex behavior | ||
const setup = () => { | ||
document.querySelectorAll('[data-reflex]').forEach(el => { | ||
if (String(el.dataset.controller).indexOf('stimulus-reflex') >= 0) return; | ||
const controllers = el.dataset.controller ? el.dataset.controller.split(' ') : []; | ||
const actions = el.dataset.action ? el.dataset.action.split(' ') : []; | ||
controllers.push('stimulus-reflex'); | ||
el.setAttribute('data-controller', controllers.join(' ')); | ||
el.dataset.reflex.split(' ').forEach(reflex => { | ||
actions.push(`${reflex.split('->')[0]}->stimulus-reflex#perform`); | ||
}); | ||
el.setAttribute('data-action', actions.join(' ')); | ||
}); | ||
}; | ||
// Initializes StimulusReflex by registering the default Stimulus controller | ||
// with the passed Stimulus application | ||
const initialize = application => { | ||
application.register('stimulus-reflex', StimulusReflexController); | ||
}; | ||
// Registers a Stimulus controller and extends it with StimulusReflex behavior | ||
// The room can be specified via a data attribute on the Stimulus controller element i.e. data-room="12345" | ||
// | ||
// controller - the Stimulus controller | ||
// options - optional configuration | ||
// * renderDelay - amount of time to delay before mutating the DOM (adds latency but reduces jitter) | ||
// | ||
const register = (controller, options = {}) => { | ||
const channel = 'StimulusReflex::Channel'; | ||
const room = controller.element.dataset.room || ''; | ||
controller.StimulusReflex = { ...options, channel, room }; | ||
createSubscription(controller); | ||
extend(controller); | ||
}; | ||
StimulusReflexController.register = register; | ||
if (!document.stimulusReflexInitialized) { | ||
document.stimulusReflexInitialized = true; | ||
window.addEventListener('load', setup); | ||
document.addEventListener('turbolinks:load', setup); | ||
document.addEventListener('cable-ready:after-morph', setup); | ||
} | ||
export default { initialize, register }; |
4939
3
107
3
+ Addedstimulus@>= 1.1.1
+ Added@hotwired/stimulus@3.2.2(transitive)
+ Added@hotwired/stimulus-webpack-helpers@1.0.1(transitive)
+ Addedstimulus@3.2.2(transitive)