9426: tags display
[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       $.ajax($(location).attr('pathname')+'/save_tags', {
35           type: 'POST',
36           data: {tag_data}
37       }).success(function(data, status, jqxhr) {
38         $('.collection-tags-status').append('<div class="collection-tags-status-label alert alert-success"><p class="contain-align-left">Saved successfully.</p></div>');
39       }).fail(function(jqxhr, status, error) {
40         $('.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>');
41       });
42     }).
43     on('click', '.collection-tag-cancel', function(e){
44       $.ajax($(location).attr('pathname')+'/tags', {
45           type: 'POST'
46       });
47     });
48
49 jQuery(function($){
50   $(document).on('click', '.collection-tag-remove', function(e) {
51     $(this).parents('tr').detach();
52   });
53
54   $(document).on('click', '.collection-tag-add', function(e) {
55     var $collection_tags = $(this).closest('.collection-tags-container');
56     var $clone = $collection_tags.find('tr.hide').clone(true).removeClass('hide');
57     $collection_tags.find('table').append($clone);
58   });
59 });