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)
55 # When using {remote:true}, or using {method:...} to use an HTTP
56 # method other than GET, move the target URI from href to
57 # data-remote-href. Otherwise, browsers offer features like "open in
58 # new window" and "copy link address" which bypass Rails' click
59 # handler and therefore end up at incorrect/nonexistent routes (by
60 # ignoring data-method) and expect to receive pages rather than
61 # javascript responses.
63 # See assets/javascripts/link_to_remote.js for supporting code.
64 def link_to *args, &block
65 if (args.last and args.last.is_a? Hash and
66 (args.last[:remote] or
67 (args.last[:method] and
68 args.last[:method].to_s.upcase != 'GET')))
70 # Capybara/phantomjs can't click_link without an href, even if
71 # the click handler means it never gets used.
72 raw super.gsub(' href="', ' href="#" data-remote-href="')
74 # Regular browsers work as desired: users can click A elements
75 # without hrefs, and click handlers fire; but there's no "copy
76 # link address" option in the right-click menu.
77 raw super.gsub(' href="', ' data-remote-href="')
85 # Returns HTML that links to the Arvados object specified in +attrvalue+
86 # Provides various output control and styling options.
88 # +attrvalue+ an Arvados model object or uuid
90 # +opts+ a set of flags to control output:
92 # [:link_text] the link text to use (may include HTML), overrides everything else
94 # [:friendly_name] whether to use the "friendly" name in the link text (by
95 # calling #friendly_link_name on the object), otherwise use the uuid
97 # [:with_class_name] prefix the link text with the class name of the model
99 # [:no_tags] disable tags in the link text (default is to show tags).
100 # Currently tags are only shown for Collections.
102 # [:thumbnail] if the object is a collection, show an image thumbnail if the
103 # collection consists of a single image file.
105 # [:no_link] don't create a link, just return the link text
107 # +style_opts+ additional HTML properties for the anchor tag, passed to link_to
109 def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
110 if (resource_class = resource_class_for_uuid(attrvalue, opts))
111 if attrvalue.is_a? ArvadosBase
113 link_uuid = attrvalue.uuid
116 link_uuid = attrvalue
118 link_name = opts[:link_text]
121 link_name = object.andand.default_name || resource_class.default_name
123 if opts[:friendly_name]
124 if attrvalue.respond_to? :friendly_link_name
125 link_name = attrvalue.friendly_link_name opts[:lookup]
128 if resource_class.name == 'Collection'
129 link_name = collections_for_object(link_uuid).andand.first.andand.friendly_link_name
131 link_name = object_for_dataclass(resource_class, link_uuid).andand.friendly_link_name
133 rescue ArvadosApiClient::NotFoundException
134 # If that lookup failed, the link will too. So don't make one.
139 if link_name.nil? or link_name.empty?
140 link_name = attrvalue
142 if opts[:with_class_name]
143 link_name = "#{resource_class.to_s}: #{link_name}"
145 if !opts[:no_tags] and resource_class == Collection
146 links_for_object(link_uuid).each do |tag|
147 if tag.link_class.in? ["tag", "identifier"]
148 tags += ' <span class="label label-info">'
149 tags += link_to tag.name, controller: "links", filters: [["link_class", "=", "tag"], ["name", "=", tag.name]].to_json
154 if opts[:thumbnail] and resource_class == Collection
155 # add an image thumbnail if the collection consists of a single image file.
156 collections_for_object(link_uuid).each do |c|
157 if c.files.length == 1 and CollectionsHelper::is_image c.files.first[1]
159 link_name += image_tag "#{url_for c}/#{CollectionsHelper::file_path c.files.first}", style: "height: 4em; width: auto"
164 style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
165 if opts[:no_link] or (resource_class == User && !current_user)
168 (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)
171 # just return attrvalue if it is not recognizable as an Arvados object or uuid.
172 if attrvalue.nil? or (attrvalue.is_a? String and attrvalue.empty?)
180 def link_to_arvados_object_if_readable(attrvalue, link_text_if_not_readable, opts={})
181 resource_class = resource_class_for_uuid(attrvalue.split('/')[0]) if attrvalue
183 return link_to_if_arvados_object attrvalue, opts
186 readable = object_readable attrvalue, resource_class
188 link_to_if_arvados_object attrvalue, opts
189 elsif opts[:required] and current_user # no need to show this for anonymous user
190 raw('<div><input type="text" style="border:none;width:100%;background:#ffdddd" disabled=true class="required unreadable-input" value="') + link_text_if_not_readable + raw('" ></input></div>')
192 link_text_if_not_readable
196 # This method takes advantage of preloaded collections and objects.
197 # Hence you can improve performance by first preloading objects
198 # related to the page context before using this method.
199 def object_readable attrvalue, resource_class=nil
200 # if it is a collection filename, check readable for the locator
201 attrvalue = attrvalue.split('/')[0] if attrvalue
203 resource_class = resource_class_for_uuid(attrvalue) if resource_class.nil?
204 return if resource_class.nil?
207 if resource_class.to_s == 'Collection'
208 if CollectionsHelper.match(attrvalue)
209 found = collection_for_pdh(attrvalue)
210 return_value = found.first if found.any?
212 found = collections_for_object(attrvalue)
213 return_value = found.first if found.any?
216 return_value = object_for_dataclass(resource_class, attrvalue)
221 def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
222 attrvalue = object.send(attr) if attrvalue.nil?
223 if not object.attribute_editable?(attr)
224 if attrvalue && attrvalue.length > 0
225 return render_attribute_as_textile( object, attr, attrvalue, false )
227 return (attr == 'name' and object.andand.default_name) ||
233 attrtype = object.class.attribute_info[attr.to_sym].andand[:type]
234 if attrtype == 'text' or attr == 'description'
235 input_type = 'textarea'
236 elsif attrtype == 'datetime'
242 attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
243 rendervalue = render_attribute_as_textile( object, attr, attrvalue, false )
248 key: object.class.to_s.underscore
252 ajax_options['data-url'] = url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore)
254 ajax_options['data-url'] = url_for(action: "create", controller: object.class.to_s.pluralize.underscore)
255 ajax_options['data-pk'][:defaults] = object.attributes
257 ajax_options['data-pk'] = ajax_options['data-pk'].to_json
258 @unique_id ||= (Time.now.to_f*1000000).to_i
259 span_id = object.uuid.to_s + '-' + attr.to_s + '-' + (@unique_id += 1).to_s
261 span_tag = content_tag 'span', rendervalue, {
262 "data-emptytext" => '(none)',
263 "data-placement" => "bottom",
264 "data-type" => input_type,
265 "data-title" => "Edit #{attr.to_s.gsub '_', ' '}",
267 "data-object-uuid" => object.uuid,
268 "data-toggle" => "manual",
269 "data-value" => attrvalue,
271 :class => "editable #{is_textile?( object, attr ) ? 'editable-textile' : ''}"
272 }.merge(htmloptions).merge(ajax_options)
273 edit_tiptitle = 'edit'
274 edit_tiptitle = 'Warning: do not use hyphens in the repository name as they will be stripped' if (object.class.to_s == 'Repository' and attr == 'name')
275 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_tiptitle) + '"><i class="fa fa-fw fa-pencil"></i></a>')
276 if htmloptions[:btnplacement] == :left
277 edit_button + ' ' + span_tag
279 span_tag + ' ' + edit_button
283 def render_pipeline_component_attribute(object, attr, subattr, value_info, htmloptions={})
286 attrvalue = value_info
288 if value_info.is_a? Hash
289 if value_info[:output_of]
290 return raw("<span class='label label-default'>#{value_info[:output_of]}</span>")
292 if value_info[:dataclass]
293 dataclass = value_info[:dataclass]
295 if value_info[:optional] != nil
296 required = (value_info[:optional] != "true")
298 if value_info[:required] != nil
299 required = value_info[:required]
302 # Pick a suitable attrvalue to show as the current value (i.e.,
303 # the one that would be used if we ran the pipeline right now).
304 if value_info[:value]
305 attrvalue = value_info[:value]
306 elsif value_info[:default]
307 attrvalue = value_info[:default]
311 preconfigured_search_str = value_info[:search_for]
314 if not object.andand.attribute_editable?(attr)
315 return link_to_arvados_object_if_readable(attrvalue, attrvalue, {friendly_name: true, required: required})
320 dataclass = dataclass.constantize
324 dataclass = ArvadosBase.resource_class_for_uuid(attrvalue)
327 id = "#{object.uuid}-#{subattr.join('-')}"
332 if value_info.is_a? Hash
336 if (dataclass == Collection) or (dataclass == File)
337 selection_param = object.class.to_s.underscore + dn
338 display_value = attrvalue
339 if value_info.is_a?(Hash)
340 if (link = Link.find? value_info[:link_uuid])
341 display_value = link.name
342 elsif value_info[:link_name]
343 display_value = value_info[:link_name]
344 elsif value_info[:selection_name]
345 display_value = value_info[:selection_name]
348 if (attr == :components) and (subattr.size > 2)
349 chooser_title = "Choose a #{dataclass == Collection ? 'dataset' : 'file'} for #{object.component_input_title(subattr[0], subattr[2])}:"
351 chooser_title = "Choose a #{dataclass == Collection ? 'dataset' : 'file'}:"
353 modal_path = choose_collections_path \
354 ({ title: chooser_title,
355 filters: [['owner_uuid', '=', object.owner_uuid]].to_json,
357 action_href: pipeline_instance_path(id: object.uuid),
358 action_method: 'patch',
359 preconfigured_search_str: (preconfigured_search_str || ""),
362 use_preview_selection: dataclass == File ? true : nil,
363 selection_param: selection_param,
364 success: 'page-refresh'
367 is_readable_input = attrvalue.present? and object_readable attrvalue, Collection
368 return content_tag('div', :class => 'input-group') do
369 html = text_field_tag(dn, display_value,
371 "form-control #{'required' if required} #{'unreadable-input' if !attrvalue.andand.empty? and !is_readable_input}")
372 html + content_tag('span', :class => 'input-group-btn') do
375 { :class => "btn btn-primary",
383 if attrvalue.is_a? String
385 elsif attrvalue.is_a?(Array) or dataclass.andand.is_a?(Class)
386 # TODO: find a way to edit with x-editable
390 # When datatype is a String or Fixnum, link_to the attrvalue
391 lt = link_to attrvalue, '#', {
392 "data-emptytext" => "none",
393 "data-placement" => "bottom",
394 "data-type" => datatype,
395 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore, merge: true),
396 "data-title" => "Set value for #{subattr[-1].to_s}",
398 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
399 "data-value" => attrvalue,
400 # "clear" button interferes with form-control's up/down arrows
401 "data-clear" => false,
402 :class => "editable #{'required' if required} form-control",
409 def render_arvados_object_list_start(list, button_text, button_href,
410 params={}, *rest, &block)
411 show_max = params.delete(:show_max) || 3
412 params[:class] ||= 'btn btn-xs btn-default'
413 list[0...show_max].each { |item| yield item }
414 unless list[show_max].nil?
415 link_to(h(button_text) +
416 raw(' <i class="fa fa-fw fa-arrow-circle-right"></i>'),
417 button_href, params, *rest)
421 def render_controller_partial partial, opts
422 cname = opts.delete :controller_name
424 render opts.merge(partial: "#{cname}/#{partial}")
425 rescue ActionView::MissingTemplate
426 render opts.merge(partial: "application/#{partial}")
430 RESOURCE_CLASS_ICONS = {
431 "Collection" => "fa-archive",
432 "Group" => "fa-users",
433 "Human" => "fa-male", # FIXME: Use a more inclusive icon.
435 "KeepDisk" => "fa-hdd-o",
436 "KeepService" => "fa-exchange",
437 "Link" => "fa-arrows-h",
438 "Node" => "fa-cloud",
439 "PipelineInstance" => "fa-gears",
440 "PipelineTemplate" => "fa-gears",
441 "Repository" => "fa-code-fork",
442 "Specimen" => "fa-flask",
443 "Trait" => "fa-clipboard",
445 "VirtualMachine" => "fa-terminal",
447 DEFAULT_ICON_CLASS = "fa-cube"
449 def fa_icon_class_for_class(resource_class, default=DEFAULT_ICON_CLASS)
450 RESOURCE_CLASS_ICONS.fetch(resource_class.to_s, default)
453 def fa_icon_class_for_uuid(uuid, default=DEFAULT_ICON_CLASS)
454 fa_icon_class_for_class(resource_class_for_uuid(uuid), default)
457 def fa_icon_class_for_object(object, default=DEFAULT_ICON_CLASS)
458 case class_name = object.class.to_s
460 object.group_class ? 'fa-folder' : 'fa-users'
462 RESOURCE_CLASS_ICONS.fetch(class_name, default)
466 def chooser_preview_url_for object, use_preview_selection=false
467 case object.class.to_s
469 polymorphic_path(object, tab_pane: 'chooser_preview', use_preview_selection: use_preview_selection)
475 def render_attribute_as_textile( object, attr, attrvalue, truncate )
476 if attrvalue && (is_textile? object, attr)
477 markup = render_markup attrvalue
478 markup = markup[0,markup.index('</p>')+4] if (truncate && markup.index('</p>'))
485 def render_localized_date(date, opts="")
486 raw("<span class='utc-date' data-utc-date='#{date}' data-utc-date-opts='noseconds'>#{date}</span>")
490 def is_textile?( object, attr )
491 is_textile = object.textile_attributes.andand.include?(attr)