Use unicode non breakable space instead of  .
[phpmyadmin/crack.git] / js / common.js
blob99c43fb925ed7578c2874090efa76bf9544d0321
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 opbject 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         if (window.frame_navigation.document.getElementById(db) == null) {
105             // db is unknown, reload complete left frame
106             refreshNavigation();
107         } else {
108             unmarkDbTable(old_db);
109             markDbTable(db);
110         }
112         // TODO: add code to expand db in lightview mode
114         // refresh querywindow
115         refreshQuerywindow();
116     }
120  * sets current selected table (called from navigation.php)
122  * @param    string    table name
123  */
124 function setTable(new_table) {
125     //alert('setTable(' + new_table + ')');
126     if (new_table != table) {
127         // table has changed
128         //alert( new_table + '(' + new_table.length + ') : ' + table );
130         table = new_table;
132         if (window.frame_navigation.document.getElementById(db + '.' + table) == null
133          && table != '') {
134             // table is unknown, reload complete left frame
135             refreshNavigation();
137         }
138         // TODO: add code to expand table in lightview mode
140         // refresh querywindow
141         refreshQuerywindow();
142     }
146  * reloads mian frame
148  * @uses     goTo()
149  * @uses     opendb_url
150  * @uses     db
151  * @uses     server
152  * @uses     table
153  * @uses     lang
154  * @uses    collation_connection
155  * @uses    encodeURIComponent()
156  * @param    string    url    name of page to be loaded
157  */
158 function refreshMain(url) {
159     if (! url) {
160         if (db) {
161             url = opendb_url;
162         } else {
163             url = 'main.php';
164         }
165     }
166     //alert(db);
167     goTo(url + '?server=' + encodeURIComponent(server) +
168         '&db=' + encodeURIComponent(db) +
169         '&table=' + encodeURIComponent(table) +
170         '&lang=' + encodeURIComponent(lang) +
171         '&collation_connection=' + encodeURIComponent(collation_connection),
172         'main');
176  * reloads navigation frame
178  * @uses     goTo()
179  * @uses     db
180  * @uses     server
181  * @uses     table
182  * @uses     lang
183  * @uses    collation_connection
184  * @uses    encodeURIComponent()
185  */
186 function refreshNavigation() {
187     goTo('navigation.php?server=' + encodeURIComponent(server) +
188         '&db=' + encodeURIComponent(db)  +
189         '&table=' + encodeURIComponent(table) +
190         '&lang=' + encodeURIComponent(lang) +
191         '&collation_connection=' + encodeURIComponent(collation_connection)
192         );
196  * adds class to element
197  */
198 function addClass(element, classname)
200     if (element != null) {
201         element.className += ' ' + classname;
202         //alert('set class: ' + classname + ', now: ' + element.className);
203     }
207  * removes class from element
208  */
209 function removeClass(element, classname)
211     if (element != null) {
212         element.className = element.className.replace(' ' + classname, '');
213         // if there is no other class anem there is no leading space
214         element.className = element.className.replace(classname, '');
215         //alert('removed class: ' + classname + ', now: ' + element.className);
216     }
219 function unmarkDbTable(db, table)
221     var element_reference = window.frame_navigation.document.getElementById(db);
222     if (element_reference != null) {
223         //alert('remove from: ' + db);
224         removeClass(element_reference.parentNode, 'marked');
225     }
227     element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
228     if (element_reference != null) {
229         //alert('remove from: ' + db + '.' + table);
230         removeClass(element_reference.parentNode, 'marked');
231     }
234 function markDbTable(db, table)
236     var element_reference = window.frame_navigation.document.getElementById(db);
237     if (element_reference != null) {
238         addClass(element_reference.parentNode, 'marked');
239         // scrolldown
240         element_reference.focus();
241         // opera marks the text, we dont want this ...
242         element_reference.blur();
243     }
245     element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
246     if (element_reference != null) {
247         addClass(element_reference.parentNode, 'marked');
248         // scrolldown
249         element_reference.focus();
250         // opera marks the text, we dont want this ...
251         element_reference.blur();
252     }
254     // return to main frame ...
255     window.frame_content.focus();
259  * sets current selected server, table and db (called from libraries/footer.inc.php)
260  */
261 function setAll( new_lang, new_collation_connection, new_server, new_db, new_table ) {
262     //alert('setAll( ' + new_lang + ', ' + new_collation_connection + ', ' + new_server + ', ' + new_db + ', ' + new_table + ' )');
263     if (new_server != server || new_lang != lang
264       || new_collation_connection != collation_connection) {
265         // something important has changed
266         server = new_server;
267         db     = new_db;
268         table  = new_table;
269         collation_connection  = new_collation_connection;
270         lang  = new_lang;
271         refreshNavigation();
272     } else if (new_db != db || new_table != table) {
273         // save new db and table
274         var old_db    = db;
275         var old_table = table;
276         db        = new_db;
277         table     = new_table;
279         if (window.frame_navigation.document.getElementById(db) == null
280           && window.frame_navigation.document.getElementById(db + '.' + table) == null ) {
281             // table or db is unknown, reload complete left frame
282             refreshNavigation();
283         } else {
284             unmarkDbTable(old_db, old_table);
285             markDbTable(db, table);
286         }
288         // TODO: add code to expand db in lightview mode
290         // refresh querywindow
291         refreshQuerywindow();
292     }
295 function reload_querywindow(db, table, sql_query)
297     if ( ! querywindow.closed && querywindow.location ) {
298         if ( ! querywindow.document.sqlform.LockFromUpdate
299           || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
300             querywindow.document.getElementById('hiddenqueryform').db.value = db;
301             querywindow.document.getElementById('hiddenqueryform').table.value = table;
303             if (sql_query) {
304                 querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
305             }
307             querywindow.document.getElementById('hiddenqueryform').submit();
308         }
309     }
313  * brings query window to front and inserts query to be edited
314  */
315 function focus_querywindow(sql_query)
317     /* if ( querywindow && !querywindow.closed && querywindow.location) { */
318     if ( !querywindow || querywindow.closed || !querywindow.location) {
319         // we need first to open the window and cannot pass the query with it
320         // as we dont know if the query exceeds max url length
321         /* url = 'querywindow.php?' + common_query + '&db=' + db + '&table=' + table + '&sql_query=SELECT * FROM'; */
322         query_to_load = sql_query;
323         open_querywindow();
324         insertQuery(0);
325     } else {
326         //var querywindow = querywindow;
327         if ( querywindow.document.getElementById('hiddenqueryform').querydisplay_tab != 'sql' ) {
328             querywindow.document.getElementById('hiddenqueryform').querydisplay_tab.value = "sql";
329             querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
330             querywindow.document.getElementById('hiddenqueryform').submit();
331             querywindow.focus();
332         } else {
333             querywindow.focus();
334         }
335     }
336     return true;
340  * inserts query string into query window textarea
341  * called from script tag in querywindow
342  */
343 function insertQuery() {
344     if (query_to_load != '' && querywindow.document && querywindow.document.getElementById && querywindow.document.getElementById('sqlquery')) {
345         querywindow.document.getElementById('sqlquery').value = query_to_load;
346         query_to_load = '';
347         return true;
348     }
349     return false;
352 function open_querywindow( url ) {
353     if ( ! url ) {
354         url = 'querywindow.php?' + common_query + '&db=' + encodeURIComponent(db) + '&table=' + encodeURIComponent(table);
355     }
357     if (!querywindow.closed && querywindow.location) {
358         goTo( url, 'query' );
359         querywindow.focus();
360     } else {
361         querywindow = window.open( url + '&init=1', '',
362             'toolbar=0,location=0,directories=0,status=1,menubar=0,' +
363             'scrollbars=yes,resizable=yes,' +
364             'width=' + querywindow_width + ',' +
365             'height=' + querywindow_height );
366     }
368     if ( ! querywindow.opener ) {
369        querywindow.opener = window.window;
370     }
372     if ( window.focus ) {
373         querywindow.focus();
374     }
376     return true;
379 function refreshQuerywindow( url ) {
381     if ( ! querywindow.closed && querywindow.location ) {
382         if ( ! querywindow.document.sqlform.LockFromUpdate
383           || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
384             open_querywindow( url )
385         }
386     }
390  * opens new url in target frame, with default beeing left frame
391  * valid is 'main' and 'querywindow' all others leads to 'left'
393  * @param    string    targeturl    new url to load
394  * @param    string    target       frame where to load the new url
395  */
396 function goTo(targeturl, target) {
397     //alert(targeturl);
398     if ( target == 'main' ) {
399         target = window.frame_content;
400     } else if ( target == 'query' ) {
401         target = querywindow;
402         //return open_querywindow( targeturl );
403     } else if ( ! target ) {
404         target = window.frame_navigation;
405     }
407     if ( target ) {
408         if ( target.location.href == targeturl ) {
409             return true;
410         } else if ( target.location.href == pma_absolute_uri + targeturl ) {
411             return true;
412         }
414         if ( safari_browser ) {
415             target.location.href = targeturl;
416         } else {
417             target.location.replace(targeturl);
418         }
419     }
421     return true;
424 // opens selected db in main frame
425 function openDb(new_db) {
426     //alert('opendb(' +  new_db + ')');
427     setDb(new_db);
428     setTable('');
429     refreshMain(opendb_url);
430     return true;
433 function updateTableTitle( table_link_id, new_title ) {
434     //alert('updateTableTitle');
435     if ( window.parent.frame_navigation.document.getElementById(table_link_id) ) {
436         var left = window.parent.frame_navigation.document;
437         left.getElementById(table_link_id).title = new_title;
438         new_title = left.getElementById('icon_' + table_link_id).alt + ': ' + new_title;
439         left.getElementById('quick_' + table_link_id).title = new_title;
440         return true;
441     }
443     return false;