12033: Generalize MultisiteLoader to MergingLoader.
[arvados.git] / apps / workbench / app / assets / javascripts / application.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4 //
5 // This is a manifest file that'll be compiled into application.js, which will include all the files
6 // listed below.
7 //
8 // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
9 // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
10 //
11 // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
12 // the compiled file.
13 //
14 // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
15 // GO AFTER THE REQUIRES BELOW.
16 //
17 //= require jquery
18 //= require jquery_ujs
19 //= require bootstrap
20 //= require bootstrap/dropdown
21 //= require bootstrap/tab
22 //= require bootstrap/tooltip
23 //= require bootstrap/popover
24 //= require bootstrap/collapse
25 //= require bootstrap/modal
26 //= require bootstrap/button
27 //= require bootstrap3-editable/bootstrap-editable
28 //= require bootstrap-tab-history
29 //= require wiselinks
30 //= require angular
31 //= require raphael
32 //= require morris
33 //= require jquery.number.min
34 //= require npm-dependencies
35 //= require mithril/stream/stream
36 //= require_tree .
37
38 window.m = Object.assign(window.Mithril, {stream: window.m.stream})
39
40 jQuery(function($){
41     $(document).ajaxStart(function(){
42       $('.modal-with-loading-spinner .spinner').show();
43     }).ajaxStop(function(){
44       $('.modal-with-loading-spinner .spinner').hide();
45     });
46
47     $('[data-toggle=tooltip]').tooltip();
48
49     $('.expand-collapse-row').on('click', function(event) {
50         var targets = $('#' + $(this).attr('data-id'));
51         if (targets.css('display') == 'none') {
52             $(this).addClass('icon-minus-sign');
53             $(this).removeClass('icon-plus-sign');
54         } else {
55             $(this).addClass('icon-plus-sign');
56             $(this).removeClass('icon-minus-sign');
57         }
58         targets.fadeToggle(200);
59     });
60
61     var ajaxCount = 0;
62
63     $(document).
64         on('ajax:send', function(e, xhr) {
65             ajaxCount += 1;
66             if (ajaxCount == 1) {
67                 $('.loading').fadeTo('fast', 1);
68             }
69         }).
70         on('ajax:complete', function(e, status) {
71             ajaxCount -= 1;
72             if (ajaxCount == 0) {
73                 $('.loading').fadeOut('fast', 0);
74             }
75         }).
76         on('ajaxSend', function(e, xhr) {
77             // jQuery triggers 'ajaxSend' event when starting an ajax call, but
78             // rails-generated ajax triggers generate 'ajax:send'.  Workbench
79             // event listeners currently expect 'ajax:send', so trigger the
80             // rails event in response to the jQuery one.
81             $(document).trigger('ajax:send');
82         }).
83         on('ajaxComplete', function(e, xhr) {
84             // See comment above about ajaxSend/ajax:send
85             $(document).trigger('ajax:complete');
86         }).
87         on('click', '.removable-tag a', function(e) {
88             var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
89             tag_span.fadeTo('fast', 0.2);
90             $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
91                    {dataType: 'json',
92                     type: 'POST',
93                     data: { '_method': 'DELETE' },
94                     context: tag_span}).
95                 done(function(data, status, jqxhr) {
96                     this.remove();
97                 }).
98                 fail(function(jqxhr, status, error) {
99                     this.addClass('label-danger').fadeTo('fast', '1');
100                 });
101             return false;
102         }).
103         on('click', 'a.add-tag-button', function(e) {
104             var jqxhr;
105             var new_tag_uuid = 'new-tag-' + Math.random();
106             var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
107             var new_tag = window.prompt("Add tag for collection "+
108                                     tag_head_uuid,
109                                     "");
110             if (new_tag == null)
111                 return false;
112             var new_tag_span =
113                 $('<span class="label label-info removable-tag"></span>').
114                 attr('data-tag-link-uuid', new_tag_uuid).
115                 text(new_tag).
116                 css('opacity', '0.2').
117                 append('&nbsp;<span class="removable-tag"><a title="Delete tag"><i class="fa fa-fw fa-trash-o"></i></a></span>');
118             $(this).
119                 parent().
120                 find('>span').
121                 append(new_tag_span).
122                 append(' ');
123             $.ajax($(this).attr('data-remote-href'),
124                            {dataType: 'json',
125                             type: $(this).attr('data-remote-method'),
126                             data: {
127                                 'link[head_uuid]': tag_head_uuid,
128                                 'link[link_class]': 'tag',
129                                 'link[name]': new_tag
130                             },
131                             context: new_tag_span}).
132                 done(function(data, status, jqxhr) {
133                     this.attr('data-tag-link-uuid', data.uuid).
134                         fadeTo('fast', '1');
135                 }).
136                 fail(function(jqxhr, status, error) {
137                     this.addClass('label-danger').fadeTo('fast', '1');
138                 });
139             return false;
140         }).
141         on('click focusin', 'input.select-on-focus', function(event) {
142             event.target.select();
143         });
144
145     $(document).
146         on('ajax:complete ready', function() {
147             // See http://getbootstrap.com/javascript/#buttons
148             $('.btn').button();
149         }).
150         on('ready ajax:complete', function() {
151             $('[data-toggle~=tooltip]').tooltip({container:'body'});
152         }).
153         on('ready ajax:complete', function() {
154             // This makes the dialog close on Esc key, obviously.
155             $('.modal').attr('tabindex', '-1')
156         }).
157         on('ready', function() {
158             // Need this to trigger input validation/synchronization callbacks because some browsers
159             // auto-fill form fields (e.g., when navigating "back" to a page where some text
160             // had been entered in a search box) without triggering a change or input event.
161             $('input').each(function(el) {
162                 $(el).trigger($.Event('input', {currentTarget: el}));
163             });
164         });
165
166     HeaderRowFixer = function(selector) {
167         this.duplicateTheadTr = function() {
168             $(selector).each(function() {
169                 var the_table = this;
170                 if ($('>tbody>tr:first>th', the_table).length > 0)
171                     return;
172                 $('>tbody', the_table).
173                     prepend($('>thead>tr', the_table).
174                             clone().
175                             css('opacity', 0));
176             });
177         }
178         this.fixThead = function() {
179             $(selector).each(function() {
180                 var widths = [];
181                 $('> tbody > tr:eq(1) > td', this).each( function(i,v){
182                     widths.push($(v).width());
183                 });
184                 for(i=0;i<widths.length;i++) {
185                     $('thead th:eq('+i+')', this).width(widths[i]);
186                 }
187             });
188         }
189     }
190
191     var fixer = new HeaderRowFixer('.table-fixed-header-row');
192     fixer.duplicateTheadTr();
193     fixer.fixThead();
194     $(window).resize(function(){
195         fixer.fixThead();
196     });
197     $(document).on('ajax:complete', function(e, status) {
198         fixer.duplicateTheadTr();
199         fixer.fixThead();
200     });
201
202     $(document).ready(function() {
203         /* When wiselinks is initialized, selection.js is not working. Since we want to stop
204            using selection.js in the near future, let's not initialize wiselinks for now. */
205
206         // window.wiselinks = new Wiselinks();
207
208         $(document).off('page:loading').on('page:loading', function(event, $target, render, url){
209             $("#page-wrapper").fadeOut(200);
210         });
211
212         $(document).off('page:redirected').on('page:redirected', function(event, $target, render, url){
213         });
214
215         $(document).off('page:always').on('page:always', function(event, xhr, settings){
216             $("#page-wrapper").fadeIn(200);
217         });
218
219         $(document).off('page:done').on('page:done', function(event, $target, status, url, data){
220         });
221
222         $(document).off('page:fail').on('page:fail', function(event, $target, status, url, error, code){
223         });
224     });
225
226     $(document).on('click', '.compute-detail', function(e) {
227         $(e.target).collapse('hide');
228     });
229
230     $(document).on('click', '.compute-node-summary', function(e) {
231         $(e.target.href).collapse('toggle');
232     });
233
234     $(document).on('click', '.force-cache-reload', function(e) {
235         history.replaceState( { nocache: true }, '' );
236     });
237 });
238
239 window.addEventListener("DOMContentLoaded", function(e) {
240     if(history.state) {
241         if(history.state.nocache) {
242             showLoadingModal();
243             history.replaceState( {}, '' );
244             location.reload(true);
245         }
246     }
247 });
248
249 function showLoadingModal() {
250     $('#loading-modal').modal('show');
251 }
252
253 function hideLoadingModal() {
254     $('#loading-modal').modal('hide');
255 }
256
257 function hasHTML5History() {
258     return !!(window.history && window.history.pushState);
259 }