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,38 @@
1
+ module Browser; class Event
2
+
3
+ class Animation < Event
4
+ def self.supported?
5
+ Browser.supports? 'Event.Animation'
6
+ end
7
+
8
+ class Definition < Definition
9
+ def animation=(value)
10
+ `#@native.animationName = #{value}`
11
+ end
12
+
13
+ def elapsed=(value)
14
+ `#@native.elapsedTime = #{value}`
15
+ end
16
+ end
17
+
18
+ if Browser.supports? 'Event.constructor'
19
+ def self.construct(name, desc)
20
+ `new AnimationEvent(#{name}, #{desc})`
21
+ end
22
+ elsif Browser.supports? 'Event.create'
23
+ def self.construct(name, desc)
24
+ %x{
25
+ var event = document.createEvent("AnimationEvent");
26
+ event.initAnimationEvent(name, desc.bubbles, desc.cancelable,
27
+ desc.animationName, desc.elapsedTime);
28
+
29
+ return event;
30
+ }
31
+ end
32
+ end if supported?
33
+
34
+ alias_native :name, :animationName
35
+ alias_native :elapsed, :elapsedTime
36
+ end
37
+
38
+ end; end
@@ -0,0 +1,33 @@
1
+ module Browser; class Event
2
+
3
+ class AudioProcessing < Event
4
+ def self.supported?
5
+ Browser.supports? 'Event.AudioProcessing'
6
+ end
7
+
8
+ class Definition < Definition
9
+ def time=(value)
10
+ `#@native.playbackTime = #{value}`
11
+ end
12
+
13
+ def input=(value)
14
+ `#@native.inputBuffer = #{value}`
15
+ end
16
+
17
+ def output=(value)
18
+ `#@native.outputBuffer = #{value}`
19
+ end
20
+ end
21
+
22
+ if Browser.supports? 'Event.constructor'
23
+ def self.construct(name, desc)
24
+ `new AudioProcessingEvent(#{name}, #{desc})`
25
+ end
26
+ end if supported?
27
+
28
+ alias_native :time, :playbackTime
29
+ alias_native :input, :inputBuffer
30
+ alias_native :output, :outputBuffer
31
+ end
32
+
33
+ end; end
@@ -0,0 +1,430 @@
1
+ module Browser
2
+
3
+ class Event
4
+ include Native
5
+
6
+ # @see https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
7
+ class Definition
8
+ include Native
9
+
10
+ # @private
11
+ def self.new(&block)
12
+ data = super(`{ bubbles: true, cancelable: true }`)
13
+ block.call(data) if block
14
+
15
+ data.to_n
16
+ end
17
+
18
+ # Set the event as bubbling.
19
+ def bubbles=(value)
20
+ `#@native.bubbles = #{value}`
21
+ end
22
+
23
+ # Set the event as cancelable.
24
+ def cancelable=(value)
25
+ `#@native.cancelable = #{value}`
26
+ end
27
+ end
28
+
29
+ module Target
30
+ # @private
31
+ def self.converters
32
+ @converters ||= []
33
+ end
34
+
35
+ # @private
36
+ def self.register(&block)
37
+ converters << block
38
+ end
39
+
40
+ # @private
41
+ def self.convert(value)
42
+ return value unless native?(value)
43
+
44
+ converters.each {|block|
45
+ if result = block.call(value)
46
+ return result
47
+ end
48
+ }
49
+
50
+ nil
51
+ end
52
+
53
+ def self.included(klass)
54
+ klass.instance_eval {
55
+ def self.target(&block)
56
+ Event::Target.register(&block)
57
+ end
58
+ }
59
+ end
60
+
61
+ class Callback
62
+ attr_reader :target, :name, :selector
63
+
64
+ # @private
65
+ def initialize(target, name, selector = nil, &block)
66
+ @target = target
67
+ @name = name
68
+ @selector = selector
69
+ @block = block
70
+ end
71
+
72
+ # Call the callback with the given event.
73
+ #
74
+ # @param event [native] the native event object
75
+ def call(event)
76
+ to_proc.call(event)
77
+ end
78
+
79
+ # Get the native function linked to the callback.
80
+ def to_proc
81
+ @proc ||= -> event {
82
+ %x{
83
+ if (!event.currentTarget) {
84
+ event.currentTarget = self.target.native;
85
+ }
86
+ }
87
+
88
+ event = Event.new(event, self)
89
+
90
+ unless event.stopped?
91
+ @block.call(event, *event.arguments)
92
+ end
93
+
94
+ !event.prevented?
95
+ }
96
+ end
97
+
98
+ # @!attribute [r] event
99
+ # @return [Class] the class for the event
100
+ def event
101
+ Event.class_for(@name)
102
+ end
103
+
104
+ # Stop listening for the event linked to the callback.
105
+ def off
106
+ target.off(self)
107
+ end
108
+ end
109
+
110
+ class Delegate
111
+ def initialize(target, name, pair)
112
+ @target = target
113
+ @name = name
114
+ @pair = pair
115
+ end
116
+
117
+ # Stop listening for the event linked to the delegate.
118
+ def off
119
+ delegate = @target.delegated[@name]
120
+ delegate.last.delete(@pair)
121
+
122
+ if delegate.last.empty?
123
+ delegate.first.off
124
+ delegate.delete(@name)
125
+ end
126
+ end
127
+ end
128
+
129
+ Delegates = Struct.new(:callback, :handlers)
130
+
131
+ # @overload on(name, &block)
132
+ #
133
+ # Start listening for an event on the target.
134
+ #
135
+ # @param name [String] the event name
136
+ #
137
+ # @yieldparam event [Event] the event
138
+ #
139
+ # @return [Callback]
140
+ #
141
+ # @overload on(name, selector, &block)
142
+ #
143
+ # Start listening for an event on the target children.
144
+ #
145
+ # @param name [String] the event name
146
+ # @param selector [String] the CSS selector to trigger the event on
147
+ #
148
+ # @yieldparam event [Event] the event
149
+ #
150
+ # @return [Delegate]
151
+ def on(name, selector = nil, &block)
152
+ raise ArgumentError, 'no block has been given' unless block
153
+
154
+ name = Event.name_for(name)
155
+
156
+ if selector
157
+ unless delegate = delegated[name]
158
+ delegate = delegated[name] = Delegates.new
159
+
160
+ if %w[blur focus].include?(name)
161
+ delegate.callback = on! name do |e|
162
+ delegate(delegate, e)
163
+ end
164
+ else
165
+ delegate.callback = on name do |e|
166
+ delegate(delegate, e)
167
+ end
168
+ end
169
+
170
+ pair = [selector, block]
171
+ delegate.handlers = [pair]
172
+
173
+ Delegate.new(self, name, pair)
174
+ else
175
+ pair = [selector, block]
176
+ delegate.handlers << pair
177
+
178
+ Delegate.new(self, name, pair)
179
+ end
180
+ else
181
+ callback = Callback.new(self, name, selector, &block)
182
+ callbacks.push(callback)
183
+
184
+ attach(callback)
185
+ end
186
+ end
187
+
188
+ # Start listening for an event in the capturing phase.
189
+ #
190
+ # @param name [String] the event name
191
+ #
192
+ # @yieldparam event [Event] the event
193
+ #
194
+ # @return [Callback]
195
+ def on!(name, &block)
196
+ raise ArgumentError, 'no block has been given' unless block
197
+
198
+ name = Event.name_for(name)
199
+ callback = Callback.new(self, name, &block)
200
+ callbacks.push(callback)
201
+
202
+ attach!(callback)
203
+ end
204
+
205
+ if Browser.supports? 'Event.addListener'
206
+ def attach(callback)
207
+ `#@native.addEventListener(#{callback.name}, #{callback.to_proc})`
208
+
209
+ callback
210
+ end
211
+
212
+ def attach!(callback)
213
+ `#@native.addEventListener(#{callback.name}, #{callback.to_proc}, true)`
214
+
215
+ callback
216
+ end
217
+ elsif Browser.supports? 'Event.attach'
218
+ def attach(callback)
219
+ if callback.event == Custom
220
+ %x{
221
+ if (!#@native.$custom) {
222
+ #@native.$custom = function(event) {
223
+ for (var i = 0, length = #@native.$callbacks.length; i < length; i++) {
224
+ var callback = #@native.$callbacks[i];
225
+
226
+ if (#{`callback`.event == Custom}) {
227
+ event.type = callback.name;
228
+
229
+ #{`callback`.call(`event`)};
230
+ }
231
+ }
232
+ };
233
+
234
+ #@native.attachEvent("ondataavailable", #@native.$custom);
235
+ }
236
+ }
237
+ else
238
+ `#@native.attachEvent("on" + #{callback.name}, #{callback.to_proc})`
239
+ end
240
+
241
+ callback
242
+ end
243
+
244
+ def attach!(callback)
245
+ case callback.name
246
+ when :blur
247
+ `#@native.attachEvent("onfocusout", #{callback.to_proc})`
248
+
249
+ when :focus
250
+ `#@native.attachEvent("onfocusin", #{callback.to_proc})`
251
+
252
+ else
253
+ warn "attach: capture doesn't work on this browser"
254
+ attach(callback)
255
+ end
256
+
257
+ callback
258
+ end
259
+ else
260
+ # @todo implement polyfill
261
+ # @private
262
+ def attach(*)
263
+ raise NotImplementedError
264
+ end
265
+
266
+ # @todo implement polyfill
267
+ # @private
268
+ def attach!(*)
269
+ raise NotImplementedError
270
+ end
271
+ end
272
+
273
+ # @overload off()
274
+ # Stop listening for any event.
275
+ #
276
+ # @overload off(what)
277
+ # Stop listening for an event.
278
+ #
279
+ # @param what [Callback, String, Regexp] what to stop listening for
280
+ def off(what = nil)
281
+ case what
282
+ when Callback
283
+ callbacks.delete(what)
284
+ detach(what)
285
+
286
+ when String
287
+ if what.include?(?*) or what.include?(??)
288
+ off(Regexp.new(what.gsub(/\*/, '.*?').gsub(/\?/, ?.)))
289
+ else
290
+ what = Event.name_for(what)
291
+
292
+ callbacks.delete_if {|callback|
293
+ if callback.name == what
294
+ detach(callback)
295
+
296
+ true
297
+ end
298
+ }
299
+ end
300
+
301
+ when Regexp
302
+ callbacks.delete_if {|callback|
303
+ if callback.name =~ what
304
+ detach(callback)
305
+
306
+ true
307
+ end
308
+ }
309
+
310
+ else
311
+ callbacks.each {|callback|
312
+ detach(callback)
313
+ }
314
+
315
+ callbacks.clear
316
+ end
317
+ end
318
+
319
+ if Browser.supports? 'Event.removeListener'
320
+ def detach(callback)
321
+ `#@native.removeEventListener(#{callback.name}, #{callback.to_proc}, false)`
322
+ end
323
+ elsif Browser.supports? 'Event.detach'
324
+ def detach(callback)
325
+ if callback.event == Custom
326
+ if callbacks.none? { |c| c.event == Custom }
327
+ %x{
328
+ #@native.detachEvent("ondataavailable", #@native.$custom);
329
+
330
+ delete #@native.$custom;
331
+ }
332
+ end
333
+ else
334
+ `#@native.detachEvent("on" + #{callback.name}, #{callback.to_proc})`
335
+ end
336
+ end
337
+ else
338
+ # @todo implement internal handler thing
339
+ # @private
340
+ def detach(callback)
341
+ raise NotImplementedError
342
+ end
343
+ end
344
+
345
+ # Trigger an event on the target.
346
+ #
347
+ # @param name [String] the event name
348
+ # @param args [Array] optional arguments to the event callback
349
+ #
350
+ # @yieldparam definition [Definition] definition to customize the event
351
+ def trigger(event, *args, &block)
352
+ if event.is_a? String
353
+ event = Event.create(event, *args, &block)
354
+ end
355
+
356
+ dispatch(event)
357
+ end
358
+
359
+ # Trigger an event on the target without bubbling.
360
+ #
361
+ # @param name [String] the event name
362
+ # @param args [Array] optional arguments to the event callback
363
+ #
364
+ # @yieldparam definition [Definition] definition to customize the event
365
+ def trigger!(event, *args, &block)
366
+ trigger event, *args do |e|
367
+ block.call(e) if block
368
+ e.bubbles = false
369
+ end
370
+ end
371
+
372
+ if Browser.supports? 'Event.dispatch'
373
+ def dispatch(event)
374
+ `#@native.dispatchEvent(#{event.to_n})`
375
+ end
376
+ elsif Browser.supports? 'Event.fire'
377
+ def dispatch(event)
378
+ if Custom === event
379
+ `#@native.fireEvent("ondataavailable", #{event.to_n})`
380
+ else
381
+ `#@native.fireEvent("on" + #{event.name}, #{event.to_n})`
382
+ end
383
+ end
384
+ else
385
+ # @todo implement polyfill
386
+ # @private
387
+ def dispatch(*)
388
+ raise NotImplementedError
389
+ end
390
+ end
391
+
392
+ private
393
+ def callbacks
394
+ %x{
395
+ if (!#@native.$callbacks) {
396
+ #@native.$callbacks = [];
397
+ }
398
+
399
+ return #@native.$callbacks;
400
+ }
401
+ end
402
+
403
+ def delegated
404
+ %x{
405
+ if (!#@native.$delegated) {
406
+ #@native.$delegated = #{{}};
407
+ }
408
+
409
+ return #@native.$delegated;
410
+ }
411
+ end
412
+
413
+ def delegate(delegates, event, element = event.target)
414
+ return if element.nil? || element == event.on
415
+
416
+ delegates.handlers.each {|selector, block|
417
+ if element =~ selector
418
+ new = event.dup
419
+ new.on = element
420
+
421
+ block.call new, *new.arguments
422
+ end
423
+ }
424
+
425
+ delegate(delegates, event, element.parent)
426
+ end
427
+ end
428
+ end
429
+
430
+ end