pager-jrails 0.3.20080506

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.
@@ -0,0 +1,174 @@
1
+ /*
2
+ *
3
+ * jRails ajax extras
4
+ * version 0.1
5
+ * <aaron@ennerchi.com> | http://www.ennerchi.com
6
+ *
7
+ */
8
+
9
+ (function($) {
10
+ $().ajaxSend(function(a, xhr, s){ //Set request headers globally
11
+ xhr.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
12
+ });
13
+ })(jQuery);
14
+
15
+ /*
16
+ *
17
+ * jRails form observer plugin
18
+ * version 0.2
19
+ * <aaron@ennerchi.com> | http://www.ennerchi.com
20
+ *
21
+ */
22
+
23
+ (function($) {
24
+ $.extend({ // Translate field to event
25
+ fieldEvent: function(el, obs) {
26
+ var field = el[0] || el, e = 'change';
27
+ if (field.type == 'radio' || field.type == 'checkbox') e = 'click';
28
+ else if (obs && field.type == 'text' || field.type == 'textarea') e = 'keyup';
29
+ return e;
30
+ }
31
+ });
32
+ $.fn.extend({ // Delayed observer for fields and forms
33
+ delayedObserver: function(delay, callback){
34
+ var el = $(this);
35
+ if (typeof window.delayedObserverStack == 'undefined') window.delayedObserverStack = [];
36
+ if (typeof window.delayedObserverCallback == 'undefined') {
37
+ window.delayedObserverCallback = function(stackPos) {
38
+ observed = window.delayedObserverStack[stackPos];
39
+ if (observed.timer) clearTimeout(observed.timer);
40
+ observed.timer = setTimeout(function(){
41
+ observed.timer = null;
42
+ observed.callback(observed.obj, observed.obj.formVal());
43
+ }, observed.delay * 1000);
44
+ observed.oldVal = observed.obj.formVal();
45
+ }
46
+ }
47
+ window.delayedObserverStack.push({
48
+ obj: el, timer: null, delay: delay,
49
+ oldVal: el.formVal(), callback: callback
50
+ });
51
+ var stackPos = window.delayedObserverStack.length-1;
52
+ if (el[0].tagName == 'FORM') {
53
+ $(':input', el).each(function(){
54
+ var field = $(this);
55
+ field.bind($.fieldEvent(field, delay), function(){
56
+ observed = window.delayedObserverStack[stackPos];
57
+ if (observed.obj.formVal() == observed.obj.oldVal) return;
58
+ else window.delayedObserverCallback(stackPos);
59
+ });
60
+ });
61
+ } else {
62
+ el.bind($.fieldEvent(el, delay), function(){
63
+ observed = window.delayedObserverStack[stackPos];
64
+ if (observed.obj.formVal() == observed.obj.oldVal) return;
65
+ else window.delayedObserverCallback(stackPos);
66
+ });
67
+ };
68
+ },
69
+ formVal: function() { // Gets form values
70
+ var el = this[0];
71
+ if(el.tagName == 'FORM') return this.serialize();
72
+ if(el.type == 'checkbox' || self.type == 'radio') return this.filter('input:checked').val() || '';
73
+ else return this.val();
74
+ }
75
+ });
76
+ })(jQuery);
77
+
78
+ /*
79
+ *
80
+ * jRails visual effects stubs
81
+ * version 0.1
82
+ * <aaron@ennerchi.com> | http://www.ennerchi.com
83
+ *
84
+ */
85
+
86
+ (function($) {
87
+ $.fn.extend({
88
+ visualEffect : function(o) {
89
+ return this.effect(o);
90
+ },
91
+ Appear : function(speed, callback) {
92
+ return this.fadeIn(speed, callback);
93
+ },
94
+ BlindDown : function(speed, callback) {
95
+ this.show('blind', { direction: 'vertical' }, speed, callback);
96
+ return this;
97
+ },
98
+ BlindUp : function(speed, callback) {
99
+ this.hide('blind', { direction: 'vertical' }, speed, callback);
100
+ return this;
101
+ },
102
+ BlindRight : function(speed, callback) {
103
+ this.show('blind', { direction: 'horizontal' }, speed, callback);
104
+ return this;
105
+ },
106
+ BlindLeft : function(speed, callback) {
107
+ this.hide('blind', { direction: 'horizontal' }, speed, callback);
108
+ return this;
109
+ },
110
+ DropOut : function(speed, callback) {
111
+ this.hide('drop', {direction: 'down' }, speed, callback);
112
+ return this;
113
+ },
114
+ DropIn : function(speed, callback) {
115
+ this.show('drop', { direction: 'up' }, speed, callback);
116
+ return this;
117
+ },
118
+ Fade : function(speed, callback) {
119
+ return this.fadeOut(speed, callback);
120
+ },
121
+ Fold : function(speed, callback) {
122
+ this.hide('fold', {}, speed, callback);
123
+ return this;
124
+ },
125
+ FoldOut : function(speed, callback) {
126
+ this.show('fold', {}, speed, callback);
127
+ return this;
128
+ },
129
+ Grow : function(speed, callback) {
130
+ this.show('scale', {}, speed, callback);
131
+ return this;
132
+ },
133
+ Highlight : function(speed, callback) {
134
+ this.show('highlight', {}, speed, callback);
135
+ return this;
136
+ },
137
+ Puff : function(speed, callback) {
138
+ this.hide('puff', {}, speed, callback);
139
+ return this;
140
+ },
141
+ Pulsate : function(speed, callback) {
142
+ this.show('pulsate', {}, speed, callback);
143
+ return this;
144
+ },
145
+ Shake : function(speed, callback) {
146
+ this.show('shake', {}, speed, callback);
147
+ return this;
148
+ },
149
+ Shrink : function(speed, callback) {
150
+ this.hide('scale', {}, speed, callback);
151
+ return this;
152
+ },
153
+ Squish : function(speed, callback) {
154
+ this.hide('scale', { origin: ['top', 'left'] }, speed, callback);
155
+ return this;
156
+ },
157
+ SlideUp : function(speed, callback) {
158
+ this.hide('slide', { direction: 'up'}, speed, callback);
159
+ return this;
160
+ },
161
+ SlideDown : function(speed, callback) {
162
+ this.show('slide', { direction: 'up'}, speed, callback);
163
+ return this;
164
+ },
165
+ SwitchOff : function(speed, callback) {
166
+ this.hide('clip', {}, speed, callback);
167
+ return this;
168
+ },
169
+ SwitchOn : function(speed, callback) {
170
+ this.show('clip', {}, speed, callback);
171
+ return this;
172
+ }
173
+ });
174
+ })(jQuery);
data/lib/jrails.rb ADDED
@@ -0,0 +1,284 @@
1
+ module ActionView
2
+ module Helpers
3
+ module PrototypeHelper
4
+ unless const_defined? :JQCALLBACKS
5
+ JQCALLBACKS = Set.new([ :beforeSend, :complete, :error, :success ] + (100..599).to_a)
6
+ AJAX_OPTIONS = Set.new([ :before, :after, :condition, :url,
7
+ :asynchronous, :method, :insertion, :position,
8
+ :form, :with, :update, :script ]).merge(JQCALLBACKS)
9
+ end
10
+
11
+ def periodically_call_remote(options = {})
12
+ frequency = options[:frequency] || 10 # every ten seconds by default
13
+ code = "setInterval(function() {#{remote_function(options)}}, #{frequency} * 1000)"
14
+ javascript_tag(code)
15
+ end
16
+
17
+ def remote_function(options)
18
+ javascript_options = options_for_ajax(options)
19
+
20
+ update = ''
21
+ if options[:update] && options[:update].is_a?(Hash)
22
+ update = []
23
+ update << "success:'#{options[:update][:success]}'" if options[:update][:success]
24
+ update << "failure:'#{options[:update][:failure]}'" if options[:update][:failure]
25
+ update = '{' + update.join(',') + '}'
26
+ elsif options[:update]
27
+ update << "'#{options[:update]}'"
28
+ end
29
+
30
+ function = "$.ajax(#{javascript_options})"
31
+
32
+ function = "#{options[:before]}; #{function}" if options[:before]
33
+ function = "#{function}; #{options[:after]}" if options[:after]
34
+ function = "if (#{options[:condition]}) { #{function}; }" if options[:condition]
35
+ function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm]
36
+ return function
37
+ end
38
+
39
+ class JavaScriptGenerator
40
+ module GeneratorMethods
41
+
42
+ def insert_html(position, id, *options_for_render)
43
+ insertion = position.to_s.downcase
44
+ insertion = 'append' if insertion == 'bottom'
45
+ insertion = 'prepend' if insertion == 'top'
46
+ call "$(\"##{id}\").#{insertion}", render(*options_for_render)
47
+ end
48
+
49
+ def replace_html(id, *options_for_render)
50
+ insert_html(:html, id, *options_for_render)
51
+ end
52
+
53
+ def replace(id, *options_for_render)
54
+ call "$(\"##{id}\").replaceWith", render(*options_for_render)
55
+ end
56
+
57
+ def remove(*ids)
58
+ call "$(\"##{ids.join(',#')}\").remove"
59
+ end
60
+
61
+ def show(*ids)
62
+ call "$(\"##{ids.join(',#')}\").show"
63
+ end
64
+
65
+ def hide(*ids)
66
+ call "$(\"##{ids.join(',#')}\").hide"
67
+ end
68
+
69
+ def toggle(*ids)
70
+ call "$(\"##{ids.join(',#')}\").toggle"
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ protected
77
+ def options_for_ajax(options)
78
+ js_options = build_callbacks(options)
79
+
80
+ url_options = options[:url]
81
+ url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash)
82
+ js_options['url'] = "'#{url_for(url_options)}'"
83
+ js_options['async'] = options[:type] != :synchronous
84
+ js_options['url'] = "'#{url_for(url_options)}'"
85
+ js_options['async'] = options[:type] != :synchronous
86
+ js_options['type'] = options[:method] ? method_option_to_s(options[:method]) : ( options[:form] ? "'post'" : nil )
87
+ js_options['dataType'] = options[:datatype] ? "'#{options[:datatype]}'" : (options[:update] ? nil : "'script'")
88
+
89
+ if options[:form]
90
+ js_options['data'] = "$.param($(this).serializeArray())"
91
+ elsif options[:submit]
92
+ js_options['data'] = "$(\"##{options[:submit]}\").serializeArray()"
93
+ elsif options[:with]
94
+ js_options['data'] = options[:with].gsub('Form.serialize(this.form)','$.param($(this.form).serializeArray())')
95
+ end
96
+
97
+ if respond_to?('protect_against_forgery?') && protect_against_forgery?
98
+ if js_options['data']
99
+ js_options['data'] << " + '&"
100
+ else
101
+ js_options['data'] = "'"
102
+ end
103
+ js_options['data'] << "#{request_forgery_protection_token}=' + encodeURIComponent('#{escape_javascript form_authenticity_token}')"
104
+ end
105
+
106
+ options_for_javascript(js_options.reject {|key, value| value.nil?})
107
+ end
108
+
109
+ def build_update(options)
110
+ insertion = 'html'
111
+ insertion = options[:position].to_s.downcase if options[:position]
112
+ insertion = 'append' if insertion == 'bottom'
113
+ insertion = 'prepend' if insertion == 'top'
114
+ "$('##{options[:update]}').#{insertion}(request);"
115
+ end
116
+
117
+ def build_observer(klass, name, options = {})
118
+ if options[:with] && (options[:with] !~ /[\{=(.]/)
119
+ options[:with] = "'#{options[:with]}=' + value"
120
+ else
121
+ options[:with] ||= 'value' unless options[:function]
122
+ end
123
+
124
+ callback = options[:function] || remote_function(options)
125
+ javascript = "$(\"##{name}\").delayedObserver("
126
+ javascript << "#{options[:frequency] || 0}, "
127
+ javascript << "function(element, value) {"
128
+ javascript << "#{callback}}"
129
+ #javascript << ", '#{options[:on]}'" if options[:on]
130
+ javascript << ")"
131
+ javascript_tag(javascript)
132
+ end
133
+
134
+ def build_callbacks(options)
135
+ callbacks = {}
136
+ options[:beforeSend] = '';
137
+ [:uninitialized,:loading,:loaded].each do |key|
138
+ options[:beforeSend] << (options[key].last == ';' ? options.delete(key) : options.delete(key) << ';') if options[key]
139
+ end
140
+ options.delete(:beforeSend) if options[:beforeSend].blank?
141
+ options[:error] = options.delete(:failure) if options[:failure]
142
+ if options[:update]
143
+ options[:success] = build_update(options) << (options[:success] ? options[:success] : '')
144
+ end
145
+ options.each do |callback, code|
146
+ if JQCALLBACKS.include?(callback)
147
+ callbacks[callback] = "function(request){#{code}}"
148
+ end
149
+ end
150
+ callbacks
151
+ end
152
+
153
+ end
154
+
155
+ class JavaScriptElementProxy < JavaScriptProxy #:nodoc:
156
+ def initialize(generator, id)
157
+ @id = id
158
+ super(generator, "$(\"##{id}\")")
159
+ end
160
+
161
+ def replace_html(*options_for_render)
162
+ call 'html', @generator.send(:render, *options_for_render)
163
+ end
164
+
165
+ def replace(*options_for_render)
166
+ call 'replaceWith', @generator.send(:render, *options_for_render)
167
+ end
168
+ end
169
+
170
+ class JavaScriptElementCollectionProxy < JavaScriptCollectionProxy #:nodoc:\
171
+ def initialize(generator, pattern)
172
+ super(generator, "$(#{pattern.to_json})")
173
+ end
174
+ end
175
+
176
+ module ScriptaculousHelper
177
+
178
+ unless const_defined? :TOGGLE_EFFECTS
179
+ TOGGLE_EFFECTS = [:toggle_appear, :toggle_slide, :toggle_blind]
180
+ end
181
+
182
+ unless const_defined? :SCRIPTACULOUS_EFFECTS
183
+ SCRIPTACULOUS_EFFECTS = {
184
+ :appear => {:method => 'fade', :options => {:mode => 'show'}},
185
+ :blind_down => {:method => 'blind', :options => {:direction => 'vertical', :mode => 'show'}},
186
+ :blind_up => {:method => 'blind', :options => {:direction => 'vertical', :mode => 'hide'}},
187
+ :blind_right => {:method => 'blind', :options => {:direction => 'horizontal', :mode => 'show'}},
188
+ :blind_left => {:method => 'blind', :options => {:direction => 'horizontal', :mode => 'hide'}},
189
+ :bounce_in => {:method => 'bounce', :options => {:direction => 'up', :mode => 'show'}},
190
+ :bounce_out => {:method => 'bounce', :options => {:direction => 'up', :mode => 'hide'}},
191
+ :drop_in => {:method => 'drop', :options => {:direction => 'up', :mode => 'show'}},
192
+ :drop_out => {:method => 'drop', :options => {:direction => 'down', :mode => 'hide'}},
193
+ :fold_in => {:method => 'fold', :options => {:mode => 'hide'}},
194
+ :fold_out => {:method => 'fold', :options => {:mode => 'show'}},
195
+ :grow => {:method => 'scale', :options => {:mode => 'show'}},
196
+ :shrink => {:method => 'scale', :options => {:mode => 'hide'}},
197
+ :slide_down => {:method => 'slide', :options => {:direction => 'up', :mode => 'show'}},
198
+ :slide_up => {:method => 'slide', :options => {:direction => 'up', :mode => 'hide'}},
199
+ :slide_right => {:method => 'slide', :options => {:direction => 'left', :mode => 'show'}},
200
+ :slide_left => {:method => 'slide', :options => {:direction => 'left', :mode => 'hide'}},
201
+ :squish => {:method => 'scale', :options => {:origin => '["top","left"]', :mode => 'hide'}},
202
+ :switch_on => {:method => 'clip', :options => {:direction => 'vertical', :mode => 'show'}},
203
+ :switch_off => {:method => 'clip', :options => {:direction => 'vertical', :mode => 'hide'}}
204
+ }
205
+ end
206
+
207
+ def visual_effect(name, element_id = false, js_options = {})
208
+ element = element_id ? element_id : "this"
209
+
210
+ if SCRIPTACULOUS_EFFECTS.has_key? name.to_sym
211
+ effect = SCRIPTACULOUS_EFFECTS[name.to_sym]
212
+ name = effect[:method]
213
+ js_options = js_options.merge effect[:options]
214
+ end
215
+
216
+ [:color, :direction, :mode].each do |option|
217
+ js_options[option] = "\"#{js_options[option]}\"" if js_options[option]
218
+ end
219
+
220
+ if js_options.has_key? :duration
221
+ speed = js_options.delete :duration
222
+ speed = (speed * 1000).to_i unless speed.nil?
223
+ else
224
+ speed = js_options.delete :speed
225
+ end
226
+
227
+ #if TOGGLE_EFFECTS.include? name.to_sym
228
+ # "Effect.toggle(#{element},'#{name.to_s.gsub(/^toggle_/,'')}',#{options_for_javascript(js_options)});"
229
+
230
+ javascript = "$(\"##{element_id}\").effect(\"#{name.to_s.downcase}\""
231
+ javascript << ",#{options_for_javascript(js_options)}" unless speed.nil? && js_options.empty?
232
+ javascript << ",#{speed}" unless speed.nil?
233
+ javascript << ")"
234
+
235
+ end
236
+
237
+ def sortable_element_js(element_id, options = {}) #:nodoc:
238
+ #convert similar attributes
239
+ options[:items] = options[:only] if options[:only]
240
+
241
+ if options[:onUpdate] || options[:url]
242
+ options[:with] ||= "$(this).sortable('serialize')"
243
+ options[:onUpdate] ||= "function(){" + remote_function(options) + "}"
244
+ end
245
+
246
+ options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) }
247
+ options[:update] = options.delete(:onUpdate) if options[:onUpdate]
248
+
249
+ [:handle].each do |option|
250
+ options[option] = "'#{options[option]}'" if options[option]
251
+ end
252
+
253
+ options[:containment] = array_or_string_for_javascript(options[:containment]) if options[:containment]
254
+ options[:items] = array_or_string_for_javascript(options[:items]) if options[:items]
255
+
256
+ %($("##{element_id}").sortable(#{options_for_javascript(options)});)
257
+ end
258
+
259
+ def draggable_element_js(element_id, options = {})
260
+ %($("##{element_id}").draggable(#{options_for_javascript(options)});)
261
+ end
262
+
263
+ def drop_receiving_element_js(element_id, options = {})
264
+ #convert similar options
265
+ options[:hoverClass] = options.delete(:hoverclass) if options[:hoverclass]
266
+ options[:drop] = options.delete(:onDrop) if options[:onDrop]
267
+
268
+ if options[:drop] || options[:url]
269
+ options[:with] ||= "'id=' + encodeURIComponent($(ui.draggable).clone().attr('id'))"
270
+ options[:drop] ||= "function(ev, ui){" + remote_function(options) + "}"
271
+ end
272
+
273
+ options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) }
274
+
275
+ options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept]
276
+ options[:hoverClass] = "'#{options[:hoverClass]}'" if options[:hoverClass]
277
+
278
+ %($("##{element_id}").droppable(#{options_for_javascript(options)});)
279
+ end
280
+
281
+ end
282
+
283
+ end
284
+ end
@@ -0,0 +1,3 @@
1
+ ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES = ['jquery','jquery-ui','jquery-fx','jrails']
2
+ ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
3
+ require 'jrails'
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'pager-jrails'
data/tasks/jrails.rake ADDED
@@ -0,0 +1,19 @@
1
+ namespace :jrails do
2
+ namespace :update do
3
+ desc "Copies the jQuery and jRails javascripts to public/javascripts"
4
+ task :javascripts do
5
+ puts "Copying files..."
6
+ project_dir = RAILS_ROOT + '/public/javascripts/'
7
+ scripts = Dir[File.join(File.dirname(__FILE__), '..') + '/javascripts/*.js']
8
+ FileUtils.cp(scripts, project_dir)
9
+ puts "files copied successfully."
10
+ end
11
+ end
12
+
13
+ namespace :install do
14
+ desc "Installs the jQuery and jRails javascripts to public/javascripts"
15
+ task :javascripts do
16
+ Rake::Task['jrails:update:javascripts'].invoke
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pager-jrails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.20080506
5
+ platform: ruby
6
+ authors:
7
+ - aaronchi@gmail.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: avanie@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - README.GEM
25
+ - CHANGELOG
26
+ files:
27
+ - CHANGELOG
28
+ - install.rb
29
+ - javascripts/jquery-fx.js
30
+ - javascripts/jquery-ui.js
31
+ - javascripts/jquery.js
32
+ - javascripts/jrails.js
33
+ - javascripts/sources/jquery-fx.js
34
+ - javascripts/sources/jquery-ui.js
35
+ - javascripts/sources/jrails.js
36
+ - lib/jrails.rb
37
+ - lib/pager-jrails.rb
38
+ - rails/init.rb
39
+ - README
40
+ - README.GEM
41
+ - tasks/jrails.rake
42
+ has_rdoc: true
43
+ homepage: http://github.com/pager/jrails
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --main
47
+ - README.GEM
48
+ - README
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.0.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Gemified jRails plugin
70
+ test_files: []
71
+