Class: Volt::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.



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

def initialize
  # Run the code to setup the page
  @page          = Model.new

  @url         = URL.new
  @params      = @url.params
  @url_tracker = UrlTracker.new(self)
  @templates   = {}

  @events = DocumentEvents.new

  if RUBY_PLATFORM == 'opal'
    # Setup escape binding for console
    `
      $(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

  # Initialize tasks so we can get the reload message
  tasks if Volt.env.development?

  if Volt.client?
    channel.on('reconnected') do
      @page._reconnected = true

      `setTimeout(function() {`
      @page._reconnected = false
      `}, 2000);`
    end
  end
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



33
34
35
# File 'lib/volt/page/page.rb', line 33

def events
  @events
end

#pageObject (readonly)

Returns the value of attribute page.



33
34
35
# File 'lib/volt/page/page.rb', line 33

def page
  @page
end

#paramsObject (readonly)

Returns the value of attribute params.



33
34
35
# File 'lib/volt/page/page.rb', line 33

def params
  @params
end

#routesObject (readonly)

Returns the value of attribute routes.



33
34
35
# File 'lib/volt/page/page.rb', line 33

def routes
  @routes
end

#urlObject (readonly)

Returns the value of attribute url.



33
34
35
# File 'lib/volt/page/page.rb', line 33

def url
  @url
end

Instance Method Details

#add_routes(&block) ⇒ Object



168
169
170
171
172
# File 'lib/volt/page/page.rb', line 168

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

#add_template(name, template, bindings) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/volt/page/page.rb', line 140

def add_template(name, template, bindings)
  # First template gets priority.  The backend will load templates in order so
  # that local templates come in before gems (so they can be overridden).
  #
  # TODO: Currently this means we will send templates to the client that will
  # not get used because they are being overridden.  Need to detect that and
  # not send them.
  unless @templates[name]
    @templates[name] = { 'html' => template, 'bindings' => bindings }
  end
end

#binding_nameObject

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



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

def binding_name
  'page'
end

#channelObject



128
129
130
131
132
133
134
135
136
# File 'lib/volt/page/page.rb', line 128

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

#cookiesObject



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

def cookies
  @cookies ||= Model.new({}, persistor: Persistors::Cookies)
end

#flashObject



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

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

#launch_consoleObject



124
125
126
# File 'lib/volt/page/page.rb', line 124

def launch_console
  puts 'Launch Console'
end


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

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
  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.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/volt/page/page.rb', line 200

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|
        page.send(:"_#{key}=", value)
      end
      `}`
    end
  end
rescue => e
  Volt.logger.error("Unable to restore: #{e.inspect}")
end

#local_storeObject



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

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

#startObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/volt/page/page.rb', line 174

def start
  # Setup to render template
  `$('body').html('<!-- $CONTENT --><!-- $/CONTENT -->');`

  load_stored_page

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

  main_controller = Main::MainController.new

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

  # Setup title reactive template
  @title_template = StringTemplateRenderer.new(self, main_controller, 'main/main/main/title')

  # Watch for changes to the title template
  proc do
    title = @title_template.html.gsub(/\n/, ' ')
    `document.title = title;`
  end.watch!
end

#storeObject



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

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

#tasksObject



91
92
93
# File 'lib/volt/page/page.rb', line 91

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

#template_loader=(callback) ⇒ Object

On the server, we can delay loading the views until they are actually requeted. This sets up an instance variable to call to load.



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

def template_loader=(callback)
  @template_loader = callback
end

#templatesObject



158
159
160
161
162
163
164
165
166
# File 'lib/volt/page/page.rb', line 158

def templates
  if @template_loader
    # Load the templates
    @template_loader.call
    @template_loader = nil
  end

  @templates
end