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