1 module ApplicationHelper
3 controller.current_user
6 def self.match_uuid(uuid)
7 /^([0-9a-z]{5})-([0-9a-z]{5})-([0-9a-z]{15})$/.match(uuid.to_s)
11 Rails.configuration.arvados_v1_base.gsub /https?:\/\/|\/arvados\/v1/,''
14 def render_markup(markup)
15 raw RedCloth.new(markup.to_s).to_html(:refs_arvados, :textile) if markup
18 def human_readable_bytes_html(n)
19 return h(n) unless n.is_a? Fixnum
20 return "0 bytes" if (n == 0)
26 (1024*1024*1024) => "GiB",
27 (1024*1024*1024*1024) => "TiB"
32 if sig >=1 and sig < 1024
34 return "%i #{v}" % sig
36 return "%0.1f #{v}" % sig
45 # cooked = ',' + raw[-3..-1] + cooked
48 #cooked = raw + cooked
51 def resource_class_for_uuid(attrvalue, opts={})
52 ArvadosBase::resource_class_for_uuid(attrvalue, opts)
56 # Returns HTML that links to the Arvados object specified in +attrvalue+
57 # Provides various output control and styling options.
59 # +attrvalue+ an Arvados model object or uuid
61 # +opts+ a set of flags to control output:
63 # [:link_text] the link text to use (may include HTML), overrides everything else
65 # [:friendly_name] whether to use the "friendly" name in the link text (by
66 # calling #friendly_link_name on the object), otherwise use the uuid
68 # [:with_class_name] prefix the link text with the class name of the model
70 # [:no_tags] disable tags in the link text (default is to show tags).
71 # Currently tags are only shown for Collections.
73 # [:thumbnail] if the object is a collection, show an image thumbnail if the
74 # collection consists of a single image file.
76 # [:no_link] don't create a link, just return the link text
78 # +style_opts+ additional HTML properties for the anchor tag, passed to link_to
80 def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
81 if (resource_class = resource_class_for_uuid(attrvalue, opts))
82 if attrvalue.is_a? ArvadosBase
84 link_uuid = attrvalue.uuid
89 link_name = opts[:link_text]
92 link_name = object.andand.default_name || resource_class.default_name
94 if opts[:friendly_name]
95 if attrvalue.respond_to? :friendly_link_name
96 link_name = attrvalue.friendly_link_name
99 if resource_class.name == 'Collection'
100 link_name = collections_for_object(link_uuid).andand.first.andand.friendly_link_name
102 link_name = object_for_dataclass(resource_class, link_uuid).andand.friendly_link_name
104 rescue ArvadosApiClient::NotFoundException
105 # If that lookup failed, the link will too. So don't make one.
110 if link_name.nil? or link_name.empty?
111 link_name = attrvalue
113 if opts[:with_class_name]
114 link_name = "#{resource_class.to_s}: #{link_name}"
116 if !opts[:no_tags] and resource_class == Collection
117 links_for_object(link_uuid).each do |tag|
118 if tag.link_class.in? ["tag", "identifier"]
119 tags += ' <span class="label label-info">'
120 tags += link_to tag.name, controller: "links", filters: [["link_class", "=", "tag"], ["name", "=", tag.name]].to_json
125 if opts[:thumbnail] and resource_class == Collection
126 # add an image thumbnail if the collection consists of a single image file.
127 collections_for_object(link_uuid).each do |c|
128 if c.files.length == 1 and CollectionsHelper::is_image c.files.first[1]
130 link_name += image_tag "#{url_for c}/#{CollectionsHelper::file_path c.files.first}", style: "height: 4em; width: auto"
135 style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
139 (link_to raw(link_name), { controller: resource_class.to_s.tableize, action: 'show', id: ((opts[:name_link].andand.uuid) || link_uuid) }, style_opts) + raw(tags)
142 # just return attrvalue if it is not recognizable as an Arvados object or uuid.
143 if attrvalue.nil? or (attrvalue.is_a? String and attrvalue.empty?)
151 def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
152 attrvalue = object.send(attr) if attrvalue.nil?
153 if !object.attribute_editable?(attr, :ever) or
154 (!object.editable? and
155 !object.owner_uuid.in?(my_projects.collect(&:uuid)))
156 if attrvalue && attrvalue.length > 0
157 return render_attribute_as_textile( object, attr, attrvalue, false )
159 return (attr == 'name' and object.andand.default_name) ||
165 case object.class.attribute_info[attr.to_sym].andand[:type]
167 input_type = 'textarea'
174 attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
175 rendervalue = render_attribute_as_textile( object, attr, attrvalue, false )
180 key: object.class.to_s.underscore
184 ajax_options['data-url'] = url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore)
186 ajax_options['data-url'] = url_for(action: "create", controller: object.class.to_s.pluralize.underscore)
187 ajax_options['data-pk'][:defaults] = object.attributes
189 ajax_options['data-pk'] = ajax_options['data-pk'].to_json
190 @unique_id ||= (Time.now.to_f*1000000).to_i
191 span_id = object.uuid.to_s + '-' + attr.to_s + '-' + (@unique_id += 1).to_s
193 span_tag = content_tag 'span', rendervalue, {
194 "data-emptytext" => '(none)',
195 "data-placement" => "bottom",
196 "data-type" => input_type,
197 "data-title" => "Edit #{attr.to_s.gsub '_', ' '}",
199 "data-object-uuid" => object.uuid,
200 "data-toggle" => "manual",
201 "data-value" => attrvalue,
203 :class => "editable #{is_textile?( object, attr ) ? 'editable-textile' : ''}"
204 }.merge(htmloptions).merge(ajax_options)
205 edit_button = raw('<a href="#" class="btn btn-xs btn-default btn-nodecorate" data-toggle="x-editable tooltip" data-toggle-selector="#' + span_id + '" data-placement="top" title="' + (htmloptions[:tiptitle] || 'edit') + '"><i class="fa fa-fw fa-pencil"></i></a>')
206 if htmloptions[:btnplacement] == :left
207 edit_button + ' ' + span_tag
209 span_tag + ' ' + edit_button
213 def render_pipeline_component_attribute(object, attr, subattr, value_info, htmloptions={})
216 attrvalue = value_info
218 if value_info.is_a? Hash
219 if value_info[:output_of]
220 return raw("<span class='label label-default'>#{value_info[:output_of]}</span>")
222 if value_info[:dataclass]
223 dataclass = value_info[:dataclass]
225 if value_info[:optional] != nil
226 required = (value_info[:optional] != "true")
228 if value_info[:required] != nil
229 required = value_info[:required]
232 # Pick a suitable attrvalue to show as the current value (i.e.,
233 # the one that would be used if we ran the pipeline right now).
234 if value_info[:value]
235 attrvalue = value_info[:value]
236 elsif value_info[:default]
237 attrvalue = value_info[:default]
241 preconfigured_search_str = value_info[:search_for]
245 !object.attribute_editable?(attr, :ever) or
246 (!object.editable? and
247 !object.owner_uuid.in?(my_projects.collect(&:uuid)))
248 return link_to_if_arvados_object attrvalue
253 dataclass = dataclass.constantize
257 dataclass = ArvadosBase.resource_class_for_uuid(attrvalue)
260 id = "#{object.uuid}-#{subattr.join('-')}"
265 if value_info.is_a? Hash
269 if dataclass == Collection
270 selection_param = object.class.to_s.underscore + dn
271 display_value = attrvalue
272 if value_info.is_a?(Hash)
273 if (link = Link.find? value_info[:link_uuid])
274 display_value = link.name
275 elsif value_info[:link_name]
276 display_value = value_info[:link_name]
279 if (attr == :components) and (subattr.size > 2)
280 chooser_title = "Choose a dataset for #{object.component_input_title(subattr[0], subattr[2])}:"
282 chooser_title = "Choose a dataset:"
284 modal_path = choose_collections_path \
285 ({ title: chooser_title,
286 filters: [['owner_uuid', '=', object.owner_uuid]].to_json,
288 action_href: pipeline_instance_path(id: object.uuid),
289 action_method: 'patch',
290 preconfigured_search_str: (preconfigured_search_str || ""),
293 selection_param: selection_param,
294 success: 'page-refresh'
297 return content_tag('div', :class => 'input-group') do
298 html = text_field_tag(dn, display_value,
300 "form-control #{'required' if required}")
301 html + content_tag('span', :class => 'input-group-btn') do
304 { :class => "btn btn-primary",
312 if dataclass.andand.is_a?(Class)
314 elsif dataclass == 'number'
316 elsif attrvalue.is_a? Array
317 # TODO: find a way to edit arrays with x-editable
319 elsif attrvalue.is_a? Fixnum or attrvalue.is_a? Float
321 elsif attrvalue.is_a? String
331 if dataclass.is_a? Class and dataclass < ArvadosBase
332 objects = get_n_objects_of_class dataclass, 10
333 objects.each do |item|
335 preload_uuids << item.uuid
337 if attrvalue and !attrvalue.empty?
338 preload_uuids << attrvalue
340 preload_links_for_objects preload_uuids
342 if attrvalue and !attrvalue.empty?
343 links_for_object(attrvalue).each do |link|
344 if link.link_class.in? ["tag", "identifier"]
345 attrtext += " [#{link.name}]"
348 selectables.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
352 itemuuids << item.uuid
353 selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
356 itemuuids.each do |itemuuid|
357 links_for_object(itemuuid).each do |link|
358 if link.link_class.in? ["tag", "identifier"]
359 selectables.each do |selectable|
360 if selectable['uuid'] == link.head_uuid
361 selectable['name'] += ' [' + link.name + ']'
369 lt = link_to attrtext, '#', {
370 "data-emptytext" => "none",
371 "data-placement" => "bottom",
372 "data-type" => datatype,
373 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore, merge: true),
374 "data-title" => "Set value for #{subattr[-1].to_s}",
376 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
377 "data-value" => attrvalue,
378 # "clear" button interferes with form-control's up/down arrows
379 "data-clear" => false,
380 :class => "editable #{'required' if required} form-control",
384 lt += raw("\n<script>")
387 lt += raw("add_form_selection_sources(#{selectables.to_json});\n")
390 lt += raw("$('[data-name=\"#{dn}\"]').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
392 lt += raw("</script>")
397 def render_arvados_object_list_start(list, button_text, button_href,
398 params={}, *rest, &block)
399 show_max = params.delete(:show_max) || 3
400 params[:class] ||= 'btn btn-xs btn-default'
401 list[0...show_max].each { |item| yield item }
402 unless list[show_max].nil?
403 link_to(h(button_text) +
404 raw(' <i class="fa fa-fw fa-arrow-circle-right"></i>'),
405 button_href, params, *rest)
409 def render_controller_partial partial, opts
410 cname = opts.delete :controller_name
412 render opts.merge(partial: "#{cname}/#{partial}")
413 rescue ActionView::MissingTemplate
414 render opts.merge(partial: "application/#{partial}")
418 RESOURCE_CLASS_ICONS = {
419 "Collection" => "fa-archive",
420 "Group" => "fa-users",
421 "Human" => "fa-male", # FIXME: Use a more inclusive icon.
423 "KeepDisk" => "fa-hdd-o",
424 "KeepService" => "fa-exchange",
425 "Link" => "fa-arrows-h",
426 "Node" => "fa-cloud",
427 "PipelineInstance" => "fa-gears",
428 "PipelineTemplate" => "fa-gears",
429 "Repository" => "fa-code-fork",
430 "Specimen" => "fa-flask",
431 "Trait" => "fa-clipboard",
433 "VirtualMachine" => "fa-terminal",
435 DEFAULT_ICON_CLASS = "fa-cube"
437 def fa_icon_class_for_class(resource_class, default=DEFAULT_ICON_CLASS)
438 RESOURCE_CLASS_ICONS.fetch(resource_class.to_s, default)
441 def fa_icon_class_for_uuid(uuid, default=DEFAULT_ICON_CLASS)
442 fa_icon_class_for_class(resource_class_for_uuid(uuid), default)
445 def fa_icon_class_for_object(object, default=DEFAULT_ICON_CLASS)
446 case class_name = object.class.to_s
448 object.group_class ? 'fa-folder' : 'fa-users'
450 RESOURCE_CLASS_ICONS.fetch(class_name, default)
454 def chooser_preview_url_for object
455 case object.class.to_s
457 polymorphic_path(object, tab_pane: 'chooser_preview')
463 def render_attribute_as_textile( object, attr, attrvalue, truncate )
464 if attrvalue && (is_textile? object, attr)
465 markup = render_markup attrvalue
466 markup = markup[0,markup.index('</p>')+4] if (truncate && markup.index('</p>'))
474 def is_textile?( object, attr )
475 is_textile = object.textile_attributes.andand.include?(attr)