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