Merge branch 'master' into 3138-wiselinks
[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 wiselinks
25 //= require_tree .
26
27 jQuery(function($){
28     $.ajaxSetup({
29         headers: {
30             'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
31         }
32     });
33
34     $(document).ajaxStart(function(){
35       $('.modal-with-loading-spinner .spinner').show();
36     }).ajaxStop(function(){
37       $('.modal-with-loading-spinner .spinner').hide();
38     });
39
40     $('[data-toggle=tooltip]').tooltip();
41
42     $('.expand-collapse-row').on('click', function(event) {
43         var targets = $('#' + $(this).attr('data-id'));
44         if (targets.css('display') == 'none') {
45             $(this).addClass('icon-minus-sign');
46             $(this).removeClass('icon-plus-sign');
47         } else {
48             $(this).addClass('icon-plus-sign');
49             $(this).removeClass('icon-minus-sign');
50         }
51         targets.fadeToggle(200);
52     });
53
54     var ajaxCount = 0;
55
56     $(document).
57         on('ajax:send', function(e, xhr) {
58             ajaxCount += 1;
59             if (ajaxCount == 1) {
60                 $('.loading').fadeTo('fast', 1);
61             }
62         }).
63         on('ajax:complete', function(e, status) {
64             ajaxCount -= 1;
65             if (ajaxCount == 0) {
66                 $('.loading').fadeOut('fast', 0);
67             }
68         }).
69         on('ajaxSend', function(e, xhr) {
70             // jQuery triggers 'ajaxSend' event when starting an ajax call, but
71             // rails-generated ajax triggers generate 'ajax:send'.  Workbench
72             // event listeners currently expect 'ajax:send', so trigger the
73             // rails event in response to the jQuery one.
74             $(document).trigger('ajax:send');
75         }).
76         on('ajaxComplete', function(e, xhr) {
77             // See comment above about ajaxSend/ajax:send
78             $(document).trigger('ajax:complete');
79         }).
80         on('click', '.removable-tag a', function(e) {
81             var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
82             tag_span.fadeTo('fast', 0.2);
83             $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
84                    {dataType: 'json',
85                     type: 'POST',
86                     data: { '_method': 'DELETE' },
87                     context: tag_span}).
88                 done(function(data, status, jqxhr) {
89                     this.remove();
90                 }).
91                 fail(function(jqxhr, status, error) {
92                     this.addClass('label-danger').fadeTo('fast', '1');
93                 });
94             return false;
95         }).
96         on('click', 'a.add-tag-button', function(e) {
97             var jqxhr;
98             var new_tag_uuid = 'new-tag-' + Math.random();
99             var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
100             var new_tag = window.prompt("Add tag for collection "+
101                                     tag_head_uuid,
102                                     "");
103             if (new_tag == null)
104                 return false;
105             var new_tag_span =
106                 $('<span class="label label-info removable-tag"></span>').
107                 attr('data-tag-link-uuid', new_tag_uuid).
108                 text(new_tag).
109                 css('opacity', '0.2').
110                 append('&nbsp;<a title="Delete tag"><i class="glyphicon glyphicon-trash"></i></a>&nbsp;');
111             $(this).
112                 parent().
113                 find('>span').
114                 append(new_tag_span).
115                 append('&nbsp; ');
116             $.ajax($(this).attr('data-remote-href'),
117                            {dataType: 'json',
118                             type: $(this).attr('data-remote-method'),
119                             data: {
120                                 'link[head_uuid]': tag_head_uuid,
121                                 'link[link_class]': 'tag',
122                                 'link[name]': new_tag
123                             },
124                             context: new_tag_span}).
125                 done(function(data, status, jqxhr) {
126                     this.attr('data-tag-link-uuid', data.uuid).
127                         fadeTo('fast', '1');
128                 }).
129                 fail(function(jqxhr, status, error) {
130                     this.addClass('label-danger').fadeTo('fast', '1');
131                 });
132             return false;
133         });
134
135     $(document).
136         on('ajax:complete ready', function() {
137             // See http://getbootstrap.com/javascript/#buttons
138             $('.btn').button();
139         });
140
141     $(document).
142         on('ready ajax:complete', function() {
143             $('[data-toggle~=tooltip]').tooltip({container:'body'});
144         });
145
146     HeaderRowFixer = function(selector) {
147         this.duplicateTheadTr = function() {
148             $(selector).each(function() {
149                 var the_table = this;
150                 if ($('>tbody>tr:first>th', the_table).length > 0)
151                     return;
152                 $('>tbody', the_table).
153                     prepend($('>thead>tr', the_table).
154                             clone().
155                             css('opacity', 0));
156             });
157         }
158         this.fixThead = function() {
159             $(selector).each(function() {
160                 var widths = [];
161                 $('> tbody > tr:eq(1) > td', this).each( function(i,v){
162                     widths.push($(v).width());
163                 });
164                 for(i=0;i<widths.length;i++) {
165                     $('thead th:eq('+i+')', this).width(widths[i]);
166                 }
167             });
168         }
169     }
170
171     var fixer = new HeaderRowFixer('.table-fixed-header-row');
172     fixer.duplicateTheadTr();
173     fixer.fixThead();
174     $(window).resize(function(){
175         fixer.fixThead();
176     });
177     $(document).on('ajax:complete', function(e, status) {
178         fixer.duplicateTheadTr();
179         fixer.fixThead();
180     });
181
182     $(document).ready(function() {
183         window.wiselinks = new Wiselinks();
184     });
185
186 });