diamonds 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +36 -0
  3. data/lib/diamonds.rb +74 -0
  4. data/lib/diamonds/Gemfile +28 -0
  5. data/lib/diamonds/README.md +254 -0
  6. data/lib/diamonds/Rakefile +5 -0
  7. data/lib/diamonds/apps/application.rb +15 -0
  8. data/lib/diamonds/apps/library.rb +321 -0
  9. data/lib/diamonds/apps/user_app.rb +5 -0
  10. data/lib/diamonds/config.ru +98 -0
  11. data/lib/diamonds/db/mysiteDB Back.sqlite3 +0 -0
  12. data/lib/diamonds/db/mysiteDB.sqlite3 +0 -0
  13. data/lib/diamonds/gems/auto_launch_browser.rb +22 -0
  14. data/lib/diamonds/gems/io.rb +146 -0
  15. data/lib/diamonds/gems/ruby_to_js.rb +173 -0
  16. data/lib/diamonds/gems/server_utils.rb +96 -0
  17. data/lib/diamonds/lib/opal-browser.rb +1 -0
  18. data/lib/diamonds/lib/opal/browser.rb +4 -0
  19. data/lib/diamonds/opal-browser.gemspec +21 -0
  20. data/lib/diamonds/opal/browser.rb +11 -0
  21. data/lib/diamonds/opal/browser/animation_frame.rb +111 -0
  22. data/lib/diamonds/opal/browser/canvas.rb +321 -0
  23. data/lib/diamonds/opal/browser/canvas/data.rb +63 -0
  24. data/lib/diamonds/opal/browser/canvas/gradient.rb +27 -0
  25. data/lib/diamonds/opal/browser/canvas/style.rb +113 -0
  26. data/lib/diamonds/opal/browser/canvas/text.rb +45 -0
  27. data/lib/diamonds/opal/browser/console.rb +103 -0
  28. data/lib/diamonds/opal/browser/cookies.rb +133 -0
  29. data/lib/diamonds/opal/browser/css.rb +24 -0
  30. data/lib/diamonds/opal/browser/css/declaration.rb +83 -0
  31. data/lib/diamonds/opal/browser/css/rule.rb +48 -0
  32. data/lib/diamonds/opal/browser/css/rule/style.rb +16 -0
  33. data/lib/diamonds/opal/browser/css/style_sheet.rb +83 -0
  34. data/lib/diamonds/opal/browser/css/unit.rb +188 -0
  35. data/lib/diamonds/opal/browser/database/sql.rb +194 -0
  36. data/lib/diamonds/opal/browser/delay.rb +78 -0
  37. data/lib/diamonds/opal/browser/dom.rb +84 -0
  38. data/lib/diamonds/opal/browser/dom/attribute.rb +26 -0
  39. data/lib/diamonds/opal/browser/dom/builder.rb +88 -0
  40. data/lib/diamonds/opal/browser/dom/cdata.rb +9 -0
  41. data/lib/diamonds/opal/browser/dom/character_data.rb +73 -0
  42. data/lib/diamonds/opal/browser/dom/comment.rb +9 -0
  43. data/lib/diamonds/opal/browser/dom/document.rb +149 -0
  44. data/lib/diamonds/opal/browser/dom/document_fragment.rb +7 -0
  45. data/lib/diamonds/opal/browser/dom/element.rb +474 -0
  46. data/lib/diamonds/opal/browser/dom/element/attributes.rb +87 -0
  47. data/lib/diamonds/opal/browser/dom/element/data.rb +67 -0
  48. data/lib/diamonds/opal/browser/dom/element/image.rb +23 -0
  49. data/lib/diamonds/opal/browser/dom/element/input.rb +28 -0
  50. data/lib/diamonds/opal/browser/dom/element/offset.rb +89 -0
  51. data/lib/diamonds/opal/browser/dom/element/position.rb +46 -0
  52. data/lib/diamonds/opal/browser/dom/element/scroll.rb +136 -0
  53. data/lib/diamonds/opal/browser/dom/element/select.rb +36 -0
  54. data/lib/diamonds/opal/browser/dom/element/size.rb +34 -0
  55. data/lib/diamonds/opal/browser/dom/element/template.rb +9 -0
  56. data/lib/diamonds/opal/browser/dom/element/textarea.rb +24 -0
  57. data/lib/diamonds/opal/browser/dom/mutation_observer.rb +178 -0
  58. data/lib/diamonds/opal/browser/dom/node.rb +464 -0
  59. data/lib/diamonds/opal/browser/dom/node_set.rb +110 -0
  60. data/lib/diamonds/opal/browser/dom/text.rb +36 -0
  61. data/lib/diamonds/opal/browser/effects.rb +50 -0
  62. data/lib/diamonds/opal/browser/event.rb +253 -0
  63. data/lib/diamonds/opal/browser/event/animation.rb +38 -0
  64. data/lib/diamonds/opal/browser/event/audio_processing.rb +33 -0
  65. data/lib/diamonds/opal/browser/event/base.rb +430 -0
  66. data/lib/diamonds/opal/browser/event/before_unload.rb +15 -0
  67. data/lib/diamonds/opal/browser/event/clipboard.rb +28 -0
  68. data/lib/diamonds/opal/browser/event/close.rb +47 -0
  69. data/lib/diamonds/opal/browser/event/composition.rb +50 -0
  70. data/lib/diamonds/opal/browser/event/custom.rb +65 -0
  71. data/lib/diamonds/opal/browser/event/device_light.rb +23 -0
  72. data/lib/diamonds/opal/browser/event/device_motion.rb +51 -0
  73. data/lib/diamonds/opal/browser/event/device_orientation.rb +48 -0
  74. data/lib/diamonds/opal/browser/event/device_proximity.rb +33 -0
  75. data/lib/diamonds/opal/browser/event/drag.rb +119 -0
  76. data/lib/diamonds/opal/browser/event/focus.rb +39 -0
  77. data/lib/diamonds/opal/browser/event/gamepad.rb +60 -0
  78. data/lib/diamonds/opal/browser/event/hash_change.rb +28 -0
  79. data/lib/diamonds/opal/browser/event/keyboard.rb +115 -0
  80. data/lib/diamonds/opal/browser/event/message.rb +70 -0
  81. data/lib/diamonds/opal/browser/event/mouse.rb +254 -0
  82. data/lib/diamonds/opal/browser/event/page_transition.rb +23 -0
  83. data/lib/diamonds/opal/browser/event/pop_state.rb +33 -0
  84. data/lib/diamonds/opal/browser/event/progress.rb +43 -0
  85. data/lib/diamonds/opal/browser/event/sensor.rb +15 -0
  86. data/lib/diamonds/opal/browser/event/storage.rb +43 -0
  87. data/lib/diamonds/opal/browser/event/touch.rb +60 -0
  88. data/lib/diamonds/opal/browser/event/ui.rb +38 -0
  89. data/lib/diamonds/opal/browser/event/wheel.rb +49 -0
  90. data/lib/diamonds/opal/browser/event_source.rb +70 -0
  91. data/lib/diamonds/opal/browser/history.rb +90 -0
  92. data/lib/diamonds/opal/browser/http.rb +169 -0
  93. data/lib/diamonds/opal/browser/http/binary.rb +58 -0
  94. data/lib/diamonds/opal/browser/http/headers.rb +109 -0
  95. data/lib/diamonds/opal/browser/http/request.rb +344 -0
  96. data/lib/diamonds/opal/browser/http/response.rb +115 -0
  97. data/lib/diamonds/opal/browser/immediate.rb +163 -0
  98. data/lib/diamonds/opal/browser/interval.rb +111 -0
  99. data/lib/diamonds/opal/browser/location.rb +87 -0
  100. data/lib/diamonds/opal/browser/navigator.rb +173 -0
  101. data/lib/diamonds/opal/browser/screen.rb +66 -0
  102. data/lib/diamonds/opal/browser/socket.rb +123 -0
  103. data/lib/diamonds/opal/browser/storage.rb +252 -0
  104. data/lib/diamonds/opal/browser/support.rb +287 -0
  105. data/lib/diamonds/opal/browser/utils.rb +74 -0
  106. data/lib/diamonds/opal/browser/version.rb +3 -0
  107. data/lib/diamonds/opal/browser/window.rb +120 -0
  108. data/lib/diamonds/opal/browser/window/scroll.rb +59 -0
  109. data/lib/diamonds/opal/browser/window/size.rb +59 -0
  110. data/lib/diamonds/opal/browser/window/view.rb +36 -0
  111. data/lib/diamonds/opal/opal-browser.rb +1 -0
  112. data/lib/diamonds/public/__OPAL_SOURCE_MAPS__/application.map +0 -0
  113. data/lib/diamonds/public/javascripts/font-awesome/HELP-US-OUT.txt +7 -0
  114. data/lib/diamonds/public/javascripts/font-awesome/css/font-awesome.css +2026 -0
  115. data/lib/diamonds/public/javascripts/font-awesome/css/font-awesome.min.css +1 -0
  116. data/lib/diamonds/public/javascripts/font-awesome/fonts/FontAwesome.otf +0 -0
  117. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.eot +0 -0
  118. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.svg +640 -0
  119. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.ttf +0 -0
  120. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.woff +0 -0
  121. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.woff2 +0 -0
  122. data/lib/diamonds/public/javascripts/font-awesome/less/animated.less +34 -0
  123. data/lib/diamonds/public/javascripts/font-awesome/less/bordered-pulled.less +25 -0
  124. data/lib/diamonds/public/javascripts/font-awesome/less/core.less +12 -0
  125. data/lib/diamonds/public/javascripts/font-awesome/less/fixed-width.less +6 -0
  126. data/lib/diamonds/public/javascripts/font-awesome/less/font-awesome.less +17 -0
  127. data/lib/diamonds/public/javascripts/font-awesome/less/icons.less +677 -0
  128. data/lib/diamonds/public/javascripts/font-awesome/less/larger.less +13 -0
  129. data/lib/diamonds/public/javascripts/font-awesome/less/list.less +19 -0
  130. data/lib/diamonds/public/javascripts/font-awesome/less/mixins.less +26 -0
  131. data/lib/diamonds/public/javascripts/font-awesome/less/path.less +15 -0
  132. data/lib/diamonds/public/javascripts/font-awesome/less/rotated-flipped.less +20 -0
  133. data/lib/diamonds/public/javascripts/font-awesome/less/stacked.less +20 -0
  134. data/lib/diamonds/public/javascripts/font-awesome/less/variables.less +688 -0
  135. data/lib/diamonds/public/javascripts/font-awesome/scss/_animated.scss +34 -0
  136. data/lib/diamonds/public/javascripts/font-awesome/scss/_bordered-pulled.scss +25 -0
  137. data/lib/diamonds/public/javascripts/font-awesome/scss/_core.scss +12 -0
  138. data/lib/diamonds/public/javascripts/font-awesome/scss/_fixed-width.scss +6 -0
  139. data/lib/diamonds/public/javascripts/font-awesome/scss/_icons.scss +677 -0
  140. data/lib/diamonds/public/javascripts/font-awesome/scss/_larger.scss +13 -0
  141. data/lib/diamonds/public/javascripts/font-awesome/scss/_list.scss +19 -0
  142. data/lib/diamonds/public/javascripts/font-awesome/scss/_mixins.scss +26 -0
  143. data/lib/diamonds/public/javascripts/font-awesome/scss/_path.scss +15 -0
  144. data/lib/diamonds/public/javascripts/font-awesome/scss/_rotated-flipped.scss +20 -0
  145. data/lib/diamonds/public/javascripts/font-awesome/scss/_stacked.scss +20 -0
  146. data/lib/diamonds/public/javascripts/font-awesome/scss/_variables.scss +688 -0
  147. data/lib/diamonds/public/javascripts/font-awesome/scss/font-awesome.scss +17 -0
  148. data/lib/diamonds/public/javascripts/jquery-2.1.4.min.js +4 -0
  149. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  150. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  151. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_flat_10_000000_40x100.png +0 -0
  152. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  153. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  154. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  155. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  156. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  157. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  158. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_222222_256x240.png +0 -0
  159. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_228ef1_256x240.png +0 -0
  160. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_ef8c08_256x240.png +0 -0
  161. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_ffd27a_256x240.png +0 -0
  162. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_ffffff_256x240.png +0 -0
  163. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.min.css +7 -0
  164. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.min.js +13 -0
  165. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.structure.min.css +5 -0
  166. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.theme.min.css +5 -0
  167. data/lib/diamonds/public/javascripts/jquery.js +4 -0
  168. data/lib/diamonds/public/javascripts/jquery.ui.touch-punch.min.js +11 -0
  169. data/lib/diamonds/public/javascripts/ne pas utiliser version en retard jeezs_editor.js b/data/lib/diamonds/public/javascripts/ne pas utiliser version en retard → jeezs_editor.js +0 -0
  170. data/lib/diamonds/public/javascripts/standalone.js +2316 -0
  171. data/lib/diamonds/public/javascripts/standalone.min.js +1 -0
  172. data/lib/diamonds/public/javascripts/wysiwyg-editor.min.css +1 -0
  173. data/lib/diamonds/public/javascripts/wysiwyg-editor.min.js +1 -0
  174. data/lib/diamonds/public/javascripts/wysiwyg.min.js +1 -0
  175. data/lib/diamonds/user_app/main.rb +45 -0
  176. data/lib/diamonds/version.rb +3 -0
  177. data/lib/diamonds/views/index.erb +35 -0
  178. data/lib/diamonds/views/javascripts/jeezs_editor.js.erb +365 -0
  179. data/lib/diamonds/views/stylesheets/main.css.erb +244 -0
  180. metadata +490 -0
@@ -0,0 +1,173 @@
1
+ module Browser
2
+
3
+ # Representation of the navigator application.
4
+ #
5
+ # @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator
6
+ class Navigator
7
+ include Native
8
+
9
+ Version = Struct.new(:major, :minor, :build)
10
+ Product = Struct.new(:name, :version)
11
+ Vendor = Struct.new(:name, :version)
12
+
13
+ # Representation of a MIME type.
14
+ class MimeType
15
+ include Native
16
+
17
+ # @!attribute [r] plugin
18
+ # @return [Plugin] the plugin for the MIME type
19
+ def plugin
20
+ Plugin.new(`#@native.enabledPlugin`)
21
+ end
22
+
23
+ # @!attribute [r] description
24
+ # @return [String] the description for the MIME type
25
+ alias_native :description
26
+
27
+ # @!attribute [r] extensions
28
+ # @return [Array<String>] the extensions for this MIME type
29
+ def extensions
30
+ `#@native.suffixes`.split(/\s*/)
31
+ end
32
+
33
+ # @!attribute [r] type
34
+ # @return [String] the MIME type
35
+ alias_native :type
36
+ end
37
+
38
+ # Representation of a navigator plugin.
39
+ #
40
+ # @see https://developer.mozilla.org/en-US/docs/Web/API/Plugin
41
+ class Plugin < Native::Array
42
+ def initialize(plugin)
43
+ super plugin do |m|
44
+ MimeType.new(m)
45
+ end
46
+ end
47
+
48
+ # @!attribute [r] description
49
+ # @return [String] the plugin description
50
+ alias_native :description
51
+
52
+ # @!attribute [r] file
53
+ # @return [String] the file associated with the plugin
54
+ alias_native :file, :filename
55
+
56
+ # @!attribute [r] name
57
+ # @return [String] the plugin name
58
+ alias_native :name
59
+
60
+ # @!attribute [r] version
61
+ # @return [String] the plugin version
62
+ alias_native :version
63
+ end
64
+
65
+ # Representation for the arary of plugins.
66
+ #
67
+ # @see https://developer.mozilla.org/en-US/docs/Web/API/NavigatorPlugins
68
+ class Plugins < Native::Array
69
+ def initialize(plugins)
70
+ super plugins do |p|
71
+ Plugin.new(p)
72
+ end
73
+ end
74
+
75
+ # Reload all browser plugins.
76
+ def refresh
77
+ `#@native.refresh(false)`
78
+ end
79
+
80
+ # Reload all browser plugins reloading pages that contain `<embed>`s.
81
+ def refresh!
82
+ `#@native.refresh(true)`
83
+ end
84
+ end
85
+
86
+ # @!attribute [r] code
87
+ # @return [String] the browser code name
88
+ alias_native :code, :appCodeName
89
+
90
+ # @!attribute [r] name
91
+ # @return [String] the browser name
92
+ alias_native :name, :appName
93
+
94
+ # @!attribute [r] version
95
+ # @return [Version] the browser version
96
+ def version
97
+ Version.new(`#@native.appVersion`, `#@native.appMinorVersion`, `#@native.buildID`)
98
+ end
99
+
100
+ # Check if cookies are enabled.
101
+ alias_native :cookies?, :cookieEnabled
102
+
103
+ # Check if DNT is disabled.
104
+ def track?
105
+ `!#@native.doNotTrack`
106
+ end
107
+
108
+ # @!attribute [r] language
109
+ # @return [String] the browser language
110
+ alias_native :language
111
+
112
+ # @!attribute [r] mime_types
113
+ # @return [Native::Array<MimeType>] the supported MIME types
114
+ def mime_types
115
+ Native::Array.new `#@native.mimeTypes`, get: :item, named: :namedItem do |m|
116
+ MimeType.new(m)
117
+ end
118
+ end
119
+
120
+ # Check if the browser is in offline mode.
121
+ def offline?
122
+ `!#@native.onLine`
123
+ end
124
+
125
+ # @!attribute [r] operating_system
126
+ # @return [String] the operating system the browser is running on
127
+ alias_native :operating_system, :oscpu
128
+
129
+ alias os operating_system
130
+
131
+ # @!attribute [r] platform
132
+ # @return [String] the platform the browser is running on
133
+ alias_native :platform
134
+
135
+ # @!attribute [r] plugins
136
+ # @return [Plugins] the enabled plugins
137
+ def plugins
138
+ Plugins.new(`#@native.plugins`)
139
+ end
140
+
141
+ # @!attribute [r] product
142
+ # @return [Product] the product name and version
143
+ def product
144
+ Product.new(`#@native.product`, `#@native.productSub`)
145
+ end
146
+
147
+ # @!attribute [r] user_agent
148
+ # @return [String] the browser's user agent
149
+ alias_native :user_agent, :userAgent
150
+
151
+ # @!attribute [r] vendor
152
+ # @return [Vendor] the vendor name and version
153
+ def vendor
154
+ Vendor.new(`#@native.vendor`, `#@native.vendorSub`)
155
+ end
156
+
157
+ # Check if Java is enabled.
158
+ def java?
159
+ `#@native.javaEnabled()`
160
+ rescue
161
+ false
162
+ end
163
+ end
164
+
165
+ class Window
166
+ # @!attribute [r] navigator
167
+ # @return [Navigator] the navigator
168
+ def navigator
169
+ Navigator.new(`#@native.navigator`) if `#@native.navigator`
170
+ end
171
+ end
172
+
173
+ end
@@ -0,0 +1,66 @@
1
+ module Browser
2
+
3
+ # Representation of the screen the window is being rendered on.
4
+ #
5
+ # @see https://developer.mozilla.org/en-US/docs/Web/API/Window.screen
6
+ class Screen
7
+ include Native
8
+ include Event::Target
9
+
10
+ target {|value|
11
+ Screen.new(value) if Native.is_a?(value, `window.Screen`)
12
+ }
13
+
14
+ Depth = Struct.new(:color, :pixel)
15
+
16
+ # @!attribute [r] width
17
+ # @return [Integer] the width of the screen in pixels
18
+ alias_native :width
19
+
20
+ # @!attribute [r] height
21
+ # @return [Integer] the height of the screen in pixels
22
+ alias_native :height
23
+
24
+ # @!attribute [r] size
25
+ # @return [Size] the size in pixels
26
+ def size
27
+ Size.new(width, height)
28
+ end
29
+
30
+ # @!attribute [r] x
31
+ # @return [Integer] the offset from the top left corner of the screen in
32
+ # pixels
33
+ alias_native :x, :top
34
+
35
+ # @!attribute [r] y
36
+ # @return [Integer] the offset from the top left corner of the screen in
37
+ # pixels
38
+ alias_native :y, :left
39
+
40
+ # @!attribute [r] position
41
+ # @return [Position] the offset from the top left corner of the screen in
42
+ # pixels
43
+ def position
44
+ Position.new(x, y)
45
+ end
46
+
47
+ # @!attribute [r] depth
48
+ # @return [Depth] the screen depth
49
+ def depth
50
+ Depth.new(`#@native.colorDepth`, `#@native.pixelDepth`)
51
+ end
52
+
53
+ # @!attribute [r] orientation
54
+ # @return [String] the orientation of the screen
55
+ alias_native :orientation
56
+ end
57
+
58
+ class Window
59
+ # @!attribute [r] screen
60
+ # @return [Screen] the screen for the window
61
+ def screen
62
+ Screen.new(`#@native.screen`)
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,123 @@
1
+ module Browser
2
+
3
+ # A {Socket} allows the browser and a server to have a bidirectional data
4
+ # connection.
5
+ #
6
+ # @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
7
+ class Socket
8
+ def self.supported?
9
+ Browser.supports? :WebSocket
10
+ end
11
+
12
+ include Native
13
+ include IO::Writable
14
+ include Event::Target
15
+
16
+ target {|value|
17
+ Socket.new(value) if Native.is_a?(value, `window.WebSocket`)
18
+ }
19
+
20
+ # Create a connection to the given URL, optionally using the given protocol.
21
+ #
22
+ # @param url [String] the URL to connect to
23
+ # @param protocol [String] the protocol to use
24
+ #
25
+ # @yield if the block has no parameters it's `instance_exec`d, otherwise it's
26
+ # called with `self`
27
+ def initialize(url, protocol = nil, &block)
28
+ if native?(url)
29
+ super(url)
30
+ elsif protocol
31
+ super(`new window.WebSocket(#{url.to_s}, #{protocol.to_n})`)
32
+ else
33
+ super(`new window.WebSocket(#{url.to_s})`)
34
+ end
35
+
36
+ if block.arity == 0
37
+ instance_exec(&block)
38
+ else
39
+ block.call(self)
40
+ end if block
41
+ end
42
+
43
+ # @!attribute [r] protocol
44
+ # @return [String] the protocol of the socket
45
+ alias_native :protocol
46
+
47
+ # @!attribute [r] url
48
+ # @return [String] the URL the socket is connected to
49
+ alias_native :url
50
+
51
+ # @!attribute [r] buffered
52
+ # @return [Integer] the amount of buffered data.
53
+ alias_native :buffered, :bufferedAmount
54
+
55
+ # @!attribute [r] type
56
+ # @return [:blob, :buffer, :string] the type of the socket
57
+ def type
58
+ %x{
59
+ switch (#@native.binaryType) {
60
+ case "blob":
61
+ return "blob";
62
+
63
+ case "arraybuffer":
64
+ return "buffer";
65
+
66
+ default:
67
+ return "string";
68
+ }
69
+ }
70
+ end
71
+
72
+ # @!attribute [r] state
73
+ # @return [:connecting, :open, :closing, :closed] the state of the socket
74
+ def state
75
+ %x{
76
+ switch (#@native.readyState) {
77
+ case window.WebSocket.CONNECTING:
78
+ return "connecting";
79
+
80
+ case window.WebSocket.OPEN:
81
+ return "open";
82
+
83
+ case window.WebSocket.CLOSING:
84
+ return "closing";
85
+
86
+ case window.WebSocket.CLOSED:
87
+ return "closed";
88
+ }
89
+ }
90
+ end
91
+
92
+ # @!attribute [r] extensions
93
+ # @return [Array<String>] the extensions used by the socket
94
+ def extensions
95
+ `#@native.extensions`.split(/\s*,\s*/)
96
+ end
97
+
98
+ # Check if the socket is alive.
99
+ def alive?
100
+ state == :open
101
+ end
102
+
103
+ # Send data to the socket.
104
+ #
105
+ # @param data [#to_n] the data to send
106
+ def write(data)
107
+ `#@native.send(#{data.to_n})`
108
+ end
109
+
110
+ alias << write
111
+
112
+ alias send write
113
+
114
+ # Close the socket.
115
+ #
116
+ # @param code [Integer, nil] the error code
117
+ # @param reason [String, nil] the reason for closing
118
+ def close(code = nil, reason = nil)
119
+ `#@native.close(#{code.to_n}, #{reason.to_n})`
120
+ end
121
+ end
122
+
123
+ end
@@ -0,0 +1,252 @@
1
+ require 'json'
2
+ require 'stringio'
3
+
4
+ module Browser
5
+
6
+ # A {Storage} allows you to store data across page loads and browser
7
+ # restarts.
8
+ #
9
+ # Compatibility
10
+ # -------------
11
+ # The compatibility layer will try various implementations in the following
12
+ # order.
13
+ #
14
+ # + [window.localStorage](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#localStorage)
15
+ # + [window.globalStorage](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#globalStorage)
16
+ # + [document.body.addBehavior](http://msdn.microsoft.com/en-us/library/ms531424(VS.85).aspx)
17
+ # + [document.cookie](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie)
18
+ #
19
+ # @see https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
20
+ # @todo remove method_defined? checks when require order is fixed
21
+ class Storage
22
+ def self.json_create(data)
23
+ data.delete(JSON.create_id)
24
+
25
+ Hash[data.map {|key, value|
26
+ [JSON.parse(key), value]
27
+ }]
28
+ end
29
+
30
+ # @!attribute [r] name
31
+ # @return [String] the name of the storage
32
+ attr_reader :name
33
+
34
+ # Create a new storage on the given window with the given name.
35
+ #
36
+ # @param window [native] the window to save the storage to
37
+ # @param name [String] the name to use to discern different storages
38
+ def initialize(window, name)
39
+ super()
40
+
41
+ @window = window
42
+ @name = name
43
+ @data = {}
44
+
45
+ autosave!
46
+ reload
47
+ end
48
+
49
+ # Check if autosaving is enabled.
50
+ #
51
+ # When autosaving is enabled the {Storage} is saved every time a change is
52
+ # made, otherwise you'll have to save it manually yourself.
53
+ def autosave?
54
+ @autosave
55
+ end
56
+
57
+ # Enable autosaving.
58
+ def autosave!
59
+ @autosave = true
60
+ end
61
+
62
+ # Disable autosaving.
63
+ def no_autosave!
64
+ @autosave = false
65
+ end
66
+
67
+ include Enumerable
68
+
69
+ # Iterate over the (key, value) pairs in the storage.
70
+ #
71
+ # @yield [key, value]
72
+ def each(&block)
73
+ return enum_for :each unless block
74
+
75
+ @data.each(&block)
76
+
77
+ self
78
+ end
79
+
80
+ def method_missing(*args, &block)
81
+ @data.__send__(*args, &block)
82
+ end
83
+
84
+ # Set a value in the storage.
85
+ def []=(key, value)
86
+ @data[key] = value
87
+
88
+ save if autosave?
89
+ end
90
+
91
+ # Delete a value from the storage.
92
+ def delete(key)
93
+ @data.delete(key).tap {
94
+ save if autosave?
95
+ }
96
+ end
97
+
98
+ # Clear the storage.
99
+ def clear
100
+ @data.clear.tap {
101
+ save if autosave?
102
+ }
103
+ end
104
+
105
+ # Replace the current storage with the given one.
106
+ #
107
+ # @param new [Hash, String] if new is a {String} it will be parsed as JSON
108
+ def replace(new)
109
+ if String === new
110
+ @data.replace(JSON.parse(new))
111
+ else
112
+ @data.replace(new)
113
+ end
114
+ end
115
+
116
+ # Call the block between a [#reload] and [#save].
117
+ def commit(&block)
118
+ autosave = @autosave
119
+ @autosave = false
120
+ result = nil
121
+
122
+ reload
123
+
124
+ begin
125
+ result = block.call
126
+ save
127
+ rescue
128
+ reload
129
+ raise
130
+ ensure
131
+ @autosave = autosave
132
+ end
133
+
134
+ result
135
+ end
136
+
137
+ def to_h
138
+ @data
139
+ end
140
+
141
+ # @!method reload
142
+ # Load the storage.
143
+
144
+ # @!method save
145
+ # Persist the current state to the storage.
146
+
147
+ if Browser.supports? 'Storage.local'
148
+ def reload
149
+ replace `#@window.localStorage[#@name] || '{}'`
150
+ end
151
+
152
+ def save
153
+ `#@window.localStorage[#@name] = #{JSON.dump(self)}`
154
+ end
155
+ elsif Browser.supports? 'Storage.global'
156
+ def reload
157
+ replace `#@window.globalStorage[#@window.location.hostname][#@name] || '{}'`
158
+ end
159
+
160
+ def save
161
+ `#@window.globalStorage[#@window.location.hostname][#@name] = #{JSON.dump(self)}`
162
+ end
163
+ elsif Browser.supports? 'Element.addBehavior'
164
+ def reload
165
+ %x{
166
+ #@element = #@window.document.createElement('link');
167
+ #@element.addBehavior('#default#userData');
168
+
169
+ #@window.document.getElementsByTagName('head')[0].appendChild(#@element);
170
+
171
+ #@element.load(#@name);
172
+ }
173
+
174
+ replace `#@element.getAttribute(#@name) || '{}'`
175
+ end
176
+
177
+ def save
178
+ %x{
179
+ #@element.setAttribute(#@name, #{JSON.dump(self)});
180
+ #@element.save(#@name);
181
+ }
182
+ end
183
+ else
184
+ def reload
185
+ $document.cookies.options expires: 60 * 60 * 24 * 365
186
+
187
+ replace $document.cookies[@name]
188
+ end
189
+
190
+ def save
191
+ $document.cookies[@name] = JSON.dump(self)
192
+ end
193
+ end
194
+
195
+ # Convert the storage to JSON.
196
+ #
197
+ # @return [String] the JSON representation
198
+ def to_json
199
+ io = StringIO.new("{")
200
+
201
+ io << JSON.create_id.to_json << ":" << self.class.name.to_json << ","
202
+
203
+ @data.each {|key, value|
204
+ io << key.to_json.to_json << ":" << value.to_json << ","
205
+ }
206
+
207
+ io.seek(-1, IO::SEEK_CUR)
208
+ io << "}"
209
+
210
+ io.string
211
+ end
212
+ end
213
+
214
+ # A {SessionStorage} allows you to store data across page reloads, as long as the session
215
+ # is active.
216
+ #
217
+ # @see https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#sessionStorage
218
+ class SessionStorage < Storage
219
+ def self.supported?
220
+ Browser.supports? 'Storage.session'
221
+ end
222
+
223
+ def reload
224
+ replace `#@window.sessionStorage[#@name] || '{}'`
225
+ end
226
+
227
+ def save
228
+ `#@window.sessionStorage[#@name] = #{JSON.dump(self)}`
229
+ end
230
+ end
231
+
232
+ class Window
233
+ # Get a storage with the given name.
234
+ #
235
+ # @param name [Symbol] the name of the storage
236
+ #
237
+ # @return [Storage]
238
+ def storage(name = :default)
239
+ Storage.new(to_n, name)
240
+ end
241
+
242
+ # Get a session storage with the given name.
243
+ #
244
+ # @param name [Symbol] the name of the storage
245
+ #
246
+ # @return [SessionStorage]
247
+ def session_storage(name = :default)
248
+ SessionStorage.new(to_n, name)
249
+ end
250
+ end
251
+
252
+ end