handicraft_ujs 1.0
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.
- data/LICENSE +0 -0
- data/README +39 -0
- data/lib/handicraft/ujs_helper.rb +17 -0
- data/lib/jquery_for_rjs.rb +202 -0
- metadata +70 -0
data/LICENSE
ADDED
File without changes
|
data/README
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
Author:: Wen-Tien Chang(ihower@handlino.com) && Kang-min Liu(gugod@handlino.com)
|
2
|
+
Copyright:: Copyright (c) 2009 Handlino Inc.
|
3
|
+
Licensed under the MIT: http://www.opensource.org/licenses/mit-license.php
|
4
|
+
|
5
|
+
handicraft_ujs is a jQuery replacement for Prototype/script.aculo.us on Rails, and it's unobtrusive and accessible.
|
6
|
+
|
7
|
+
== usage example
|
8
|
+
|
9
|
+
1.
|
10
|
+
|
11
|
+
link_to_remote 'foobar', :url => items_path, :method => :post
|
12
|
+
|
13
|
+
becomes
|
14
|
+
|
15
|
+
link_to 'foobar', items_path, :class => 'h-post'
|
16
|
+
|
17
|
+
2.
|
18
|
+
|
19
|
+
remote_form_for item , :url => item_path(item) , :html => { :method => :put }
|
20
|
+
|
21
|
+
becomes
|
22
|
+
|
23
|
+
form_for item ,item_path(item) , :html => { :method => :put, :class => 'h-put' }
|
24
|
+
|
25
|
+
3.
|
26
|
+
|
27
|
+
button_to_remote 'foobar', :url => items_path, :method => :get
|
28
|
+
|
29
|
+
becomes
|
30
|
+
|
31
|
+
button_to 'foobar', items_path, :method => 'get', :class => 'h-get'
|
32
|
+
|
33
|
+
|
34
|
+
more example please see http://github.com/ihower/handicraft-interfaces
|
35
|
+
|
36
|
+
== thanks
|
37
|
+
|
38
|
+
* http://blog.lawrencepit.com/2008/09/04/unobtrusive-jquery-rails/
|
39
|
+
* http://ennerchi.com/projects/jrails
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Handicraft
|
2
|
+
module UjsHelper
|
3
|
+
|
4
|
+
# Passes the authenticity token for use in javascript
|
5
|
+
def yield_authenticity_token
|
6
|
+
if protect_against_forgery?
|
7
|
+
"<script type='text/javascript'>
|
8
|
+
//<![CDATA[
|
9
|
+
window._auth_token_name = '#{request_forgery_protection_token}';
|
10
|
+
window._auth_token = '#{form_authenticity_token}';
|
11
|
+
//]]>
|
12
|
+
</script>"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
# Extract from jRails http://ennerchi.com/projects/jrails
|
2
|
+
module ActionView
|
3
|
+
module Helpers
|
4
|
+
|
5
|
+
module JavaScriptHelper
|
6
|
+
|
7
|
+
# This function can be used to render rjs inline
|
8
|
+
#
|
9
|
+
# <%= javascript_function do |page|
|
10
|
+
# page.replace_html :list, :partial => 'list', :object => @list
|
11
|
+
# end %>
|
12
|
+
#
|
13
|
+
def javascript_function(*args, &block)
|
14
|
+
html_options = args.extract_options!
|
15
|
+
function = args[0] || ''
|
16
|
+
|
17
|
+
html_options.symbolize_keys!
|
18
|
+
function = update_page(&block) if block_given?
|
19
|
+
javascript_tag(function)
|
20
|
+
end
|
21
|
+
|
22
|
+
def jquery_id(id)
|
23
|
+
id.to_s.count('#.*,>+~:[/ ') == 0 ? "##{id}" : id
|
24
|
+
end
|
25
|
+
|
26
|
+
def jquery_ids(ids)
|
27
|
+
Array(ids).map{|id| jquery_id(id)}.join(',')
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
module PrototypeHelper
|
33
|
+
|
34
|
+
unless const_defined? :JQUERY_VAR
|
35
|
+
JQUERY_VAR = '$'
|
36
|
+
end
|
37
|
+
|
38
|
+
class JavaScriptGenerator
|
39
|
+
module GeneratorMethods
|
40
|
+
|
41
|
+
def insert_html(position, id, *options_for_render)
|
42
|
+
insertion = position.to_s.downcase
|
43
|
+
insertion = 'append' if insertion == 'bottom'
|
44
|
+
insertion = 'prepend' if insertion == 'top'
|
45
|
+
call "#{JQUERY_VAR}(\"#{jquery_id(id)}\").#{insertion}", render(*options_for_render)
|
46
|
+
end
|
47
|
+
|
48
|
+
def replace_html(id, *options_for_render)
|
49
|
+
insert_html(:html, id, *options_for_render)
|
50
|
+
end
|
51
|
+
|
52
|
+
def replace(id, *options_for_render)
|
53
|
+
call "#{JQUERY_VAR}(\"#{jquery_id(id)}\").replaceWith", render(*options_for_render)
|
54
|
+
end
|
55
|
+
|
56
|
+
def remove(*ids)
|
57
|
+
call "#{JQUERY_VAR}(\"#{jquery_ids(ids)}\").remove"
|
58
|
+
end
|
59
|
+
|
60
|
+
def show(*ids)
|
61
|
+
call "#{JQUERY_VAR}(\"#{jquery_ids(ids)}\").show"
|
62
|
+
end
|
63
|
+
|
64
|
+
def hide(*ids)
|
65
|
+
call "#{JQUERY_VAR}(\"#{jquery_ids(ids)}\").hide"
|
66
|
+
end
|
67
|
+
|
68
|
+
def toggle(*ids)
|
69
|
+
call "#{JQUERY_VAR}(\"#{jquery_ids(ids)}\").toggle"
|
70
|
+
end
|
71
|
+
|
72
|
+
def jquery_id(id)
|
73
|
+
id.to_s.count('#.*,>+~:[/ ') == 0 ? "##{id}" : id
|
74
|
+
end
|
75
|
+
|
76
|
+
def jquery_ids(ids)
|
77
|
+
Array(ids).map{|id| jquery_id(id)}.join(',')
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
class JavaScriptElementProxy < JavaScriptProxy #:nodoc:
|
86
|
+
|
87
|
+
unless const_defined? :JQUERY_VAR
|
88
|
+
JQUERY_VAR = ActionView::Helpers::PrototypeHelper::JQUERY_VAR
|
89
|
+
end
|
90
|
+
|
91
|
+
def initialize(generator, id)
|
92
|
+
id = id.to_s.count('#.*,>+~:[/ ') == 0 ? "##{id}" : id
|
93
|
+
@id = id
|
94
|
+
super(generator, "#{JQUERY_VAR}(\"#{id}\")")
|
95
|
+
end
|
96
|
+
|
97
|
+
def replace_html(*options_for_render)
|
98
|
+
call 'html', @generator.send(:render, *options_for_render)
|
99
|
+
end
|
100
|
+
|
101
|
+
def replace(*options_for_render)
|
102
|
+
call 'replaceWith', @generator.send(:render, *options_for_render)
|
103
|
+
end
|
104
|
+
|
105
|
+
def reload(options_for_replace={})
|
106
|
+
replace(options_for_replace.merge({ :partial => @id.to_s.sub(/^#/,'') }))
|
107
|
+
end
|
108
|
+
|
109
|
+
def value()
|
110
|
+
call 'val()'
|
111
|
+
end
|
112
|
+
|
113
|
+
def value=(value)
|
114
|
+
call 'val', value
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
class JavaScriptElementCollectionProxy < JavaScriptCollectionProxy #:nodoc:\
|
120
|
+
|
121
|
+
unless const_defined? :JQUERY_VAR
|
122
|
+
JQUERY_VAR = ActionView::Helpers::PrototypeHelper::JQUERY_VAR
|
123
|
+
end
|
124
|
+
|
125
|
+
def initialize(generator, pattern)
|
126
|
+
super(generator, "#{JQUERY_VAR}(#{pattern.to_json})")
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
module ScriptaculousHelper
|
131
|
+
|
132
|
+
unless const_defined? :JQUERY_VAR
|
133
|
+
JQUERY_VAR = ActionView::Helpers::PrototypeHelper::JQUERY_VAR
|
134
|
+
end
|
135
|
+
|
136
|
+
unless const_defined? :SCRIPTACULOUS_EFFECTS
|
137
|
+
SCRIPTACULOUS_EFFECTS = {
|
138
|
+
:appear => {:method => 'fadeIn'},
|
139
|
+
:blind_down => {:method => 'blind', :mode => 'show', :options => {:direction => 'vertical'}},
|
140
|
+
:blind_up => {:method => 'blind', :mode => 'hide', :options => {:direction => 'vertical'}},
|
141
|
+
:blind_right => {:method => 'blind', :mode => 'show', :options => {:direction => 'horizontal'}},
|
142
|
+
:blind_left => {:method => 'blind', :mode => 'hide', :options => {:direction => 'horizontal'}},
|
143
|
+
:bounce_in => {:method => 'bounce', :mode => 'show', :options => {:direction => 'up'}},
|
144
|
+
:bounce_out => {:method => 'bounce', :mode => 'hide', :options => {:direction => 'up'}},
|
145
|
+
:drop_in => {:method => 'drop', :mode => 'show', :options => {:direction => 'up'}},
|
146
|
+
:drop_out => {:method => 'drop', :mode => 'hide', :options => {:direction => 'down'}},
|
147
|
+
:fade => {:method => 'fadeOut'},
|
148
|
+
:fold_in => {:method => 'fold', :mode => 'hide'},
|
149
|
+
:fold_out => {:method => 'fold', :mode => 'show'},
|
150
|
+
:grow => {:method => 'scale', :mode => 'show'},
|
151
|
+
:shrink => {:method => 'scale', :mode => 'hide'},
|
152
|
+
:slide_down => {:method => 'slide', :mode => 'show', :options => {:direction => 'up'}},
|
153
|
+
:slide_up => {:method => 'slide', :mode => 'hide', :options => {:direction => 'up'}},
|
154
|
+
:slide_right => {:method => 'slide', :mode => 'show', :options => {:direction => 'left'}},
|
155
|
+
:slide_left => {:method => 'slide', :mode => 'hide', :options => {:direction => 'left'}},
|
156
|
+
:squish => {:method => 'scale', :mode => 'hide', :options => {:origin => "['top','left']"}},
|
157
|
+
:switch_on => {:method => 'clip', :mode => 'show', :options => {:direction => 'vertical'}},
|
158
|
+
:switch_off => {:method => 'clip', :mode => 'hide', :options => {:direction => 'vertical'}},
|
159
|
+
:toggle_appear => {:method => 'fadeToggle'},
|
160
|
+
:toggle_slide => {:method => 'slide', :mode => 'toggle', :options => {:direction => 'up'}},
|
161
|
+
:toggle_blind => {:method => 'blind', :mode => 'toggle', :options => {:direction => 'vertical'}},
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
def visual_effect(name, element_id = false, js_options = {})
|
166
|
+
element = element_id ? element_id : "this"
|
167
|
+
|
168
|
+
if SCRIPTACULOUS_EFFECTS.has_key? name.to_sym
|
169
|
+
effect = SCRIPTACULOUS_EFFECTS[name.to_sym]
|
170
|
+
name = effect[:method]
|
171
|
+
mode = effect[:mode]
|
172
|
+
js_options = js_options.merge(effect[:options]) if effect[:options]
|
173
|
+
end
|
174
|
+
|
175
|
+
[:color, :direction].each do |option|
|
176
|
+
js_options[option] = "'#{js_options[option]}'" if js_options[option]
|
177
|
+
end
|
178
|
+
|
179
|
+
if js_options.has_key? :duration
|
180
|
+
speed = js_options.delete :duration
|
181
|
+
speed = (speed * 1000).to_i unless speed.nil?
|
182
|
+
else
|
183
|
+
speed = js_options.delete :speed
|
184
|
+
end
|
185
|
+
|
186
|
+
if ['fadeIn','fadeOut','fadeToggle'].include?(name)
|
187
|
+
javascript = "#{JQUERY_VAR}('#{jquery_id(element_id)}').#{name}("
|
188
|
+
javascript << "#{speed}" unless speed.nil?
|
189
|
+
javascript << ");"
|
190
|
+
else
|
191
|
+
javascript = "#{JQUERY_VAR}('#{jquery_id(element_id)}').#{mode || 'effect'}('#{name}'"
|
192
|
+
javascript << ",#{options_for_javascript(js_options)}" unless speed.nil? && js_options.empty?
|
193
|
+
javascript << ",#{speed}" unless speed.nil?
|
194
|
+
javascript << ");"
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: handicraft_ujs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: "1.0"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Handlino Inc.
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-07 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Handicraft UJS
|
22
|
+
email:
|
23
|
+
- dev@handlino.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/handicraft/ujs_helper.rb
|
32
|
+
- lib/jquery_for_rjs.rb
|
33
|
+
- LICENSE
|
34
|
+
- README
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://handlino.com
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.7
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Handicraft UJS
|
69
|
+
test_files: []
|
70
|
+
|