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