Add and remove collection tags with ajax.
[arvados.git] / apps / workbench / app / assets / javascripts / application.js
1 // This is a manifest file that'll be compiled into application.js, which will include all the files
2 // listed below.
3 //
4 // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5 // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6 //
7 // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8 // the compiled file.
9 //
10 // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11 // GO AFTER THE REQUIRES BELOW.
12 //
13 //= require jquery
14 //= require jquery_ujs
15 //= require bootstrap
16 //= require bootstrap/dropdown
17 //= require bootstrap/tab
18 //= require bootstrap/tooltip
19 //= require bootstrap/popover
20 //= require bootstrap/collapse
21 //= require bootstrap3-editable/bootstrap-editable
22 //= require_tree .
23
24 jQuery(function($){
25     $.ajaxSetup({
26         headers: {
27             'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
28         }
29     });
30     $('.editable').editable();
31     $('[data-toggle=tooltip]').tooltip();
32
33     $('.expand-collapse-row').on('click', function(event) {
34         var targets = $('#' + $(this).attr('data-id'));
35         if (targets.css('display') == 'none') {
36             $(this).addClass('icon-minus-sign');
37             $(this).removeClass('icon-plus-sign');
38         } else {
39             $(this).addClass('icon-plus-sign');
40             $(this).removeClass('icon-minus-sign');
41         }
42         targets.fadeToggle(200);
43     });
44     $(document).
45         on('ajax:send', function(e, xhr) {
46             $('.loading').fadeTo('fast', 1);
47         }).
48         on('ajax:complete', function(e, status) {
49             $('.loading').fadeOut('fast', 0);
50         }).
51         on('click', '.removable-tag a', function(e) {
52             var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
53             tag_span.fadeTo('fast', 0.2);
54             $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
55                    {dataType: 'json',
56                     type: 'POST',
57                     data: { '_method': 'DELETE' },
58                     context: tag_span}).
59                 done(function(data, status, jqxhr) {
60                     this.remove();
61                 }).
62                 fail(function(jqxhr, status, error) {
63                     this.addClass('label-danger').fadeTo('fast', '1');
64                 });
65             return false;
66         }).
67         on('click', 'a.add-tag-button', function(e) {
68             var jqxhr;
69             var new_tag_uuid = 'new-tag-' + Math.random();
70             var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
71             var new_tag = window.prompt("Add tag for collection "+
72                                     tag_head_uuid,
73                                     "");
74             if (new_tag == null)
75                 return false;
76             var new_tag_span =
77                 $('<span class="label label-info removable-tag"></span>').
78                 attr('data-tag-link-uuid', new_tag_uuid).
79                 text(new_tag).
80                 css('opacity', '0.2').
81                 append('&nbsp;<a><i class="glyphicon glyphicon-trash"></i></a>&nbsp;');
82             $(this).
83                 parent().
84                 find('>span').
85                 append(new_tag_span).
86                 append(' ');
87             $.ajax($(this).attr('data-remote-href'),
88                            {dataType: 'json',
89                             type: $(this).attr('data-remote-method'),
90                             data: {
91                                 'link[head_kind]': 'arvados#collection',
92                                 'link[head_uuid]': tag_head_uuid,
93                                 'link[link_class]': 'tag',
94                                 'link[name]': new_tag
95                             },
96                             context: new_tag_span}).
97                 done(function(data, status, jqxhr) {
98                     this.attr('data-tag-link-uuid', data.uuid).
99                         fadeTo('fast', '1');
100                 }).
101                 fail(function(jqxhr, status, error) {
102                     this.addClass('label-danger').fadeTo('fast', '1');
103                 });
104             return false;
105         });
106
107     HeaderRowFixer = function(selector) {
108         var tables = $(selector);
109         this.duplicateTheadTr = function() {
110             $('>tbody', tables).each(function(){
111                 $(this).prepend($('thead>tr', this).clone().css('opacity:0'));
112             });
113         }
114         this.fixThead = function() {
115             tables.each(function() {
116                 var widths = [];
117                 $('> tbody > tr:eq(1) > td', this).each( function(i,v){
118                     widths.push($(v).width());
119                 });
120                 for(i=0;i<widths.length;i++) {
121                     $('thead th:eq('+i+')', this).width(widths[i]);
122                 }
123             });
124         }
125     }
126     var fixer = new HeaderRowFixer('.table-fixed-header-row');
127     fixer.fixThead();
128     fixer.duplicateTheadTr();
129     $(window).resize(function(){
130         fixer.fixThead();
131     });
132 })(jQuery);