Module: Zena::Use::Display::ImageTags

Includes:
Common
Included in:
ViewMethods
Defined in:
lib/zena/use/display.rb

Overview

Common

Constant Summary collapse

IMG_TAG_EXTRA_RE =
%r{UUID|(PATH)_([a-z]+)\.([a-z]+)}
IMG_TAG_EXTRA_JS =
%r{\[JS\](.*?)\[/JS\]}

Instance Method Summary collapse

Methods included from Common

#icon_finder

Instance Method Details

#asset_img_tag(obj, opts) ⇒ Object

<img> tag definition to show an Image / mp3 document FIXME: this should live inside zafu



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
# File 'lib/zena/use/display.rb', line 71

def asset_img_tag(obj, opts)
  if img_tag = obj.prop["img_tag_#{opts[:mode] || 'std'}"]
    # Hand made image tag
    if img_tag.kind_of?(String)
      # We use code to make it raw.
      uuid = "img#{UUIDTools::UUID.random_create.to_s.gsub('-','')[0..6]}"
      tag = img_tag.gsub(IMG_TAG_EXTRA_RE) do
        if $& == 'UUID'
          uuid
        elsif $1 == 'PATH'
          zen_path(obj, :mode => $2, :format => $3)
        end
      end
      tag.gsub!(IMG_TAG_EXTRA_JS) do
        self.js_data << $1
        ''
      end
      return raw_content(tag)
    end
  elsif img_tag = obj.prop["img_tag"]
    if img_tag.kind_of?(Hash) && img_tag = img_tag[opts[:mode] || 'std']
      return raw_content(img_tag.gsub('UUID', "img#{UUIDTools::UUID.random_create.to_s.gsub('-','')[0..6]}"))
    end
  end
  
  if obj.kind_of?(Image)
    res     = {}
    format  = Iformat[opts[:mode]] || Iformat['std']

    [:id, :border].each do |k|
      next unless opts[k]
      res[k]    = opts[k]
    end

    res[:alt]   = opts[:alt] || fquote(obj.title)
    res[:src]   = data_path(obj, :mode => (format[:size] == :keep ? nil : format[:name]), :host => opts[:host])
    res[:class] = opts[:class] || format[:name]

    # compute image size
    res[:width]  = obj.width(format)
    res[:height] = obj.height(format)
    if popup = format[:popup]

      if popup_fmt = Iformat[popup[:name]]
        options = popup[:options]
        keys    = popup[:show]
        res[:onclick] = 'Zena.popup(this)'
        res[:id]    ||= unique_id
        data = {}
        data['src'] = data_path(obj, :mode => (popup[:size] == :keep ? nil : popup[:name]), :host => opts[:host])
        data['width']   = obj.width(popup_fmt)
        data['height']  = obj.height(popup_fmt)

        data['fields'] = fields = {}
        data['keys']   = field_keys = []
        keys.each do |k|
          case k
          when 'navigation'
            field_keys << k
            data[k] = true
          else
            if v = obj.prop[k]
              field_keys << k
              case options[k]
              when 'raw'
                fields[k] = v
              when 'link'
                fields[k] = link_to(v, zen_path(obj))
              else
                fields[k] = zazen(v)
              end
            end
          end
        end

        self.js_data << "$('#{res[:id]}')._popup = #{data.to_json};"
      end
    end
    res
  elsif obj.kind_of?(Document) && obj.ext == 'mp3' && (opts[:mode].nil? || opts[:mode] == 'std' || opts[:mode] == 'button')
    # rough wrap to use the 'button'
    # we differ '<object...>' by using a placeholder to avoid the RedCloth escaping.
    add_place_holder( %{ <object type="application/x-shockwave-flash"
      data="/images/swf/xspf/musicplayer.swf?&song_url=#{CGI.escape(data_path(obj))}"
      width="17" height="17">
      <param name="movie"
      value="/images/swf/xspf/musicplayer.swf?&song_url=#{CGI.escape(data_path(obj))}" />
      <img src="/images/sound_mute.png"
      width="16" height="16" alt="" />
    </object> } )
  end
end

#crop_formats(obj) ⇒ Object

This is used by _crop.rhtml



20
21
22
23
24
25
26
27
28
# File 'lib/zena/use/display.rb', line 20

def crop_formats(obj)
  buttons = ['jpg', 'png']
  ext = Zena::TYPE_TO_EXT[obj.content_type]
  ext = ext ? ext[0] : obj.ext
  buttons << ext unless buttons.include?(ext)
  buttons.map do |e|
    "<input type='radio' name='node[crop][format]' value='#{e}'#{e==ext ? " checked='checked'" : ''}/> #{e} "
  end
end

#generic_img_tag(obj, opts) ⇒ Object

<img> tag definition for the generic icon (image showing class of element).



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
# File 'lib/zena/use/display.rb', line 165

def generic_img_tag(obj, opts)
  res = {}
  [:class, :id, :border, :style].each do |k|
    next unless opts[k]
    res[k] = opts[k]
  end

  if obj.vclass.kind_of?(VirtualClass) && !obj.vclass.icon.blank?
    # TODO: we could use a 'zip' to an image as 'icon' (but we would need some caching to avoid multiple loading during doc listing)
    res[:src]     = obj.vclass.icon
    res[:alt]     = opts[:alt] || (_('%{type} node') % {:type => obj.vclass.name})
    res[:class] ||= obj.klass
    # no width, height available
    return res
  end

  # default generic icon from /images/ext folder
  res[:width]  = 32
  res[:height] = 32

  if obj.kind_of?(Document)
    name = obj.ext
    res[:alt] = opts[:alt] || (_('%{ext} document') % {:ext => name})
    res[:class] ||= 'doc'
  else
    name = obj.klass.downcase
    res[:alt] = opts[:alt] || (_('%{ext} node') % {:ext => obj.klass})
    res[:class] ||= 'node'
  end

  if !File.exist?("#{RAILS_ROOT}/public/images/ext/#{name}.png")
    name = 'other'
  end

  res[:src] = "/images/ext/#{name}.png"

  if opts[:mode] && (format = Iformat[opts[:mode]]) && format[:size] != :keep
    # resize image
    img = Zena::Use::ImageBuilder.new(:path=>"#{RAILS_ROOT}/public#{res[:src]}", :width=>32, :height=>32)
    img.transform!(format)
    if (img.width == res[:width] && img.height == res[:height])
      # ignore mode
      res[:mode] = nil
    else
      res[:width]  = img.width
      res[:height] = img.height

      new_file = "#{name}_#{format[:name]}.png"
      path     = "#{RAILS_ROOT}/public/images/ext/#{new_file}"
      unless File.exist?(path)
        # make new image with the mode
        if img.dummy?
          File.cp("#{RAILS_ROOT}/public/images/ext/#{name}.png", path)
        else
          File.open(path, "wb") { |f| f.syswrite(img.read) }
        end
      end

      res[:src] = "/images/ext/#{new_file}"
    end
  end

  res[:src] = "http://#{opts[:host]}#{res[:src]}" if opts[:host]

  res
end

#img_tag(obj, opts = {}) ⇒ Object

Display an image tag for the given node. If no mode is provided, ‘full’ is used. Options are ‘:mode’, ‘:id’, ‘:alt’, ‘:alt_src’ and ‘:class’. If no class option is passed, the format is used as the image class. Example : img_tag(@node, :mode=>‘pv’) => <img src=‘/sites/test.host/data/jpg/20/bird_pv.jpg’ height=‘80’ width=‘80’ alt=‘bird’ class=‘pv’/>



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
# File 'lib/zena/use/display.rb', line 33

def img_tag(obj, opts={})
  return '' unless obj
  # try:
  # 1. tag on element data (Image, mp3 document)
  res = asset_img_tag(obj, opts)

  # 2. tag using alt_src data
  # FIXME: replace recompilation by executing the find here
  # alt_src = alt_src.kind_of?(String) ? Node.do_find(:first, alt_src) : alt_src
  if !res && alt_src = opts[:alt_src]
    if alt_src == 'icon'
      alt_src = icon_finder
    else
      alt_src = "#{alt_src.split(',').join(' or ')}"
    end

    if icon = obj.find(:first, alt_src)
      return img_tag(icon, opts.merge(:alt_src => nil))
    end
  end
  
  # 3. generic icon
  res ||= generic_img_tag(obj, opts)

  if res.kind_of?(Hash)
    out = "<img"
    [:src, :width, :height, :alt, :id, :class, :style, :border, :onclick].each do |k|
      next unless v = res[k]
      out << " #{k}='#{v}'"
    end
    out + "/>"
  else
    res
  end
end