9426: collection Tags tab (in progress)
authorradhika <radhika@curoverse.com>
Fri, 16 Jun 2017 03:06:13 +0000 (23:06 -0400)
committerradhika <radhika@curoverse.com>
Fri, 23 Jun 2017 18:26:08 +0000 (14:26 -0400)
Arvados-DCO-1.1-Signed-off-by: Radhika Chippada <radhika@curoverse.com>

apps/workbench/app/assets/javascripts/edit_collection_tags.js [new file with mode: 0644]
apps/workbench/app/controllers/collections_controller.rb
apps/workbench/app/views/collections/_show_tag_rows.html.erb [new file with mode: 0644]
apps/workbench/app/views/collections/_show_tags.html.erb [new file with mode: 0644]
apps/workbench/app/views/collections/save_tags.js.erb [new file with mode: 0644]
apps/workbench/config/routes.rb

diff --git a/apps/workbench/app/assets/javascripts/edit_collection_tags.js b/apps/workbench/app/assets/javascripts/edit_collection_tags.js
new file mode 100644 (file)
index 0000000..26b0ba7
--- /dev/null
@@ -0,0 +1,52 @@
+// On loading of a collection, enable the "lock" button and
+// disable all file modification controls (upload, rename, delete)
+$(document).
+    on('click', '.collection-tag-save, .collection-tag-cancel', function(event) {
+        $('.edit-collection-tags').removeClass('disabled');
+        $('.collection-tag-add').addClass('hide');
+        $('.collection-tag-remove').addClass('hide');
+        $('.collection-tag-save').addClass('hide');
+        $('.collection-tag-cancel').addClass('hide');
+        $('.collection-tag-field').prop("contenteditable", false);
+    }).
+    on('click', '.edit-collection-tags', function(event) {
+        $('.edit-collection-tags').addClass('disabled');
+        $('.collection-tag-add').removeClass('hide');
+        $('.collection-tag-remove').removeClass('hide');
+        $('.collection-tag-save').removeClass('hide');
+        $('.collection-tag-cancel').removeClass('hide');
+        $('.collection-tag-field').prop("contenteditable", true);
+        $('div').remove('.collection-tags-status-label');
+    }).
+    on('click', '.collection-tag-save', function(e){
+      var tag_data = {};
+      var $tags = $(".collection-tags-table");
+      $tags.find('tr').each(function (i, el) {
+        var $tds = $(this).find('td');
+        var $key = $tds.eq(1).text();
+        if ($key && $key.trim().length > 0) {
+          tag_data[$key.trim()] = $tds.eq(2).text().trim();
+        }
+      });
+
+      $.ajax($(location).attr('pathname')+'/save_tags', {
+          type: 'POST',
+          data: {tag_data}
+      }).success(function(data, status, jqxhr) {
+        $('.collection-tags-status').append('<div class="collection-tags-status-label alert alert-success"><p class="contain-align-left">Saved successfully.</p></div>');
+      }).fail(function(jqxhr, status, error) {
+        $('.collection-tags-status').append('<div class="collection-tags-status-label alert alert-danger"><p class="contain-align-left">We are sorry. There was an error saving tags. Please try again.</p></div>');
+      });
+    });
+
+jQuery(function($){
+  $(document).on('click', '.collection-tag-remove', function(e) {
+    $(this).parents('tr').detach();
+  });
+
+  $(document).on('click', '.collection-tag-add', function(e) {
+    var $collection_tags = $(this).closest('.collection-tags-container');
+    var $clone = $collection_tags.find('tr.hide').clone(true).removeClass('hide');
+    $collection_tags.find('table').append($clone);
+  });
+});
index dc9ed43c409b64a2838ad74d3e165609c49e1e62..8b3cc2feb860f2483b5572930ca5a4ac32053cb1 100644 (file)
@@ -20,7 +20,7 @@ class CollectionsController < ApplicationController
   RELATION_LIMIT = 5
 
   def show_pane_list
-    panes = %w(Files Upload Provenance_graph Used_by Advanced)
+    panes = %w(Files Upload Tags Provenance_graph Used_by Advanced)
     panes = panes - %w(Upload) unless (@object.editable? rescue false)
     panes
   end
@@ -345,6 +345,15 @@ class CollectionsController < ApplicationController
     end
   end
 
+  def save_tags
+    props = @object.properties
+    props[:tags] = params['tag_data']
+    if @object.update_attributes properties: props
+    else
+      self.render_error status: 422
+    end
+  end
+
   protected
 
   def find_usable_token(token_list)
diff --git a/apps/workbench/app/views/collections/_show_tag_rows.html.erb b/apps/workbench/app/views/collections/_show_tag_rows.html.erb
new file mode 100644 (file)
index 0000000..da69925
--- /dev/null
@@ -0,0 +1,26 @@
+<%
+  tags = object.properties[:tags]
+%>
+
+        <% tags.andand.each do |k, v| %>
+          <tr class="collection-tag-<%=k%>">
+            <td>
+              <i class="glyphicon glyphicon-remove collection-tag-remove hide" style="cursor: pointer;"></i>
+            </td>
+            <td class="collection-tag-field">
+              <%= k %>
+            </td>
+            <td class="collection-tag-field">
+              <%= v %>
+            </td>
+          </tr>
+        <% end %>
+
+        <!-- A hidden row to add new tag -->
+        <tr class="collection-tag-hidden hide">
+          <td>
+            <i class="glyphicon glyphicon-remove collection-tag-remove hide" style="cursor: pointer"></i>
+          </td>
+          <td class="collection-tag-field"></td>
+          <td class="collection-tag-field"></td>
+        </tr>
diff --git a/apps/workbench/app/views/collections/_show_tags.html.erb b/apps/workbench/app/views/collections/_show_tags.html.erb
new file mode 100644 (file)
index 0000000..e1b0de7
--- /dev/null
@@ -0,0 +1,46 @@
+<%
+  object = @object unless object
+%>
+
+  <div class="collection-tags-container" style="padding-left:2em;padding-right:2em;">
+    <% if object.editable? %>
+      <p title="Edit tags" id="edit-collection-tags">
+        <button type="button" class="btn btn-primary edit-collection-tags">Edit</button>
+      </p>
+    <% end %>
+
+    <table class="table table-condensed table-fixedlayout collection-tags-table" border="1">
+      <colgroup>
+        <col width="5%" />
+        <col width="25%" />
+        <col width="70%" />
+      </colgroup>
+
+      <thead>
+        <tr>
+          <th></th>
+          <th>Key</th>
+          <th>Value</th>
+        </tr>
+      </thead>
+
+      <tbody class="collection-tag-rows">
+        <%= render partial: 'show_tag_rows', locals: {object: object} %>
+      </tbody>
+    </table>
+    <div>
+      <% if object.editable? %>
+        <div class="pull-left">
+          <button class="btn btn-primary btn-sm collection-tag-add hide"><i class="glyphicon glyphicon-plus"></i> Add new tag </button>
+        </div>
+        <div class="pull-right">
+          <%= link_to(save_tags_collection_path, {class: 'btn btn-primary collection-tag-save hide', :remote => true, method: 'post', return_to: request.url}) do %>
+            Save
+          <% end %>
+          <button type="button" class="btn btn-sm btn-primary collection-tag-cancel hide">Cancel</button>
+        </div>
+
+        <div><div class="collection-tags-status"/></div></div>
+      <% end %>
+    </div>
+  </div>
diff --git a/apps/workbench/app/views/collections/save_tags.js.erb b/apps/workbench/app/views/collections/save_tags.js.erb
new file mode 100644 (file)
index 0000000..e69de29
index badb471d64ae666924fb06a21a408237bbed3f26..880cc15f6dd5db52feacc62648472fc655c3035b 100644 (file)
@@ -88,6 +88,7 @@ ArvadosWorkbench::Application.routes.draw do
     post 'unshare', :on => :member
     get 'choose', on: :collection
     post 'remove_selected_files', on: :member
+    post 'save_tags', on: :member
   end
   get('/collections/download/:uuid/:reader_token/*file' => 'collections#show_file',
       format: false)