Merge branch '2986-arv-edit' closes #2986
[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 bootstrap/modal
22 //= require bootstrap/button
23 //= require bootstrap3-editable/bootstrap-editable
24 //= require_tree .
25
26 jQuery(function($){
27     $.ajaxSetup({
28         headers: {
29             'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
30         }
31     });
32     $('[data-toggle=tooltip]').tooltip();
33
34     $('.expand-collapse-row').on('click', function(event) {
35         var targets = $('#' + $(this).attr('data-id'));
36         if (targets.css('display') == 'none') {
37             $(this).addClass('icon-minus-sign');
38             $(this).removeClass('icon-plus-sign');
39         } else {
40             $(this).addClass('icon-plus-sign');
41             $(this).removeClass('icon-minus-sign');
42         }
43         targets.fadeToggle(200);
44     });
45
46     var ajaxCount = 0;
47
48     $(document).
49         on('ajax:send', function(e, xhr) {
50             ajaxCount += 1;
51             if (ajaxCount == 1) {
52                 $('.loading').fadeTo('fast', 1);
53             }
54         }).
55         on('ajax:complete', function(e, status) {
56             ajaxCount -= 1;
57             if (ajaxCount == 0) {
58                 $('.loading').fadeOut('fast', 0);
59             }
60         }).
61         on('ajaxSend', function(e, xhr) {
62             // jQuery triggers 'ajaxSend' event when starting an ajax call, but
63             // rails-generated ajax triggers generate 'ajax:send'.  Workbench
64             // event listeners currently expect 'ajax:send', so trigger the
65             // rails event in response to the jQuery one.
66             $(document).trigger('ajax:send');
67         }).
68         on('ajaxComplete', function(e, xhr) {
69             // See comment above about ajaxSend/ajax:send
70             $(document).trigger('ajax:complete');
71         }).
72         on('click', '.removable-tag a', function(e) {
73             var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
74             tag_span.fadeTo('fast', 0.2);
75             $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
76                    {dataType: 'json',
77                     type: 'POST',
78                     data: { '_method': 'DELETE' },
79                     context: tag_span}).
80                 done(function(data, status, jqxhr) {
81                     this.remove();
82                 }).
83                 fail(function(jqxhr, status, error) {
84                     this.addClass('label-danger').fadeTo('fast', '1');
85                 });
86             return false;
87         }).
88         on('click', 'a.add-tag-button', function(e) {
89             var jqxhr;
90             var new_tag_uuid = 'new-tag-' + Math.random();
91             var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
92             var new_tag = window.prompt("Add tag for collection "+
93                                     tag_head_uuid,
94                                     "");
95             if (new_tag == null)
96                 return false;
97             var new_tag_span =
98                 $('<span class="label label-info removable-tag"></span>').
99                 attr('data-tag-link-uuid', new_tag_uuid).
100                 text(new_tag).
101                 css('opacity', '0.2').
102                 append('&nbsp;<a title="Delete tag"><i class="glyphicon glyphicon-trash"></i></a>&nbsp;');
103             $(this).
104                 parent().
105                 find('>span').
106                 append(new_tag_span).
107                 append('&nbsp; ');
108             $.ajax($(this).attr('data-remote-href'),
109                            {dataType: 'json',
110                             type: $(this).attr('data-remote-method'),
111                             data: {
112                                 'link[head_uuid]': tag_head_uuid,
113                                 'link[link_class]': 'tag',
114                                 'link[name]': new_tag
115                             },
116                             context: new_tag_span}).
117                 done(function(data, status, jqxhr) {
118                     this.attr('data-tag-link-uuid', data.uuid).
119                         fadeTo('fast', '1');
120                 }).
121                 fail(function(jqxhr, status, error) {
122                     this.addClass('label-danger').fadeTo('fast', '1');
123                 });
124             return false;
125         });
126
127     $(document).
128         on('ajax:complete ready', function() {
129             // See http://getbootstrap.com/javascript/#buttons
130             $('.btn').button();
131         });
132
133     $(document).
134         on('ready ajax:complete', function() {
135             $('[data-toggle~=tooltip]').tooltip({container:'body'});
136         });
137
138     HeaderRowFixer = function(selector) {
139         this.duplicateTheadTr = function() {
140             $(selector).each(function() {
141                 var the_table = this;
142                 if ($('>tbody>tr:first>th', the_table).length > 0)
143                     return;
144                 $('>tbody', the_table).
145                     prepend($('>thead>tr', the_table).
146                             clone().
147                             css('opacity', 0));
148             });
149         }
150         this.fixThead = function() {
151             $(selector).each(function() {
152                 var widths = [];
153                 $('> tbody > tr:eq(1) > td', this).each( function(i,v){
154                     widths.push($(v).width());
155                 });
156                 for(i=0;i<widths.length;i++) {
157                     $('thead th:eq('+i+')', this).width(widths[i]);
158                 }
159             });
160         }
161     }
162
163     var fixer = new HeaderRowFixer('.table-fixed-header-row');
164     fixer.duplicateTheadTr();
165     fixer.fixThead();
166     $(window).resize(function(){
167         fixer.fixThead();
168     });
169     $(document).on('ajax:complete', function(e, status) {
170         fixer.duplicateTheadTr();
171         fixer.fixThead();
172     });
173 });