Module: LucidComponent::Api

Defined in:
lib/isomorfeus_preact/lucid_component/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 2

def self.included(base)
  base.instance_exec do
    # stores
    attr_accessor :app_store
    attr_accessor :class_store

    def class_store
      @class_store ||= ::LucidComponent::ClassStoreProxy.new(self.to_s)
    end

    # preloading
    def preload(&block)
      pdm_proc = proc do
        if !self.state.preloaded && @_preload_promise
          @_preload_promise.then { self.state.preloaded = true }
        end
      end
      %x{
        base.preload_block = block;
        base.preload_did_mount_proc = pdm_proc;
      }
    end

    def while_loading(option = nil, &block)
      wl_block = proc do
        if @_preload_promise && @_preload_promise.resolved?
          instance_exec(&`base.render_block`)
        else
          instance_exec(@_preload_promise, &block)
        end
      end
      `base.while_loading_block = wl_block`
    end

    # styles
    def styles(styles_hash = nil, &block)
      styles_hash = block.call if block_given?
      if styles_hash
        component_name = self.to_s
        %x{
          let rule_name = component_name.replace(/:/g, '_');
          let ogni = Opal.global.NanoCSSInstance;
          if (base.css_styles && #{Isomorfeus.production?}) { return base.css_styles; }
          else if(#{Isomorfeus.development?}) {
            if (#{on_browser?}) {
              ogni.delete_from_sheet(rule_name);
              ogni.delete_from_rule_blocks(rule_name);
              ogni.hydrate_force_put = true;
            }
          }
          if (typeof styles_hash.$is_wrapped_style !== 'undefined') {
            base.css_styles = styles_hash;
          } else {
            let css;
            if (typeof styles_hash.$to_n === 'function') { css = styles_hash.$to_n(); }
            else { css = styles_hash; }
            let nano_styles = ogni.sheet(css, rule_name);
            base.css_styles = #{::LucidComponent::StylesWrapper.new(`nano_styles`)};
          }
        }
      end
      %x{
        if (!base.css_styles) { return nil; }
        return base.css_styles;
      }
    end
    alias_method :styles=, :styles
  end

  # stores
  def local_store
    LocalStore
  end

  def session_store
    SessionStore
  end

  def theme
    props.theme
  end

  # preloading
  def execute_preload_block
    begin
      @_preload_promise = instance_exec(&self.class.JS[:preload_block])
    rescue => e
      %x{
        console.error(e.message);
        console.error(e.stack);
      }
    end
    if @_preload_promise
      @_preload_promise.fail do |result|
        err_text = "#{self.class.name}: preloading failed, last result: #{result.nil? ? 'nil' : result}!"
        `console.error(err_text)`
      end
      @_preload_promise.resolved?
    else
      false
    end
  end

  def preloaded?
    !!state.preloaded
  end

  # styles
  def styles
    %x{
      let c = self.$class()
      if (typeof(c.css_styles) === 'undefined') { return nil; }
      return c.css_styles;
    }
  end

  # requires transport
  def current_user
    Isomorfeus.current_user
  end
end

Instance Method Details

#current_userObject

requires transport



119
120
121
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 119

def current_user
  Isomorfeus.current_user
end

#execute_preload_blockObject

preloading



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 85

def execute_preload_block
  begin
    @_preload_promise = instance_exec(&self.class.JS[:preload_block])
  rescue => e
    %x{
      console.error(e.message);
      console.error(e.stack);
    }
  end
  if @_preload_promise
    @_preload_promise.fail do |result|
      err_text = "#{self.class.name}: preloading failed, last result: #{result.nil? ? 'nil' : result}!"
      `console.error(err_text)`
    end
    @_preload_promise.resolved?
  else
    false
  end
end

#local_storeObject

stores



72
73
74
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 72

def local_store
  LocalStore
end

#preloaded?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 105

def preloaded?
  !!state.preloaded
end

#session_storeObject



76
77
78
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 76

def session_store
  SessionStore
end

#stylesObject

styles



110
111
112
113
114
115
116
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 110

def styles
  %x{
    let c = self.$class()
    if (typeof(c.css_styles) === 'undefined') { return nil; }
    return c.css_styles;
  }
end

#themeObject



80
81
82
# File 'lib/isomorfeus_preact/lucid_component/api.rb', line 80

def theme
  props.theme
end