Added thumbnail option to #link_to_if_arvados_object, enabled on pipeline instance...
[arvados.git] / apps / workbench / app / helpers / application_helper.rb
1 module ApplicationHelper
2   def current_user
3     controller.current_user
4   end
5
6   def self.match_uuid(uuid)
7     /^([0-9a-z]{5})-([0-9a-z]{5})-([0-9a-z]{15})$/.match(uuid.to_s)
8   end
9
10   def current_api_host
11     Rails.configuration.arvados_v1_base.gsub /https?:\/\/|\/arvados\/v1/,''
12   end
13
14   def render_content_from_database(markup)
15     raw RedCloth.new(markup).to_html
16   end
17
18   def human_readable_bytes_html(n)
19     return h(n) unless n.is_a? Fixnum
20
21     orders = {
22       1 => "bytes",
23       1024 => "KiB",
24       (1024*1024) => "MiB",
25       (1024*1024*1024) => "GiB",
26       (1024*1024*1024*1024) => "TiB"
27     }
28
29     orders.each do |k, v|
30       sig = (n.to_f/k)
31       if sig >=1 and sig < 1024
32         if v == 'bytes'
33           return "%i #{v}" % sig
34         else
35           return "%0.1f #{v}" % sig
36         end
37       end
38     end
39
40     return h(n)
41       #raw = n.to_s
42     #cooked = ''
43     #while raw.length > 3
44     #  cooked = ',' + raw[-3..-1] + cooked
45     #  raw = raw[0..-4]
46     #end
47     #cooked = raw + cooked
48   end
49
50   def resource_class_for_uuid(attrvalue, opts={})
51     ArvadosBase::resource_class_for_uuid(attrvalue, opts)
52   end
53
54   def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
55     if (resource_class = resource_class_for_uuid(attrvalue, opts))
56       link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
57       link_name = opts[:link_text]
58       if !link_name
59         link_name = link_uuid
60
61         if opts[:friendly_name]
62           if attrvalue.respond_to? :friendly_link_name
63             link_name = attrvalue.friendly_link_name
64           else
65             begin
66               link_name = resource_class.find(link_uuid).friendly_link_name
67             rescue RuntimeError
68               # If that lookup failed, the link will too. So don't make one.
69               return attrvalue
70             end
71           end
72         end
73         if opts[:with_class_name]
74           link_name = "#{resource_class.to_s}: #{link_name}"
75         end
76         if !opts[:no_tags] and resource_class == Collection
77           Link.where(head_uuid: link_uuid, link_class: ["tag", "identifier"]).each do |tag|
78             link_name += ' <span class="label label-info">' + html_escape(tag.name) + '</span>'
79           end
80         end
81         if opts[:thumbnail] and resource_class == Collection
82           maximg = 3
83           Collection.where(uuid: link_uuid).each do |c|
84             c.files.each do |file|
85               if CollectionsHelper::is_image file[1]
86                 link_name += " "
87                 link_name += image_tag "#{url_for c}/#{CollectionsHelper::file_path file}", style: "height: 4em; width: auto"
88                 maximg -= 1
89               end
90               break if maximg == 0
91             end
92           end
93         end
94       end
95       style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
96       if opts[:no_link]
97         raw(link_name)
98       else
99         link_to raw(link_name), { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
100       end
101     else
102       attrvalue
103     end
104   end
105
106   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
107     attrvalue = object.send(attr) if attrvalue.nil?
108     return attrvalue if !object.attribute_editable? attr
109
110     input_type = 'text'
111     case object.class.attribute_info[attr.to_sym].andand[:type]
112     when 'text'
113       input_type = 'textarea'
114     when 'datetime'
115       input_type = 'date'
116     else
117       input_type = 'text'
118     end
119
120     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
121
122     link_to attrvalue.to_s, '#', {
123       "data-emptytext" => "none",
124       "data-placement" => "bottom",
125       "data-type" => input_type,
126       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
127       "data-title" => "Update #{attr.gsub '_', ' '}",
128       "data-name" => attr,
129       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
130       :class => "editable"
131     }.merge(htmloptions)
132   end
133
134   def render_editable_subattribute(object, attr, subattr, template, htmloptions={})
135     if object
136       attrvalue = object.send(attr)
137       subattr.each do |k|
138         if attrvalue and attrvalue.is_a? Hash
139           attrvalue = attrvalue[k]
140         else
141           break
142         end
143       end
144     end
145
146     datatype = nil
147     required = true
148     if template
149       #puts "Template is #{template.class} #{template.is_a? Hash} #{template}"
150       if template.is_a? Hash
151         if template[:output_of]
152           return raw("<span class='label label-default'>#{template[:output_of]}</span>")
153         end
154         if template[:dataclass]
155           dataclass = template[:dataclass]
156         end
157         if template[:optional] != nil
158           required = (template[:optional] != "true")
159         end
160         if template[:required] != nil
161           required = template[:required]
162         end
163       end
164     end
165
166     rsc = template
167     if template.is_a? Hash
168       if template[:value]
169         rsc = template[:value]
170       elsif template[:default]
171         rsc = template[:default]
172       end
173     end
174
175     return link_to_if_arvados_object(rsc) if !object
176     return link_to_if_arvados_object(attrvalue) if !object.attribute_editable? attr
177
178     if dataclass
179       begin
180         dataclass = dataclass.constantize
181       rescue NameError
182       end
183     else
184       dataclass = ArvadosBase.resource_class_for_uuid(rsc)
185     end
186
187     if dataclass && dataclass.is_a?(Class)
188       datatype = 'select'
189     elsif dataclass == 'number'
190       datatype = 'number'
191 1    else
192       if template.is_a? Array
193         # ?!?
194       elsif template.is_a? String
195         if /^\d+$/.match(template)
196           datatype = 'number'
197         else
198           datatype = 'text'
199         end
200       end
201     end
202
203     id = "#{object.uuid}-#{subattr.join('-')}"
204     dn = "[#{attr}]"
205     subattr.each do |a|
206       dn += "[#{a}]"
207     end
208
209     if attrvalue.is_a? String
210       attrvalue = attrvalue.strip
211     end
212
213     attrtext = attrvalue
214     if dataclass and dataclass.is_a? Class
215       items = []
216       if attrvalue and !attrvalue.empty?
217         Link.where(head_uuid: attrvalue, link_class: ["tag", "identifier"]).each do |tag|
218           attrtext += " [#{tag.name}]"
219         end
220         items.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
221       end
222       #dataclass.where(uuid: attrvalue).each do |item|
223       #  items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
224       #end
225       itemuuids = []
226       dataclass.limit(10).each do |item|
227         itemuuids << item.uuid
228         items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
229       end
230       Link.where(head_uuid: itemuuids, link_class: ["tag", "identifier"]).each do |tag|
231         items.each do |item|
232           if item.uuid == tag.head_uuid
233             item.name += ' [' + tag.name + ']'
234           end
235         end
236       end
237     end
238
239     lt = link_to attrtext, '#', {
240       "data-emptytext" => "none",
241       "data-placement" => "bottom",
242       "data-type" => datatype,
243       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
244       "data-title" => "Set value for #{subattr[-1].to_s}",
245       "data-name" => dn,
246       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
247       "data-showbuttons" => "false",
248       "data-value" => attrvalue,
249       :class => "editable #{'required' if required}",
250       :id => id
251     }.merge(htmloptions)
252
253     lt += raw("\n<script>")
254
255     if items and items.length > 0
256       lt += raw("add_form_selection_sources(#{items.to_json});\n")
257     end
258
259     lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
260
261     lt += raw("</script>")
262
263     lt
264   end
265 end