9426: delete all tags
[arvados.git] / apps / workbench / app / assets / javascripts / edit_collection_tags.js
1 // On loading of a collection, enable the "lock" button and
2 // disable all file modification controls (upload, rename, delete)
3 $(document).
4     on('click', '.collection-tag-save, .collection-tag-cancel', function(event) {
5         $('.edit-collection-tags').removeClass('disabled');
6         $('#edit-collection-tags').attr("title", "Edit tags");
7         $('.collection-tag-add').addClass('hide');
8         $('.collection-tag-remove').addClass('hide');
9         $('.collection-tag-save').addClass('hide');
10         $('.collection-tag-cancel').addClass('hide');
11         $('.collection-tag-field').prop("contenteditable", false);
12     }).
13     on('click', '.edit-collection-tags', function(event) {
14         $('.edit-collection-tags').addClass('disabled');
15         $('#edit-collection-tags').attr("title", "");
16         $('.collection-tag-add').removeClass('hide');
17         $('.collection-tag-remove').removeClass('hide');
18         $('.collection-tag-save').removeClass('hide');
19         $('.collection-tag-cancel').removeClass('hide');
20         $('.collection-tag-field').prop("contenteditable", true);
21         $('div').remove('.collection-tags-status-label');
22     }).
23     on('click', '.collection-tag-save', function(e){
24       var tag_data = {};
25       var $tags = $(".collection-tags-table");
26       $tags.find('tr').each(function (i, el) {
27         var $tds = $(this).find('td');
28         var $key = $tds.eq(1).text();
29         if ($key && $key.trim().length > 0) {
30           tag_data[$key.trim()] = $tds.eq(2).text().trim();
31         }
32       });
33
34       if(jQuery.isEmptyObject(tag_data)){
35         tag_data["empty"]=true
36       } else {
37         tag_data = {tag_data}
38       }
39
40       $.ajax($(location).attr('pathname')+'/save_tags', {
41           type: 'POST',
42           data: tag_data
43       }).success(function(data, status, jqxhr) {
44         $('.collection-tags-status').append('<div class="collection-tags-status-label alert alert-success"><p class="contain-align-left">Saved successfully.</p></div>');
45       }).fail(function(jqxhr, status, error) {
46         $('.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>');
47       });
48     }).
49     on('click', '.collection-tag-cancel', function(e){
50       $.ajax($(location).attr('pathname')+'/tags', {
51           type: 'GET'
52       });
53     });
54
55 jQuery(function($){
56   $(document).on('click', '.collection-tag-remove', function(e) {
57     $(this).parents('tr').detach();
58   });
59
60   $(document).on('click', '.collection-tag-add', function(e) {
61     var $collection_tags = $(this).closest('.collection-tags-container');
62     var $clone = $collection_tags.find('tr.hide').clone(true).removeClass('hide');
63     $collection_tags.find('table').append($clone);
64   });
65 });