Class: Waw::ActionController::JSGeneration

Inherits:
Controller show all
Defined in:
lib/waw/controllers/action/js_generation.rb

Constant Summary collapse

HEADER =

Header of the generated javascript file

<<-EOF
  /* This file is automatically generated by Waw. Any edit will probably be lost
   * next time the application is started. */
EOF

Constants included from EnvironmentUtils

EnvironmentUtils::DEPRECATED_MSG

Instance Method Summary collapse

Methods inherited from Controller

#execute

Methods included from Rack::Delegator

#_visit, #delegate, #find_rack_app, #find_url_of, #has_delegate?, #is_delegate?, #visit

Methods included from EnvironmentUtils

#env, #session_get, #session_has_key?, #session_set, #session_unset

Methods included from ScopeUtils

#config, #find_kernel_context, #logger, #params, #rack_env, #real_session, #request, #resources, #response, #root_folder, #session

Instance Method Details

#call(env) ⇒ Object

Acts as a waw controller



22
23
24
25
26
27
28
# File 'lib/waw/controllers/action/js_generation.rb', line 22

def call(env)
  buffer = []
  generate_js(kernel, buffer)
  [ 200, 
    {'Content-Type' => 'application/javascript'},
    buffer ]
end

#error(msg) ⇒ Object

Raises a ConfigurationError

Raises:



17
18
19
# File 'lib/waw/controllers/action/js_generation.rb', line 17

def error(msg)
  raise ConfigurationError, msg
end

#generate_js(waw_kernel, buffer) ⇒ Object

Generate the javascript code for installed action controllers.



54
55
56
57
58
59
60
61
62
63
# File 'lib/waw/controllers/action/js_generation.rb', line 54

def generate_js(waw_kernel, buffer)
  # 1) header first
  buffer << strip(HEADER, 8) << "\n"
  
  # 2) messages
  generate_js_messages(waw_kernel, buffer)
  
  # 3) controllers
  generate_js_controllers(waw_kernel, buffer)
end

#generate_js_controllers(waw_kernel, buffer) ⇒ Object

Generates the js controllers



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/waw/controllers/action/js_generation.rb', line 83

def generate_js_controllers(waw_kernel, buffer)
  ActionController.controllers.sort{|c1, c2| c1.class.name <=> c2.class.name}.each do |controller|
    # Let find the URL for this controller
    app = waw_kernel.find_rack_app{|a| controller===a}
    url = app ? app.find_url_of(app) : nil
    unless url
      waw_kernel.logger.warn("Skipping #{controller}, which does not seem to be mapped to an URL")
    else
      header = <<-EOF
        /* Actions contributed by #{controller}, mapped to #{url} */
      EOF
      buffer << strip(header, 14)
      controller.actions.keys.sort{|k1, k2| k1.to_s <=> k2.to_s}.each do |name|
        action = controller.actions[name]
        generate_js_for_action(waw_kernel, action, buffer)
      end
    end
  end
end

#generate_js_for_action(waw_kernel, action, buffer) ⇒ Object

Generates the javascript code for a given action



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/waw/controllers/action/js_generation.rb', line 104

def generate_js_for_action(waw_kernel, action, buffer)
  action_js = <<-THEEND
    function #{action.id}(request_data, form) {
      $.ajax({type: "POST", url: "#{action.url}", data: request_data, dataType: "json",
        error: function(data) {
    		  window.location = '/500';
    		},
    		success: function(data) {
  THEEND
  action_js << action.routing.generate_js_routing(action, 0).gsub(/^/, " "*16) << "\n"
  action_js << <<-THEEND
    		}
    	});
    	return false;
    }  
  THEEND
  buffer << strip(action_js, 10)
end

#generate_js_messages(waw_kernel, buffer) ⇒ Object

Generates the javascript code for message resources.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/waw/controllers/action/js_generation.rb', line 66

def generate_js_messages(waw_kernel, buffer)
  if waw_kernel.resources.has_resource?(:messages)
    buffer << "/* Messages, from waw.resources.messages */\n"
    messages = {}
    waw_kernel.resources.messages.each do |name, value|
      messages[name] = value 
    end  
    buffer << "var messages = new Array();\n"
    messages.keys.sort{|k1, k2| k1.to_s <=> k2.to_s}.each do |name|
      value = messages[name]
      buffer << "messages['#{name}'] = \"#{value}\";\n"    
    end
    buffer << "\n"
  end
end

#run(waw_kernel) ⇒ Object

Start hook start callback required by Waw. Generates the javascript code if the configuration variable ‘code_at_startup’ is true.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/waw/controllers/action/js_generation.rb', line 32

def run(waw_kernel)
  return unless waw_kernel.config.code_at_startup
  
  # Now, find the StaticController, that should be installed
  static = waw_kernel.find_rack_app {|app| ::Waw::StaticController===app}
  error("Code generation expects a ::Waw::StaticController being installed.") if static.nil?
  
  # Find the folder now
  folder = File.join(waw_kernel.root_folder, static.public_folder, 'js')
  error("Code generation expects #{folder} to be an existing directory.") unless File.directory?(folder)
  
  # Look for the application name
  appname = waw_kernel.config.knows?(:application_name) ? waw_kernel.config.application_name : 'waw'
  
  # And let's go now!
  file = File.join(folder, "#{appname}_generated.js")
  File.open(file, 'w') do |buffer|
    generate_js(waw_kernel, buffer)
  end
end

#strip(str, n) ⇒ Object

Strip n spaces at the begining of a string



12
13
14
# File 'lib/waw/controllers/action/js_generation.rb', line 12

def strip(str, n)
  str.gsub(/\t/, "  ").gsub(/^\s{#{n}}/, '')
end