Module: Raw::ScriptFilter

Extended by:
JavascriptHelper
Defined in:
lib/raw/compiler/filter/script.rb

Overview

Generates client-side javascript code. DOES NOT WORK AT THE MOMENT, have to convert this.

Class Method Summary collapse

Class Method Details

.transform(text, compiler) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/raw/compiler/filter/script.rb', line 13

def self.transform(text, compiler)
  begin
    client = constant("#{compiler.controller}::Client")    
  rescue NameError => ex 
    client = nil
  end

  if client
    client.send :include, Nitro::ScriptGenerator
    client = client.new
    
    functions = {}
    params = {}
        
    text.scan(/__nc_(.*)\((.*)\)/) do |match|
      functions[match.first] = true
      params[match.last] = true
    end    
    
    script = ''
    
    for fun in functions.keys
      begin
        client.buffer = ''
        ret = client.send(fun.to_sym)
        
        # the function code is accumulated in the client 
        # buffer.
        
        function_code = client.buffer
        
        # if the client action returns a String append it to
        # the function code.
        
        function_code << ret if ret.is_a?(String)
        
        script << %{
          function __nc_#{fun}(params) {
            #{function_code}
          }
        }
      rescue Object => ex
        Logger.debug ex.inspect if $DBG
      end
    end

  end
  
  # Inject css in the head tag.
  
  if css_buffer = compiler.shared[:css_buffer]
    text.sub!(/<\/head>/) do |match|
      %{
        <style>
          #{css_buffer}
        </style>
      </head>
      }
    end
  end

  # Inject required javascript files in the head tag.

  required_files = Javascript.required_files
  if crf = compiler.shared[:js_required_files]
    required_files.concat(crf.keys)
  end
  
  unless required_files.empty?
    text.sub!(/<\/head>/) do |match|
      %{
        #{include_script *required_files}
      </head>
      }
    end
  end
  
  # Inject javascript just before the end of the
  # template.
  #--
  # gmosx: injection happens at the end, so that the DOM
  # tree is created. Dunno if this is valid xhtml though.
  #++

  js_buffer = compiler.shared[:js_buffer]
  
  if script or js_buffer
    text.sub!(/<\/body>/) do |match|
      %{
        <script type="text/javascript">
          #{script}
          #{js_buffer}
        </script>
      </body>
      }
    end
  end
  
  return text
end