Class: Volt::Browser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(volt_app) ⇒ Browser

Returns a new instance of Browser.



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

def initialize(volt_app)
  @volt_app = volt_app
  # Run the code to setup the page

  @url_tracker = UrlTracker.new(@volt_app)

  @events = DocumentEvents.new

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

  if RUBY_PLATFORM == 'opal'
    if Volt.in_browser?
      # Setup click handler for links
      `
        $(document).on('click', 'a', function(event) {
          var browser = #{Volt.current_app.browser};
          return browser.$link_clicked($(this).attr('href'), event);
        });
      `
    end
  end

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

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

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/volt/volt/client_setup/browser.rb', line 5

def events
  @events
end

Instance Method Details

#binding_nameObject

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



64
65
66
# File 'lib/volt/volt/client_setup/browser.rb', line 64

def binding_name
  'page'
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/volt/volt/client_setup/browser.rb', line 41

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

  # Normalize url
  if @volt_app.url.parse(url)
    if event
      # Handled new url
      `event.stopPropagation();`
    end

    # Clear the flash
    @volt_app.flash.clear

    # return false to stop the event propigation
    return false
  end

  # Not stopping, process link normally
  true
end

#load_stored_pageObject

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



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

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

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

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

#startObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/volt/volt/client_setup/browser.rb', line 68

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(@volt_app)

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

  # Setup title reactive template
  @title_template = StringTemplateRenderer.new(@volt_app, 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