Merge branch '2800-python-global-state' into 2800-pgs
[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 wiselinks
25 //= require_tree .
26
27 jQuery(function($){
28     $.ajaxSetup({
29         headers: {
30             'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
31         }
32     });
33
34     $(document).ajaxStart(function(){
35       $('.modal-with-loading-spinner .spinner').show();
36     }).ajaxStop(function(){
37       $('.modal-with-loading-spinner .spinner').hide();
38     });
39
40     $('[data-toggle=tooltip]').tooltip();
41
42     $('.expand-collapse-row').on('click', function(event) {
43         var targets = $('#' + $(this).attr('data-id'));
44         if (targets.css('display') == 'none') {
45             $(this).addClass('icon-minus-sign');
46             $(this).removeClass('icon-plus-sign');
47         } else {
48             $(this).addClass('icon-plus-sign');
49             $(this).removeClass('icon-minus-sign');
50         }
51         targets.fadeToggle(200);
52     });
53
54     var ajaxCount = 0;
55
56     $(document).
57         on('ajax:send', function(e, xhr) {
58             ajaxCount += 1;
59             if (ajaxCount == 1) {
60                 $('.loading').fadeTo('fast', 1);
61             }
62         }).
63         on('ajax:complete', function(e, status) {
64             ajaxCount -= 1;
65             if (ajaxCount == 0) {
66                 $('.loading').fadeOut('fast', 0);
67             }
68         }).
69         on('ajaxSend', function(e, xhr) {
70             // jQuery triggers 'ajaxSend' event when starting an ajax call, but
71             // rails-generated ajax triggers generate 'ajax:send'.  Workbench
72             // event listeners currently expect 'ajax:send', so trigger the
73             // rails event in response to the jQuery one.
74             $(document).trigger('ajax:send');
75         }).
76         on('ajaxComplete', function(e, xhr) {
77             // See comment above about ajaxSend/ajax:send
78             $(document).trigger('ajax:complete');
79         }).
80         on('click', '.removable-tag a', function(e) {
81             var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
82             tag_span.fadeTo('fast', 0.2);
83             $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
84                    {dataType: 'json',
85                     type: 'POST',
86                     data: { '_method': 'DELETE' },
87                     context: tag_span}).
88                 done(function(data, status, jqxhr) {
89                     this.remove();
90                 }).
91                 fail(function(jqxhr, status, error) {
92                     this.addClass('label-danger').fadeTo('fast', '1');
93                 });
94             return false;
95         }).
96         on('click', 'a.add-tag-button', function(e) {
97             var jqxhr;
98             var new_tag_uuid = 'new-tag-' + Math.random();
99             var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
100             var new_tag = window.prompt("Add tag for collection "+
101                                     tag_head_uuid,
102                                     "");
103             if (new_tag == null)
104                 return false;
105             var new_tag_span =
106                 $('<span class="label label-info removable-tag"></span>').
107                 attr('data-tag-link-uuid', new_tag_uuid).
108                 text(new_tag).
109                 css('opacity', '0.2').
110                 append('&nbsp;<a title="Delete tag"><i class="glyphicon glyphicon-trash"></i></a>&nbsp;');
111             $(this).
112                 parent().
113                 find('>span').
114                 append(new_tag_span).
115                 append('&nbsp; ');
116             $.ajax($(this).attr('data-remote-href'),
117                            {dataType: 'json',
118                             type: $(this).attr('data-remote-method'),
119                             data: {
120                                 'link[head_uuid]': tag_head_uuid,
121                                 'link[link_class]': 'tag',
122                                 'link[name]': new_tag
123                             },
124                             context: new_tag_span}).
125                 done(function(data, status, jqxhr) {
126                     this.attr('data-tag-link-uuid', data.uuid).
127                         fadeTo('fast', '1');
128                 }).
129                 fail(function(jqxhr, status, error) {
130                     this.addClass('label-danger').fadeTo('fast', '1');
131                 });
132             return false;
133         });
134
135     $(document).
136         on('ajax:complete ready', function() {
137             // See http://getbootstrap.com/javascript/#buttons
138             $('.btn').button();
139         }).
140         on('ready ajax:complete', function() {
141             $('[data-toggle~=tooltip]').tooltip({container:'body'});
142         }).
143         on('ready ajax:complete', function() {
144             // This makes the dialog close on Esc key, obviously.
145             $('.modal').attr('tabindex', '-1')
146         });
147
148     HeaderRowFixer = function(selector) {
149         this.duplicateTheadTr = function() {
150             $(selector).each(function() {
151                 var the_table = this;
152                 if ($('>tbody>tr:first>th', the_table).length > 0)
153                     return;
154                 $('>tbody', the_table).
155                     prepend($('>thead>tr', the_table).
156                             clone().
157                             css('opacity', 0));
158             });
159         }
160         this.fixThead = function() {
161             $(selector).each(function() {
162                 var widths = [];
163                 $('> tbody > tr:eq(1) > td', this).each( function(i,v){
164                     widths.push($(v).width());
165                 });
166                 for(i=0;i<widths.length;i++) {
167                     $('thead th:eq('+i+')', this).width(widths[i]);
168                 }
169             });
170         }
171     }
172
173     var fixer = new HeaderRowFixer('.table-fixed-header-row');
174     fixer.duplicateTheadTr();
175     fixer.fixThead();
176     $(window).resize(function(){
177         fixer.fixThead();
178     });
179     $(document).on('ajax:complete', function(e, status) {
180         fixer.duplicateTheadTr();
181         fixer.fixThead();
182     });
183
184     $(document).ready(function() {
185         /* When wiselinks is initialized, selection.js is not working. Since we want to stop
186            using selection.js in the near future, let's not initialize wiselinks for now. */
187
188         // window.wiselinks = new Wiselinks();
189
190         $(document).off('page:loading').on('page:loading', function(event, $target, render, url){
191             $("#page-wrapper").fadeOut(200);
192         });
193
194         $(document).off('page:redirected').on('page:redirected', function(event, $target, render, url){
195         });
196
197         $(document).off('page:always').on('page:always', function(event, xhr, settings){
198             $("#page-wrapper").fadeIn(200);
199         });
200
201         $(document).off('page:done').on('page:done', function(event, $target, status, url, data){
202         });
203
204         $(document).off('page:fail').on('page:fail', function(event, $target, status, url, error, code){
205         });
206     });
207
208 });