Skip to content

Commit

Permalink
Allow resources to be inlined in the HTML. Convert the current bootst…
Browse files Browse the repository at this point in the history
…rap code to a directly inlined JS. This way it can be minified.
  • Loading branch information
Charles Jolley committed Oct 13, 2009
1 parent 5fe5d37 commit 8642d3e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Buildfile
Expand Up @@ -73,6 +73,9 @@ mode :all do
# frameworks automatically if found.
:theme => 'sproutcore/standard_theme',

# use default bootstrap framework
:bootstrap_inline => 'sproutcore/bootstrap:javascript',

:use_packed => true,

# if set to true then the index.html will build into the global language
Expand Down
84 changes: 84 additions & 0 deletions lib/sproutcore/helpers/static_helper.rb
Expand Up @@ -115,6 +115,90 @@ def javascripts_for_client(target_name = nil, opts = {})
urls.join("\n")
end

# Detects and includes any bootstrap code
#
def bootstrap

ret = []

# Reference any external bootstrap scripts
if (resources_names = target.config.bootstrap)
Array(resources_names).each do |resource_name|
ret << %(<script src="#{sc_static(resource_name)}" type="text/javascript" ></script>)
end
end

# Reference any inlined bootstrap scripts
if (resources_names = target.config.bootstrap_inline)
Array(resources_names).each do |resource_name|
ret << inline_javascript(resource_name)
end
end

# Reference any @content_for_bootstrap
if @content_for_bootstrap
ret << %(<script type="text/javascript">\n#{@content_for_bootstrap}\n</script>)
end

return ret * "\n"
end

# Attempts to include the named javascript entry inline to the file
#
# === Options
# language:: the language to use. defaults to current
#
def inline_javascript(resource_name, opts ={})

resource_name = resource_name.to_s

# determine which manifest to search. if a language is explicitly
# specified, lookup manifest for that language. otherwise use
# current manifest.
m = self.manifest
if opts[:language]
m = target.manifest_for(:language => opts[:language]).build!
end

entry = m.find_entry(resource_name, :entry_type => :javascript)
if entry.nil?
entry = m.find_entry(resource_name, :hidden => true, :entry_type => :javascript)
end

return '' if entry.nil?

ret = File.readlines(entry.build!.build_path)*''
return %(<script type="text/javascript">\n#{ret}\n</script>)
end

# Attempts to include the named javascript entry inline to the file
#
# === Options
# language:: the language to use. defaults to current
#
def inline_stylesheet(resource_name, opts ={})

resource_name = resource_name.to_s

# determine which manifest to search. if a language is explicitly
# specified, lookup manifest for that language. otherwise use
# current manifest.
m = self.manifest
if opts[:language]
m = target.manifest_for(:language => opts[:language]).build!
end

entry = m.find_entry(resource_name, :entry_type => :stylesheet)
if entry.nil?
entry = m.find_entry(resource_name, :hidden => true, :entry_type => :stylesheet)
end

return '' if entry.nil?

ret = File.readlines(entry.build!.build_path)
return %(<style>\n#{ret*"\n"}\n</style>)
end

# Attempts to render the named entry as a partial
#
# === Options
Expand Down
4 changes: 2 additions & 2 deletions lib/sproutcore/models/manifest.rb
Expand Up @@ -350,7 +350,7 @@ def find_entry(fragment, opts = {}, seen=nil)
def unique_staging_path(path)
paths = entries(:hidden => true).map { |e| e.staging_path }
while paths.include?(path)
path = path.sub(/(__\$[0-9]+)?(\.\w+)?$/,"__$#{next_staging_uuid}\\2")
path = path.sub(/(__\$[0-9]+)?(\.\w+)?$/,"__#{next_staging_uuid}\\2")
end
return path
end
Expand All @@ -360,7 +360,7 @@ def unique_staging_path(path)
def unique_cache_path(path)
paths = entries(:hidden => true).map { |e| e.cache_path }
while paths.include?(path)
path = path.sub(/(__\$[0-9]+)?(\.\w+)?$/,"__$#{next_staging_uuid}\\2")
path = path.sub(/(__\$[0-9]+)?(\.\w+)?$/,"__#{next_staging_uuid}\\2")
end
return path
end
Expand Down

0 comments on commit 8642d3e

Please sign in to comment.