Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / js / server_variables.js
blobe9bc1cf81721a40ad58966ba24551a7f50f361c5
1 function editVariable(link) {
2     var varName = $(link).parent().parent().find('th:first').first().text().replace(/ /g,'_');
3     var mySaveLink = $(saveLink);
4     var myCancelLink = $(cancelLink);
5     var $cell = $(link).parent();
6     
7     $cell.addClass('edit');
8     // remove edit link
9     $cell.find('a.editLink').remove();
10     
11     mySaveLink.click(function() {
12         $.get('server_variables.php?' + url_query,
13           { ajax_request: true, type: 'setval', varName: varName, varValue: $cell.find('input').attr('value') },
14           function(data) {
15             if(data.success) $cell.html(data.variable);
16             else {
17                 PMA_ajaxShowMessage(data.error);
18                 $cell.html($cell.find('span.oldContent').html());
19             }
20             $cell.removeClass('edit');
21           },
22           'json'
23         );
24         return false;
25     });
26     
27     myCancelLink.click(function() {
28         $cell.html($cell.find('span.oldContent').html());
29         $cell.removeClass('edit');
30         return false;
31     });
32           
33     
34     $.get('server_variables.php?' + url_query,
35           { ajax_request: true, type: 'getval', varName: varName },
36           function(data) {
37               // hide original content
38               $cell.html('<span class="oldContent" style="display:none;">' + $cell.html() + '</span>');
39               // put edit field and save/cancel link
40               $cell.prepend('<table class="serverVariableEditTable" border="0"><tr><td></td><td style="width:100%;"><input type="text" value="' + data + '"/></td></tr</table>');
41               $cell.find('table td:first').append(mySaveLink);
42               $cell.find('table td:first').append(myCancelLink);
43           }
44     );
45           
46     return false;
49 $(function() {    
50     var textFilter=null;
51     var odd_row=false;
52     var testString = 'abcdefghijklmnopqrstuvwxyz0123456789,ABCEFGHIJKLMOPQRSTUVWXYZ';
53     var $tmpDiv;
54     var charWidth;
55     
56     // Global vars
57     editLink = '<a href="#" class="editLink" onclick="return editVariable(this);"><img class="icon ic_b_edit" src="themes/dot.gif" alt=""> '+PMA_messages['strEdit']+'</a>';
58     saveLink = '<a href="#" class="saveLink"><img class="icon ic_b_save" src="themes/dot.gif" alt=""> '+PMA_messages['strSave']+'</a> ';
59     cancelLink = '<a href="#" class="cancelLink"><img class="icon ic_b_close" src="themes/dot.gif" alt=""> '+PMA_messages['strCancel']+'</a> ';
62     $.ajaxSetup({
63         cache:false
64     });
65     
66     /* Variable editing */
67     if(isSuperuser) {
68         $('table.data tbody tr td:nth-child(2)').hover(
69             function() {
70                 // Only add edit element if it is the global value, not session value and not when the element is being edited
71                 if($(this).parent().children('th').length > 0 && ! $(this).hasClass('edit'))
72                     $(this).prepend(editLink);
73             },
74             function() {
75                 $(this).find('a.editLink').remove();
76             }
77         );
78     }
79     
80     /*** This code snippet takes care that the table stays readable. It cuts off long strings the table overlaps the window size ***/
81     $('table.data').after($tmpDiv=$('<span>'+testString+'</span>'));
82     charWidth = $tmpDiv.width() / testString.length;
83     $tmpDiv.remove();
84     
85     $(window).resize(limitTableWidth);
86     limitTableWidth();
87     
88     function limitTableWidth() {
89         var fulltext;
90         var charDiff;
91         var maxTableWidth;
92         var $tmpTable;
93         
94         $('table.data').after($tmpTable=$('<table id="testTable" style="width:100%;"><tr><td>'+testString+'</td></tr></table>'));
95         maxTableWidth = $('#testTable').width(); 
96         $tmpTable.remove();
97         charDiff =  ($('table.data').width()-maxTableWidth) / charWidth;
98         
99         if($('body').innerWidth() < $('table.data').width()+10 || $('body').innerWidth() > $('table.data').width()+20) {
100             var maxChars=0;
101             
102             $('table.data tbody tr td:nth-child(2)').each(function() {
103                 maxChars=Math.max($(this).text().length,maxChars);
104             });
105             
106             // Do not resize smaller if there's only 50 chars displayed already
107             if(charDiff > 0 && maxChars < 50) return;
108             
109             $('table.data tbody tr td:nth-child(2)').each(function() {
110                 if((charDiff>0 && $(this).text().length > maxChars-charDiff) || (charDiff<0 && $(this).find('abbr.cutoff').length>0)) {
111                     if($(this).find('abbr.cutoff').length > 0)
112                         fulltext = $(this).find('abbr.cutoff').attr('title');
113                     else {
114                         fulltext = $(this).text();
115                         // Do not cut off elements with html in it and hope they are not too long
116                         if(fulltext.length != $(this).html().length) return 0;
117                     }
118                     
119                     if(fulltext.length < maxChars-charDiff)
120                         $(this).html(fulltext);
121                     else $(this).html('<abbr class="cutoff" title="'+fulltext+'">'+fulltext.substr(0,maxChars-charDiff-3)+'...</abbr>');
122                 }
123             });
124         }
125     }
126     
127     // Filter options are invisible for disabled js users
128     $('fieldset#tableFilter').css('display','');
129     
130     $('#filterText').keyup(function(e) {
131         if($(this).val().length==0) textFilter=null;
132         else textFilter = new RegExp("(^| )"+$(this).val().replace('_',' '),'i');
133         filterVariables();
134     });
135     
136     function filterVariables() {
137         odd_row=false;
138         var mark_next=false;
139         var firstCell;
140         
141         $('table.filteredData tbody tr').each(function() {
142             firstCell = $(this).children(':first');
143             
144             if(mark_next || textFilter==null || textFilter.exec(firstCell.text())) {
145                 // If current row is 'marked', also display next row
146                 if($(this).hasClass('marked') && !mark_next)
147                     mark_next=true;
148                 else mark_next=false;
150                 odd_row = !odd_row;                    
151                 $(this).css('display','');
152                 if(odd_row) {
153                     $(this).addClass('odd');
154                     $(this).removeClass('even');
155                 } else {
156                     $(this).addClass('even');
157                     $(this).removeClass('odd');
158                 }
159             } else {
160                 $(this).css('display','none');
161             }
162         });
163     }