maiha-mjs 0.0.2 → 0.0.3

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 (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/mjs/helper.rb +114 -20
  3. metadata +3 -2
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ AUTHOR = "maiha"
9
9
  EMAIL = "maiha@wota.jp"
10
10
  HOMEPAGE = "http://github.com/maiha/mjs"
11
11
  SUMMARY = "A slice for the Merb framework that offers Ajax actions like RJS with jQuery"
12
- GEM_VERSION = "0.0.1"
12
+ GEM_VERSION = "0.0.3"
13
13
 
14
14
  spec = Gem::Specification.new do |s|
15
15
  s.rubyforge_project = 'merb'
data/lib/mjs/helper.rb CHANGED
@@ -6,35 +6,129 @@ module Mjs
6
6
  @page ||= Mjs::JavaScriptContext.new
7
7
  end
8
8
 
9
+ def remote_function(opts)
10
+ build_href(opts)
11
+ if opts[:submit]
12
+ "$.ajax(%s);" % options_for_ajax(opts)
13
+ else
14
+ "$.getScript('#{opts[:href]}')"
15
+ end
16
+ end
17
+
9
18
  # override! :link_to # for Ajax
10
19
  def link_to(name, url='', opts={})
11
20
  opts[:href] ||= url
12
- if url.is_a?(DataMapper::Resource)
13
- record = opts[:href]
14
- if record.new_record?
15
- opts[:href] = resource(record.class.name.downcase.pluralize.intern, :new)
16
- else
17
- opts[:href] = resource(record)
18
- end
19
- end
20
-
21
21
  opts[:remote] ||= true if opts[:submit]
22
22
  return super unless opts.delete(:remote)
23
23
 
24
- submit = opts.delete(:submit)
25
- target =
26
- case submit
27
- when Symbol then "$('##{submit} input')"
28
- when String then "$('#{submit}')"
29
- when NilClass # GET requst
30
- else
31
- raise ArgumentError, "link_to :submit expects Symbol or String, but got #{submit.class.name}"
32
- end
33
-
34
- ajax = submit ? "$.post('#{opts[:href]}', #{target}.serialize(), null, 'script');" : "$.getScript('#{opts[:href]}')"
24
+ ajax = remote_function(opts)
35
25
  opts[:onclick] = "#{opts.delete(:onclick)}; #{ajax}; return false;"
36
26
  opts[:href] = '#'
37
27
  %{<a #{ opts.to_xml_attributes }>#{name}</a>}
38
28
  end
29
+
30
+ ######################################################################
31
+ ### derived from actionpack
32
+
33
+ JS_ESCAPE_MAP = {
34
+ '\\' => '\\\\', '</' => '<\/',
35
+ "\r\n" => '\n',
36
+ "\n" => '\n', "\r" => '\n',
37
+ '"' => '\\"',
38
+ "'" => "\\'" }
39
+
40
+ # Escape carrier returns and single and double quotes for JavaScript segments.
41
+ def escape_javascript(javascript)
42
+ if javascript
43
+ javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
44
+ else
45
+ ''
46
+ end
47
+ end
48
+
49
+ private
50
+ def build_href(opts)
51
+ if opts[:href].is_a?(DataMapper::Resource)
52
+ record = opts[:href]
53
+ if record.new_record?
54
+ opts[:href] = resource(record.class.name.downcase.pluralize.intern, :new)
55
+ else
56
+ opts[:href] = resource(record)
57
+ end
58
+ end
59
+ opts
60
+ end
61
+
62
+ AJAX_FUNCTIONS = {
63
+ :before => :beforeSend,
64
+ :before_send => :beforeSend,
65
+ :beforeSend => :beforeSend,
66
+ :complete => :complete,
67
+ :success => :success,
68
+ :error => :error,
69
+ :dataFilter => :dataFilter,
70
+ :data_filter => :dataFilter,
71
+ :xhr => :xhr,
72
+ }
73
+
74
+ def options_for_ajax(options)
75
+ js_options = build_callbacks(options)
76
+
77
+ submit = options.delete(:submit)
78
+ target =
79
+ case submit
80
+ when Symbol then "$('##{submit} input')"
81
+ when String then "$('#{submit}')"
82
+ when NilClass # GET requst
83
+ else
84
+ raise ArgumentError, "link_to :submit expects Symbol or String, but got #{submit.class.name}"
85
+ end
86
+
87
+ build_href(options)
88
+
89
+ js_options[:type] = "'POST'"
90
+ js_options[:url] = "'#{options[:href]}'"
91
+ js_options[:data] = "#{target}.serialize()"
92
+ js_options[:dataType] = "'script'"
93
+
94
+ options_for_javascript(js_options)
95
+ end
96
+
97
+ def options_for_javascript(options)
98
+ if options.empty?
99
+ '{}'
100
+ else
101
+ "{#{options.keys.map { |k| "#{k}:#{options[k]}" }.sort.join(', ')}}"
102
+ end
103
+ end
104
+
105
+ def jquery_selector(key)
106
+ case key
107
+ when Symbol then "$('##{key}')"
108
+ when String then "$('#{key}')"
109
+ else
110
+ raise "invalid jquery selector: [#{key.class}] #{key}"
111
+ end
112
+ end
113
+
114
+ def build_callbacks(options)
115
+ callbacks = {}
116
+
117
+ # special callback
118
+ spinner = options.delete(:spinner)
119
+ if spinner
120
+ options[:before] = [options[:before], "%s.show()" % jquery_selector(spinner)].compact * ';'
121
+ options[:complete] = [options[:complete], "%s.hide()" % jquery_selector(spinner)].compact * ';'
122
+ end
123
+
124
+ options.each do |callback, code|
125
+ if (name = AJAX_FUNCTIONS[callback])
126
+ callbacks[name.to_s] = "function(request){#{code}}"
127
+ end
128
+ end
129
+
130
+ callbacks
131
+ end
132
+
39
133
  end
40
134
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maiha-mjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - maiha
@@ -9,11 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-12 00:00:00 -08:00
12
+ date: 2009-03-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: merb-slices
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements: