Permalink
Browse files

Separate editing JavaScript

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@798 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
1 parent c6595fb commit 45f5cdd33055b498337b4d55a7452d3225a9fce6 jakubvrana committed Jul 3, 2009
Showing with 240 additions and 239 deletions.
  1. +237 −0 adminer/editing.js
  2. +0 −238 adminer/functions.js
  3. +1 −0 adminer/include/design.inc.php
  4. +1 −1 compile.php
  5. +1 −0 editor/editing.js
View
237 adminer/editing.js
@@ -0,0 +1,237 @@
+// Adminer specific functions
+
+var added = '.', row_count;
+
+function re_escape(s) {
+ return s.replace(/[\[\]\\^$*+?.(){|}]/, '\\$&');
+}
+
+function idf_escape(s) {
+ return '`' + s.replace(/`/, '``') + '`';
+}
+
+function editing_name_change(field) {
+ var name = field.name.substr(0, field.name.length - 7);
+ var type = field.form[name + '[type]'];
+ var opts = type.options;
+ var table = re_escape(field.value);
+ var column = '';
+ var match;
+ if ((match = /(.+)_(.+)/.exec(table)) || (match = /(.*[a-z])([A-Z].*)/.exec(table))) { // limited to single word columns
+ table = match[1];
+ column = match[2];
+ }
+ var plural = '(?:e?s)?';
+ var tab_col = table + plural + '_?' + column;
+ var re = new RegExp('(^' + idf_escape(table + plural) + '\\.' + idf_escape(column) + '$' // table_column
+ + '|^' + idf_escape(tab_col) + '\\.' // table
+ + '|^' + idf_escape(column + plural) + '\\.' + idf_escape(table) + '$' // column_table
+ + ')|\\.' + idf_escape(tab_col) + '$' // column
+ , 'i');
+ var candidate; // don't select anything with ambiguous match (like column `id`)
+ for (var i = opts.length; i--; ) {
+ if (opts[i].value.substr(0, 1) != '`') { // common type
+ if (i == opts.length - 2 && candidate && !match[1] && name == 'fields[1]') { // single target table, link to column, first field - probably `id`
+ return false;
+ }
+ break;
+ }
+ if (match = re.exec(opts[i].value)) {
+ if (candidate) {
+ return false;
+ }
+ candidate = i;
+ }
+ }
+ if (candidate) {
+ opts.selectedIndex = candidate;
+ type.onchange();
+ }
+}
+
+function editing_add_row(button, allowed) {
+ if (allowed && row_count >= allowed) {
+ return false;
+ }
+ var match = /([0-9]+)(\.[0-9]+)?/.exec(button.name);
+ var x = match[0] + (match[2] ? added.substr(match[2].length) : added) + '1';
+ var row = button.parentNode.parentNode;
+ var row2 = row.cloneNode(true);
+ var tags = row.getElementsByTagName('select');
+ var tags2 = row2.getElementsByTagName('select');
+ for (var i=0; i < tags.length; i++) {
+ tags2[i].name = tags[i].name.replace(/([0-9.]+)/, x);
+ tags2[i].selectedIndex = tags[i].selectedIndex;
+ }
+ tags = row.getElementsByTagName('input');
+ tags2 = row2.getElementsByTagName('input');
+ for (var i=0; i < tags.length; i++) {
+ if (tags[i].name == 'auto_increment_col') {
+ tags2[i].value = x;
+ tags2[i].checked = false;
+ }
+ tags2[i].name = tags[i].name.replace(/([0-9.]+)/, x);
+ if (/\[(orig|field|comment)/.test(tags[i].name)) {
+ tags2[i].value = '';
+ }
+ }
+ tags[0].onchange = function () {
+ editing_name_change(tags[0]);
+ };
+ row.parentNode.insertBefore(row2, row.nextSibling);
+ added += '0';
+ row_count++;
+ return tags2[0];
+}
+
+function editing_remove_row(button) {
+ var field = button.form[button.name.replace(/drop_col(.+)/, 'fields$1[field]')];
+ field.parentNode.removeChild(field);
+ button.parentNode.parentNode.style.display = 'none';
+ return true;
+}
+
+function editing_type_change(type) {
+ var name = type.name.substr(0, type.name.length - 6);
+ var text = type.options[type.selectedIndex].text;
+ for (var i=0; i < type.form.elements.length; i++) {
+ var el = type.form.elements[i];
+ if (el.name == name + '[collation]') {
+ el.className = (/(char|text|enum|set)$/.test(text) ? '' : 'hidden');
+ }
+ if (el.name == name + '[unsigned]') {
+ el.className = (/(int|float|double|decimal)$/.test(text) ? '' : 'hidden');
+ }
+ }
+}
+
+function column_comments_click(checked) {
+ var trs = document.getElementById('edit-fields').getElementsByTagName('tr');
+ for (var i=0; i < trs.length; i++) {
+ trs[i].getElementsByTagName('td')[5].className = (checked ? '' : 'hidden');
+ }
+}
+
+function partition_by_change(el) {
+ var partition_table = /RANGE|LIST/.test(el.options[el.selectedIndex].text);
+ el.form['partitions'].className = (partition_table || !el.selectedIndex ? 'hidden' : '');
+ document.getElementById('partition-table').className = (partition_table ? '' : 'hidden');
+}
+
+function partition_name_change(el) {
+ var row = el.parentNode.parentNode.cloneNode(true);
+ row.firstChild.firstChild.value = '';
+ el.parentNode.parentNode.parentNode.appendChild(row);
+ el.onchange = function () {};
+}
+
+
+
+function foreign_add_row(field) {
+ var row = field.parentNode.parentNode.cloneNode(true);
+ var selects = row.getElementsByTagName('select');
+ for (var i=0; i < selects.length; i++) {
+ selects[i].name = selects[i].name.replace(/\]/, '1$&');
+ selects[i].selectedIndex = 0;
+ }
+ field.parentNode.parentNode.parentNode.appendChild(row);
+ field.onchange = function () { };
+}
+
+
+
+function indexes_add_row(field) {
+ var row = field.parentNode.parentNode.cloneNode(true);
+ var spans = row.getElementsByTagName('span');
+ row.getElementsByTagName('td')[1].innerHTML = '<span>' + spans[spans.length - 1].innerHTML + '</span>';
+ var selects = row.getElementsByTagName('select');
+ for (var i=0; i < selects.length; i++) {
+ selects[i].name = selects[i].name.replace(/indexes\[[0-9]+/, '$&1');
+ selects[i].selectedIndex = 0;
+ }
+ var input = row.getElementsByTagName('input')[0];
+ input.name = input.name.replace(/indexes\[[0-9]+/, '$&1');
+ input.value = '';
+ field.parentNode.parentNode.parentNode.appendChild(row);
+ field.onchange = function () { };
+}
+
+function indexes_add_column(field) {
+ var column = field.parentNode.cloneNode(true);
+ var select = column.getElementsByTagName('select')[0];
+ select.name = select.name.replace(/\]\[[0-9]+/, '$&1');
+ select.selectedIndex = 0;
+ var input = column.getElementsByTagName('input')[0];
+ input.name = input.name.replace(/\]\[[0-9]+/, '$&1');
+ input.value = '';
+ field.parentNode.parentNode.appendChild(column);
+ field.onchange = function () { };
+}
+
+
+
+var that, x, y, em, table_pos;
+
+function schema_mousedown(el, event) {
+ that = el;
+ x = event.clientX - el.offsetLeft;
+ y = event.clientY - el.offsetTop;
+}
+
+function schema_mousemove(ev) {
+ if (that !== undefined) {
+ ev = ev || event;
+ var left = (ev.clientX - x) / em;
+ var top = (ev.clientY - y) / em;
+ var divs = that.getElementsByTagName('div');
+ var line_set = { };
+ for (var i=0; i < divs.length; i++) {
+ if (divs[i].className == 'references') {
+ var div2 = document.getElementById((divs[i].id.substr(0, 4) == 'refs' ? 'refd' : 'refs') + divs[i].id.substr(4));
+ var ref = (table_pos[divs[i].title] ? table_pos[divs[i].title] : [ div2.parentNode.offsetTop / em, 0 ]);
+ var left1 = -1;
+ var is_top = true;
+ var id = divs[i].id.replace(/^ref.(.+)-.+/, '$1');
+ if (divs[i].parentNode != div2.parentNode) {
+ left1 = Math.min(0, ref[1] - left) - 1;
+ divs[i].style.left = left1 + 'em';
+ divs[i].getElementsByTagName('div')[0].style.width = -left1 + 'em';
+ var left2 = Math.min(0, left - ref[1]) - 1;
+ div2.style.left = left2 + 'em';
+ div2.getElementsByTagName('div')[0].style.width = -left2 + 'em';
+ is_top = (div2.offsetTop + ref[0] * em > divs[i].offsetTop + top * em);
+ }
+ if (!line_set[id]) {
+ var line = document.getElementById(divs[i].id.replace(/^....(.+)-[0-9]+$/, 'refl$1'));
+ var shift = ev.clientY - y - that.offsetTop;
+ line.style.left = (left + left1) + 'em';
+ if (is_top) {
+ line.style.top = (line.offsetTop + shift) / em + 'em';
+ }
+ if (divs[i].parentNode != div2.parentNode) {
+ line = line.getElementsByTagName('div')[0];
+ line.style.height = (line.offsetHeight + (is_top ? -1 : 1) * shift) / em + 'em';
+ }
+ line_set[id] = true;
+ }
+ }
+ }
+ that.style.left = left + 'em';
+ that.style.top = top + 'em';
+ }
+}
+
+function schema_mouseup(ev) {
+ if (that !== undefined) {
+ ev = ev || event;
+ table_pos[that.firstChild.firstChild.firstChild.data] = [ (ev.clientY - y) / em, (ev.clientX - x) / em ];
+ that = undefined;
+ var date = new Date();
+ date.setMonth(date.getMonth() + 1);
+ var s = '';
+ for (var key in table_pos) {
+ s += '_' + key + ':' + Math.round(table_pos[key][0] * 10000) / 10000 + 'x' + Math.round(table_pos[key][1] * 10000) / 10000;
+ }
+ document.cookie = 'adminer_schema=' + encodeURIComponent(s.substr(1)) + '; expires=' + date + '; path=' + location.pathname + location.search;
+ }
+}
View
238 adminer/functions.js
@@ -60,241 +60,3 @@ function select_add_row(field) {
field.parentNode.parentNode.appendChild(row);
field.onchange = function () { };
}
-
-
-
-var added = '.', row_count;
-
-function re_escape(s) {
- return s.replace(/[\[\]\\^$*+?.(){|}]/, '\\$&');
-}
-
-function idf_escape(s) {
- return '`' + s.replace(/`/, '``') + '`';
-}
-
-function editing_name_change(field) {
- var name = field.name.substr(0, field.name.length - 7);
- var type = field.form[name + '[type]'];
- var opts = type.options;
- var table = re_escape(field.value);
- var column = '';
- var match;
- if ((match = /(.+)_(.+)/.exec(table)) || (match = /(.*[a-z])([A-Z].*)/.exec(table))) { // limited to single word columns
- table = match[1];
- column = match[2];
- }
- var plural = '(?:e?s)?';
- var tab_col = table + plural + '_?' + column;
- var re = new RegExp('(^' + idf_escape(table + plural) + '\\.' + idf_escape(column) + '$' // table_column
- + '|^' + idf_escape(tab_col) + '\\.' // table
- + '|^' + idf_escape(column + plural) + '\\.' + idf_escape(table) + '$' // column_table
- + ')|\\.' + idf_escape(tab_col) + '$' // column
- , 'i');
- var candidate; // don't select anything with ambiguous match (like column `id`)
- for (var i = opts.length; i--; ) {
- if (opts[i].value.substr(0, 1) != '`') { // common type
- if (i == opts.length - 2 && candidate && !match[1] && name == 'fields[1]') { // single target table, link to column, first field - probably `id`
- return false;
- }
- break;
- }
- if (match = re.exec(opts[i].value)) {
- if (candidate) {
- return false;
- }
- candidate = i;
- }
- }
- if (candidate) {
- opts.selectedIndex = candidate;
- type.onchange();
- }
-}
-
-function editing_add_row(button, allowed) {
- if (allowed && row_count >= allowed) {
- return false;
- }
- var match = /([0-9]+)(\.[0-9]+)?/.exec(button.name);
- var x = match[0] + (match[2] ? added.substr(match[2].length) : added) + '1';
- var row = button.parentNode.parentNode;
- var row2 = row.cloneNode(true);
- var tags = row.getElementsByTagName('select');
- var tags2 = row2.getElementsByTagName('select');
- for (var i=0; i < tags.length; i++) {
- tags2[i].name = tags[i].name.replace(/([0-9.]+)/, x);
- tags2[i].selectedIndex = tags[i].selectedIndex;
- }
- tags = row.getElementsByTagName('input');
- tags2 = row2.getElementsByTagName('input');
- for (var i=0; i < tags.length; i++) {
- if (tags[i].name == 'auto_increment_col') {
- tags2[i].value = x;
- tags2[i].checked = false;
- }
- tags2[i].name = tags[i].name.replace(/([0-9.]+)/, x);
- if (/\[(orig|field|comment)/.test(tags[i].name)) {
- tags2[i].value = '';
- }
- }
- tags[0].onchange = function () {
- editing_name_change(tags[0]);
- };
- row.parentNode.insertBefore(row2, row.nextSibling);
- added += '0';
- row_count++;
- return tags2[0];
-}
-
-function editing_remove_row(button) {
- var field = button.form[button.name.replace(/drop_col(.+)/, 'fields$1[field]')];
- field.parentNode.removeChild(field);
- button.parentNode.parentNode.style.display = 'none';
- return true;
-}
-
-function editing_type_change(type) {
- var name = type.name.substr(0, type.name.length - 6);
- var text = type.options[type.selectedIndex].text;
- for (var i=0; i < type.form.elements.length; i++) {
- var el = type.form.elements[i];
- if (el.name == name + '[collation]') {
- el.className = (/(char|text|enum|set)$/.test(text) ? '' : 'hidden');
- }
- if (el.name == name + '[unsigned]') {
- el.className = (/(int|float|double|decimal)$/.test(text) ? '' : 'hidden');
- }
- }
-}
-
-function column_comments_click(checked) {
- var trs = document.getElementById('edit-fields').getElementsByTagName('tr');
- for (var i=0; i < trs.length; i++) {
- trs[i].getElementsByTagName('td')[5].className = (checked ? '' : 'hidden');
- }
-}
-
-function partition_by_change(el) {
- var partition_table = /RANGE|LIST/.test(el.options[el.selectedIndex].text);
- el.form['partitions'].className = (partition_table || !el.selectedIndex ? 'hidden' : '');
- document.getElementById('partition-table').className = (partition_table ? '' : 'hidden');
-}
-
-function partition_name_change(el) {
- var row = el.parentNode.parentNode.cloneNode(true);
- row.firstChild.firstChild.value = '';
- el.parentNode.parentNode.parentNode.appendChild(row);
- el.onchange = function () {};
-}
-
-
-
-function foreign_add_row(field) {
- var row = field.parentNode.parentNode.cloneNode(true);
- var selects = row.getElementsByTagName('select');
- for (var i=0; i < selects.length; i++) {
- selects[i].name = selects[i].name.replace(/\]/, '1$&');
- selects[i].selectedIndex = 0;
- }
- field.parentNode.parentNode.parentNode.appendChild(row);
- field.onchange = function () { };
-}
-
-
-
-function indexes_add_row(field) {
- var row = field.parentNode.parentNode.cloneNode(true);
- var spans = row.getElementsByTagName('span');
- row.getElementsByTagName('td')[1].innerHTML = '<span>' + spans[spans.length - 1].innerHTML + '</span>';
- var selects = row.getElementsByTagName('select');
- for (var i=0; i < selects.length; i++) {
- selects[i].name = selects[i].name.replace(/indexes\[[0-9]+/, '$&1');
- selects[i].selectedIndex = 0;
- }
- var input = row.getElementsByTagName('input')[0];
- input.name = input.name.replace(/indexes\[[0-9]+/, '$&1');
- input.value = '';
- field.parentNode.parentNode.parentNode.appendChild(row);
- field.onchange = function () { };
-}
-
-function indexes_add_column(field) {
- var column = field.parentNode.cloneNode(true);
- var select = column.getElementsByTagName('select')[0];
- select.name = select.name.replace(/\]\[[0-9]+/, '$&1');
- select.selectedIndex = 0;
- var input = column.getElementsByTagName('input')[0];
- input.name = input.name.replace(/\]\[[0-9]+/, '$&1');
- input.value = '';
- field.parentNode.parentNode.appendChild(column);
- field.onchange = function () { };
-}
-
-
-
-var that, x, y, em, table_pos;
-
-function schema_mousedown(el, event) {
- that = el;
- x = event.clientX - el.offsetLeft;
- y = event.clientY - el.offsetTop;
-}
-
-function schema_mousemove(ev) {
- if (that !== undefined) {
- ev = ev || event;
- var left = (ev.clientX - x) / em;
- var top = (ev.clientY - y) / em;
- var divs = that.getElementsByTagName('div');
- var line_set = { };
- for (var i=0; i < divs.length; i++) {
- if (divs[i].className == 'references') {
- var div2 = document.getElementById((divs[i].id.substr(0, 4) == 'refs' ? 'refd' : 'refs') + divs[i].id.substr(4));
- var ref = (table_pos[divs[i].title] ? table_pos[divs[i].title] : [ div2.parentNode.offsetTop / em, 0 ]);
- var left1 = -1;
- var is_top = true;
- var id = divs[i].id.replace(/^ref.(.+)-.+/, '$1');
- if (divs[i].parentNode != div2.parentNode) {
- left1 = Math.min(0, ref[1] - left) - 1;
- divs[i].style.left = left1 + 'em';
- divs[i].getElementsByTagName('div')[0].style.width = -left1 + 'em';
- var left2 = Math.min(0, left - ref[1]) - 1;
- div2.style.left = left2 + 'em';
- div2.getElementsByTagName('div')[0].style.width = -left2 + 'em';
- is_top = (div2.offsetTop + ref[0] * em > divs[i].offsetTop + top * em);
- }
- if (!line_set[id]) {
- var line = document.getElementById(divs[i].id.replace(/^....(.+)-[0-9]+$/, 'refl$1'));
- var shift = ev.clientY - y - that.offsetTop;
- line.style.left = (left + left1) + 'em';
- if (is_top) {
- line.style.top = (line.offsetTop + shift) / em + 'em';
- }
- if (divs[i].parentNode != div2.parentNode) {
- line = line.getElementsByTagName('div')[0];
- line.style.height = (line.offsetHeight + (is_top ? -1 : 1) * shift) / em + 'em';
- }
- line_set[id] = true;
- }
- }
- }
- that.style.left = left + 'em';
- that.style.top = top + 'em';
- }
-}
-
-function schema_mouseup(ev) {
- if (that !== undefined) {
- ev = ev || event;
- table_pos[that.firstChild.firstChild.firstChild.data] = [ (ev.clientY - y) / em, (ev.clientX - x) / em ];
- that = undefined;
- var date = new Date();
- date.setMonth(date.getMonth() + 1);
- var s = '';
- for (var key in table_pos) {
- s += '_' + key + ':' + Math.round(table_pos[key][0] * 10000) / 10000 + 'x' + Math.round(table_pos[key][1] * 10000) / 10000;
- }
- document.cookie = 'adminer_schema=' + encodeURIComponent(s.substr(1)) + '; expires=' + date + '; path=' + location.pathname + location.search;
- }
-}
View
1 adminer/include/design.inc.php
@@ -19,6 +19,7 @@ function page_header($title, $error = "", $breadcrumb = array(), $title2 = "") {
<body onload="load_jush();<?php echo (isset($_COOKIE["adminer_version"]) ? "" : " verify_version('$VERSION');"); ?>">
<script type="text/javascript" src="../adminer/functions.js"></script>
+<script type="text/javascript" src="./editing.js"></script>
<div id="content">
<?php
View
2 compile.php
@@ -197,7 +197,7 @@ function compile_file($match) {
}
$file = preg_replace_callback("~compile_file\\('([^']+)', '([^']+)'\\)~", 'compile_file', $file); // integrate static files
$replace = 'htmlspecialchars(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\1&amp;version=' . $VERSION;
-$file = preg_replace('~\\.\\./adminer/(default\\.css|functions\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
+$file = preg_replace('~(?:\\.\\./adminer/|\\./)(default\\.css|functions\\.js|editing\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
$file = preg_replace('~\\.\\./adminer/((plus|cross|up|down|arrow)\\.gif)~', '" . ' . $replace, $file);
$file = str_replace("../externals/jush/", "http://jush.sourceforge.net/", $file);
$file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
View
1 editor/editing.js
@@ -0,0 +1 @@
+// Editor specific functions

0 comments on commit 45f5cdd

Please sign in to comment.