Module: SC::Helpers::StaticHelper

Included in:
Builder::Html
Defined in:
lib/sproutcore/helpers/static_helper.rb

Instance Method Summary collapse

Instance Method Details

#javascripts_for_client(target_name = nil, opts = {}) ⇒ Object

This method will return the HTML to link to all the javascripts required by the client. If you pass no options, the current client will be used.

client_name = the name of the client to render or nil to use the current :language => the language to render. defaults to @language.



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
# File 'lib/sproutcore/helpers/static_helper.rb', line 78

def javascripts_for_client(target_name = nil, opts = {})

  # normalize params
  if target_name.kind_of?(Hash) && opts.nil?
    opts = target_name
    target_name = nil
  end
  opts = {} if opts.nil?

  # process options
  t = target_name ? target.target_for(target_name) : target

  # collect urls from entries
  urls = []
  combine_javascript = t.config.combine_javascript
  combined_entries(t, opts, 'javascript.js', 'javascript-packed.js') do |cur_target, cur_entry|
    
    # include either the entry URL or URL of ordered entries
    # depending on setup
    if cur_target.config.combine_javascript
      urls << cur_entry.cacheable_url
    else
      urls += cur_entry.ordered_entries.map { |e| e.cacheable_url }
    end
    
    # add any stylesheet libs from the target
    urls += (cur_target.config.javascript_libs || [])
  end

  # Convert to HTML and return
  urls = urls.map do |url|
    %(  <script type="text/javascript" src="#{url}"></script>)
  end

  # Add preferred language definition...
  urls << %(<script type="text/javascript">String.preferredLanguage = "#{language}";</script>)

  urls.join("\n")
end

#loc(string, opts = {}) ⇒ Object

Localizes the passed string, using the optional passed options.



175
176
177
178
179
# File 'lib/sproutcore/helpers/static_helper.rb', line 175

def loc(string, opts = {})
  string = string.nil? ? '' : string.to_s
  language = opts[:language] || self.language
  return strings_hash(language)[string] || string
end

#partial(resource_name, opts = {}) ⇒ Object

Attempts to render the named entry as a partial

Options

language:: the language to use. defaults to current


123
124
125
126
127
128
129
130
131
132
# File 'lib/sproutcore/helpers/static_helper.rb', line 123

def partial(resource_name, opts = {})
  resource_name = resource_name.to_s
  m = self.manifest
  if opts[:language]
    m = target.manifest_for(:language => opts[:language]).build! 
  end

  entry = m.find_entry(resource_name, :hidden => true, :entry_type => :html)
  return entry.nil? ? '' : render_partial(entry) 
end

#sc_resource(resource_name, opts = nil) ⇒ Object

Allows you to specify HTML resource this html template should be merged into. Optionally also specify the layout file to use when building this resource.

Example

<% sc_resource :foo, :layout => 'sproutcore:lib/index.html' %>


168
169
170
171
172
# File 'lib/sproutcore/helpers/static_helper.rb', line 168

def sc_resource(resource_name, opts = nil)
  opts = opts.nil? ? (resource_name.kind_of?(Hash) ? resource_name : {}) : opts
  @layout = opts[:layout] if opts[:layout]
  return ''
end

#sc_static(resource_name, opts = {}) ⇒ Object Also known as: static_url

Returns the URL for the named resource



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sproutcore/helpers/static_helper.rb', line 135

def sc_static(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)
  return '' if entry.nil?
  return entry.friendly_url if opts[:friendly] && entry.friendly_url
  return entry.cacheable_url
end

#sc_target(resource_name, opts = {}) ⇒ Object

Returns the URL of the named target’s index.html, if it has one



155
156
157
158
159
# File 'lib/sproutcore/helpers/static_helper.rb', line 155

def sc_target(resource_name, opts = {})
  opts[:friendly] = true
  resource_name = "#{resource_name}:index.html"
  sc_static(resource_name, opts)
end

#stylesheets_for_client(target_name = nil, opts = nil) ⇒ Object

This method will return the HTML to link to all the stylesheets required by the named bundle. If you pass no options, the current client will be used.

bundle_name = the name of the bundle to render or nil to use the current :language => the language to render. defaults to current language



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
# File 'lib/sproutcore/helpers/static_helper.rb', line 25

def stylesheets_for_client(target_name = nil, opts = nil)

  # normalize params
  if target_name.kind_of?(Hash) && opts.nil?
    opts = target_name
    target_name = nil
  end
  opts = {} if opts.nil?

  # process options
  include_method = opts[:include_method] ||= :link
  t = target_name ? target.target_for(target_name) : target

  # collect urls from entries
  urls = []
  combine_stylesheets = t.config.combine_stylesheets
  combined_entries(t, opts, 'stylesheet.css', 'stylesheet-packed.css') do |cur_target, cur_entry|
    # include either the entry URL or URL of ordered entries
    # depending on setup
    if combine_stylesheets
      urls << cur_entry.cacheable_url
    else
      urls += cur_entry.ordered_entries.map { |e| e.cacheable_url }
    end
    
    # add any stylesheet libs from the target
    urls += (cur_target.config.stylesheet_libs || [])
  end
    
  # Convert to HTML and return
  urls = urls.map do |url|
    if include_method == :import
      %(  @import url('#{url}');)
    else
      %(  <link href="#{url}" rel="stylesheet" type="text/css" />)
    end
  end
  
  # if include style is @import, surround with style tags
  if include_method == :import
    %(<style type="text/css">\n#{urls * "\n"}</style>)
  else
    urls.join("\n")
  end
end

#theme_name(opts = {}) ⇒ Object

Returns the CSS class name dictated by the current theme. You can also pass an optional default value to use if no theme is specified in the config. The value returned here will use either the theme_name set in the target’s config or the theme_name set by the theme framework, if set.



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/sproutcore/helpers/static_helper.rb', line 186

def theme_name(opts ={})
  ret = opts[:default] || 'sc-theme'
  if target.config.theme_name
    ret = target.config.theme_name
  elsif target.config.theme
    if theme_target = target.target_for(target.config.theme)
      ret = theme_target.config.theme_name || ret
    end
  end
  return ret
end

#title(cur_target = nil) ⇒ Object



198
199
200
201
# File 'lib/sproutcore/helpers/static_helper.rb', line 198

def title(cur_target=nil)
  cur_target = self.target if cur_target.nil?
  cur_target.config.title || cur_target.target_name.to_s.sub(/^\//,'').gsub(/[-_\/]/,' ').split(' ').map { |x| x.capitalize }.join(' ')
end