Sha256: b5fbf43b487ca0938c74c0c9d441861a080559c7b02179eac2cf12cbe4dda2ed

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module Locomotive
  module Steam
    module Liquid
      module Drops

        # Required to allow the sync between the Locomotive editor
        # and the string/text inputs of a section and section block
        class SectionEditorSettingData < ::Liquid::Drop

          def initialize(section)
            @section = section
          end

          def liquid_method_missing(meth)
            block   = nil
            prefix  = "section-#{@context['section'].id}"
            matches = (@context['forloop.name'] || '').match(SECTIONS_BLOCK_FORLOOP_REGEXP)

            # are we inside a block?
            if matches && variable_name = matches[:name]
              block = @context[variable_name]
              prefix += "-block.#{block.id}"
            end

            # only string and text inputs can synced
            if is_text?(meth.to_s, block)
              %( data-locomotive-editor-setting="#{prefix}.#{meth}")
            else
              ''
            end
          end

          private

          def is_text?(id, block)
            settings = block ? block_settings(block['type']) : section_settings

            # can happen if the developer forgets to assign a type to
            # the default blocks
            return false if settings.blank?

            text_inputs(settings).include?(id)
          end

          def text_inputs(settings)
            settings.map do |input|
              %w(text textarea).include?(input['type']) ? input['id'] : nil
            end.compact
          end

          def block_settings(type)
            @section.definition['blocks'].find do |block|
              block['type'] == type
            end&.fetch('settings', nil)
          end

          def section_settings
            @section.definition['settings']
          end
        end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotivecms_steam-1.5.3 lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb