opal 0.3.2 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. data/README.md +85 -127
  2. data/bin/opal +3 -3
  3. data/lib/core.rb +114 -0
  4. data/{gems/core/lib → lib}/core/array.rb +68 -187
  5. data/{gems/core/lib → lib}/core/basic_object.rb +22 -5
  6. data/lib/core/class.rb +38 -0
  7. data/{gems/core/lib → lib}/core/dir.rb +1 -1
  8. data/lib/core/enumerable.rb +33 -0
  9. data/{gems/core/lib → lib}/core/error.rb +1 -4
  10. data/{gems/core/lib → lib}/core/false_class.rb +0 -0
  11. data/{gems/core/lib → lib}/core/file.rb +11 -3
  12. data/{gems/core/lib → lib}/core/hash.rb +12 -0
  13. data/{gems/core/lib → lib}/core/kernel.rb +114 -86
  14. data/lib/core/match_data.rb +33 -0
  15. data/lib/core/module.rb +97 -0
  16. data/{gems/core/lib → lib}/core/nil_class.rb +0 -0
  17. data/{gems/core/lib → lib}/core/numeric.rb +5 -0
  18. data/{gems/core/lib → lib}/core/object.rb +0 -0
  19. data/{gems/core/lib → lib}/core/proc.rb +13 -3
  20. data/{gems/core/lib → lib}/core/range.rb +10 -0
  21. data/{gems/core/lib → lib}/core/regexp.rb +33 -1
  22. data/{gems/core/lib → lib}/core/string.rb +25 -10
  23. data/{gems/core/lib → lib}/core/symbol.rb +3 -3
  24. data/{gems/core/lib → lib}/core/top_self.rb +0 -0
  25. data/{gems/core/lib → lib}/core/true_class.rb +0 -0
  26. data/lib/dev.rb +169 -0
  27. data/{gems/ospec/lib → lib}/ospec.rb +1 -0
  28. data/lib/ospec/autorun.rb +8 -0
  29. data/{gems/ospec/lib → lib}/ospec/dsl.rb +2 -2
  30. data/{gems/ospec/lib → lib}/ospec/example.rb +0 -0
  31. data/{gems/ospec/lib → lib}/ospec/example/before_and_after_hooks.rb +0 -0
  32. data/{gems/ospec/lib → lib}/ospec/example/errors.rb +0 -0
  33. data/{gems/ospec/lib → lib}/ospec/example/example_group.rb +0 -0
  34. data/{gems/ospec/lib → lib}/ospec/example/example_group_factory.rb +0 -3
  35. data/{gems/ospec/lib → lib}/ospec/example/example_group_hierarchy.rb +4 -3
  36. data/{gems/ospec/lib → lib}/ospec/example/example_group_methods.rb +1 -1
  37. data/{gems/ospec/lib → lib}/ospec/example/example_group_proxy.rb +3 -2
  38. data/{gems/ospec/lib → lib}/ospec/example/example_methods.rb +1 -1
  39. data/{gems/ospec/lib → lib}/ospec/example/example_proxy.rb +7 -7
  40. data/{gems/ospec/lib → lib}/ospec/expectations.rb +0 -0
  41. data/{gems/ospec/lib → lib}/ospec/expectations/errors.rb +0 -0
  42. data/{gems/ospec/lib → lib}/ospec/expectations/fail_with.rb +4 -3
  43. data/{gems/ospec/lib → lib}/ospec/expectations/handler.rb +6 -0
  44. data/lib/ospec/helpers/scratch.rb +18 -0
  45. data/{gems/ospec/lib → lib}/ospec/matchers.rb +2 -2
  46. data/{gems/ospec/lib → lib}/ospec/matchers/be.rb +0 -0
  47. data/{gems/ospec/lib → lib}/ospec/matchers/generated_descriptions.rb +0 -0
  48. data/{gems/ospec/lib → lib}/ospec/matchers/operator_matcher.rb +2 -0
  49. data/lib/ospec/matchers/raise_error.rb +38 -0
  50. data/lib/ospec/runner.rb +90 -0
  51. data/{gems/ospec/lib → lib}/ospec/runner/example_group_runner.rb +10 -13
  52. data/lib/ospec/runner/formatter/html_formatter.rb +139 -0
  53. data/{gems/ospec/lib → lib}/ospec/runner/formatter/terminal_formatter.rb +0 -0
  54. data/{gems/ospec/lib → lib}/ospec/runner/options.rb +1 -3
  55. data/{gems/ospec/lib → lib}/ospec/runner/reporter.rb +0 -9
  56. data/lib/racc/parser.rb +165 -0
  57. data/lib/strscan.rb +52 -0
  58. data/{lib → opal_lib}/opal.rb +2 -0
  59. data/opal_lib/opal/build_methods.rb +51 -0
  60. data/opal_lib/opal/builder.rb +164 -0
  61. data/opal_lib/opal/bundle.rb +70 -0
  62. data/opal_lib/opal/command.rb +68 -0
  63. data/{lib → opal_lib}/opal/context.rb +21 -9
  64. data/{lib → opal_lib}/opal/context/console.rb +0 -2
  65. data/opal_lib/opal/context/file_system.rb +34 -0
  66. data/{lib → opal_lib}/opal/context/loader.rb +26 -8
  67. data/opal_lib/opal/gem.rb +84 -0
  68. data/{lib → opal_lib}/opal/rake/builder_task.rb +2 -2
  69. data/opal_lib/opal/rake/spec_task.rb +32 -0
  70. data/{lib → opal_lib}/opal/ruby/nodes.rb +730 -109
  71. data/{lib → opal_lib}/opal/ruby/parser.rb +90 -23
  72. data/opal_lib/opal/ruby/ruby_parser.rb +4862 -0
  73. data/opal_lib/opal/ruby/ruby_parser.y +1454 -0
  74. data/opal_lib/opal/version.rb +4 -0
  75. data/runtime/class.js +359 -0
  76. data/runtime/debug.js +84 -0
  77. data/runtime/fs.js +199 -0
  78. data/runtime/init.js +558 -0
  79. data/runtime/loader.js +351 -0
  80. data/runtime/module.js +109 -0
  81. data/runtime/post.js +10 -0
  82. data/runtime/pre.js +7 -0
  83. data/runtime/runtime.js +351 -0
  84. metadata +88 -175
  85. data/.gitignore +0 -7
  86. data/Changelog +0 -31
  87. data/LICENSE +0 -75
  88. data/Rakefile +0 -86
  89. data/gems/core/README.md +0 -14
  90. data/gems/core/Rakefile +0 -8
  91. data/gems/core/core.gemspec +0 -13
  92. data/gems/core/lib/core.rb +0 -34
  93. data/gems/core/lib/core/class.rb +0 -31
  94. data/gems/core/lib/core/module.rb +0 -100
  95. data/gems/core/lib/core/vm.rb +0 -16
  96. data/gems/core/spec/core/array/append_spec.rb +0 -30
  97. data/gems/core/spec/core/array/assoc_spec.rb +0 -29
  98. data/gems/core/spec/core/array/at_spec.rb +0 -37
  99. data/gems/core/spec/core/array/clear_spec.rb +0 -22
  100. data/gems/core/spec/core/array/collect_bang_spec.rb +0 -27
  101. data/gems/core/spec/core/array/collect_spec.rb +0 -27
  102. data/gems/core/spec/core/array/compact_spec.rb +0 -41
  103. data/gems/core/spec/core/array/concat_spec.rb +0 -15
  104. data/gems/core/spec/core/array/constructor_spec.rb +0 -14
  105. data/gems/core/spec/core/array/each_spec.rb +0 -9
  106. data/gems/core/spec/core/array/element_reference_spec.rb +0 -4
  107. data/gems/core/spec/core/array/first_spec.rb +0 -35
  108. data/gems/core/spec/core/array/include_spec.rb +0 -9
  109. data/gems/core/spec/core/array/join_spec.rb +0 -6
  110. data/gems/core/spec/core/array/last_spec.rb +0 -51
  111. data/gems/core/spec/core/array/length_spec.rb +0 -6
  112. data/gems/core/spec/core/array/map_spec.rb +0 -33
  113. data/gems/core/spec/core/array/reverse_spec.rb +0 -6
  114. data/gems/core/spec/core/builtin_constants/builtin_constants_spec.rb +0 -7
  115. data/gems/core/spec/core/false/and_spec.rb +0 -10
  116. data/gems/core/spec/core/false/inspect_spec.rb +0 -6
  117. data/gems/core/spec/core/false/or_spec.rb +0 -10
  118. data/gems/core/spec/core/false/to_s_spec.rb +0 -6
  119. data/gems/core/spec/core/false/xor_spec.rb +0 -10
  120. data/gems/core/spec/core/file/join_spec.rb +0 -19
  121. data/gems/core/spec/core/hash/assoc_spec.rb +0 -32
  122. data/gems/core/spec/core/kernel/instance_eval_spec.rb +0 -0
  123. data/gems/core/spec/core/kernel/loop_spec.rb +0 -24
  124. data/gems/core/spec/core/kernel/raise_spec.rb +0 -0
  125. data/gems/core/spec/core/module/attr_accessor_spec.rb +0 -28
  126. data/gems/core/spec/core/number/lt_spec.rb +0 -12
  127. data/gems/core/spec/core/string/sub_spec.rb +0 -24
  128. data/gems/core/spec/core/true/and_spec.rb +0 -10
  129. data/gems/core/spec/core/true/inspect_spec.rb +0 -6
  130. data/gems/core/spec/core/true/or_spec.rb +0 -10
  131. data/gems/core/spec/core/true/to_s_spec.rb +0 -6
  132. data/gems/core/spec/core/true/xor_spec.rb +0 -10
  133. data/gems/core/spec/language/and_spec.rb +0 -61
  134. data/gems/core/spec/language/array_spec.rb +0 -68
  135. data/gems/core/spec/language/block_spec.rb +0 -38
  136. data/gems/core/spec/language/break_spec.rb +0 -36
  137. data/gems/core/spec/language/case_spec.rb +0 -103
  138. data/gems/core/spec/language/def_spec.rb +0 -21
  139. data/gems/core/spec/language/eigenclass_spec.rb +0 -60
  140. data/gems/core/spec/language/file_spec.rb +0 -13
  141. data/gems/core/spec/language/fixtures/block.rb +0 -21
  142. data/gems/core/spec/language/fixtures/super.rb +0 -293
  143. data/gems/core/spec/language/hash_spec.rb +0 -29
  144. data/gems/core/spec/language/if_spec.rb +0 -54
  145. data/gems/core/spec/language/loop_spec.rb +0 -11
  146. data/gems/core/spec/language/metaclass_spec.rb +0 -21
  147. data/gems/core/spec/language/method_spec.rb +0 -124
  148. data/gems/core/spec/language/next_spec.rb +0 -25
  149. data/gems/core/spec/language/or_spec.rb +0 -34
  150. data/gems/core/spec/language/redo_spec.rb +0 -24
  151. data/gems/core/spec/language/regexp_spec.rb +0 -26
  152. data/gems/core/spec/language/rescue_spec.rb +0 -20
  153. data/gems/core/spec/language/return_spec.rb +0 -47
  154. data/gems/core/spec/language/string_spec.rb +0 -25
  155. data/gems/core/spec/language/super_spec.rb +0 -32
  156. data/gems/core/spec/language/until_spec.rb +0 -157
  157. data/gems/core/spec/language/variables_spec.rb +0 -155
  158. data/gems/core/spec/language/while_spec.rb +0 -163
  159. data/gems/core/spec/spec_helper.rb +0 -5
  160. data/gems/core_fs/README.md +0 -19
  161. data/gems/dev/Rakefile +0 -5
  162. data/gems/dev/lib/dev.js +0 -99
  163. data/gems/dev/lib/dev/generator.js +0 -1264
  164. data/gems/dev/lib/dev/parser.js +0 -979
  165. data/gems/dev/lib/dev/ruby_parser.js +0 -1088
  166. data/gems/dev/lib/dev/ruby_parser.y +0 -1267
  167. data/gems/dev/lib/dev/string_scanner.js +0 -38
  168. data/gems/dev/tools/racc2js/README.md +0 -39
  169. data/gems/dev/tools/racc2js/math_parser.js +0 -222
  170. data/gems/dev/tools/racc2js/math_parser.rb +0 -133
  171. data/gems/dev/tools/racc2js/math_parser.y +0 -28
  172. data/gems/dev/tools/racc2js/parser.js +0 -218
  173. data/gems/dev/tools/racc2js/racc2js.rb +0 -153
  174. data/gems/json/README.md +0 -4
  175. data/gems/json/json.gemspec +0 -14
  176. data/gems/json/lib/json.rb +0 -64
  177. data/gems/json/lib/json/ext.rb +0 -51
  178. data/gems/json/lib/json/json2.js +0 -481
  179. data/gems/ospec/README.md +0 -0
  180. data/gems/ospec/lib/ospec/autorun.rb +0 -3
  181. data/gems/ospec/lib/ospec/runner.rb +0 -40
  182. data/gems/ospec/lib/ospec/runner/formatter/html_formatter.rb +0 -91
  183. data/gems/ospec/ospec.gemspec +0 -0
  184. data/gems/rquery/README.md +0 -9
  185. data/gems/rquery/lib/rquery.rb +0 -10
  186. data/gems/rquery/lib/rquery/ajax.rb +0 -4
  187. data/gems/rquery/lib/rquery/css.rb +0 -96
  188. data/gems/rquery/lib/rquery/document.rb +0 -25
  189. data/gems/rquery/lib/rquery/element.rb +0 -292
  190. data/gems/rquery/lib/rquery/event.rb +0 -108
  191. data/gems/rquery/lib/rquery/jquery.js +0 -8177
  192. data/gems/rquery/lib/rquery/request.rb +0 -138
  193. data/gems/rquery/lib/rquery/response.rb +0 -49
  194. data/gems/rquery/rquery.gemspec +0 -16
  195. data/lib/opal.js +0 -1597
  196. data/lib/opal/builder.rb +0 -117
  197. data/lib/opal/bundle.rb +0 -131
  198. data/lib/opal/command.rb +0 -11
  199. data/lib/opal/context/file_system.rb +0 -19
  200. data/lib/opal/gem.rb +0 -153
  201. data/lib/opal/ruby/ruby_parser.rb +0 -5170
  202. data/lib/opal/ruby/ruby_parser.y +0 -1298
  203. data/opal.gemspec +0 -15
data/gems/ospec/README.md DELETED
File without changes
@@ -1,3 +0,0 @@
1
- require 'ospec'
2
-
3
- Spec::Runner.autorun
@@ -1,40 +0,0 @@
1
- require 'ospec/runner/options'
2
- require 'ospec/runner/reporter'
3
- require 'ospec/runner/example_group_runner'
4
-
5
- require 'ospec/runner/formatter/html_formatter'
6
- require 'ospec/runner/formatter/terminal_formatter'
7
-
8
- module Spec
9
- module Runner
10
-
11
- def self.run
12
- options.run_examples
13
- end
14
-
15
- def self.options
16
- @options ||= Options.new
17
- @options
18
- end
19
-
20
- def self.autorun
21
- if ARGV.length == 0
22
- puts "Spec: no input files given"
23
- return
24
- end
25
-
26
- ARGV.each do |spec|
27
- # puts "should try and load #{spec}"
28
- if File.exists? spec
29
- # puts "loading: " + File.expand_path(spec)
30
- require File.expand_path(spec)
31
- else
32
- raise "Bad spec to load (does not exist): #{spec}"
33
- end
34
- end
35
-
36
- Spec::Runner.run
37
- end
38
-
39
- end # Runner
40
- end
@@ -1,91 +0,0 @@
1
- module Spec
2
- module Runner
3
- module Formatter
4
- class HtmlFormatter
5
-
6
- attr_reader :example_group, :example_group_number
7
-
8
- def initialize(options)
9
- @options = options
10
- @example_group_number = 0
11
- @example_number = 0
12
- @header_red = nil
13
-
14
- setup_page_dom
15
- end
16
-
17
- ##
18
- # Get the DOM ready for output
19
- #
20
- def setup_page_dom
21
- content = [
22
- '<div class="rspec-report">',
23
- '<div id="rspec-header">',
24
- '<div id="label">',
25
- '<h1>Rspec Code Examples</h1>',
26
- '<div>',
27
-
28
- '<div id="summary">',
29
- '<p id="totals">0</p>',
30
- '<p id="duration">0</p>',
31
- '</div>',
32
- '</div>',
33
- '<div id="results">',
34
- '</div>',
35
- '</div>'
36
- ].join ''
37
-
38
- puts "content is: "
39
- puts content
40
- end
41
-
42
- def start(number_of_examples)
43
-
44
- end
45
-
46
- def end
47
- @end_time = Time.now
48
- end
49
-
50
- def results_output
51
- @results_output ||= Browser::Element.from_native(`document.getElementById('results')`)
52
- end
53
-
54
- def example_group_started(example_group)
55
- @example_group = example_group
56
- @example_group_red = false
57
- # @example_group_number += 1
58
-
59
- @example_group_div = results_output.div :class_name => "example_group"
60
- @example_group_dl = @example_group_div.dl
61
- @example_group_dt = @example_group_dl.dt :content => example_group.description, :id => "example_group_#{example_group_number}"
62
- end
63
-
64
- def example_started(example)
65
-
66
- end
67
-
68
- def example_failed(example, counter, failure)
69
- @header_red = true
70
- @example_group_red = true
71
-
72
- dd = @example_group_dl.dd :class_name => "spec failed"
73
-
74
- dd.span :content => example.description,
75
- :class_name => "failed_spec_name"
76
-
77
- failure_div = dd.div :class_name => "failure"
78
- failure_div.div(:class_name => "message").pre(:content => failure.exception.message)
79
- end
80
-
81
- def example_passed(example)
82
- @example_group_dl.dd(:class_name => "spec passed").span(:content => example.description, :class_name => "passed_spec_name")
83
- end
84
-
85
- def example_pending(example, message)
86
- @example_group_dl.dd(:class_name => "spec not_implemented").span(:content => "#{example.description} (PENDING: #{message})", :class_name => "not_implemented_spec_name")
87
- end
88
- end
89
- end
90
- end
91
- end
File without changes
@@ -1,9 +0,0 @@
1
- rQuery
2
- ======
3
-
4
- rQuery is a ruby wrapper around jQuery, intended to be used by opal.
5
- Although this is distributed as a common-js package that can be used by
6
- node, its main intended runtime is in the browser.
7
-
8
- See https://github.com/adambeynon/opal
9
-
@@ -1,10 +0,0 @@
1
- require 'json'
2
-
3
- require 'rquery/jquery'
4
- require 'rquery/document'
5
- require 'rquery/element'
6
- require 'rquery/css'
7
- require 'rquery/event'
8
- require 'rquery/request'
9
- require 'rquery/response'
10
-
@@ -1,4 +0,0 @@
1
- class Ajax
2
- nil
3
- end
4
-
@@ -1,96 +0,0 @@
1
- class Element
2
-
3
- # Adds the given class or classes to each element in the receiver. Multiple
4
- # classes may be added by seperating them with a space. These classes do not
5
- # replace any previously set classes, but are instead added.
6
- #
7
- # @param [String] name the class(es) name(s)
8
- # @return [Element] returns the receiver
9
- def add_class(name)
10
- `return self.addClass(name);`
11
- end
12
-
13
- # Removes the given class or classes to each element in the receiver. Multiple
14
- # classes may be removed at once by passing in a space seperated string of
15
- # classnames. To remove all classes, dont pass in a parameter, which will
16
- # remove all set classes.
17
- #
18
- # @param [String] name the classes to remove
19
- # @return [Element] returns the receiver
20
- def remove_class(name = nil)
21
- if name
22
- `self.removeClass(name)`
23
- else
24
- `self.removeClass()`
25
- end
26
- end
27
-
28
- # Returns `true` if any of the matched elements contain the given `name` in
29
- # their classes, `false` otherwise.
30
- #
31
- # @param [String] name class name to check for
32
- # @return [true, false]
33
- def has_class?(name)
34
- `return self.hasClass(name) ? Qtrue : Qfalse;`
35
- end
36
-
37
- def style(key = nil, value = nil)
38
- if key.nil?
39
- # raise "need to return a StyleHash"
40
- @style_hash ||= StyleHash.new self
41
- elsif value.nil?
42
- raise "need to use getter for key"
43
- else
44
- raise "need to use setter"
45
- end
46
- end
47
-
48
- alias_method :css, :style
49
-
50
- # Basically used to catch method_missing calls when we try to set style
51
- # properties, or try to retrieve them. All calls onto style will be
52
- # either trying to set or set a property, so we just do the right thing.
53
- class StyleHash < BasicObject
54
- def initialize(element)
55
- `return self.$element = element;`
56
- end
57
-
58
- # Method missing will be called for getting and setting properties. If we
59
- # are setting a property, then our css_property will have a '=' at the end
60
- # of it, and its value will be css_value. If we are just getting a value,
61
- # then css_property will not have an '=', and css_value will remain nil.
62
- #
63
- # {#[]} and {#[]=} can handle aref and aset independantly also, so they
64
- # will not be forwarded here.
65
- def method_missing(css_property, css_value = nil)
66
- `var name = #{css_property.to_s};
67
- console.log("===== " + name);
68
- if (css_value == nil) {
69
- console.log("getting property!");
70
- return self.$element.css(css_property);
71
- } else {
72
- console.log("need to remove = from css_property name");
73
- console.log("setting property!");
74
- return self.$element.css(css_property, css_value);
75
- }`
76
- end
77
-
78
- # We will be accessing to_s and inspect a lot during development, so lets
79
- # just define them here. They do not conflict with any css property, so
80
- # no issues will be presented.
81
- def to_s
82
- "#<StyleHash element: " + `self.$element`.inspect + ">"
83
- end
84
-
85
- def inspect
86
- to_s
87
- end
88
-
89
- # Again, during development we may want to put stuff to console, so we just
90
- # use Object's puts implementation
91
- def puts(str)
92
- Object.puts str
93
- end
94
- end
95
- end
96
-
@@ -1,25 +0,0 @@
1
- module Document
2
- # Accepts a block that will be called once the document is ready. This uses
3
- # the underlying $(function() { ... }) mechanism. Note, multiple blocks may
4
- # be passed to this function and they will be called in order.
5
- #
6
- # @example
7
- #
8
- # Document.ready? do
9
- # puts "document is now ready"
10
- # end
11
- #
12
- # @return [Document] returns self
13
- def self.ready?
14
- `$(function() { #{yield}; })` if block_given?
15
- self
16
- end
17
-
18
- # Returns an {Element} representing the <head> element of the page.
19
- #
20
- # @return {Element} head element
21
- def self.head
22
- `return $(document.getElementsByTagName('head')[0]);`
23
- end
24
- end
25
-
@@ -1,292 +0,0 @@
1
- # jQuery objects themselves are bridged to the ruby Element class. This allows
2
- # direct access to jquery through ruby, and makes the creation and accessing of
3
- # jquery objects really fast and efficient (there is minimal overhead introduced
4
- # by wrapping them).
5
- #
6
- # This is accomplished basically by adding the requied opal properties onto jquery
7
- # as if they were just plugins; $m, $klass and $flags. These are the same names as
8
- # added to the core prototypes as to avoid name confliction as much as possible.
9
- OpalVM.bridged_class `$.fn`, 'Element'
10
-
11
- # Element is used two-fold. It is the equivalent of the jQuery object in js,
12
- # and is used to create new element literals. Therefore, an Element instance
13
- # can actually represent 1, many, or even 0 actual elements. Element for this
14
- # reason is actually more like an array of elements, where its instance
15
- # methods are applied to 0 or more actual elements. Accessing elements
16
- # individually through {#first} etc, still returns an Element instance with
17
- # a singular element literal.
18
- class Element
19
- # Top level selector. This is the equivalent of passing a selector string
20
- # to the jQuery object. This searches essentially in the context of the
21
- # document, and returns an Element instance with all matched elements.
22
- #
23
- # @example
24
- #
25
- # Element.find 'div > p'
26
- # # => #<Element 0x91232>
27
- #
28
- # @param [String] selector
29
- # @return [Element]
30
- def self.find(selector)
31
- from_native `$(selector)`
32
- end
33
-
34
- # alias_method :[], :find
35
-
36
- # Initialze is used when simply creating a new Element, the equivalent of
37
- # document.createElement('tagName'). It is then wrapped by jquery and
38
- # becomes the sole element belonging to this instance.
39
- #
40
- # Creating a new Element instance should only be used when actually creating
41
- # a new DOM element. For referencing already existing elements, instead use
42
- # one of the selector methods, {Element#find} etc.
43
- #
44
- # NOTE: initialize is not used due to limitations on creating/adding to jquery
45
- # objects, so we need to override .new instead.
46
- #
47
- # @return [Element]
48
- def self.new(type = 'div')
49
- `return $(document.createElement(type));`
50
- end
51
-
52
- # PRIVATE!!!!!
53
- #
54
- # Should really be a js method? Basically this takes a jquery object, which
55
- # is NOT a ruby object, and returns a new Element instance with the given
56
- # jquery object as its content. The passed jq variable should be a jquery
57
- # object.
58
- def self.from_native(elem)
59
- `return $(elem);`
60
- end
61
-
62
- # Returns a string representation of the collection of elements. If the
63
- # receiver contains a single element, then it will display the tag name,
64
- # id and class of the receiver. If more than one element is present, then
65
- # the inspection string will just contain the number of contained elements.
66
- #
67
- # @return [String]
68
- def to_s
69
- if length == 1
70
- str = "#<Element #{tag}"
71
- str += ", id=#{id.inspect}" if id
72
- str + ">"
73
- else
74
- "#<Element length: #{length}>"
75
- end
76
- end
77
-
78
- # Returns the tag name of the receiver. All tag names are in lowercase and
79
- # will overcome any cross browser differences. If there are no elements in
80
- # the receiver then the tag name will be an empty string. If there are more
81
- # than one elements, the tag name will be that of the first element in the
82
- # collection.
83
- #
84
- # @return [String]
85
- def tag
86
- `return self[0] ? self[0].tagName.toLowerCase() : '';`
87
- end
88
-
89
- # Returns the number of elements in the receiver.
90
- #
91
- # @return [Numeric]
92
- def length
93
- `return self.length;`
94
- end
95
-
96
- alias_method :size, :length
97
-
98
- # Indexed reference - Returns the matched element at the given index, or
99
- # returns a slice of elements beginning at the given index for the given
100
- # count of elements. Negative indecies count backwards from the end of the
101
- # set of matched elements, so that -1 would be the last matched element.
102
- # This will return nil if the given index is not in range.
103
- #
104
- # TODO: count functionality not yet implemented - only simple index
105
- # referencing.
106
- #
107
- # @param [Numeric] index index to start at
108
- # @param [Numeric] count last index
109
- # @return [Element, nil]
110
- def [](index, count = nil)
111
- `var size = self.length;
112
-
113
- if (index < 0) index += size;
114
-
115
- if (index >= size || index < 0) return nil;
116
-
117
- return $(self[index]);`
118
- end
119
-
120
- # Returns the original selector passed to find the elements. If no selector
121
- # was given, for example when creating a new object literal, or by passing
122
- # in a current element, then the selector will just be an empty string.
123
- #
124
- # @return [String]
125
- def selector
126
- `return self.selector;`
127
- end
128
-
129
- # Yields the block for each matched element in the receiver. The block is
130
- # passed the element as the first param, and the index as the second.
131
- #
132
- # @example
133
- #
134
- # Element.find('div').each do |elem, idx|
135
- # puts "at idx #{idx} we found: #{elem}."
136
- # end
137
- #
138
- # @return [Element] returns the receiver
139
- def each
140
- `var length = self.length;
141
-
142
- for (var i = 0; i < length; i++) {
143
- try {
144
- #{yield `$(self[i])`, `i`};
145
- } catch (e) {
146
- throw e;
147
- }
148
- }
149
-
150
- return self;`
151
- end
152
-
153
- # Get or set the value of an attribute for the first element in this set of
154
- # matched elements. This will only happen for the first matched element, so
155
- # to apply this to all matches, use {#each} or loop over each item with
156
- # another construct.
157
- #
158
- #
159
- def attr(name, value = nil)
160
- if value.nil?
161
- `self.attr(#{name.to_s}) || ''`
162
- else
163
- `self.attr(#{name.to_s}, value)`
164
- end
165
- end
166
-
167
- # Returns the id attr of the first matched element. If the set of elements
168
- # is empty, then nil is just returned. An optional value may be passed
169
- # which will be set as the id of the first element. This is the same as
170
- # setting the id with {#id=} but this allows for chained calls, and it also
171
- # returns the receiver (to allow for chained calls).
172
- #
173
- # @example Retrieving the id
174
- #
175
- # Element['div'].id
176
- # # => 'some_id'
177
- #
178
- # @example Setting the id
179
- #
180
- # Element['div'].id('new_id')
181
- # # => #<Element 0x827>
182
- #
183
- # @param [String] value optional id to set
184
- # @return [Element, String]
185
- def id(value = nil)
186
- if value
187
- `return self.attr('id', value);`
188
- self
189
- else
190
- `return self.attr('id');`
191
- end
192
- end
193
-
194
- # Sets the id of the first matched element in the receiver.
195
- #
196
- # @example
197
- #
198
- # Element['div].id = "some_new_id"
199
- #
200
- # @param [String] value the id to set
201
- # @return [String] returns the set value
202
- def id=(value)
203
- `self.attr('id', value)`
204
- value
205
- end
206
-
207
- # Returns the html content of the first matched element in the receiver.
208
- # If the optional `content` is given, then this will set the html content
209
- # in the same manner than {#html=} does, except that this method will
210
- # return the receiver so that it can be used for chaining calls.
211
- #
212
- def html(content = nil)
213
- if content
214
- `self.html(content)`
215
- else
216
- `self.html()`
217
- end
218
- end
219
-
220
- def html=(content)
221
- `return self.html(content);`
222
- end
223
-
224
- # Return the first matched element in the receiver.
225
- #
226
- # @return [Element]
227
- def first
228
- `return self.first();`
229
- end
230
-
231
- # Remove all the child nodes from each of the matched elements in the
232
- # receiver. This method will remove all text contents of the receiver
233
- # as well, as they are also text nodes belonging to the receiver. By
234
- # using jQuery underpinnings, all event listeners of all children are
235
- # also removed first to avoid memory leaks.
236
- #
237
- # @return [Element] returns the receiver
238
- def clear
239
- `return self.empty();`
240
- end
241
-
242
- alias_method :empty, :clear
243
-
244
- # Removes the set of matched elements from the DOM. This method works
245
- # similarly to {#clear}, but the matched element is also removed, as well
246
- # as its children. Also, like {#clear}, all event handlers are removed
247
- # first. An optional selector may be passed which removes those children
248
- # of the matched elements that match the given selector.
249
- #
250
- # @return [Element] returns the receiver
251
- def remove
252
- `return self.remove();`
253
- end
254
-
255
- # Inserts the given content to the end of each element in the set
256
- # represented by the receiver.
257
- #
258
- # @param [String, Element] content content to insert
259
- # @return [Element] returns the receiver
260
- def append(content)
261
- `return self.append(content);`
262
- end
263
-
264
- # Inserts the given `content` before each member in the receiver. The
265
- # content may be a string, or an element.
266
- #
267
- # @param [String, Element] content the content to insert
268
- # @return [Element] returns the receiver
269
- def before(content)
270
- `return self.before(content);`
271
- end
272
-
273
- # Inserts the `content` before each member in the receiver. Content
274
- # may be a string or an Element instance.
275
- #
276
- # @param [String, Element] content content to insert
277
- # @return [Element] returns the receiver
278
- def after(content)
279
- `return self.after(content);`
280
- end
281
-
282
- # @group Events
283
- def mouse_down
284
- `return self.mousedown(function(event) {
285
- #{yield `event`};
286
- });`
287
- end
288
-
289
- # @endgroup
290
-
291
- end
292
-