Module: Ziya::Helper

Included in:
Components::Draw, Ziya::Helpers::BaseHelper
Defined in:
lib/ziya/ziya_helper.rb

Instance Method Summary collapse

Instance Method Details

#block_is_within_action_view?(block) ⇒ Boolean

Returns:

  • (Boolean)


366
367
368
# File 'lib/ziya/ziya_helper.rb', line 366

def block_is_within_action_view?(block)
  eval("defined? _erbout", block.binding)
end

#capture_block(*args, &block) ⇒ Object



357
358
359
# File 'lib/ziya/ziya_helper.rb', line 357

def capture_block( *args, &block )
    block.call(*args)
end

#chart_pathObject



159
# File 'lib/ziya/ziya_helper.rb', line 159

def chart_path()    "/charts"; end

#charts_swfObject



155
# File 'lib/ziya/ziya_helper.rb', line 155

def charts_swf()    "library_path=%s/charts_library&xml_source=%s"; end

#class_idObject



160
# File 'lib/ziya/ziya_helper.rb', line 160

def class_id()      "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" end

#codebaseObject



161
# File 'lib/ziya/ziya_helper.rb', line 161

def codebase()      "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0"; end

#composite_urlObject



154
# File 'lib/ziya/ziya_helper.rb', line 154

def composite_url() "%s/charts.swf?library_path=%s/charts_library&xml_source=%s" end

#content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/ziya/ziya_helper.rb', line 345

def (name, content_or_options_with_block = nil, options = nil, escape = true, &block)
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    content = capture_block(&block)
     = (name, content, options, escape)
    block_is_within_action_view?(block) ? concat(, block.binding) : 
  else
    content = content_or_options_with_block
    (name, content, options, escape)
  end
end

#content_tag_string(name, content, options, escape = true) ⇒ Object



361
362
363
364
# File 'lib/ziya/ziya_helper.rb', line 361

def (name, content, options, escape = true)
  tag_options = tag_options(options, escape) if options
  "<#{name}#{tag_options}>#{content}</#{name}>"
end

#escape_charsObject



370
371
372
# File 'lib/ziya/ziya_helper.rb', line 370

def escape_chars 
  { '&' => '&amp;', '"' => '&quot;', '>' => '&gt;', '<' => '&lt;' }
end

#escape_once(html) ⇒ Object



324
325
326
# File 'lib/ziya/ziya_helper.rb', line 324

def escape_once(html)
  html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| escape_chars[special] }
end

#escape_url(url) ⇒ Object

escape url



294
295
296
# File 'lib/ziya/ziya_helper.rb', line 294

def escape_url( url )
  CGI.escape( url.gsub( /&amp;/, '&' ) )
end

#gauge_pathObject



158
# File 'lib/ziya/ziya_helper.rb', line 158

def gauge_path()    "/gauges"; end

#gauges_swfObject



157
# File 'lib/ziya/ziya_helper.rb', line 157

def gauges_swf()    "%s/gauge.swf?xml_source=%s"; end

#gen_composite_path(swf_chart_dir, url) ⇒ Object

flash chart library path



145
146
147
# File 'lib/ziya/ziya_helper.rb', line 145

def gen_composite_path( swf_chart_dir, url )
  composite_url % [swf_chart_dir, swf_chart_dir, escape_url( url )] 
end

#gen_swf_path(path_directive, swf_dir, url) ⇒ Object

generates swf path



164
165
166
# File 'lib/ziya/ziya_helper.rb', line 164

def gen_swf_path( path_directive, swf_dir, url )
  path_directive % [swf_dir, escape_url( url )]
end

#generate_flash_tag(url, swf_path, swf_file, options) ⇒ Object

generated the object and embed tag necessary for the flash movie



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/ziya/ziya_helper.rb', line 218

def generate_flash_tag( url, swf_path, swf_file, options )
  # Setup options for opaque mode
  setup_wmode( options )      
  
  # setup width and height
  setup_movie_size( options )
  
  color_param  = tag( 'param', {:name => 'bgcolor', :value => options[:bgcolor]}, true )
  color_param += tag( 'param', {:name  => "wmode", :value => options[:wmode]}, true )

  xml_swf_path = gen_swf_path( swf_path, options[:swf_path], url )
  xml_swf_path << "&timestamp=#{Time.now.to_i}" if options[:cache] == false
  xml_swf_path << "&timeout=#{options[:timeout]}" if options[:timeout]
  xml_swf_path << "&stage_width=#{options[:width]}&stage_height=#{options[:height]}" if options[:use_stage] == true 
  
  # if options[:tag_type] == "object"
    tags = <<-TAGS
    <object codebase="#{codebase}" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="#{options[:id]}" height="#{options[:height]}" width="#{options[:width]}">
      <param name="scale"             value="noscale">
      <param name="salign"            value="#{options[:salign]}">            
      <param name="bgcolor"           value="#{options[:bgcolor]}">
      <param name="wmode"             value="#{options[:wmode]}">                                  
      <param name="movie"             value="#{options[:swf_path]}/#{swf_file}">            
      <param name="FlashVars"         value="#{xml_swf_path}&chart_id=#{options[:id]}">
      <param name="menu"              value="true">
      <param name="allowFullScreen"   value="true">
      <param name="allowScriptAccess" value="sameDomain">            
      <param name="quality"           value="high">
      <param name="play"              value="true">                        
      <param name="devicefont"        value="false">
        <embed scale="noscale" 
          allowfullscreen="true" 
          allowscriptaccess="sameDomain" 
          bgcolor="#{options[:bgcolor]}" 
          devicefont="false" 
          flashvars="#{xml_swf_path}" 
          menu="true" 
          name="#{options[:id]}" 
          play="true" 
          pluginspage="http://www.macromedia.com/go/getflashplayer" 
          quality="high" 
          salign="#{options[:salign]}" 
          src="#{options[:swf_path]}/#{swf_file}" 
          type="application/x-shockwave-flash" 
          wmode="#{options[:wmode]}" 
          align="#{options[:align]}" 
          height="#{options[:height]}" 
          width="#{options[:width]}"/>            
   </object>        
  TAGS
# else
 #    tags = <<-TAGS
 #      <embed scale="noscale" 
 #        allowfullscreen="true" 
 #        allowscriptaccess="sameDomain" 
 #        bgcolor="#{options[:bgcolor]}" 
 #        devicefont="false" 
 #        flashvars="#{xml_swf_path}" 
 #        menu="true" 
 #        name="#{options[:id]}" 
 #        play="true" 
 #        pluginspage="http://www.macromedia.com/go/getflashplayer" 
 #        quality="high" 
 #        salign="#{options[:salign]}" 
 #        src="#{options[:swf_path]}/#{swf_file}" 
 #        type="application/x-shockwave-flash" 
 #        wmode="#{options[:wmode]}" 
 #        align="#{options[:align]}" 
 #        height="#{options[:height]}" 
 #        width="#{options[:width]}"/>
 #    TAGS
 # end 
 tags
end

#generate_old_style_flash_tag(url, swf_path, options) ⇒ Object



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
# File 'lib/ziya/ziya_helper.rb', line 168

def generate_old_style_flash_tag( url, swf_path, options )
  # Setup options for opaque mode
  setup_wmode( options )      
  
  # setup width and height
  setup_movie_size( options )
  
  color_param  = tag( 'param', {:name => 'bgcolor', :value => options[:bgcolor]}, true )
  color_param += tag( 'param', {:name  => "wmode", :value => options[:wmode]}, true )

  xml_swf_path = swf_path % [options[:swf_path], url]
  xml_swf_path << "&timestamp=#{Time.now.to_i}" if options[:use_cache] == true
  xml_swf_path << "&timeout=#{options[:timeout]}&retry=#{options[:retry]}" if options[:timeout] == true
  xml_swf_path << "&stage_width=#{options[:width]}&stage_height=#{options[:height]}" if options[:use_stage] == true
  tags = <<-TAGS
    <object codebase="#{codebase}" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="#{options[:id]}" height="#{options[:height]}" width="#{options[:width]}">
      <param name="scale" value="noscale">
      <param name="align" value="#{options[:align]}">            
      <param name="bgcolor" value="#{options[:bgcolor]}">
      <param name="wmode" value="#{options[:wmode]}">                                  
      <param name="movie" value="#{xml_swf_path}">
      <param name="menu" value="true">
      <param name="allowFullScreen" value="true">
      <param name="allowScriptAccess" value="sameDomain">            
      <param name="quality" value="high">
      <param name="play" value="true">                        
      <param name="devicefont" value="false">
      <embed scale="noscale"
        allowfullscreen="true" 
        allowscriptaccess="sameDomain" 
        bgcolor="#{options[:bgcolor]}" 
        devicefont="false" 
        src="#{xml_swf_path}" 
        menu="true" 
        name="#{options[:id]}" 
        play="true" 
        pluginspage="http://www.macromedia.com/go/getflashplayer" 
        quality="high" 
        salign="#{options[:salign]}" 
        src="#{xml_swf_path}" 
        type="application/x-shockwave-flash" 
        wmode="#{options[:wmode]}" 
        salign="#{options[:salign]}" 
        height="#{options[:height]}" 
        width="#{options[:width]}">
   </object>        
  TAGS
end

#mimeObject

Const accessors…



153
# File 'lib/ziya/ziya_helper.rb', line 153

def mime()          "application/x-shockwave-flash"; end

#plugin_urlObject



156
# File 'lib/ziya/ziya_helper.rb', line 156

def plugin_url()    "http://www.macromedia.com/go/getflashplayer"; end

#setup_movie_size(options) ⇒ Object

Check args for size option in the format wXy (Submitted by Sam Livingston-Gray)



311
312
313
314
315
316
317
# File 'lib/ziya/ziya_helper.rb', line 311

def setup_movie_size( options )
  if options[:size] =~ /(\d+)x(\d+)/
    options[:width]  = $1
    options[:height] = $2
    options.delete :size
  end
end

#setup_wmode(options) ⇒ Object

setup up wmode Set the wmode to opaque if a bgcolor is specified. If not set to transparent mode unless user overrides it



301
302
303
304
305
306
307
308
# File 'lib/ziya/ziya_helper.rb', line 301

def setup_wmode( options )
  if options[:bgcolor]
    options[:wmode] = "opaque" unless options[:wmode]
  else
    options[:wmode]   = "transparent" unless options[:wmode]
    options[:bgcolor] = "#FFFFFF"
  end
end

#tag(name, options = nil, open = false, escape = true) ⇒ Object

All this stolen form rails to make Ziya work with other fmks.…



320
321
322
# File 'lib/ziya/ziya_helper.rb', line 320

def tag(name, options = nil, open = false, escape = true)
 "<#{name}#{tag_options(options, escape) if options}" + (open ? ">" : " />")
end

#tag_options(options, escape = true) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/ziya/ziya_helper.rb', line 328

def tag_options(options, escape = true)
  unless !options or options.empty?
    attrs = []
    if escape
      options.each do |key, value|
        next unless value
        key = key.to_s
        value = escape_once(value)
        attrs << %(#{key}="#{value}")
      end
    else
      attrs = options.map { |key, value| %(#{key}="#{value}") }
    end
    " #{attrs.sort * ' '}" unless attrs.empty?
  end
end

#ziya_chart(url, chart_options = {}) ⇒ Object


generates neccessary html tags to display a chart.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ziya/ziya_helper.rb', line 126

def ziya_chart( url, chart_options = {} )
  options = { :width          => "400",
              :height         => "300",
              :tag_type       => "embed",
              :align          => "l",
              :salign         => "tl",
              :scale          => "noscale",
              :class          => "",   
              :id             => "ziya_chart",
              :swf_path       => chart_path,
              :use_cache      => false,
              :timeout        => nil,
              :use_stage      => false
            }.merge!(chart_options)

  generate_flash_tag( url, charts_swf, "charts.swf", options )
end

#ziya_chart_js(url, chart_options = {}) ⇒ Object

generates necessary object and embed tags to display a flash movie on various browsers using javascript



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
# File 'lib/ziya/ziya_helper.rb', line 34

def ziya_chart_js( url, chart_options={} )
  options = { :width          => "400",
              :height         => "300",
              :bgcolor        => "000000",
              :wmode          => "opaque",
              :menu           => true,
              :full_screen    => true,
              :align          => "l",
              :salign         => "tl",
              :scale          => "noscale",
              :use_cache      => false,
              :timeout        => nil,
              :retry          => 2,
              :use_stage      => false,                  
              :id             => "ziya_chart",
              :swf_path       => chart_path,
  }.merge!(chart_options)

  # Setup options for opaque mode
  setup_wmode( options )      
  
  # setup width and height
  setup_movie_size( options )
  
  xml_swf_path = charts_swf % [options[:swf_path], escape_url(url)]
  xml_swf_path << "&timestamp=#{Time.now.to_i}" if options[:cache] == false
  xml_swf_path << "&timeout=#{options[:timeout]}&retry=#{options[:retry]}" if options[:timeout]
  xml_swf_path << "&stage_width=#{options[:width]}&stage_height=#{options[:height]}" if options[:use_stage] == true 
  
  js = <<-JS
    <script language="javascript" type="text/javascript">
    if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
      alert( "This page requires AC_RunActiveContent.js." );
    } 
    else {
      var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
      if( hasRightVersion ) { 
        AC_FL_RunContent(
          'codebase'         , 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
          'width'            , '#{options[:width]}',
          'height'           , '#{options[:height]}',
          'scale'            , '#{options[:scale]}',
          'salign'           , '#{options[:salign]}',
          'bgcolor'          , '#{options[:bgcolor]}',
          'wmode'            , '#{options[:wmode]}',
          'movie'            , '#{options[:swf_path]}/charts',
          'src'              , '#{options[:swf_path]}/charts',
          'FlashVars'        , '#{xml_swf_path}', 
          'id'               , '#{options[:id]}',
          'name'             , '#{options[:id]}',
          'menu'             , '#{options[:menu]}',
          'allowFullScreen'  , '#{options[:full_screen]}',
          'allowScriptAccess','sameDomain',
          'quality'          , 'high',
          'align'            , '#{options[:align]}',
          'pluginspage'      , 'http://www.macromedia.com/go/getflashplayer',
          'play'             , 'true',
          'devicefont'       , 'false'
        ); 
      } 
      else { 
        var alternateContent = 'This content requires the Adobe Flash Player. '
        + '<u><a href=http://www.macromedia.com/go/getflash/>Get Flash</a></u>.';
        document.write(alternateContent); 
      }
    }
    </script>
  JS
end

#ziya_gauge(url, gauge_options = {}) ⇒ Object


generates necessary html tags to display a gauge.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ziya/ziya_helper.rb', line 106

def ziya_gauge( url, gauge_options={} )
  options = { :width          => "200",
              :height         => "200",
              :align          => "middle",
              :scale          => "noscale",
              :salign         => "",
              :class          => "",        
              :id             => "ziya_gauge",
              :swf_path       => gauge_path,
              :use_cache      => false,
              :timeout        => false,
              :retry          => 2,
              :use_stage      => false
            }.merge!(gauge_options)
                  
  generate_old_style_flash_tag( url, gauges_swf, options )
end

#ziya_javascript_include_tagObject


generates a javascript tag to include the js script to create object and embed tags



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ziya/ziya_helper.rb', line 17

def ziya_javascript_include_tag
  js = <<-JS
    <script language="javascript" type="text/javascript">
      AC_FL_RunContent = 0;
      DetectFlashVer = 0;
    </script>
    <script src="/charts/AC_RunActiveContent.js" language="javascript"></script>
    <script language="javascript" type="text/javascript">
      var requiredMajorVersion = 9;
      var requiredMinorVersion = 0;
      var requiredRevision = 45;
    </script>
   JS
end