/** Toggle visibility
* @param string
* @return boolean
*/
function toggle(id) {
var el = document.getElementById(id);
el.className = (el.className == 'hidden' ? '' : 'hidden');
return true;
}
/** Set permanent cookie
* @param string
* @param number
* @param string optional
*/
function cookie(assign, days) {
var date = new Date();
date.setDate(date.getDate() + days);
document.cookie = assign + '; expires=' + date;
}
/** Verify current Adminer version
*/
function verifyVersion() {
cookie('adminer_version=0', 1);
var script = document.createElement('script');
script.src = location.protocol + '//www.adminer.org/version.php';
document.body.appendChild(script);
}
/** Get value of select
* @param HTMLSelectElement
* @return string
*/
function selectValue(select) {
var selected = select.options[select.selectedIndex];
return ((selected.attributes.value || {}).specified ? selected.value : selected.text);
}
/** Get parent node with specified tag name.
* @param HTMLElement
* @param string regular expression
* @return HTMLElement
*/
function parentTag(el, tag) {
var re = new RegExp('^' + tag + '$', 'i');
while (el && !re.test(el.tagName)) {
el = el.parentNode;
}
return el;
}
/** Set checked class
* @param HTMLInputElement
*/
function trCheck(el) {
var tr = parentTag(el, 'tr');
tr.className = tr.className.replace(/(^|\s)checked(\s|$)/, '$2') + (el.checked ? ' checked' : '');
}
/** Check all elements matching given name
* @param HTMLInputElement
* @param RegExp
*/
function formCheck(el, name) {
var elems = el.form.elements;
for (var i=0; i < elems.length; i++) {
if (name.test(elems[i].name)) {
elems[i].checked = el.checked;
trCheck(elems[i]);
}
}
}
/** Check all rows in
*/
function tableCheck() {
var tables = document.getElementsByTagName('table');
for (var i=0; i < tables.length; i++) {
if (/(^|\s)checkable(\s|$)/.test(tables[i].className)) {
var trs = tables[i].getElementsByTagName('tr');
for (var j=0; j < trs.length; j++) {
trCheck(trs[j].firstChild.firstChild);
}
}
}
}
/** Uncheck single element
* @param string
*/
function formUncheck(id) {
var el = document.getElementById(id);
el.checked = false;
trCheck(el);
}
/** Get number of checked elements matching given name
* @param HTMLInputElement
* @param RegExp
* @return number
*/
function formChecked(el, name) {
var checked = 0;
var elems = el.form.elements;
for (var i=0; i < elems.length; i++) {
if (name.test(elems[i].name) && elems[i].checked) {
checked++;
}
}
return checked;
}
/** Select clicked row
* @param MouseEvent
* @param [boolean] force click
*/
function tableClick(event, click) {
click = (click || !window.getSelection || getSelection().isCollapsed);
var el = event.target || event.srcElement;
while (!/^tr$/i.test(el.tagName)) {
if (/^(table|a|input|textarea)$/i.test(el.tagName)) {
if (el.type != 'checkbox') {
return;
}
checkboxClick(event, el);
click = false;
}
el = el.parentNode;
}
el = el.firstChild.firstChild;
if (click) {
el.checked = !el.checked;
el.onclick && el.onclick();
}
trCheck(el);
}
var lastChecked;
/** Shift-click on checkbox for multiple selection.
* @param MouseEvent
* @param HTMLInputElement
*/
function checkboxClick(event, el) {
if (!el.name) {
return;
}
if (event.shiftKey && (!lastChecked || lastChecked.name == el.name)) {
var checked = (lastChecked ? lastChecked.checked : true);
var inputs = parentTag(el, 'table').getElementsByTagName('input');
var checking = !lastChecked;
for (var i=0; i < inputs.length; i++) {
var input = inputs[i];
if (input.name === el.name) {
if (checking) {
input.checked = checked;
trCheck(input);
}
if (input === el || input === lastChecked) {
if (checking) {
break;
}
checking = true;
}
}
}
} else {
lastChecked = el;
}
}
/** Set HTML code of an element
* @param string
* @param string undefined to set parentNode to
*/
function setHtml(id, html) {
var el = document.getElementById(id);
if (el) {
if (html == undefined) {
el.parentNode.innerHTML = ' ';
} else {
el.innerHTML = html;
}
}
}
/** Find node position
* @param Node
* @return number
*/
function nodePosition(el) {
var pos = 0;
while (el = el.previousSibling) {
pos++;
}
return pos;
}
/** Go to the specified page
* @param string
* @param string
* @param [MouseEvent]
*/
function pageClick(href, page, event) {
if (!isNaN(page) && page) {
href += (page != 1 ? '&page=' + (page - 1) : '');
location.href = href;
}
}
/** Display items in menu
* @param HTMLElement
* @param MouseEvent
*/
function menuOver(el, event) {
var a = event.target;
if (/^a$/i.test(a.tagName) && a.offsetLeft + a.offsetWidth > a.parentNode.offsetWidth - 15) { // 15 - ellipsis
el.style.overflow = 'visible';
}
}
/** Hide items in menu
* @param HTMLElement
*/
function menuOut(el) {
el.style.overflow = 'auto';
}
/** Add row in select fieldset
* @param HTMLSelectElement
*/
function selectAddRow(field) {
field.onchange = function () {
selectFieldChange(field.form);
};
field.onchange();
var row = field.parentNode.cloneNode(true);
var selects = row.getElementsByTagName('select');
for (var i=0; i < selects.length; i++) {
selects[i].name = selects[i].name.replace(/[a-z]\[\d+/, '$&1');
selects[i].selectedIndex = 0;
}
var inputs = row.getElementsByTagName('input');
if (inputs.length) {
inputs[0].name = inputs[0].name.replace(/[a-z]\[\d+/, '$&1');
inputs[0].value = '';
inputs[0].className = '';
}
field.parentNode.parentNode.appendChild(row);
}
/** Clear column name after resetting search
* @param HTMLInputElement
*/
function selectSearchSearch(el) {
if (!el.value) {
el.parentNode.firstChild.selectedIndex = 0;
}
}
/** Toggles column context menu
* @param HTMLElement
* @param [string] extra class name
*/
function columnMouse(el, className) {
var spans = el.getElementsByTagName('span');
for (var i=0; i < spans.length; i++) {
if (/column/.test(spans[i].className)) {
spans[i].className = 'column' + (className || '');
}
}
}
/** Fill column in search field
* @param string
*/
function selectSearch(name) {
var el = document.getElementById('fieldset-search');
el.className = '';
var divs = el.getElementsByTagName('div');
for (var i=0; i < divs.length; i++) {
var div = divs[i];
if (/select/i.test(div.firstChild.tagName) && selectValue(div.firstChild) == name) {
break;
}
}
if (i == divs.length) {
div.firstChild.value = name;
div.firstChild.onchange();
}
div.lastChild.focus();
}
/** Check if Ctrl key (Command key on Mac) was pressed
* @param KeyboardEvent|MouseEvent
* @return boolean
*/
function isCtrl(event) {
return (event.ctrlKey || event.metaKey) && !event.altKey; // shiftKey allowed
}
/** Send form by Ctrl+Enter on