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