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