2760: Replace dashboard tables with news feed. More ability to link via names rather...
[arvados.git] / apps / workbench / app / helpers / application_helper.rb
index 7231ff8addaac462791c8001f9f5fd7b16941f6d..f24cdb7a9be993408a1958287963ee4a3d913b1d 100644 (file)
@@ -15,6 +15,24 @@ module ApplicationHelper
     raw RedCloth.new(markup).to_html
   end
 
+  def human_readable_time_html t
+    begin
+      t = Time.parse(t) unless t.is_a? Time
+    rescue
+      return ''
+    end
+    now = Time.now
+    delta = (now - t).abs
+    if delta < 12.hours
+      t.strftime('%I:%M%P').downcase.sub(/^0/,'')
+    elsif (delta < 2.months and now>t) or t.strftime('%Y')==now.strftime('%Y')
+      # recent, or this year (hopefully unambiguous in context)
+      t.strftime('%b %d').sub(/ 0/,' ')
+    else
+      t.strftime('%b %d, %Y').sub(/ 0/,' ')
+    end
+  end
+
   def human_readable_bytes_html(n)
     return h(n) unless n.is_a? Fixnum
 
@@ -83,7 +101,9 @@ module ApplicationHelper
       if !link_name
         link_name = link_uuid
 
-        if opts[:friendly_name]
+        if opts[:name_link]
+          link_name = opts[:name_link].name
+        elsif opts[:friendly_name]
           if attrvalue.respond_to? :friendly_link_name
             link_name = attrvalue.friendly_link_name
           else
@@ -98,7 +118,9 @@ module ApplicationHelper
         if opts[:with_class_name]
           link_name = "#{resource_class.to_s}: #{link_name}"
         end
-        if !opts[:no_tags] and resource_class == Collection
+        if !opts[:name_link] and
+            !opts[:no_tags] and
+            resource_class == Collection
           Link.where(head_uuid: link_uuid, link_class: ["tag", "identifier"]).each do |tag|
             link_name += ' <span class="label label-info">' + html_escape(tag.name) + '</span>'
           end
@@ -141,16 +163,29 @@ module ApplicationHelper
 
     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
 
-    link_to attrvalue.to_s, '#', {
+    ajax_options = {
+      "data-pk" => {
+        id: object.uuid,
+        key: object.class.to_s.underscore
+      }
+    }
+    if object.uuid
+      ajax_options['data-url'] = url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore)
+    else
+      ajax_options['data-url'] = url_for(action: "create", controller: object.class.to_s.pluralize.underscore)
+      ajax_options['data-pk'][:defaults] = object.attributes
+    end
+    ajax_options['data-pk'] = ajax_options['data-pk'].to_json
+
+    content_tag 'span', attrvalue.to_s, {
       "data-emptytext" => "none",
       "data-placement" => "bottom",
       "data-type" => input_type,
-      "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
       "data-title" => "Update #{attr.gsub '_', ' '}",
       "data-name" => attr,
-      "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
+      "data-object-uuid" => object.uuid,
       :class => "editable"
-    }.merge(htmloptions)
+    }.merge(htmloptions).merge(ajax_options)
   end
 
   def render_pipeline_component_attribute(object, attr, subattr, value_info, htmloptions={})
@@ -218,27 +253,27 @@ module ApplicationHelper
       dn += '[value]'
     end
 
-    items = []
+    selectables = []
     attrtext = attrvalue
     if dataclass and dataclass.is_a? Class
       if attrvalue and !attrvalue.empty?
         Link.where(head_uuid: attrvalue, link_class: ["tag", "identifier"]).each do |tag|
           attrtext += " [#{tag.name}]"
         end
-        items.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
+        selectables.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
       end
       #dataclass.where(uuid: attrvalue).each do |item|
-      #  items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
+      #  selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
       #end
       itemuuids = []
       dataclass.limit(10).each do |item|
         itemuuids << item.uuid
-        items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
+        selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
       end
       Link.where(head_uuid: itemuuids, link_class: ["tag", "identifier"]).each do |tag|
-        items.each do |item|
-          if item.uuid == tag.head_uuid
-            item.name += ' [' + tag.name + ']'
+        selectables.each do |selectable|
+          if selectable['uuid'] == tag.head_uuid
+            selectable['name'] += ' [' + tag.name + ']'
           end
         end
       end
@@ -260,8 +295,8 @@ module ApplicationHelper
 
     lt += raw("\n<script>")
 
-    if items.any?
-      lt += raw("add_form_selection_sources(#{items.to_json});\n")
+    if selectables.any?
+      lt += raw("add_form_selection_sources(#{selectables.to_json});\n")
     end
 
     lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")