mack-javascript 0.6.1

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,125 @@
1
+ module Mack
2
+ module JavaScript
3
+ module Framework
4
+ class Jquery
5
+ class << self
6
+ def insert_html(position, id, html)
7
+ insertion = position.to_s.downcase
8
+ insertion = 'append' if insertion == 'bottom'
9
+ insertion = 'prepend' if insertion == 'top'
10
+ "$(\"##{id}\").#{insertion}('#{html}')"
11
+ end
12
+
13
+ def replace_html(id, html)
14
+ insert_html(:html, id, html)
15
+ end
16
+
17
+ def replace(id, html)
18
+ "$(\"##{id}\").replaceWith('#{html}')"
19
+ end
20
+
21
+ def remove(*ids)
22
+ "$(\"##{ids.join(',#')}\").remove()"
23
+ end
24
+
25
+ def show(*ids)
26
+ "$(\"##{ids.join(',#')}\").show()"
27
+ end
28
+
29
+ def hide(*ids)
30
+ "$(\"##{ids.join(',#')}\").hide()"
31
+ end
32
+
33
+ def toggle(*ids)
34
+ "$(\"##{ids.join(',#')}\").toggle()"
35
+ end
36
+
37
+
38
+ # def draggable(id, options = {})
39
+ # record @context.send(:draggable_element_js, id, options)
40
+ # end
41
+ #
42
+ # def visual_effect(name, id = nil, options = {})
43
+ # record @context.send(:visual_effect, name, id, options)
44
+ # end
45
+ #
46
+ # def drop_receiving(id, options = {})
47
+ # record @context.send(:drop_receiving_element_js, id, options)
48
+ # end
49
+ #
50
+ def remote_function(options)
51
+ javascript_options = options_for_ajax(options)
52
+ update = ''
53
+ if options[:update] && options[:update].is_a?(Hash)
54
+ update = []
55
+ update << "success:'#{options[:update][:success]}'" if options[:update][:success]
56
+ update << "failure:'#{options[:update][:failure]}'" if options[:update][:failure]
57
+ update = '{' + update.join(',') + '}'
58
+ elsif options[:update]
59
+ update << "'#{options[:update]}'"
60
+ end
61
+
62
+ function = "$.ajax(#{javascript_options})"
63
+
64
+ function = "#{options[:before]}; #{function}" if options[:before]
65
+ function = "#{function}; #{options[:after]}" if options[:after]
66
+ function = "if (#{options[:condition]}) { #{function}; }" if options[:condition]
67
+ function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm]
68
+ return function
69
+ end
70
+
71
+ protected
72
+ def options_for_ajax(options)
73
+ js_options = build_callbacks(options)
74
+ js_options['url'] = "'#{options[:url]}'"
75
+ js_options['async'] = options[:type] != :synchronous
76
+ js_options['type'] = options[:method] ? "'#{options[:method]}'" : "'post'"
77
+ js_options['dataType'] = options[:datatype] ? "'#{options[:datatype]}'" : (options[:update] ? nil : "'script'")
78
+
79
+ if options[:form]
80
+ js_options['data'] = "$.param($(this).serializeArray())"
81
+ elsif options[:submit]
82
+ js_options['data'] = "$(\"##{options[:submit]}\").serializeArray()"
83
+ elsif options[:with]
84
+ js_options['data'] = options[:with].gsub('Form.serialize(this.form)','$.param($(this.form).serializeArray())')
85
+ end
86
+ options_for_javascript(js_options.reject {|key, value| value.nil?})
87
+ end
88
+
89
+ def options_for_javascript(options)
90
+ '{' + options.map {|k, v| "#{k}:#{v}"}.sort.join(', ') + '}'
91
+ end
92
+
93
+ def build_callbacks(options)
94
+ callbacks = {}
95
+ options[:beforeSend] = '';
96
+ [:uninitialized,:loading,:loaded].each do |key|
97
+ options[:beforeSend] << (options[key].last == ';' ? options.delete(key) : options.delete(key) << ';') if options[key]
98
+ end
99
+ options.delete(:beforeSend) if options[:beforeSend].blank?
100
+ options[:error] = options.delete(:failure) if options[:failure]
101
+ if options[:update]
102
+ options[:success] = build_update(options) << (options[:success] ? options[:success] : '')
103
+ end
104
+ options.each do |callback, code|
105
+ if [:beforeSend, :complete, :error, :success ].include?(callback)
106
+ callbacks[callback] = "function(request){#{code}}"
107
+ end
108
+ end
109
+ callbacks
110
+ end
111
+
112
+ def build_update(options)
113
+ insertion = 'html'
114
+ insertion = options[:position].to_s.downcase if options[:position]
115
+ insertion = 'append' if insertion == 'bottom'
116
+ insertion = 'prepend' if insertion == 'top'
117
+ "$('##{options[:update]}').#{insertion}(request);"
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,112 @@
1
+ module Mack
2
+ module JavaScript
3
+ module Framework
4
+ class Prototype
5
+ @@callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :failure, :success ] +
6
+ [100,101] + (200..206).to_a + (300..307).to_a + (400..417).to_a + (500..505).to_a
7
+ class << self
8
+ def insert_html(position, id, html)
9
+ insertion = position.to_s.camelcase
10
+ "new Insertion.#{insertion}('#{id}', '#{html}')"
11
+ end
12
+
13
+ def replace_html(id, html)
14
+ "Element.update('#{id}', '#{html}')"
15
+ end
16
+
17
+ def replace(id, html)
18
+ "Element.replace('#{id}', '#{html}')"
19
+ end
20
+
21
+ def remove(*ids)
22
+ "#{ids.to_json}.each(Element.remove)"
23
+ end
24
+
25
+ def show(*ids)
26
+ "#{ids.to_json}.each(Element.show)"
27
+ end
28
+
29
+ def toggle(*ids)
30
+ "#{ids.to_json}.each(Element.toggle)"
31
+ end
32
+
33
+ # def draggable(id, options = {})
34
+ # record @context.send(:draggable_element_js, id, options)
35
+ # end
36
+ #
37
+ # def visual_effect(name, id = nil, options = {})
38
+ # record @context.send(:visual_effect, name, id, options)
39
+ # end
40
+ #
41
+ # def drop_receiving(id, options = {})
42
+ # record @context.send(:drop_receiving_element_js, id, options)
43
+ # end
44
+ #
45
+ def remote_function(options)
46
+ javascript_options = options_for_ajax(options)
47
+ "new Ajax.Request('#{options[:url]}', #{javascript_options.to_json})"
48
+ update = ''
49
+ if options[:update] && options[:update].is_a?(Hash)
50
+ update = []
51
+ update << "success:'#{options[:update][:success]}'" if options[:update][:success]
52
+ update << "failure:'#{options[:update][:failure]}'" if options[:update][:failure]
53
+ update = '{' + update.join(',') + '}'
54
+ elsif options[:update]
55
+ update << "'#{options[:update]}'"
56
+ end
57
+
58
+ function = update.empty? ?
59
+ "new Ajax.Request(" :
60
+ "new Ajax.Updater(#{update}, "
61
+
62
+ url_options = options[:url]
63
+ url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash)
64
+ function << "'#{url_options}'"
65
+ function << ", #{javascript_options})"
66
+
67
+ function = "#{options[:before]}; #{function}" if options[:before]
68
+ function = "#{function}; #{options[:after]}" if options[:after]
69
+ function = "if (#{options[:condition]}) { #{function}; }" if options[:condition]
70
+ function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm]
71
+
72
+ return function
73
+ end
74
+
75
+ protected
76
+ def options_for_ajax(options)
77
+ js_options = build_callbacks(options)
78
+ js_options['asynchronous'] = options[:type] != :synchronous
79
+ js_options['method'] = "'#{options[:method]}'" if options[:method]
80
+ js_options['insertion'] = "Insertion.#{options[:position].to_s.camelize}" if options[:position]
81
+ js_options['evalScripts'] = options[:script].nil? || options[:script]
82
+
83
+ if options[:form]
84
+ js_options['parameters'] = 'Form.serialize(this)'
85
+ elsif options[:submit]
86
+ js_options['parameters'] = "Form.serialize('#{options[:submit]}')"
87
+ elsif options[:with]
88
+ js_options['parameters'] = options[:with]
89
+ end
90
+ options_for_javascript(js_options.reject {|key, value| value.nil?})
91
+ end
92
+
93
+ def options_for_javascript(options)
94
+ '{' + options.map {|k, v| "#{k}:#{v}"}.sort.join(', ') + '}'
95
+ end
96
+
97
+ def build_callbacks(options)
98
+ callbacks = {}
99
+ options.each do |callback, code|
100
+ if @@callbacks.include?(callback)
101
+ name = 'on' + callback.to_s.capitalize
102
+ callbacks[name] = "function(request){#{code}}"
103
+ end
104
+ end
105
+ callbacks
106
+ end
107
+
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,57 @@
1
+ module Mack
2
+ module JavaScript
3
+ class ScriptGenerator
4
+ def initialize
5
+ @lines = ''
6
+ end
7
+
8
+ def method_missing(sym, *args)
9
+ self << self.class.framework.send(sym, *args)
10
+ end
11
+
12
+ def <<(s)
13
+ @lines << s + ";"
14
+ end
15
+
16
+ def to_s
17
+ @lines
18
+ end
19
+
20
+ def alert(message)
21
+ self << "alert('#{message}')"
22
+ end
23
+
24
+ def call(*args, &block)
25
+ s = args.shift + '('
26
+ a = []
27
+ args.each {|arg| a << arg.to_json}
28
+ self << s + a.join(',') + ')'
29
+ end
30
+
31
+ def assign(variable, value)
32
+ self << "#{variable} = #{value.to_json}"
33
+ end
34
+
35
+ def delay(seconds = 1, &block)
36
+ self << "setTimeout(function() {\n\n" + yield(Mack::JavaScript::ScriptGenerator.new) + "}, #{(seconds * 1000).to_i})"
37
+ end
38
+
39
+ class << self
40
+
41
+ def framework
42
+ "Mack::JavaScript::Framework::#{framework_name}".constantize
43
+ end
44
+
45
+ def framework=(args)
46
+ @@framework_name = args.camelcase
47
+ end
48
+
49
+ private
50
+ def framework_name
51
+ @@framework_name ||= app_config.mack.js_framework.camelcase
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ module Mack
2
+ module Testing
3
+ module Helpers
4
+ #simulates an ajax request
5
+ def xhr(method, uri, options = {})
6
+ options = {:input => options.to_params} if method == :post || method == :put
7
+ build_response(request.send(method, uri, build_request_options(options.merge({"HTTP_X_REQUESTED_WITH" => 'XMLHttpRequest', "HTTP_ACCEPT" => 'text/javascript, text/html, application/xml, text/xml, */*'}))))
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,33 @@
1
+ module Mack
2
+ module Rendering # :nodoc:
3
+ module Engine # :nodoc:
4
+ # Allows use of the Builder::XmlMarkup engine to be used with rendering.
5
+ class Rjs < Mack::Rendering::Engine::Base
6
+
7
+ def render(io, binding)
8
+ @_jsp_page = Mack::JavaScript::ScriptGenerator.new
9
+ view_template.instance_variable_set("@_jsp_page", @_jsp_page)
10
+ eval(io, binding)
11
+ @_jsp_page.to_s
12
+ end
13
+
14
+ def extension
15
+ :rjs
16
+ end
17
+
18
+ module ViewTemplateHelpers
19
+ def page
20
+ @_jsp_page
21
+ end
22
+ end # ViewTemplateHelpers
23
+
24
+ end # RJS
25
+ end # Engine
26
+ end # Rendering
27
+ end # Mack
28
+
29
+ Mack::Rendering::ViewTemplate.send(:include, Mack::Rendering::Engine::Rjs::ViewTemplateHelpers)
30
+ Mack::Rendering::Engine::Registry.instance.register(:action, :rjs)
31
+ Mack::Rendering::Engine::Registry.instance.register(:template, :rjs)
32
+ Mack::Rendering::Engine::Registry.instance.register(:partial, :rjs)
33
+ Mack::Rendering::Engine::Registry.instance.register(:js, :rjs)
@@ -0,0 +1,33 @@
1
+ module Mack
2
+ module Rendering # :nodoc:
3
+ module Type # :nodoc:
4
+ # Used to render an XML template that's relative to a controller.
5
+ #
6
+ # Example:
7
+ # class UsersController < Mack::Controller::Base
8
+ # # /users/:id
9
+ # def show
10
+ # @user = User.first(params(:id))
11
+ # end
12
+ # # /users
13
+ # def index
14
+ # @users = User.all
15
+ # render(:xml, :list)
16
+ # end
17
+ # end
18
+ # When some calls /users/1.xml the file: app/views/users/show.xml.builder will be rendered.
19
+ # When some calls /users.xml the file: app/views/users/list.xml.builder will be rendered.
20
+ class Js < Mack::Rendering::Type::FileBase
21
+
22
+ # See Mack::Rendering::Type::FileBase render_file for more information.
23
+ def render
24
+ self.options[:format] = "js"
25
+ self.controller.response["Content-Type"] = Mack::Utils::MimeTypes[self.options[:format]]
26
+ x_file = File.join(self.controller_view_path, "#{self.render_value}.#{self.options[:format]}")
27
+ render_file(x_file, :js)
28
+ end
29
+
30
+ end # Xml
31
+ end # Type
32
+ end # Rendering
33
+ end # Mack
@@ -0,0 +1,42 @@
1
+ module Mack
2
+ module ViewHelpers
3
+ module HtmlHelpers
4
+ # Renders javascript in an html file
5
+ #
6
+ # Example:
7
+ # <%= update_page do |page|
8
+ # page.alert('this gets translated to js!')
9
+ # page.show('my_hidden_div')
10
+ # end
11
+ # %>
12
+ #This returns (assuming Prototype is your Js Framework):
13
+ # <script type='text/javascript'>
14
+ # alert('this gets translated to js!');
15
+ # $('my_hidden_div').show();
16
+ # </script>
17
+ def update_page(&block)
18
+ sg = Mack::JavaScript::ScriptGenerator.new
19
+ yield(sg)
20
+ "<script type='#{Mack::Utils::MimeTypes[:js]}'>\n" + sg.to_s.gsub(';', ";\n") + "</script>"
21
+ end
22
+
23
+ def link_to_remote(link_text, ajax_options, options = {})
24
+ link_to_function(link_text, remote_function(ajax_options), options)
25
+ end
26
+
27
+ def link_to_function(link_text, *args, &block)
28
+ function = args[0] || ''
29
+ options = args[1] || {}
30
+ function = yield(Mack::JavaScript::ScriptGenerator.new).chop if block_given?
31
+ options[:onclick] = (options[:onclick] ? "#{options[:onclick]}; " : "") + "#{function}; return false;"
32
+ link_to(link_text, '#', options)
33
+ end
34
+
35
+ # Returns javascript that does an Ajax call
36
+ def remote_function(options)
37
+ Mack::JavaScript::ScriptGenerator.framework.remote_function(options)
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ module Mack
2
+ module ViewHelpers
3
+ module StringHelpers
4
+
5
+ # Escapes Javascript
6
+ #
7
+ def escape_javascript(javascript)
8
+ (javascript || '').gsub('\\','\0\0').gsub('</','<\/').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
9
+ end
10
+
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mack-javascript
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.1
5
+ platform: ruby
6
+ authors:
7
+ - Gerardo Pis-Lopez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-04 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json_pure
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.2
24
+ version:
25
+ description: RJS support for the Mack Framework
26
+ email:
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/mack-javascript/generators/javascript_generator.rb
35
+ - lib/mack-javascript/generators/manifest.yml
36
+ - lib/mack-javascript/generators/templates/javascripts/controls.js.template
37
+ - lib/mack-javascript/generators/templates/javascripts/dragdrop.js.template
38
+ - lib/mack-javascript/generators/templates/javascripts/effects.js.template
39
+ - lib/mack-javascript/generators/templates/javascripts/jquery-fx.js.template
40
+ - lib/mack-javascript/generators/templates/javascripts/jquery-ui.js.template
41
+ - lib/mack-javascript/generators/templates/javascripts/jquery.js.template
42
+ - lib/mack-javascript/generators/templates/javascripts/prototype.js.template
43
+ - lib/mack-javascript/helpers/jquery_helper.rb
44
+ - lib/mack-javascript/helpers/prototype_helper.rb
45
+ - lib/mack-javascript/helpers/script_generator.rb
46
+ - lib/mack-javascript/helpers/testing_helpers.rb
47
+ - lib/mack-javascript/rendering/engine/rjs.rb
48
+ - lib/mack-javascript/rendering/type/js.rb
49
+ - lib/mack-javascript/view_helpers/html_helpers.rb
50
+ - lib/mack-javascript/view_helpers/string_helpers.rb
51
+ - lib/mack-javascript.rb
52
+ - README
53
+ has_rdoc: true
54
+ homepage: http://www.mackframework.com
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: magrathea
76
+ rubygems_version: 1.2.0
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: JavaScript in Mack
80
+ test_files: []
81
+