Merge branch 'master' into 3198-writable-fuse
[arvados.git] / apps / workbench / app / assets / javascripts / application.js
1 // This is a manifest file that'll be compiled into application.js, which will include all the files
2 // listed below.
3 //
4 // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5 // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6 //
7 // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8 // the compiled file.
9 //
10 // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11 // GO AFTER THE REQUIRES BELOW.
12 //
13 //= require jquery
14 //= require jquery_ujs
15 //= require bootstrap
16 //= require bootstrap/dropdown
17 //= require bootstrap/tab
18 //= require bootstrap/tooltip
19 //= require bootstrap/popover
20 //= require bootstrap/collapse
21 //= require bootstrap/modal
22 //= require bootstrap/button
23 //= require bootstrap3-editable/bootstrap-editable
24 //= require bootstrap-tab-history
25 //= require wiselinks
26 //= require angular
27 //= require raphael
28 //= require morris
29 //= require jquery.number.min
30 //= require_tree .
31
32 jQuery(function($){
33     $(document).ajaxStart(function(){
34       $('.modal-with-loading-spinner .spinner').show();
35     }).ajaxStop(function(){
36       $('.modal-with-loading-spinner .spinner').hide();
37     });
38
39     $('[data-toggle=tooltip]').tooltip();
40
41     $('.expand-collapse-row').on('click', function(event) {
42         var targets = $('#' + $(this).attr('data-id'));
43         if (targets.css('display') == 'none') {
44             $(this).addClass('icon-minus-sign');
45             $(this).removeClass('icon-plus-sign');
46         } else {
47             $(this).addClass('icon-plus-sign');
48             $(this).removeClass('icon-minus-sign');
49         }
50         targets.fadeToggle(200);
51     });
52
53     var ajaxCount = 0;
54
55     $(document).
56         on('ajax:send', function(e, xhr) {
57             ajaxCount += 1;
58             if (ajaxCount == 1) {
59                 $('.loading').fadeTo('fast', 1);
60             }
61         }).
62         on('ajax:complete', function(e, status) {
63             ajaxCount -= 1;
64             if (ajaxCount == 0) {
65                 $('.loading').fadeOut('fast', 0);
66             }
67         }).
68         on('ajaxSend', function(e, xhr) {
69             // jQuery triggers 'ajaxSend' event when starting an ajax call, but
70             // rails-generated ajax triggers generate 'ajax:send'.  Workbench
71             // event listeners currently expect 'ajax:send', so trigger the
72             // rails event in response to the jQuery one.
73             $(document).trigger('ajax:send');
74         }).
75         on('ajaxComplete', function(e, xhr) {
76             // See comment above about ajaxSend/ajax:send
77             $(document).trigger('ajax:complete');
78         }).
79         on('click', '.removable-tag a', function(e) {
80             var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
81             tag_span.fadeTo('fast', 0.2);
82             $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
83                    {dataType: 'json',
84                     type: 'POST',
85                     data: { '_method': 'DELETE' },
86                     context: tag_span}).
87                 done(function(data, status, jqxhr) {
88                     this.remove();
89                 }).
90                 fail(function(jqxhr, status, error) {
91                     this.addClass('label-danger').fadeTo('fast', '1');
92                 });
93             return false;
94         }).
95         on('click', 'a.add-tag-button', function(e) {
96             var jqxhr;
97             var new_tag_uuid = 'new-tag-' + Math.random();
98             var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
99             var new_tag = window.prompt("Add tag for collection "+
100                                     tag_head_uuid,
101                                     "");
102             if (new_tag == null)
103                 return false;
104             var new_tag_span =
105                 $('<span class="label label-info removable-tag"></span>').
106                 attr('data-tag-link-uuid', new_tag_uuid).
107                 text(new_tag).
108                 css('opacity', '0.2').
109                 append('&nbsp;<span class="removable-tag"><a title="Delete tag"><i class="fa fa-fw fa-trash-o"></i></a></span>');
110             $(this).
111                 parent().
112                 find('>span').
113                 append(new_tag_span).
114                 append(' ');
115             $.ajax($(this).attr('data-remote-href'),
116                            {dataType: 'json',
117                             type: $(this).attr('data-remote-method'),
118                             data: {
119                                 'link[head_uuid]': tag_head_uuid,
120                                 'link[link_class]': 'tag',
121                                 'link[name]': new_tag
122                             },
123                             context: new_tag_span}).
124                 done(function(data, status, jqxhr) {
125                     this.attr('data-tag-link-uuid', data.uuid).
126                         fadeTo('fast', '1');
127                 }).
128                 fail(function(jqxhr, status, error) {
129                     this.addClass('label-danger').fadeTo('fast', '1');
130                 });
131             return false;
132         }).
133         on('click focusin', 'input.select-on-focus', function(event) {
134             event.target.select();
135         });
136
137     $(document).
138         on('ajax:complete ready', function() {
139             // See http://getbootstrap.com/javascript/#buttons
140             $('.btn').button();
141         }).
142         on('ready ajax:complete', function() {
143             $('[data-toggle~=tooltip]').tooltip({container:'body'});
144         }).
145         on('ready ajax:complete', function() {
146             // This makes the dialog close on Esc key, obviously.
147             $('.modal').attr('tabindex', '-1')
148         }).
149         on('ready', function() {
150             // Need this to trigger input validation/synchronization callbacks because some browsers
151             // auto-fill form fields (e.g., when navigating "back" to a page where some text
152             // had been entered in a search box) without triggering a change or input event.
153             $('input').trigger('input');
154         });
155
156     HeaderRowFixer = function(selector) {
157         this.duplicateTheadTr = function() {
158             $(selector).each(function() {
159                 var the_table = this;
160                 if ($('>tbody>tr:first>th', the_table).length > 0)
161                     return;
162                 $('>tbody', the_table).
163                     prepend($('>thead>tr', the_table).
164                             clone().
165                             css('opacity', 0));
166             });
167         }
168         this.fixThead = function() {
169             $(selector).each(function() {
170                 var widths = [];
171                 $('> tbody > tr:eq(1) > td', this).each( function(i,v){
172                     widths.push($(v).width());
173                 });
174                 for(i=0;i<widths.length;i++) {
175                     $('thead th:eq('+i+')', this).width(widths[i]);
176                 }
177             });
178         }
179     }
180
181     var fixer = new HeaderRowFixer('.table-fixed-header-row');
182     fixer.duplicateTheadTr();
183     fixer.fixThead();
184     $(window).resize(function(){
185         fixer.fixThead();
186     });
187     $(document).on('ajax:complete', function(e, status) {
188         fixer.duplicateTheadTr();
189         fixer.fixThead();
190     });
191
192     $(document).ready(function() {
193         /* When wiselinks is initialized, selection.js is not working. Since we want to stop
194            using selection.js in the near future, let's not initialize wiselinks for now. */
195
196         // window.wiselinks = new Wiselinks();
197
198         $(document).off('page:loading').on('page:loading', function(event, $target, render, url){
199             $("#page-wrapper").fadeOut(200);
200         });
201
202         $(document).off('page:redirected').on('page:redirected', function(event, $target, render, url){
203         });
204
205         $(document).off('page:always').on('page:always', function(event, xhr, settings){
206             $("#page-wrapper").fadeIn(200);
207         });
208
209         $(document).off('page:done').on('page:done', function(event, $target, status, url, data){
210         });
211
212         $(document).off('page:fail').on('page:fail', function(event, $target, status, url, error, code){
213         });
214     });
215
216     $(document).on('click', '.compute-detail', function(e) {
217         $(e.target).collapse('hide');
218     });
219
220     $(document).on('click', '.compute-node-summary', function(e) {
221         $(e.target.href).collapse('toggle');
222     });
223
224     $(document).on('click', '.force-cache-reload', function(e) {
225         history.replaceState( { nocache: true }, '' );
226     });
227 });
228
229 window.addEventListener("DOMContentLoaded", function(e) {
230     if(history.state) {
231         if(history.state.nocache) {
232             showLoadingModal();
233             history.replaceState( {}, '' );
234             location.reload(true);
235         }
236     }
237 });
238
239 function showLoadingModal() {
240     $('#loading-modal').modal('show');
241 }
242
243 function hideLoadingModal() {
244     $('#loading-modal').modal('hide');
245 }
246
247 function hasHTML5History() {
248     return !!(window.history && window.history.pushState);
249 }