Add and remove collection tags with ajax.
authorTom Clegg <tom@curoverse.com>
Sun, 16 Feb 2014 10:17:15 +0000 (02:17 -0800)
committerTom Clegg <tom@curoverse.com>
Sun, 16 Feb 2014 10:17:15 +0000 (02:17 -0800)
apps/workbench/app/assets/javascripts/application.js
apps/workbench/app/controllers/application_controller.rb
apps/workbench/app/controllers/collections_controller.rb
apps/workbench/app/views/collections/_index_tbody.html.erb

index 2f3b0944465c4b7f7677f735d4930cb6be0097ac..92cce3e31c4d501645e0598f96a8135f5873f716 100644 (file)
@@ -49,24 +49,58 @@ jQuery(function($){
             $('.loading').fadeOut('fast', 0);
         }).
         on('click', '.removable-tag a', function(e) {
-            $(this).parents('[data-tag-link-uuid]').eq(0).remove();
+            var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
+            tag_span.fadeTo('fast', 0.2);
+            $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
+                   {dataType: 'json',
+                    type: 'POST',
+                    data: { '_method': 'DELETE' },
+                    context: tag_span}).
+                done(function(data, status, jqxhr) {
+                    this.remove();
+                }).
+                fail(function(jqxhr, status, error) {
+                    this.addClass('label-danger').fadeTo('fast', '1');
+                });
             return false;
         }).
         on('click', 'a.add-tag-button', function(e) {
-            new_tag = window.prompt("Add tag for collection "+
-                                    $(this).parents('tr').attr('data-object-uuid'),
+            var jqxhr;
+            var new_tag_uuid = 'new-tag-' + Math.random();
+            var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
+            var new_tag = window.prompt("Add tag for collection "+
+                                    tag_head_uuid,
                                     "");
-            if (new_tag != null) {
-                new_tag_uuid = 'xxx';
-                $(this).
-                    parent().
-                    find('>span').
-                    append($('<span class="label label-info removable-tag" data-tag-link-uuid=""></span>').
-                           attr('data-tag-link-uuid', new_tag_uuid).
-                           text(new_tag).
-                           append('&nbsp;<a><i class="glyphicon glyphicon-trash"></i></a>&nbsp;')).
-                    append(' ');
-            }
+            if (new_tag == null)
+                return false;
+            var new_tag_span =
+                $('<span class="label label-info removable-tag"></span>').
+                attr('data-tag-link-uuid', new_tag_uuid).
+                text(new_tag).
+                css('opacity', '0.2').
+                append('&nbsp;<a><i class="glyphicon glyphicon-trash"></i></a>&nbsp;');
+            $(this).
+                parent().
+                find('>span').
+                append(new_tag_span).
+                append(' ');
+            $.ajax($(this).attr('data-remote-href'),
+                           {dataType: 'json',
+                            type: $(this).attr('data-remote-method'),
+                            data: {
+                                'link[head_kind]': 'arvados#collection',
+                                'link[head_uuid]': tag_head_uuid,
+                                'link[link_class]': 'tag',
+                                'link[name]': new_tag
+                            },
+                            context: new_tag_span}).
+                done(function(data, status, jqxhr) {
+                    this.attr('data-tag-link-uuid', data.uuid).
+                        fadeTo('fast', '1');
+                }).
+                fail(function(jqxhr, status, error) {
+                    this.addClass('label-danger').fadeTo('fast', '1');
+                });
             return false;
         });
 
index 02ebc8b3a198090c83fe98e5c3f99f18c0993c9d..e94428e92dca51ad00e415f38dac8ff69c35c4ef 100644 (file)
@@ -107,9 +107,10 @@ class ApplicationController < ActionController::Base
   end
 
   def create
-    @object ||= model_class.new params[model_class.to_s.singularize.to_sym]
+    @object ||= model_class.new params[model_class.to_s.underscore.singularize]
     @object.save!
     respond_to do |f|
+      f.json { render json: @object }
       f.html {
         redirect_to(params[:return_to] || @object)
       }
@@ -120,6 +121,7 @@ class ApplicationController < ActionController::Base
   def destroy
     if @object.destroy
       respond_to do |f|
+        f.json { render json: @object }
         f.html {
           redirect_to(params[:return_to] || :back)
         }
index f8895da5d38c8dd7ca8fcc8da70252f5ecdfebb3..b6997b97595d59c1f3f905995628e34da381e224 100644 (file)
@@ -19,7 +19,7 @@ class CollectionsController < ApplicationController
     @collection_info = {}
     @collections.each do |c|
       @collection_info[c.uuid] = {
-        tags: [],
+        tag_links: [],
         wanted: false,
         wanted_by_me: false,
         provenance: [],
@@ -31,7 +31,7 @@ class CollectionsController < ApplicationController
       info = @collection_info[link.head_uuid]
       case link.link_class
       when 'tag'
-        info[:tags] << link.name
+        info[:tag_links] << link
       when 'resources'
         info[:wanted] = true
         info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
index f7209ee10da7c2f190a458c26f2f369b5adff59e..fad520ebd1a65bf995785bfaf2cca8377e9aa94a 100644 (file)
     <% end %>
   </td>
   <td class="add-tag-button">
-    <a class="btn btn-xs btn-info add-tag-button pull-right"><i class="glyphicon glyphicon-plus"></i>&nbsp;Add</a>
+    <a class="btn btn-xs btn-info add-tag-button pull-right" data-remote-href="<%= url_for(controller: 'links', action: 'create') %>" data-remote-method="post"><i class="glyphicon glyphicon-plus"></i>&nbsp;Add</a>
     <span>
     <% if @collection_info[c.uuid] %>
-      <% @collection_info[c.uuid][:tags].uniq.each do |tag| %>
-        <span class="label label-info removable-tag" data-tag-link-uuid=""><%= tag %>&nbsp;<a><i class="glyphicon glyphicon-trash"></i></a></span>
+      <% @collection_info[c.uuid][:tag_links].each do |tag_link| %>
+        <span class="label label-info removable-tag" data-tag-link-uuid="<%= tag_link.uuid %>"><%= tag_link.name %>&nbsp;<a><i class="glyphicon glyphicon-trash"></i></a></span>
       <% end %>
     <% end %>
     </span>