Class: Page

Inherits:
Object show all
Defined in:
lib/volt/page/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePage

Returns a new instance of Page.



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
# File 'lib/volt/page/page.rb', line 45

def initialize
  # debugger
  puts "------ Page Loaded -------"
  @model_classes = {}

  # Run the code to setup the page
  @page = ReactiveValue.new(Model.new)

  @url = ReactiveValue.new(URL.new)
  @params = @url.params
  @url_tracker = UrlTracker.new(self)

  @events = DocumentEvents.new
  @draw_cycle = DrawCycle.new

  if RUBY_PLATFORM == 'opal'
    # Setup escape binding for console
    %x{
      $(document).keyup(function(e) {
        if (e.keyCode == 27) {
          Opal.gvars.page.$launch_console();
        }
      });

      $(document).on('click', 'a', function(event) {
        return Opal.gvars.page.$link_clicked($(this).attr('href'), event);
      });
    }
  end
end

Instance Attribute Details

#draw_cycleObject (readonly)

Returns the value of attribute draw_cycle.



43
44
45
# File 'lib/volt/page/page.rb', line 43

def draw_cycle
  @draw_cycle
end

#eventsObject (readonly)

Returns the value of attribute events.



43
44
45
# File 'lib/volt/page/page.rb', line 43

def events
  @events
end

#pageObject (readonly)

Returns the value of attribute page.



43
44
45
# File 'lib/volt/page/page.rb', line 43

def page
  @page
end

#paramsObject (readonly)

Returns the value of attribute params.



43
44
45
# File 'lib/volt/page/page.rb', line 43

def params
  @params
end

#routesObject (readonly)

Returns the value of attribute routes.



43
44
45
# File 'lib/volt/page/page.rb', line 43

def routes
  @routes
end

#templatesObject (readonly)

Returns the value of attribute templates.



43
44
45
# File 'lib/volt/page/page.rb', line 43

def templates
  @templates
end

#urlObject (readonly)

Returns the value of attribute url.



43
44
45
# File 'lib/volt/page/page.rb', line 43

def url
  @url
end

Instance Method Details

#add_model(model_name) ⇒ Object



139
140
141
142
143
# File 'lib/volt/page/page.rb', line 139

def add_model(model_name)
  # puts "ADD MODEL: #{model_name.inspect} - #{model_name.camelize.inspect}"

  @model_classes[["*", "_#{model_name}"]] = Object.const_get(model_name.camelize)
end

#add_routes(&block) ⇒ Object



152
153
154
155
# File 'lib/volt/page/page.rb', line 152

def add_routes(&block)
  @routes = Routes.new.define(&block)
  @url.cur.router = @routes
end

#add_template(name, template, bindings) ⇒ Object



145
146
147
148
149
150
# File 'lib/volt/page/page.rb', line 145

def add_template(name, template, bindings)
  # puts "Add Template: #{name}\n#{template.inspect}\n#{bindings.inspect}"
  @templates ||= {}
  @templates[name] = {'html' => template, 'bindings' => bindings}
  # puts "Add Template: #{name}"
end

#binding_nameObject

We provide a binding_name, so we can bind events on the document



117
118
119
# File 'lib/volt/page/page.rb', line 117

def binding_name
  'page'
end

#channelObject



125
126
127
128
129
130
131
132
133
# File 'lib/volt/page/page.rb', line 125

def channel
  @channel ||= begin
    if Volt.client?
      ReactiveValue.new(Channel.new)
    else
      ReactiveValue.new(ChannelStub.new)
    end
  end
end

#flashObject



76
77
78
# File 'lib/volt/page/page.rb', line 76

def flash
  @flash ||= ReactiveValue.new(Model.new({}, persistor: Persistors::Flash))
end

#launch_consoleObject



121
122
123
# File 'lib/volt/page/page.rb', line 121

def launch_console
  puts "Launch Console"
end


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/volt/page/page.rb', line 92

def link_clicked(url='', event=nil)
  # Skip when href == ''
  return false if url.blank?

  # Normalize url
  # Benchmark.bm(1) do
  if @url.parse(url)
    if event
      # Handled new url
      `event.stopPropagation();`
    end

    # Clear the flash
    flash.clear

    # return false to stop the event propigation
    return false
  end
  # end

  # Not stopping, process link normally
  return true
end

#load_stored_pageObject

When the page is reloaded from the backend, we store the $page.page, so we can reload the page in the exact same state. Speeds up development.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/volt/page/page.rb', line 186

def load_stored_page
  if Volt.client?
    if `sessionStorage`
      page_obj_str = nil

      `page_obj_str = sessionStorage.getItem('___page');`
      `if (page_obj_str) {`
        `sessionStorage.removeItem('___page');`

        JSON.parse(page_obj_str).each_pair do |key, value|
          self.page.send(:"#{key}=", value)
        end
      `}`
    end
  end
rescue => e
  puts "Unable to restore: #{e.inspect}"
end

#local_storeObject



84
85
86
# File 'lib/volt/page/page.rb', line 84

def local_store
  @local_store ||= ReactiveValue.new(Model.new({}, persistor: Persistors::LocalStore))
end

#startObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/volt/page/page.rb', line 157

def start
  # Setup to render template
  Element.find('body').html = "<!-- $CONTENT --><!-- $/CONTENT -->"

  load_stored_page

  # Do the initial url params parse
  @url_tracker.url_updated(true)

  main_controller = IndexController.new

  # Setup main page template
  TemplateRenderer.new(self, DomTarget.new, main_controller, 'CONTENT', 'home/index/index/body')

  # Setup title listener template
  title_target = AttributeTarget.new
  title_target.on('changed') do
    title = title_target.to_html
    # puts "SET TITLE: #{title.inspect}: #{title_target.inspect}"
    `document.title = title;`
  end
  TemplateRenderer.new(self, title_target, main_controller, "main", "home/index/index/title")

  # TODO: this dom ready should really happen in the template renderer
  main_controller.dom_ready if main_controller.respond_to?(:dom_ready)
end

#storeObject



80
81
82
# File 'lib/volt/page/page.rb', line 80

def store
  @store ||= ReactiveValue.new(Model.new({}, persistor: Persistors::StoreFactory.new(tasks)))
end

#tasksObject



88
89
90
# File 'lib/volt/page/page.rb', line 88

def tasks
  @tasks ||= Tasks.new(self)
end