Class: Browser::Window

Inherits:
Object show all
Includes:
Event::Target, NativeCachedWrapper
Defined in:
opal/browser/window.rb,
opal/browser/dom.rb,
opal/browser/delay.rb,
opal/browser/crypto.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/visual_viewport.rb

Overview

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

Defined Under Namespace

Classes: Size, View

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Event::Target

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

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Instance Attribute Details

#cryptoCrypto (readonly)

Returns the crypto interface of this window.

Returns:

  • (Crypto)

    the crypto interface of this window



75
76
77
# File 'opal/browser/crypto.rb', line 75

def crypto
  @crypto ||= Crypto.new(`#@native.crypto`)
end

#historyHistory (readonly)

Returns the history for this window.

Returns:

  • (History)

    the history for this window



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

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



81
82
83
# File 'opal/browser/location.rb', line 81

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

Returns the navigator.

Returns:



270
271
272
# File 'opal/browser/navigator.rb', line 270

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

#openerWindow (readonly)

Returns reference to the window that opened the window using open.

Returns:

  • (Window)

    reference to the window that opened the window using open



71
72
73
# File 'opal/browser/window.rb', line 71

def opener
  @opener ||= Browser::Window.new(`#@native.opener`)
end

#parentWindow (readonly)

Returns parent of the current window or subframe.

Returns:

  • (Window)

    parent of the current window or subframe



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

def parent
  @parent ||= Browser::Window.new(`#@native.parent`)
end

#screenScreen (readonly)

Returns the screen for the window.

Returns:

  • (Screen)

    the screen for the window



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

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

#topWindow (readonly)

Returns reference to the topmost window in the window hierarchy.

Returns:

  • (Window)

    reference to the topmost window in the window hierarchy



65
66
67
# File 'opal/browser/window.rb', line 65

def top
  @top ||= Browser::Window.new(`#@native.top`)
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



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

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



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

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



110
111
112
# File 'opal/browser/window.rb', line 110

def close
  `#{@native}.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:



99
100
101
# File 'opal/browser/console.rb', line 99

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

#documentDOM::Document

Get the DOM::Document for this window.

Returns:



111
112
113
# File 'opal/browser/dom.rb', line 111

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



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

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



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

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

#prompt(value, default = nil) ⇒ Object

Display a prompt dialog with the passed string as text.



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

def prompt(value, default=nil)
  `#@native.prompt(value, #{default || ""}) || nil`
end

#resolve_after(time) ⇒ Promise

Returns a promise that will resolve after the given seconds.

Parameters:

  • time (Float)

    the seconds after it gets called

Returns:

  • (Promise)

    the promise that will resolve after timeout happens



59
60
61
62
63
# File 'opal/browser/delay.rb', line 59

def resolve_after(time)
  promise = Promise.new
  Delay.new(@native, time) { promise.resolve }.start
  promise
end

#scrollDOM::Element::Scroll

Get the DOM::Element::Scroll for this window.



92
93
94
# File 'opal/browser/window.rb', line 92

def scroll
  @scroll ||= DOM::Element::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)


105
106
107
# File 'opal/browser/window.rb', line 105

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:



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

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

#sizeSize

Get the Size for this window.

Returns:



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

def size
  @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:



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

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

#viewView

Get the View for the window.

Returns:



78
79
80
# File 'opal/browser/window.rb', line 78

def view
  @view ||= View.new(self)
end

#visual_viewportObject



31
32
33
34
35
36
37
38
# File 'opal/browser/visual_viewport.rb', line 31

def visual_viewport
  @visual_viewport ||= VisualViewport.new(`#@native.visualViewport`)

  # Polyfill can take some time to load
  @visual_viewport.native ||= `#@native.visualViewport`

  @visual_viewport
end