3.3.9.2 ChangeLog
[phpmyadmin-themes.git] / js / common.js
blob8ba7bcd0fb65a029a301c0f977db8019a2831191
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * common functions used for communicating between main, navigation and querywindow
4  *
5  * @version $Id$
6  */
8 /**
9  * holds the browser query window
10  */
11 var querywindow = '';
13 /**
14  * holds the query to be load from a new query window
15  */
16 var query_to_load = '';
18 /**
19  * attach a function to object event
20  *
21  * <code>
22  * addEvent(window, 'load', PMA_initPage);
23  * </code>
24  * @param object or id
25  * @param string event type (load, mouseover, focus, ...)
26  * @param function to be attached
27  */
28 function addEvent(obj, type, fn)
30     if (obj.attachEvent) {
31         obj['e' + type + fn] = fn;
32         obj[type + fn] = function() {obj['e' + type + fn](window.event);}
33         obj.attachEvent('on' + type, obj[type + fn]);
34     } else {
35         obj.addEventListener(type, fn, false);
36     }
39 /**
40  * detach/remove a function from an object event
41  *
42  * @param object or id
43  * @param event type (load, mouseover, focus, ...)
44  * @param function naem of function to be attached
45  */
46 function removeEvent(obj, type, fn)
48     if (obj.detachEvent) {
49         obj.detachEvent('on' + type, obj[type + fn]);
50         obj[type + fn] = null;
51     } else {
52         obj.removeEventListener(type, fn, false);
53     }
56 /**
57  * get DOM elements by html class
58  *
59  * @param string class_name - name of class
60  * @param node node - search only sub nodes of this node (optional)
61  * @param string tag - search only these tags (optional)
62  */
63 function getElementsByClassName(class_name, node, tag)
65     var classElements = new Array();
67     if (node == null) {
68         node = document;
69     }
70     if (tag == null) {
71         tag = '*';
72     }
74     var j = 0, teststr;
75     var els = node.getElementsByTagName(tag);
76     var elsLen = els.length;
78     for (i = 0; i < elsLen; i++) {
79         if (els[i].className.indexOf(class_name) != -1) {
80             teststr = "," + els[i].className.split(" ").join(",") + ",";
81             if (teststr.indexOf("," + class_name + ",") != -1) {
82                 classElements[j] = els[i];
83                 j++;
84             }
85         }
86     }
87     return classElements;
90 /**
91  * sets current selected db
92  *
93  * @param    string    db name
94  */
95 function setDb(new_db) {
96     //alert('setDb(' + new_db + ')');
97     if (new_db != db) {
98         // db has changed
99         //alert( new_db + '(' + new_db.length + ') : ' + db );
101         var old_db = db;
102         db = new_db;
104         // the db name as an id exists only when LeftFrameLight is false
105         if (window.frame_navigation.document.getElementById(db) == null) {
106             // happens when LeftFrameLight is true
107             // db is unknown, reload complete left frame
108             refreshNavigation();
109         } else {
110             // happens when LeftFrameLight is false
111             unmarkDbTable(old_db);
112             markDbTable(db);
113         }
115         // TODO: add code to expand db in lightview mode
117         // refresh querywindow
118         refreshQuerywindow();
119     }
123  * sets current selected table (called from navigation.php)
125  * @param    string    table name
126  */
127 function setTable(new_table) {
128     //alert('setTable(' + new_table + ')');
129     if (new_table != table) {
130         // table has changed
131         //alert( new_table + '(' + new_table.length + ') : ' + table );
133         table = new_table;
135         if (window.frame_navigation.document.getElementById(db + '.' + table) == null
136          && table != '') {
137             // table is unknown, reload complete left frame
138             refreshNavigation();
140         }
141         // TODO: add code to expand table in lightview mode
143         // refresh querywindow
144         refreshQuerywindow();
145     }
149  * reloads main frame
151  * @uses     goTo()
152  * @uses     opendb_url
153  * @uses     token
154  * @uses     db
155  * @uses     server
156  * @uses     table
157  * @uses     lang
158  * @uses    collation_connection
159  * @uses    encodeURIComponent()
160  * @param    string    url    name of page to be loaded
161  */
162 function refreshMain(url) {
163     if (! url) {
164         if (db) {
165             url = opendb_url;
166         } else {
167             url = 'main.php';
168         }
169     }
170     //alert(db);
171     goTo(url + '?server=' + encodeURIComponent(server) +
172         '&token=' + encodeURIComponent(token) +
173         '&db=' + encodeURIComponent(db) +
174         '&table=' + encodeURIComponent(table) +
175         '&lang=' + encodeURIComponent(lang) +
176         '&collation_connection=' + encodeURIComponent(collation_connection),
177         'main');
181  * reloads navigation frame
183  * @uses     goTo()
184  * @uses     token
185  * @uses     db
186  * @uses     server
187  * @uses     table
188  * @uses     lang
189  * @uses    collation_connection
190  * @uses    encodeURIComponent()
191  */
192 function refreshNavigation() {
193     goTo('navigation.php?server=' + encodeURIComponent(server) +
194         '&token=' + encodeURIComponent(token)  +
195         '&db=' + encodeURIComponent(db)  +
196         '&table=' + encodeURIComponent(table) +
197         '&lang=' + encodeURIComponent(lang) +
198         '&collation_connection=' + encodeURIComponent(collation_connection)
199         );
203  * adds class to element
204  */
205 function addClass(element, classname)
207     if (element != null) {
208         element.className += ' ' + classname;
209         //alert('set class: ' + classname + ', now: ' + element.className);
210     }
214  * removes class from element
215  */
216 function removeClass(element, classname)
218     if (element != null) {
219         element.className = element.className.replace(' ' + classname, '');
220         // if there is no other class anem there is no leading space
221         element.className = element.className.replace(classname, '');
222         //alert('removed class: ' + classname + ', now: ' + element.className);
223     }
226 function unmarkDbTable(db, table)
228     var element_reference = window.frame_navigation.document.getElementById(db);
229     if (element_reference != null) {
230         //alert('remove from: ' + db);
231         removeClass(element_reference.parentNode, 'marked');
232     }
234     element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
235     if (element_reference != null) {
236         //alert('remove from: ' + db + '.' + table);
237         removeClass(element_reference.parentNode, 'marked');
238     }
241 function markDbTable(db, table)
243     var element_reference = window.frame_navigation.document.getElementById(db);
244     if (element_reference != null) {
245         addClass(element_reference.parentNode, 'marked');
246         // scrolldown
247         element_reference.focus();
248         // opera marks the text, we dont want this ...
249         element_reference.blur();
250     }
252     element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
253     if (element_reference != null) {
254         addClass(element_reference.parentNode, 'marked');
255         // scrolldown
256         element_reference.focus();
257         // opera marks the text, we dont want this ...
258         element_reference.blur();
259     }
261     // return to main frame ...
262     window.frame_content.focus();
266  * sets current selected server, table and db (called from libraries/footer.inc.php)
267  */
268 function setAll( new_lang, new_collation_connection, new_server, new_db, new_table, new_token ) {
269     //alert('setAll( ' + new_lang + ', ' + new_collation_connection + ', ' + new_server + ', ' + new_db + ', ' + new_table + ', ' + new_token + ' )');
270     if (new_server != server || new_lang != lang
271       || new_collation_connection != collation_connection) {
272         // something important has changed
273         server = new_server;
274         db     = new_db;
275         table  = new_table;
276         collation_connection  = new_collation_connection;
277         lang  = new_lang;
278         token  = new_token;
279         refreshNavigation();
280     } else if (new_db != db || new_table != table) {
281         // save new db and table
282         var old_db    = db;
283         var old_table = table;
284         db        = new_db;
285         table     = new_table;
287         if (window.frame_navigation.document.getElementById(db) == null
288           && window.frame_navigation.document.getElementById(db + '.' + table) == null ) {
289             // table or db is unknown, reload complete left frame
290             refreshNavigation();
291         } else {
292             unmarkDbTable(old_db, old_table);
293             markDbTable(db, table);
294         }
296         // TODO: add code to expand db in lightview mode
298         // refresh querywindow
299         refreshQuerywindow();
300     }
303 function reload_querywindow(db, table, sql_query)
305     if ( ! querywindow.closed && querywindow.location ) {
306         if ( ! querywindow.document.sqlform.LockFromUpdate
307           || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
308             querywindow.document.getElementById('hiddenqueryform').db.value = db;
309             querywindow.document.getElementById('hiddenqueryform').table.value = table;
311             if (sql_query) {
312                 querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
313             }
315             querywindow.document.getElementById('hiddenqueryform').submit();
316         }
317     }
321  * brings query window to front and inserts query to be edited
322  */
323 function focus_querywindow(sql_query)
325     /* if ( querywindow && !querywindow.closed && querywindow.location) { */
326     if ( !querywindow || querywindow.closed || !querywindow.location) {
327         // we need first to open the window and cannot pass the query with it
328         // as we dont know if the query exceeds max url length
329         /* url = 'querywindow.php?' + common_query + '&db=' + db + '&table=' + table + '&sql_query=SELECT * FROM'; */
330         query_to_load = sql_query;
331         open_querywindow();
332         insertQuery(0);
333     } else {
334         //var querywindow = querywindow;
335         if ( querywindow.document.getElementById('hiddenqueryform').querydisplay_tab != 'sql' ) {
336             querywindow.document.getElementById('hiddenqueryform').querydisplay_tab.value = "sql";
337             querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
338             querywindow.document.getElementById('hiddenqueryform').submit();
339             querywindow.focus();
340         } else {
341             querywindow.focus();
342         }
343     }
344     return true;
348  * inserts query string into query window textarea
349  * called from script tag in querywindow
350  */
351 function insertQuery() {
352     if (query_to_load != '' && querywindow.document && querywindow.document.getElementById && querywindow.document.getElementById('sqlquery')) {
353         querywindow.document.getElementById('sqlquery').value = query_to_load;
354         query_to_load = '';
355         return true;
356     }
357     return false;
360 function open_querywindow( url ) {
361     if ( ! url ) {
362         url = 'querywindow.php?' + common_query + '&db=' + encodeURIComponent(db) + '&table=' + encodeURIComponent(table);
363     }
365     if (!querywindow.closed && querywindow.location) {
366         goTo( url, 'query' );
367         querywindow.focus();
368     } else {
369         querywindow = window.open( url + '&init=1', '',
370             'toolbar=0,location=0,directories=0,status=1,menubar=0,' +
371             'scrollbars=yes,resizable=yes,' +
372             'width=' + querywindow_width + ',' +
373             'height=' + querywindow_height );
374     }
376     if ( ! querywindow.opener ) {
377        querywindow.opener = window.window;
378     }
380     if ( window.focus ) {
381         querywindow.focus();
382     }
384     return true;
387 function refreshQuerywindow( url ) {
389     if ( ! querywindow.closed && querywindow.location ) {
390         if ( ! querywindow.document.sqlform.LockFromUpdate
391           || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
392             open_querywindow( url )
393         }
394     }
398  * opens new url in target frame, with default being left frame
399  * valid is 'main' and 'querywindow' all others leads to 'left'
401  * @param    string    targeturl    new url to load
402  * @param    string    target       frame where to load the new url
403  */
404 function goTo(targeturl, target) {
405     //alert(targeturl);
406     if ( target == 'main' ) {
407         target = window.frame_content;
408     } else if ( target == 'query' ) {
409         target = querywindow;
410         //return open_querywindow( targeturl );
411     } else if ( ! target ) {
412         target = window.frame_navigation;
413     }
415     if ( target ) {
416         if ( target.location.href == targeturl ) {
417             return true;
418         } else if ( target.location.href == pma_absolute_uri + targeturl ) {
419             return true;
420         }
422         if ( safari_browser ) {
423             target.location.href = targeturl;
424         } else {
425             target.location.replace(targeturl);
426         }
427     }
429     return true;
432 // opens selected db in main frame
433 function openDb(new_db) {
434     //alert('opendb(' +  new_db + ')');
435     setDb(new_db);
436     setTable('');
437     refreshMain(opendb_url);
438     return true;
441 function updateTableTitle( table_link_id, new_title ) {
442     //alert('updateTableTitle');
443     if ( window.parent.frame_navigation.document && window.parent.frame_navigation.document.getElementById(table_link_id) ) {
444         var left = window.parent.frame_navigation.document;
446         var link = left.getElementById(table_link_id);
447         link.title = window.parent.pma_text_default_tab + ': ' + new_title;
449         var link = left.getElementById('quick_' + table_link_id);
450         link.title = window.parent.pma_text_left_default_tab + ': ' + new_title;
452         return true;
453     }
455     return false;