Sha256: 4119179a0ec6652d0a0e57f6d5151eb1c4ea12b72b62d6d18c25af879f818955

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module StimulusReflex
  class SelectorBroadcaster < Broadcaster
    def broadcast(_, data = {})
      morphs.each do |morph|
        selectors, html = morph
        updates = create_update_collection(selectors, html)
        updates.each do |update|
          document = StimulusReflex::HTML::DocumentFragment.new(update.html)
          match = document.match(update.selector)
          if match.present?
            operations << [update.selector, StimulusReflex.config.morph_operation]
            cable_ready.send StimulusReflex.config.morph_operation, {
              selector: update.selector,
              html: match.inner_html,
              payload: payload,
              children_only: true,
              permanent_attribute_name: permanent_attribute_name,
              stimulus_reflex: data.merge(morph: to_sym)
            }
          else
            operations << [update.selector, StimulusReflex.config.replace_operation]
            cable_ready.send StimulusReflex.config.replace_operation, {
              selector: update.selector,
              html: update.html.to_s,
              payload: payload,
              stimulus_reflex: data.merge(morph: to_sym)
            }
          end
        end
      end

      cable_ready.broadcast
      morphs.clear
    end

    def morphs
      @morphs ||= []
    end

    def append_morph(selectors, html)
      morphs << [selectors, html]
    end

    def to_sym
      :selector
    end

    def selector?
      true
    end

    def to_s
      "Selector"
    end

    private

    def create_update_collection(selectors, html)
      updates = selectors.is_a?(Hash) ? selectors : {selectors => html}
      updates.map do |key, value|
        StimulusReflex::Broadcasters::Update.new(key, value, reflex)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
stimulus_reflex-3.5.3 lib/stimulus_reflex/broadcasters/selector_broadcaster.rb
stimulus_reflex-3.5.0.rc4 lib/stimulus_reflex/broadcasters/selector_broadcaster.rb
stimulus_reflex-3.5.0.rc3 lib/stimulus_reflex/broadcasters/selector_broadcaster.rb
stimulus_reflex-3.5.0.rc2 lib/stimulus_reflex/broadcasters/selector_broadcaster.rb
stimulus_reflex-3.5.0.rc1 lib/stimulus_reflex/broadcasters/selector_broadcaster.rb
stimulus_reflex-3.5.0.pre10 lib/stimulus_reflex/broadcasters/selector_broadcaster.rb