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