Merge branch 'master' into 2525-java-sdk
[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_tree .
25
26 jQuery(function($){
27     $.ajaxSetup({
28         headers: {
29             'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
30         }
31     });
32     $('.editable').editable();
33     $('[data-toggle=tooltip]').tooltip();
34
35     $('.expand-collapse-row').on('click', function(event) {
36         var targets = $('#' + $(this).attr('data-id'));
37         if (targets.css('display') == 'none') {
38             $(this).addClass('icon-minus-sign');
39             $(this).removeClass('icon-plus-sign');
40         } else {
41             $(this).addClass('icon-plus-sign');
42             $(this).removeClass('icon-minus-sign');
43         }
44         targets.fadeToggle(200);
45     });
46
47     $(document).
48         on('ajax:send', function(e, xhr) {
49             $('.loading').fadeTo('fast', 1);
50         }).
51         on('ajax:complete', function(e, status) {
52             $('.loading').fadeOut('fast', 0);
53         }).
54         on('click', '.removable-tag a', function(e) {
55             var tag_span = $(this).parents('[data-tag-link-uuid]').eq(0)
56             tag_span.fadeTo('fast', 0.2);
57             $.ajax('/links/' + tag_span.attr('data-tag-link-uuid'),
58                    {dataType: 'json',
59                     type: 'POST',
60                     data: { '_method': 'DELETE' },
61                     context: tag_span}).
62                 done(function(data, status, jqxhr) {
63                     this.remove();
64                 }).
65                 fail(function(jqxhr, status, error) {
66                     this.addClass('label-danger').fadeTo('fast', '1');
67                 });
68             return false;
69         }).
70         on('click', 'a.add-tag-button', function(e) {
71             var jqxhr;
72             var new_tag_uuid = 'new-tag-' + Math.random();
73             var tag_head_uuid = $(this).parents('tr').attr('data-object-uuid');
74             var new_tag = window.prompt("Add tag for collection "+
75                                     tag_head_uuid,
76                                     "");
77             if (new_tag == null)
78                 return false;
79             var new_tag_span =
80                 $('<span class="label label-info removable-tag"></span>').
81                 attr('data-tag-link-uuid', new_tag_uuid).
82                 text(new_tag).
83                 css('opacity', '0.2').
84                 append('&nbsp;<a title="Delete tag"><i class="glyphicon glyphicon-trash"></i></a>&nbsp;');
85             $(this).
86                 parent().
87                 find('>span').
88                 append(new_tag_span).
89                 append('&nbsp; ');
90             $.ajax($(this).attr('data-remote-href'),
91                            {dataType: 'json',
92                             type: $(this).attr('data-remote-method'),
93                             data: {
94                                 'link[head_uuid]': tag_head_uuid,
95                                 'link[link_class]': 'tag',
96                                 'link[name]': new_tag
97                             },
98                             context: new_tag_span}).
99                 done(function(data, status, jqxhr) {
100                     this.attr('data-tag-link-uuid', data.uuid).
101                         fadeTo('fast', '1');
102                 }).
103                 fail(function(jqxhr, status, error) {
104                     this.addClass('label-danger').fadeTo('fast', '1');
105                 });
106             return false;
107         });
108
109     $(document).
110         on('ajax:complete ready', function() {
111             // See http://getbootstrap.com/javascript/#buttons
112             $('.btn').button();
113         });
114
115     HeaderRowFixer = function(selector) {
116         this.duplicateTheadTr = function() {
117             $(selector).each(function() {
118                 var the_table = this;
119                 if ($('>tbody>tr:first>th', the_table).length > 0)
120                     return;
121                 $('>tbody', the_table).
122                     prepend($('>thead>tr', the_table).
123                             clone().
124                             css('opacity', 0));
125             });
126         }
127         this.fixThead = function() {
128             $(selector).each(function() {
129                 var widths = [];
130                 $('> tbody > tr:eq(1) > td', this).each( function(i,v){
131                     widths.push($(v).width());
132                 });
133                 for(i=0;i<widths.length;i++) {
134                     $('thead th:eq('+i+')', this).width(widths[i]);
135                 }
136             });
137         }
138     }
139
140     var fixer = new HeaderRowFixer('.table-fixed-header-row');
141     fixer.duplicateTheadTr();
142     fixer.fixThead();
143     $(window).resize(function(){
144         fixer.fixThead();
145     });
146     $(document).on('ajax:complete', function(e, status) {
147         fixer.duplicateTheadTr();
148         fixer.fixThead();
149     });
150 });