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