reflex_behaviors 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ const added = []
2
+
3
+ const dependencies = {
4
+ LeaderLine: {
5
+ src:
6
+ 'https://cdnjs.cloudflare.com/ajax/libs/leader-line/1.0.7/leader-line.min.js',
7
+ integrity:
8
+ 'sha512-0dNdzMjpT6pJdFGF1DwybFCfm3K/lzHhxaMXC/92J9/DZujHlqYFqmhTOAoD0o+LkeEsVK2ar/ESs7/Q2B6wJg==',
9
+ global: 'LeaderLine'
10
+ },
11
+
12
+ PlainDraggable: {
13
+ src:
14
+ 'https://cdn.jsdelivr.net/npm/plain-draggable@2.5.14/plain-draggable.min.js',
15
+ global: 'PlainDraggable'
16
+ }
17
+ }
18
+
19
+ function exists (dependency) {
20
+ if (dependency.global && self[dependency.global]) return true
21
+ if (document.querySelector(`[src='${dependency.src}']`)) return true
22
+ return added.includes(dependency)
23
+ }
24
+
25
+ function add (dependency) {
26
+ if (exists(dependency)) return
27
+ added.push(dependency)
28
+
29
+ const { src, integrity } = dependency
30
+ const script = document.createElement('script')
31
+ script.setAttribute('src', src)
32
+ script.setAttribute('crossorigin', 'anonymous')
33
+ script.setAttribute('referrerpolicy', 'no-referrer')
34
+ if (integrity) script.setAttribute('integrity', integrity)
35
+ document.head.appendChild(script)
36
+ }
37
+
38
+ function remove (dependency) {
39
+ if (!added.includes(dependency)) return
40
+ added.splice(added.indexOf(dependency), 1)
41
+
42
+ const { src } = dependency
43
+ const el = document.querySelector(`script[src='${src}']`)
44
+ if (el) el.remove()
45
+ if (dependency.global && self[dependency.global])
46
+ self[dependency.global] = null
47
+ }
48
+
49
+ function removeAll () {
50
+ ;[...added].forEach(dependency => remove(dependency))
51
+ }
52
+
53
+ export default { ...dependencies, add, remove, removeAll }
@@ -6,9 +6,13 @@ export default class SupervisorElement extends HTMLElement {
6
6
  this.enabledDevtools = {}
7
7
  this.attachShadow({ mode: 'open' })
8
8
  this.shadowRoot.innerHTML = this.html
9
- this.shadowRoot
10
- .querySelector('button')
11
- .addEventListener('click', () => this.close())
9
+ this.shadowRoot.querySelector('button').addEventListener('click', () =>
10
+ this.dispatchEvent(
11
+ new CustomEvent('reflex-behaviors:devtools-close', {
12
+ bubbles: true
13
+ })
14
+ )
15
+ )
12
16
 
13
17
  this.addEventListener('change', event => {
14
18
  const devtoolElement = event.target
@@ -2,12 +2,7 @@ import { appendHTML } from '../utils/dom'
2
2
  import DevtoolElement from './elements/devtool_element'
3
3
  import SupervisorElement from './elements/supervisor_element'
4
4
  import TooltipElement from './elements/tooltip_element'
5
- import {
6
- addLeaderLineDependency,
7
- addPlainDraggableDependency,
8
- removeLeaderLineDependency,
9
- removePlainDraggableDependency
10
- } from './dependencies'
5
+ import dependencies from './dependencies'
11
6
 
12
7
  customElements.define('reflex-behaviors-devtool', DevtoolElement)
13
8
  customElements.define('reflex-behaviors-devtool-supervisor', SupervisorElement)
@@ -33,14 +28,13 @@ function stop () {
33
28
  })
34
29
  )
35
30
  supervisorElement = null
36
- removeLeaderLineDependency()
37
- removePlainDraggableDependency()
31
+ dependencies.removeAll()
38
32
  }
39
33
 
40
34
  function start () {
41
35
  if (started()) return
42
- addLeaderLineDependency()
43
- addPlainDraggableDependency()
36
+ dependencies.add(dependencies.LeaderLine)
37
+ dependencies.add(dependencies.PlainDraggable)
44
38
  supervisorElement = appendHTML(
45
39
  '<reflex-behaviors-devtool-supervisor></reflex-behaviors-devtool-supervisor>'
46
40
  )
@@ -87,6 +81,7 @@ addEventListener('turbo:load', autoRestart)
87
81
  addEventListener('turbo-frame:load', autoRestart)
88
82
  addEventListener('turbo-reflex:success', autoRestart)
89
83
  addEventListener('turbo-reflex:finish', autoRestart)
84
+ addEventListener('reflex-behaviors:devtools-close', stop)
90
85
 
91
86
  function register (name, label) {
92
87
  if (!supervisorElement) return
@@ -46,11 +46,15 @@ export default class ToggleDevtool {
46
46
  if (name === this.name) removeHighlight(this.trigger)
47
47
  })
48
48
 
49
- document.addEventListener('click', event => {
49
+ addEventListener('click', event => {
50
50
  if (event.target.closest('reflex-behaviors-devools-tooltip')) return
51
- activeToggle = null
52
- this.hide()
51
+ this.hide(true)
53
52
  })
53
+
54
+ addEventListener('turbo:load', () => this.hide(true))
55
+ addEventListener('turbo-frame:load', () => this.hide(true))
56
+ addEventListener('turbo-reflex:success', () => this.hide(true))
57
+ addEventListener('turbo-reflex:finish', () => this.hide(true))
54
58
  }
55
59
 
56
60
  get enabled () {
@@ -101,7 +105,7 @@ export default class ToggleDevtool {
101
105
  console.table(data)
102
106
  }
103
107
 
104
- hide () {
108
+ hide (clearActiveToggle) {
105
109
  document.querySelectorAll('.leader-line').forEach(el => el.remove())
106
110
  document
107
111
  .querySelectorAll('reflex-behaviors-devools-tooltip')
@@ -112,6 +116,8 @@ export default class ToggleDevtool {
112
116
  .forEach(el => {
113
117
  if (!el.tagName.match(/toggle-trigger/i)) removeHighlight(el)
114
118
  })
119
+
120
+ if (clearActiveToggle) activeToggle = null
115
121
  }
116
122
 
117
123
  createRenderingTooltip () {
@@ -1,6 +1,3 @@
1
- import TurboReady from 'turbo_ready'
2
- TurboReady.initialize(Turbo.StreamActions)
3
-
4
1
  import 'turbo_reflex'
5
2
  import './elements'
6
3
  import devtools from './devtools'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReflexBehaviors
4
- VERSION = "0.0.6"
4
+ VERSION = "0.0.7"
5
5
  end
data/package.json CHANGED
@@ -1,17 +1,16 @@
1
1
  {
2
2
  "name": "reflex_behaviors",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
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",
7
7
  "author": "Nate Hopkins (hopsoft) <natehop@gmail.com>",
8
8
  "license": "MIT",
9
9
  "dependencies": {
10
- "turbo_ready": ">= 0.1.2",
11
- "turbo_reflex": ">= 0.0.28"
10
+ "turbo_ready": ">= 0.1.3",
11
+ "turbo_reflex": ">= 0.0.29"
12
12
  },
13
13
  "peerDependencies": {
14
- "@hotwired/turbo": ">= 7.2",
15
14
  "@hotwired/turbo-rails": ">= 7.2"
16
15
  },
17
16
  "devDependencies": {
@@ -22,8 +22,8 @@ Gem::Specification.new do |s|
22
22
 
23
23
  s.add_dependency "rails", ">= 6.1"
24
24
  s.add_dependency "turbo-rails", ">= 1.1"
25
- s.add_dependency "turbo_ready", ">= 0.1.2"
26
- s.add_dependency "turbo_reflex", ">= 0.0.28"
25
+ s.add_dependency "turbo_ready", ">= 0.1.3"
26
+ s.add_dependency "turbo_reflex", ">= 0.0.29"
27
27
 
28
28
  s.add_development_dependency "magic_frozen_string_literal"
29
29
  s.add_development_dependency "minitest-reporters"