chriswarren / include_google_js

Use Google's copies of major Javascript libraries instead of the local copies.

include_google_js / lib / include_google_js.rb
100644 241 lines (214 sloc) 9.007 kb
1
2
3
4
5
6
7
8
9
10
11
12
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
module IncludeGoogleJs
  require 'ping'
  
  @@default_javascripts = ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES.dup
  @@google_js_libs = %w[prototype scriptaculous jquery jquery-ui mootools dojo yui swfobject]
  @@scriptaculous_files = %w[scriptaculous builder controls dragdrop effects slider sound unittest]
  @@default_google_js_libs = %w[prototype scriptaculous]
  
  def self.included(base)
    base.alias_method_chain :javascript_include_tag, :google_js
  end
  
  def javascript_include_tag_with_google_js(*sources)
    # split apart the sources, check for :cache, confirm that we're using :include_google_js, and grab :versions
    options = sources.extract_options!.stringify_keys
    use_cache = options.delete("cache")
    javascript_versions = options.delete("versions") || {}
    use_google_js = options.delete("include_google_js") && IncludeGoogleJs.confirm_internet_connection
    
    if ActionController::Base.perform_caching && use_cache # Using the locally cached libraries
      joined_javascript_name = (use_cache == true ? "all" : use_cache) + ".js"
      joined_javascript_path = File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, joined_javascript_name)
 
      write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources))
      return javascript_src_tag(joined_javascript_name, options)
    else
      libraries = IncludeGoogleJs.expand_javascript_sources(sources, use_google_js)
      base_html = libraries["local"].collect { |source| javascript_src_tag(source, options) }.join("\n") # local JS files
      if use_google_js
        html = IncludeGoogleJs.google_script_tags(libraries["google"],javascript_versions)
        html += base_html
      else
        html = base_html
      end
      return html
    end
  end
 
  def self.expand_javascript_sources(sources,use_google_libraries)
    local_javascript_files = []
    google_javascript_files = []
    if sources.include?(:all) # All libraries, get everything in the javascripts folder, see which are hosted by Google
      if use_google_libraries
        local_javascript_files += IncludeGoogleJs.all_non_hosted_local_javascript_files
        google_javascript_files += IncludeGoogleJs.all_google_hosted_local_javascript_files
      else
        local_javascript_files += IncludeGoogleJs.all_local_javascript_files
        google_javascript_files += []
      end
    elsif sources.include?(:defaults)
      if use_google_libraries
        local_javascript_files += %w[application]
        google_javascript_files += IncludeGoogleJs.default_sources
      else
        local_javascript_files += @@default_javascripts + %w[application]
        google_javascript_files += []
      end
    else
      sources = IncludeGoogleJs.check_for_dependencies(sources)
      sources.collect do |source|
        if IncludeGoogleJs.does_google_host?(source) && use_google_libraries
          google_javascript_files << source
        else
          local_javascript_files << source
        end
      end
    end
    return { "google" => google_javascript_files, "local" => local_javascript_files }
  end
  
  def self.check_for_dependencies(source)
    if source.include?("scriptaculous") && !source.include?("prototype")
      source.unshift("prototype")
    elsif source.include?("jquery-ui") && !source.include?("jquery")
      source.unshift("jquery")
    end
    return source
  end
  
  def self.determine_source(source, collection)
    case source
    when Symbol
      collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}")
    else
      source
    end
  end
  
  def self.google_script_tags(libraries,versions)
    html = %Q{
<script src='http://www.google.com/jsapi'></script>
<script>
}
      libraries.each do |js_lib|
      version = versions.has_key?(js_lib.to_sym) ? versions.fetch(js_lib.to_sym) : IncludeGoogleJs.get_file_version(js_lib)
      html += %Q{google.load("#{js_lib}", "#{version}");
}
    end
    html += %Q{</script>
}
  end
  
  def self.all_local_javascript_files
    Dir[File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).gsub(/\.\w+$/, '') }.sort
  end
  
  def self.all_google_hosted_local_javascript_files
    all_javascript_files = IncludeGoogleJs.all_local_javascript_files
    all_javascript_files.delete_if { |file| !IncludeGoogleJs.does_google_host?(file) }
    return all_javascript_files
  end
  
  def self.all_non_hosted_local_javascript_files
    all_javascript_files = IncludeGoogleJs.all_local_javascript_files
    all_javascript_files.delete_if { |file| IncludeGoogleJs.does_google_host?(file) }
    all_javascript_files.delete_if { |file| @@scriptaculous_files.include?(file) }
    return all_javascript_files
  end
  
  def self.remove_google_hosted_libraries_from(array)
    google_libraries = []
    array.each do |library|
      google_libraries << library if IncludeGoogleJs.does_google_host?(library)
    end
    google_libraries.each do |library|
      array.delete(library)
    end
    return array
  end
  
  def self.does_google_host?(file)
    @@google_js_libs.include?(file)
  end
  
  def self.default_sources
    sources = []
    sources += @@default_google_js_libs
    return sources
  end
  
  def self.confirm_internet_connection(url="ajax.googleapis.com")
    Ping.pingecho(url,5,80) # --> true or false
  end
  
  def self.get_file_version(file)
    version = "1"
    return IncludeGoogleJs.send("parse_#{file.gsub("-","_")}")
  end
  
  def self.parse_prototype(file="prototype")
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js"))
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js")).each do |line|
        return line.match(/[\d.]+/)[0] if line.include?("Version")
      end
    else
      return nil
    end
  end
  
  def self.parse_scriptaculous(file="scriptaculous")
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js"))
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js")).each do |line|
        return line.match(/scriptaculous.js v([\d.]+)/i)[1]
      end
    else
      return nil
    end
  end
  
  def self.parse_jquery(file="jquery")
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js"))
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js")).each do |line|
        version_array = line.scan(/jquery:\W?"([\d.]+)"/x).to_s
        return version_array.to_s unless version_array.blank?
      end
    else
      return nil
    end
  end
  
  def self.parse_jquery_ui(file="jquery-ui")
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js"))
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js")).each do |line|
        version_array = line.scan(/version:\W?"([\d.]+)"/x).to_s
        return version_array.to_s unless version_array.blank?
      end
    else
      return nil
    end
  end
  
  def self.parse_mootools(file="mootools")
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js"))
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js")).each do |line|
        return line.match(/version:["|']([\d\.]+)["|']/i)[1] if line.include?("version")
      end
    else
      return nil
    end
  end
  
  def self.parse_dojo(file="dojo")
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js"))
      version = nil
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "#{file}.js")).each do |line|
        match = line.scan(/\b[major|minor|patch]{5,}:([\d]+)/i)
        if match.size > 0
          version = match.shift.to_s
          match.each do |x|
            version += "."+x.to_s
          end
          return version
        end
      end
    else
      return nil
    end
  end
  
  def self.parse_yui
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "yui/build/yuiloader/yuiloader-min.js"))
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "yui/build/yuiloader/yuiloader-min.js")).each do |line|
        return line.match(/^version: ([\d.]+)/i)[1] if line.match(/^version: ([\d.]+)/i)
      end
    else
      return nil
    end
  end
  
  def self.parse_swfobject
    if File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "yui/build/yuiloader/yuiloader-min.js"))
      File.open(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, "swfobject/swfobject.js")).each do |line|
        return line.match(/SWFObject v([\d\.]+)/i)[1] if line.match(/SWFObject v([\d\.]+)/i)
      end
    else
      return nil
    end
  end
  
end