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


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

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

#historyHistory (readonly)

Returns the history for this window.

Returns:

  • (History)

    the history for this window


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

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


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

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

Returns the navigator.

Returns:


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

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


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

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


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

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


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

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

Class Method Details

.open(url, options) ⇒ Object


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

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.


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

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

  value
end

#closeObject


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

def close
  `#{@native}.close()`
end

#confirm(value) ⇒ Object

Display a confirmation dialog with the passed string as text.


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

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

#consoleConsole

Get the Console for this window.

Returns:


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

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

#documentDOM::Document

Get the DOM::Document for this window.

Returns:


102
103
104
# File 'opal/browser/dom.rb', line 102

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, default = nil) ⇒ Object

Display a prompt dialog with the passed string as text.


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

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


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

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.


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

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)

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

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:


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

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:


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:


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

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

#visual_viewportObject


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

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

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

  @visual_viewport
end