Editing input values of pipeline_instances mostly works, but needs polishing.
[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           begin
63             link_name = resource_class.find(link_uuid).friendly_link_name
64           rescue RuntimeError
65             # If that lookup failed, the link will too. So don't make one.
66             return attrvalue
67           end
68         end
69         if opts[:with_class_name]
70           link_name = "#{resource_class.to_s}: #{link_name}"
71         end
72       end
73       style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
74       link_to link_name, { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
75     else
76       attrvalue
77     end
78   end
79
80   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
81     attrvalue = object.send(attr) if attrvalue.nil?
82     return attrvalue if !object.attribute_editable? attr
83
84     input_type = 'text'
85     case object.class.attribute_info[attr.to_sym].andand[:type]
86     when 'text'
87       input_type = 'textarea'
88     when 'datetime'
89       input_type = 'date'
90     else
91       input_type = 'text'
92     end
93
94     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
95
96     link_to attrvalue.to_s, '#', {
97       "data-emptytext" => "none",
98       "data-placement" => "bottom",
99       "data-type" => input_type,
100       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
101       "data-title" => "Update #{attr.gsub '_', ' '}",
102       "data-name" => attr,
103       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
104       :class => "editable"
105     }.merge(htmloptions)
106   end
107
108   def render_editable_subattribute(object, attr, subattr, template, htmloptions={})
109     attrvalue = object.send(attr)
110     subattr.each do |k|
111       if attrvalue and attrvalue.is_a? Hash
112         attrvalue = attrvalue[k]
113       else
114         break
115       end
116     end
117
118     datatype = nil
119     if template
120       puts "Template is #{template.class} #{template.is_a? Hash} #{template}"
121       if template.is_a? Hash
122         if template[:output_of]
123           return raw("<span class='label label-default'>#{template[:output_of]}</span>")
124         elsif template[:datatype]
125           datatype = template[:datatype]
126         end
127       end
128     end
129
130     return attrvalue if !object.attribute_editable? attr
131
132     if not datatype
133       dataclass = ArvadosBase.resource_class_for_uuid(template)
134       if dataclass
135         datatype = 'select'
136       else
137         if template.is_a? Array
138           # ?!?
139         elsif template.is_a? String
140           if /^\d+$/.match(template)
141             datatype = 'number'
142           else
143             datatype = 'text'
144           end
145         end
146       end
147     end
148
149     id = "#{object.uuid}-#{subattr.join('-')}"
150     dn = "[#{attr}]"
151     subattr.each do |a|
152       dn += "[#{a}]"
153     end
154
155     if dataclass
156       items = []
157       dataclass.where(uuid: attrvalue).each do |item|
158         items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
159       end
160       dataclass.limit(19).each do |item|
161         items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
162       end
163     end
164
165     lt = link_to attrvalue, '#', {
166       "data-emptytext" => "none",
167       "data-placement" => "bottom",
168       "data-type" => datatype,
169       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
170       "data-title" => "Update #{subattr[-1].to_s.titleize}",
171       "data-name" => dn,
172       "data-value" => attrvalue,
173       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
174       :class => "editable",
175       :id => id
176     }.merge(htmloptions)
177
178     lt += raw(<<EOF
179
180 <script>
181     add_form_selection_sources(#{items.to_json});
182     $('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });
183 </script>
184 EOF
185 )
186     lt 
187   end
188 end