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