Sha256: f2a68b7ef6c84b65058f21fbc6843f4a0ac7cb1dbb9cf6a48998aae5cb8ed5d5

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require 'react/component'

module Hyperloop
  module D3
    module Mixin
      def self.included(base)
        base.include(Hyperloop::Component::Mixin)
        base.class_eval do
          param data: nil

          def _set_dom_node(dom_node)
            @_dom_node = dom_node
          end

          def data
            @_data
          end
          
          def selection
            if @_dom_node
              selection = ::D3.select(@_dom_node)
            else
              raise "No DOM Node available for D3::Selection"
            end
          end

          def redraw
            if @_dom_node && @_d3_render_block
              @_d3_render_block.call(selection, @_data)
            end
          end

          def self.before_unmount_with_selection(&block)
            before_unmount do
              if @_dom_node && block
                block.call(selection, @_data)
              end
            end
          end

          def self.render_with_selection(tag = 'SVG', &block)
            render do
              if block
                @_d3_render_block = block
              else
                @_d3_render_block = proc { |selection, data| selection.text("Please supply a block for render_with_selection")}
              end
              @_data = params.data
              send(tag, ref: method(:_set_dom_node).to_proc)
            end
          end

          def should_component_update?
            false
          end

          after_mount do
            redraw
          end

          before_receive_props do |new_props|
            if new_props[:data] != @_data
              @_data = new_props[:data] 
              redraw
            end
          end
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hyper-d3-1.0.0.lap28 lib/hyperloop/d3/mixin.rb
hyper-d3-1.0.0.lap27 lib/hyperloop/d3/mixin.rb