Conditional Ajax for other db operations
[phpmyadmin/crack.git] / js / common.js
blob0ab1e8890e86eed264dea3d015345eeb3649c124
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * common functions used for communicating between main, navigation and querywindow
4  *
5  */
7 /**
8  * holds the browser query window
9  */
10 var querywindow = '';
12 /**
13  * holds the query to be load from a new query window
14  */
15 var query_to_load = '';
17 /**
18  * attach a function to object event
19  *
20  * <code>
21  * addEvent(window, 'load', PMA_initPage);
22  * </code>
23  * @param object or id
24  * @param string event type (load, mouseover, focus, ...)
25  * @param function to be attached
26  */
27 function addEvent(obj, type, fn)
29     if (obj.attachEvent) {
30         obj['e' + type + fn] = fn;
31         obj[type + fn] = function() {obj['e' + type + fn](window.event);}
32         obj.attachEvent('on' + type, obj[type + fn]);
33     } else {
34         obj.addEventListener(type, fn, false);
35     }
38 /**
39  * detach/remove a function from an object event
40  *
41  * @param object or id
42  * @param event type (load, mouseover, focus, ...)
43  * @param function naem of function to be attached
44  */
45 function removeEvent(obj, type, fn)
47     if (obj.detachEvent) {
48         obj.detachEvent('on' + type, obj[type + fn]);
49         obj[type + fn] = null;
50     } else {
51         obj.removeEventListener(type, fn, false);
52     }
55 /**
56  * get DOM elements by html class
57  *
58  * @param string class_name - name of class
59  * @param node node - search only sub nodes of this node (optional)
60  * @param string tag - search only these tags (optional)
61  */
62 function getElementsByClassName(class_name, node, tag)
64     var classElements = new Array();
66     if (node == null) {
67         node = document;
68     }
69     if (tag == null) {
70         tag = '*';
71     }
73     var j = 0, teststr;
74     var els = node.getElementsByTagName(tag);
75     var elsLen = els.length;
77     for (i = 0; i < elsLen; i++) {
78         if (els[i].className.indexOf(class_name) != -1) {
79             teststr = "," + els[i].className.split(" ").join(",") + ",";
80             if (teststr.indexOf("," + class_name + ",") != -1) {
81                 classElements[j] = els[i];
82                 j++;
83             }
84         }
85     }
86     return classElements;
89 /**
90  * sets current selected db
91  *
92  * @param    string    db name
93  */
94 function setDb(new_db) {
95     //alert('setDb(' + new_db + ')');
96     if (new_db != db) {
97         // db has changed
98         //alert( new_db + '(' + new_db.length + ') : ' + db );
100         var old_db = db;
101         db = new_db;
103         // the db name as an id exists only when LeftFrameLight is false
104         if (window.frame_navigation.document.getElementById(db) == null) {
105             // happens when LeftFrameLight is true
106             // db is unknown, reload complete left frame
107             refreshNavigation();
108         } else {
109             // happens when LeftFrameLight is false
110             unmarkDbTable(old_db);
111             markDbTable(db);
112         }
114         // TODO: add code to expand db in lightview mode
116         // refresh querywindow
117         refreshQuerywindow();
118     }
122  * sets current selected table (called from navigation.php)
124  * @param    string    table name
125  */
126 function setTable(new_table) {
127     //alert('setTable(' + new_table + ')');
128     if (new_table != table) {
129         // table has changed
130         //alert( new_table + '(' + new_table.length + ') : ' + table );
132         table = new_table;
134         if (window.frame_navigation.document.getElementById(db + '.' + table) == null
135          && table != '') {
136             // table is unknown, reload complete left frame
137             refreshNavigation();
139         }
140         // TODO: add code to expand table in lightview mode
142         // refresh querywindow
143         refreshQuerywindow();
144     }
148  * reloads main frame
150  * @uses     goTo()
151  * @uses     opendb_url
152  * @uses     token
153  * @uses     db
154  * @uses     server
155  * @uses     table
156  * @uses     lang
157  * @uses    collation_connection
158  * @uses    encodeURIComponent()
159  * @param    string    url    name of page to be loaded
160  */
161 function refreshMain(url) {
162     if (! url) {
163         if (db) {
164             url = opendb_url;
165         } else {
166             url = 'main.php';
167         }
168     }
169     //alert(db);
170     goTo(url + '?server=' + encodeURIComponent(server) +
171         '&token=' + encodeURIComponent(token) +
172         '&db=' + encodeURIComponent(db) +
173         '&table=' + encodeURIComponent(table) +
174         '&lang=' + encodeURIComponent(lang) +
175         '&collation_connection=' + encodeURIComponent(collation_connection),
176         'main');
180  * reloads navigation frame
182  * @uses     goTo()
183  * @uses     token
184  * @uses     db
185  * @uses     server
186  * @uses     table
187  * @uses     lang
188  * @uses    collation_connection
189  * @uses    encodeURIComponent()
190  * @param    boolean    force   force reloading 
191  */
192 function refreshNavigation(force) {
193     // The goTo() function won't refresh in case the target
194     // url is the same as the url given as parameter, but sometimes
195     // we want to refresh anyway. 
196     if (typeof force != undefined && force && window.parent && window.parent.frame_navigation) {
197         window.parent.frame_navigation.location.reload();
198     } else {
199         goTo('navigation.php?server=' + encodeURIComponent(server) +
200             '&token=' + encodeURIComponent(token)  +
201             '&db=' + encodeURIComponent(db)  +
202             '&table=' + encodeURIComponent(table) +
203             '&lang=' + encodeURIComponent(lang) +
204             '&collation_connection=' + encodeURIComponent(collation_connection)
205             );
206     }
210  * adds class to element
211  */
212 function addClass(element, classname)
214     if (element != null) {
215         $("#"+element).addClass(classname);
216         //alert('set class: ' + classname + ', now: ' + element.className);
217     }
221  * removes class from element
222  */
223 function removeClass(element, classname)
225     if (element != null) {
226         $("#"+element).removeClass(classname);
227         //alert('removed class: ' + classname + ', now: ' + element.className);
228     }
231 function unmarkDbTable(db, table)
233     var element_reference = window.frame_navigation.document.getElementById(db);
234     if (element_reference != null) {
235         //alert('remove from: ' + db);
236         removeClass(element_reference.parentNode, 'marked');
237     }
239     element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
240     if (element_reference != null) {
241         //alert('remove from: ' + db + '.' + table);
242         removeClass(element_reference.parentNode, 'marked');
243     }
246 function markDbTable(db, table)
248     var element_reference = window.frame_navigation.document.getElementById(db);
249     if (element_reference != null) {
250         addClass(element_reference.parentNode, 'marked');
251         // scrolldown
252         element_reference.focus();
253         // opera marks the text, we dont want this ...
254         element_reference.blur();
255     }
257     element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
258     if (element_reference != null) {
259         addClass(element_reference.parentNode, 'marked');
260         // scrolldown
261         element_reference.focus();
262         // opera marks the text, we dont want this ...
263         element_reference.blur();
264     }
266     // return to main frame ...
267     window.frame_content.focus();
271  * sets current selected server, table and db (called from libraries/footer.inc.php)
272  */
273 function setAll( new_lang, new_collation_connection, new_server, new_db, new_table, new_token ) {
274     //alert('setAll( ' + new_lang + ', ' + new_collation_connection + ', ' + new_server + ', ' + new_db + ', ' + new_table + ', ' + new_token + ' )');
275     if (new_server != server || new_lang != lang
276       || new_collation_connection != collation_connection) {
277         // something important has changed
278         server = new_server;
279         db     = new_db;
280         table  = new_table;
281         collation_connection  = new_collation_connection;
282         lang  = new_lang;
283         token  = new_token;
284         refreshNavigation();
285     } else if (new_db != db || new_table != table) {
286         // save new db and table
287         var old_db    = db;
288         var old_table = table;
289         db        = new_db;
290         table     = new_table;
292         if (window.frame_navigation.document.getElementById(db) == null
293           && window.frame_navigation.document.getElementById(db + '.' + table) == null ) {
294             // table or db is unknown, reload complete left frame
295             refreshNavigation();
296         } else {
297             unmarkDbTable(old_db, old_table);
298             markDbTable(db, table);
299         }
301         // TODO: add code to expand db in lightview mode
303         // refresh querywindow
304         refreshQuerywindow();
305     }
308 function reload_querywindow(db, table, sql_query)
310     if ( ! querywindow.closed && querywindow.location ) {
311         if ( ! querywindow.document.sqlform.LockFromUpdate
312           || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
313             querywindow.document.getElementById('hiddenqueryform').db.value = db;
314             querywindow.document.getElementById('hiddenqueryform').table.value = table;
316             if (sql_query) {
317                 querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
318             }
320             querywindow.document.getElementById('hiddenqueryform').submit();
321         }
322     }
326  * brings query window to front and inserts query to be edited
327  */
328 function focus_querywindow(sql_query)
330     /* if ( querywindow && !querywindow.closed && querywindow.location) { */
331     if ( !querywindow || querywindow.closed || !querywindow.location) {
332         // we need first to open the window and cannot pass the query with it
333         // as we dont know if the query exceeds max url length
334         /* url = 'querywindow.php?' + common_query + '&db=' + db + '&table=' + table + '&sql_query=SELECT * FROM'; */
335         query_to_load = sql_query;
336         open_querywindow();
337         insertQuery(0);
338     } else {
339         //var querywindow = querywindow;
340         if ( querywindow.document.getElementById('hiddenqueryform').querydisplay_tab != 'sql' ) {
341             querywindow.document.getElementById('hiddenqueryform').querydisplay_tab.value = "sql";
342             querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
343             querywindow.document.getElementById('hiddenqueryform').submit();
344             querywindow.focus();
345         } else {
346             querywindow.focus();
347         }
348     }
349     return true;
353  * inserts query string into query window textarea
354  * called from script tag in querywindow
355  */
356 function insertQuery() {
357     if (query_to_load != '' && querywindow.document && querywindow.document.getElementById && querywindow.document.getElementById('sqlquery')) {
358         querywindow.document.getElementById('sqlquery').value = query_to_load;
359         query_to_load = '';
360         return true;
361     }
362     return false;
365 function open_querywindow( url ) {
366     if ( ! url ) {
367         url = 'querywindow.php?' + common_query + '&db=' + encodeURIComponent(db) + '&table=' + encodeURIComponent(table);
368     }
370     if (!querywindow.closed && querywindow.location) {
371         goTo( url, 'query' );
372         querywindow.focus();
373     } else {
374         querywindow = window.open( url + '&init=1', '',
375             'toolbar=0,location=0,directories=0,status=1,menubar=0,' +
376             'scrollbars=yes,resizable=yes,' +
377             'width=' + querywindow_width + ',' +
378             'height=' + querywindow_height );
379     }
381     if ( ! querywindow.opener ) {
382        querywindow.opener = window.window;
383     }
385     if ( window.focus ) {
386         querywindow.focus();
387     }
389     return true;
392 function refreshQuerywindow( url ) {
394     if ( ! querywindow.closed && querywindow.location ) {
395         if ( ! querywindow.document.sqlform.LockFromUpdate
396           || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
397             open_querywindow( url )
398         }
399     }
403  * opens new url in target frame, with default being left frame
404  * valid is 'main' and 'querywindow' all others leads to 'left'
406  * @param    string    targeturl    new url to load
407  * @param    string    target       frame where to load the new url
408  */
409 function goTo(targeturl, target) {
410     //alert(targeturl);
411     if ( target == 'main' ) {
412         target = window.frame_content;
413     } else if ( target == 'query' ) {
414         target = querywindow;
415         //return open_querywindow( targeturl );
416     } else if ( ! target ) {
417         target = window.frame_navigation;
418     }
420     if ( target ) {
421         if ( target.location.href == targeturl ) {
422             return true;
423         } else if ( target.location.href == pma_absolute_uri + targeturl ) {
424             return true;
425         }
427         if ( safari_browser ) {
428             target.location.href = targeturl;
429         } else {
430             target.location.replace(targeturl);
431         }
432     }
434     return true;
437 // opens selected db in main frame
438 function openDb(new_db) {
439     //alert('opendb(' +  new_db + ')');
440     setDb(new_db);
441     setTable('');
442     refreshMain(opendb_url);
443     return true;
446 function updateTableTitle( table_link_id, new_title ) {
447     //alert('updateTableTitle');
448     if ( window.parent.frame_navigation.document && window.parent.frame_navigation.document.getElementById(table_link_id) ) {
449         var left = window.parent.frame_navigation.document;
451         var link = left.getElementById(table_link_id);
452         link.title = window.parent.pma_text_default_tab + ': ' + new_title;
454         var link = left.getElementById('quick_' + table_link_id);
455         link.title = window.parent.pma_text_left_default_tab + ': ' + new_title;
457         return true;
458     }
460     return false;