reflex_behaviors 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +1,27 @@
1
1
  import ReflexElement from './reflex_element'
2
- import devtools from '../devtools'
2
+ import DevtoolSupervisor from '../devtools/supervisor'
3
+ import ToggleDevtool from '../devtools/toggle'
3
4
 
4
5
  export default class ToggleTriggerElement extends ReflexElement {
5
- constructor () {
6
- super()
7
-
8
- this.addEventListener('mouseenter', event => {
9
- if (devtools.isEnabled('toggle')) {
10
- clearTimeout(this.mouseLeaveTimeout)
11
- event.target.target.classList.add('debug')
12
- event.target.showDebugTooltips()
13
- }
14
- })
6
+ connectedCallback () {
7
+ super.connectedCallback()
15
8
 
16
- this.addEventListener('mouseleave', event => {
17
- if (devtools.isEnabled('toggle')) {
18
- clearTimeout(this.mouseLeaveTimeout)
19
- this.mouseLeaveTimeout = setTimeout(() => {
20
- event.target.target.classList.remove('debug')
21
- document
22
- .querySelectorAll('.reflex-behaviors-tooltip')
23
- .forEach(tooltip => tooltip.remove())
24
- }, 250)
25
- }
26
- })
27
- }
9
+ const mouseenter = () => this.devtool.show()
10
+ const mouseleave = () => this.devtool.hide()
28
11
 
29
- showDebugTooltips () {
30
- const sharedViewPath = this.sharedViewPath
31
- let shared = false
32
- let title = `controls: ${this.controls}`
33
- let body = this.viewStack.map(path => {
34
- shared = shared || path === sharedViewPath
35
- return `<div class='${shared ? 'shared' : null}'>${path}<div>`
12
+ document.addEventListener('reflex-behaviors:devtools-start', () => {
13
+ this.devtool = new ToggleDevtool(this)
14
+ this.addEventListener('mouseenter', mouseenter)
15
+ this.addEventListener('mouseleave', mouseleave)
36
16
  })
37
- devtools.tooltip(this, title, body.join(''), 'trigger', 'top')
38
17
 
39
- shared = false
40
- title = `id: ${this.target.id}`
41
- body = this.target.viewStack.map(path => {
42
- shared = shared || path === sharedViewPath
43
- return `<div class='${shared ? 'shared' : null}'>${path}<div>`
18
+ document.addEventListener('reflex-behaviors:devtools-stop', () => {
19
+ this.removeEventListener('mouseenter', mouseenter)
20
+ this.removeEventListener('mouseleave', mouseleave)
21
+ delete this.devtool
44
22
  })
45
- devtools.tooltip(this.target, title, body.join(''), 'target', 'bottom')
23
+
24
+ DevtoolSupervisor.restart()
46
25
  }
47
26
 
48
27
  collapse () {
@@ -54,19 +33,38 @@ export default class ToggleTriggerElement extends ReflexElement {
54
33
  }
55
34
  }
56
35
 
57
- get sharedViewPath () {
58
- const targetViewStack = this.target.viewStack
59
- return this.viewStack.find(path => targetViewStack.includes(path))
36
+ get sharedViews () {
37
+ if (!this.target) return []
38
+ const reducer = (memo, view) => {
39
+ if (this.target.viewStack.includes(view)) memo.push(view)
40
+ return memo
41
+ }
42
+ return this.viewStack.reduce(reducer.bind(this), [])
60
43
  }
61
44
 
62
- get controls () {
63
- return this.getAttribute('aria-controls')
45
+ get renderingInfo () {
46
+ if (!this.dataset.render) return {}
47
+ return JSON.parse(this.dataset.render)
48
+ }
49
+
50
+ get renderingPartial () {
51
+ return this.renderingInfo.partial
52
+ }
53
+
54
+ get renderingElement () {
55
+ const { id } = this.renderingInfo
56
+ if (!id) return null
57
+ return document.getElementById(id)
64
58
  }
65
59
 
66
60
  get expanded () {
67
61
  return this.getAttribute('aria-expanded') === 'true'
68
62
  }
69
63
 
64
+ get controls () {
65
+ return this.getAttribute('aria-controls')
66
+ }
67
+
70
68
  get target () {
71
69
  return document.getElementById(this.controls)
72
70
  }
@@ -94,6 +92,7 @@ addEventListener(
94
92
  )
95
93
 
96
94
  addEventListener('click', event => {
95
+ if (event.target.tagName.match(/reflex-behaviors-devtool/i)) return
97
96
  setTimeout(() => {
98
97
  const selector =
99
98
  'toggle-trigger[aria-controls][aria-expanded="true"][data-auto-collapse="true"]'
@@ -5,8 +5,4 @@ import 'turbo_reflex'
5
5
  import './elements'
6
6
  import devtools from './devtools'
7
7
 
8
- const { enable, disable, start, stop } = devtools
9
-
10
- self.ReflexBehavors = {
11
- devtools: { enable, disable, start, stop }
12
- }
8
+ self.ReflexBehaviors = { devtools }
@@ -8,7 +8,6 @@ module ReflexBehaviors
8
8
  end
9
9
 
10
10
  class Engine < ::Rails::Engine
11
- config.assets.precompile << "reflex_behaviors_manifest.js"
12
11
  config.reflex_behaviors = ActiveSupport::OrderedOptions.new
13
12
 
14
13
  ActiveSupport.on_load(:action_controller) do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReflexBehaviors
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reflex_behaviors",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Pre-built easy to use reactive TurboReflex behaviors for Rails/Hotwire apps.",
5
5
  "main": "app/javascript/index.js",
6
6
  "repository": "https://github.com/hopsoft/reflex_behaviors",