Class: Browser::Window

Inherits:
Object show all
Includes:
Event::Target, Native
Defined in:
opal/browser/window.rb,
opal/browser/dom.rb,
opal/browser/delay.rb,
opal/browser/screen.rb,
opal/browser/console.rb,
opal/browser/history.rb,
opal/browser/storage.rb,
opal/browser/interval.rb,
opal/browser/location.rb,
opal/browser/navigator.rb,
opal/browser/window/size.rb,
opal/browser/window/view.rb,
opal/browser/window/scroll.rb

Overview

Wrapper class for the window object, an instance of it gets set to $window.

Defined Under Namespace

Classes: Scroll, Size, View

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Event::Target

#off, #on, #on!, #trigger, #trigger!

Instance Attribute Details

#historyHistory (readonly)

Returns the history for this window.

Returns:

  • (History)

    the history for this window



85
86
87
# File 'opal/browser/history.rb', line 85

def history
  History.new(`#@native.history`) if `#@native.history`
end

#locationLocation (readonly)

Returns the location for the window.

Returns:

  • (Location)

    the location for the window



74
75
76
# File 'opal/browser/location.rb', line 74

def location
  Location.new(`#@native.location`) if `#@native.location`
end

Returns the navigator.

Returns:



168
169
170
# File 'opal/browser/navigator.rb', line 168

def navigator
  Navigator.new(`#@native.navigator`) if `#@native.navigator`
end

#screenScreen (readonly)

Returns the screen for the window.

Returns:

  • (Screen)

    the screen for the window



61
62
63
# File 'opal/browser/screen.rb', line 61

def screen
  Screen.new(`#@native.screen`)
end

Class Method Details

.open(url, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'opal/browser/window.rb', line 10

def self.open(url, options)
  name     = options.delete(:name)
  features = options.map {|key, value|
    value = case value
            when true  then :yes
            when false then :no
            else            value
            end

    "#{key}=#{value}"
  }.join(?,)

  %x{
    var win = window.open(#{url}, #{name}, #{features});

    if (win == null) {
      return nil;
    }

    return #{new(`win`)};
  }
end

Instance Method Details

#after(time, &block) ⇒ Delay

Execute a block after the given seconds.

Parameters:

  • time (Float)

    the seconds after it gets called

Returns:

  • (Delay)

    the object representing the timeout



39
40
41
# File 'opal/browser/delay.rb', line 39

def after(time, &block)
  Delay.new(@native, time, &block).tap(&:start)
end

#after!(time, &block) ⇒ Delay

Execute a block after the given seconds, you have to call [#start] on it yourself.

Parameters:

  • time (Float)

    the seconds after it gets called

Returns:

  • (Delay)

    the object representing the timeout



49
50
51
# File 'opal/browser/delay.rb', line 49

def after!(time, &block)
  Delay.new(@native, time, &block)
end

#alert(value) ⇒ Object

Alert the passed string.



41
42
43
44
45
# File 'opal/browser/window.rb', line 41

def alert(value)
  `#@native.alert(value)`

  value
end

#closeObject



92
93
94
95
96
97
98
# File 'opal/browser/window.rb', line 92

def close
  %x{
    return (window.open('', '_self', '') && window.close()) ||
           (window.opener = null && window.close()) ||
           (window.opener = '' && window.close());
  }
end

#confirm(value) ⇒ Object

Display a confirmation dialog with the passed string as text.



53
54
55
# File 'opal/browser/window.rb', line 53

def confirm(value)
  `#@native.confirm(value) || false`
end

#consoleConsole

Get the Console for this window.

Returns:



96
97
98
# File 'opal/browser/console.rb', line 96

def console
  Console.new(`#@native.console`)
end

#documentDOM::Document

Get the DOM::Document for this window.

Returns:



77
78
79
# File 'opal/browser/dom.rb', line 77

def document
  DOM(`#@native.document`)
end

#every(time, &block) ⇒ Interval

Execute the block every given seconds.

Parameters:

  • time (Float)

    the seconds between every call

Returns:

  • (Interval)

    the object representing the interval



72
73
74
# File 'opal/browser/interval.rb', line 72

def every(time, &block)
  Interval.new(@native, time, &block).tap(&:start)
end

#every!(time, &block) ⇒ Interval

Execute the block every given seconds, you have to call [#start] on it yourself.

Parameters:

  • time (Float)

    the seconds between every call

Returns:

  • (Interval)

    the object representing the interval



82
83
84
# File 'opal/browser/interval.rb', line 82

def every!(time, &block)
  Interval.new(@native, time, &block)
end

#prompt(value) ⇒ Object

Display a prompt dialog with the passed string as text.



48
49
50
# File 'opal/browser/window.rb', line 48

def prompt(value)
  `#@native.prompt(value) || nil`
end

#scrollScroll

Get the Scroll for this window.

Returns:



74
75
76
# File 'opal/browser/window.rb', line 74

def scroll
  Scroll.new(self)
end

#send(message, options = {}) ⇒ Object

Send a message to the window.

Parameters:

  • message (String)

    the message

  • options (Hash) (defaults to: {})

    optional to: target

Raises:

  • (NotImplementedError)


87
88
89
# File 'opal/browser/window.rb', line 87

def send(message, options = {})
  `#@native.postMessage(#{message}, #{options[:to] || '*'})`
end

#session_storage(name = :default) ⇒ SessionStorage

Get a session storage with the given name.

Parameters:

  • name (Symbol) (defaults to: :default)

    the name of the storage

Returns:



247
248
249
# File 'opal/browser/storage.rb', line 247

def session_storage(name = :default)
  SessionStorage.new(to_n, name)
end

#sizeSize

Get the Size for this window.

Returns:



67
68
69
# File 'opal/browser/window.rb', line 67

def size
  Size.new(self)
end

#storage(name = :default) ⇒ Storage

Get a storage with the given name.

Parameters:

  • name (Symbol) (defaults to: :default)

    the name of the storage

Returns:



238
239
240
# File 'opal/browser/storage.rb', line 238

def storage(name = :default)
  Storage.new(to_n, name)
end

#viewView

Get the View for the window.

Returns:



60
61
62
# File 'opal/browser/window.rb', line 60

def view
  View.new(self)
end