2009-07-03 12:38:21 +00:00
// Adminer specific functions
2009-12-17 14:53:26 +00:00
/ * * L o a d s y n t a x h i g h l i g h t i n g
2010-04-21 12:01:32 +00:00
* @ param string first three characters of database system version
2009-12-17 14:53:26 +00:00
* /
2011-02-15 17:11:24 +00:00
function bodyLoad ( version ) {
2011-03-24 07:24:57 +00:00
if ( history . state !== undefined ) { // copied from editor/static/editing.js
onpopstate ( history ) ;
}
2009-12-17 13:22:28 +00:00
var jushRoot = '../externals/jush/' ;
2009-07-29 15:07:57 +00:00
var script = document . createElement ( 'script' ) ;
2009-12-17 13:22:28 +00:00
script . src = jushRoot + 'jush.js' ;
2009-07-29 15:07:57 +00:00
script . onload = function ( ) {
2009-09-11 20:08:19 +00:00
if ( window . jush ) { // IE runs in case of an error too
2011-01-31 19:35:48 +00:00
jush . create _links = ' target="_blank" rel="noreferrer"' ;
2011-03-16 12:14:40 +00:00
jush . urls . sql _sqlset = jush . urls . sql [ 0 ] = jush . urls . sqlset [ 0 ] = jush . urls . sqlstatus [ 0 ] = 'http://dev.mysql.com/doc/refman/' + version + '/en/$key' ;
var pgsql = 'http://www.postgresql.org/docs/' + version + '/static/' ;
jush . urls . pgsql _pgsqlset = jush . urls . pgsql [ 0 ] = pgsql + '$key' ;
jush . urls . pgsqlset [ 0 ] = pgsql + 'runtime-config-$key.html#GUC-$1' ;
2009-12-17 13:22:28 +00:00
jush . style ( jushRoot + 'jush.css' ) ;
2010-05-02 21:37:05 +00:00
if ( window . jushLinks ) {
jush . custom _links = jushLinks ;
}
2010-11-12 16:17:46 +00:00
jush . highlight _tag ( 'code' , 0 ) ;
2009-09-11 19:45:56 +00:00
}
2009-09-11 20:08:19 +00:00
} ;
2009-07-29 15:07:57 +00:00
script . onreadystatechange = function ( ) {
2009-09-11 19:45:56 +00:00
if ( /^(loaded|complete)$/ . test ( script . readyState ) ) {
2009-07-29 15:07:57 +00:00
script . onload ( ) ;
}
2009-09-11 20:08:19 +00:00
} ;
2009-07-29 15:07:57 +00:00
document . body . appendChild ( script ) ;
2009-07-11 19:45:57 +00:00
}
2009-12-17 14:53:26 +00:00
/ * * G e t v a l u e o f d y n a m i c a l l y c r e a t e d f o r m f i e l d
* @ param HTMLFormElement
* @ param string
* @ return HTMLElement
* /
2009-12-17 13:22:28 +00:00
function formField ( form , name ) {
2010-10-19 11:28:43 +00:00
// required in IE < 8, form.elements[name] doesn't work
2009-09-11 17:08:18 +00:00
for ( var i = 0 ; i < form . length ; i ++ ) {
if ( form [ i ] . name == name ) {
return form [ i ] ;
}
}
}
2009-12-17 14:53:26 +00:00
/ * * T r y t o c h a n g e i n p u t t y p e t o p a s s w o r d o r t o t e x t
* @ param HTMLInputElement
* @ param boolean
* /
2009-12-17 13:22:28 +00:00
function typePassword ( el , disable ) {
2009-09-11 17:08:18 +00:00
try {
el . type = ( disable ? 'text' : 'password' ) ;
} catch ( e ) {
}
}
2011-01-31 13:48:40 +00:00
function loginDriver ( driver ) {
var trs = driver . parentNode . parentNode . parentNode . rows ;
for ( var i = 1 ; i < trs . length ; i ++ ) {
trs [ i ] . className = ( /sqlite/ . test ( driver . value ) ? 'hidden' : '' ) ;
}
}
2009-09-11 17:08:18 +00:00
2009-12-17 13:22:28 +00:00
var added = '.' , rowCount ;
2009-07-03 12:38:21 +00:00
2011-03-14 13:17:28 +00:00
/ * * C h e c k i f v a l i s e q u a l t o a - d e l i m i t e r - b w h e r e d e l i m i t e r i s ' _ ' , ' ' o r b i g l e t t e r
2009-12-17 14:53:26 +00:00
* @ param string
2011-03-14 13:17:28 +00:00
* @ param string
* @ param string
* @ return boolean
2009-12-17 14:53:26 +00:00
* /
2011-03-14 13:17:28 +00:00
function delimiterEqual ( val , a , b ) {
return ( val == a + '_' + b || val == a + b || val == a + b . charAt ( 0 ) . toUpperCase ( ) + b . substr ( 1 ) ) ;
2009-07-03 12:38:21 +00:00
}
2009-12-17 14:53:26 +00:00
/ * * E s c a p e s t r i n g t o u s e a s i d e n t i f i e r
* @ param string
* @ return string
* /
2009-12-17 13:22:28 +00:00
function idfEscape ( s ) {
2010-05-20 20:06:34 +00:00
return s . replace ( /`/ , '``' ) ;
2009-07-03 12:38:21 +00:00
}
2009-12-17 14:53:26 +00:00
/ * * D e t e c t f o r e i g n k e y
* @ param HTMLInputElement
* /
2009-12-17 13:22:28 +00:00
function editingNameChange ( field ) {
2009-07-03 12:38:21 +00:00
var name = field . name . substr ( 0 , field . name . length - 7 ) ;
2009-12-17 13:22:28 +00:00
var type = formField ( field . form , name + '[type]' ) ;
2009-07-03 12:38:21 +00:00
var opts = type . options ;
var candidate ; // don't select anything with ambiguous match (like column `id`)
2011-03-14 13:17:28 +00:00
var val = field . value ;
2009-07-03 12:38:21 +00:00
for ( var i = opts . length ; i -- ; ) {
2011-03-14 13:17:28 +00:00
var match = /(.+)`(.+)/ . exec ( opts [ i ] . value ) ;
if ( ! match ) { // common type
if ( candidate && i == opts . length - 2 && val == opts [ candidate ] . value . replace ( /.+`/ , '' ) && name == 'fields[1]' ) { // single target table, link to column, first field - probably `id`
return ;
2009-07-03 12:38:21 +00:00
}
break ;
}
2011-03-14 13:17:28 +00:00
var table = match [ 1 ] ;
var column = match [ 2 ] ;
var tables = [ table , table . replace ( /s$/ , '' ) , table . replace ( /es$/ , '' ) ] ;
for ( var j = 0 ; j < tables . length ; j ++ ) {
table = tables [ j ] ;
if ( val == column || val == table || delimiterEqual ( val , table , column ) || delimiterEqual ( val , column , table ) ) {
if ( candidate ) {
return ;
}
candidate = i ;
break ;
2009-07-03 12:38:21 +00:00
}
}
}
if ( candidate ) {
2009-09-11 17:08:18 +00:00
type . selectedIndex = candidate ;
2009-07-03 12:38:21 +00:00
type . onchange ( ) ;
}
}
2009-12-17 14:53:26 +00:00
/ * * A d d t a b l e r o w f o r n e x t f i e l d
* @ param HTMLInputElement
* @ param boolean
* @ param boolean
* @ return boolean
* /
2009-12-17 13:22:28 +00:00
function editingAddRow ( button , allowed , focus ) {
if ( allowed && rowCount >= allowed ) {
2009-07-03 12:38:21 +00:00
return false ;
}
2010-10-22 22:02:24 +00:00
var match = /(\d+)(\.\d+)?/ . exec ( button . name ) ;
2009-07-03 12:38:21 +00:00
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' ) ;
2009-09-25 09:52:24 +00:00
var input = tags2 [ 0 ] ; // IE loose tags2 after insertBefore()
2009-07-03 12:38:21 +00:00
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 ) ;
2009-09-10 11:45:14 +00:00
if ( /\[(orig|field|comment|default)/ . test ( tags [ i ] . name ) ) {
2009-07-03 12:38:21 +00:00
tags2 [ i ] . value = '' ;
}
2009-09-10 11:45:14 +00:00
if ( /\[(has_default)/ . test ( tags [ i ] . name ) ) {
tags2 [ i ] . checked = false ;
}
2009-07-03 12:38:21 +00:00
}
tags [ 0 ] . onchange = function ( ) {
2009-12-17 13:22:28 +00:00
editingNameChange ( tags [ 0 ] ) ;
2009-07-03 12:38:21 +00:00
} ;
row . parentNode . insertBefore ( row2 , row . nextSibling ) ;
2009-09-25 09:52:24 +00:00
if ( focus ) {
input . onchange = function ( ) {
2009-12-17 13:22:28 +00:00
editingNameChange ( input ) ;
2009-09-25 09:52:24 +00:00
} ;
input . focus ( ) ;
}
2009-07-03 12:38:21 +00:00
added += '0' ;
2009-12-17 13:22:28 +00:00
rowCount ++ ;
2009-09-25 09:52:24 +00:00
return true ;
2009-07-03 12:38:21 +00:00
}
2009-12-17 14:53:26 +00:00
/ * * R e m o v e t a b l e r o w f o r f i e l d
* @ param HTMLInputElement
* @ return boolean
* /
2009-12-17 13:22:28 +00:00
function editingRemoveRow ( button ) {
var field = formField ( button . form , button . name . replace ( /drop_col(.+)/ , 'fields$1[field]' ) ) ;
2009-07-03 12:38:21 +00:00
field . parentNode . removeChild ( field ) ;
button . parentNode . parentNode . style . display = 'none' ;
return true ;
}
2009-12-17 13:22:28 +00:00
var lastType = '' ;
2009-12-17 14:53:26 +00:00
/ * * C l e a r l e n g t h a n d h i d e c o l l a t i o n o r u n s i g n e d
* @ param HTMLSelectElement
* /
2009-12-17 13:22:28 +00:00
function editingTypeChange ( type ) {
2009-07-03 12:38:21 +00:00
var name = type . name . substr ( 0 , type . name . length - 6 ) ;
2009-12-17 13:22:28 +00:00
var text = selectValue ( type ) ;
2009-07-03 12:38:21 +00:00
for ( var i = 0 ; i < type . form . elements . length ; i ++ ) {
var el = type . form . elements [ i ] ;
2009-12-17 12:54:17 +00:00
if ( el . name == name + '[length]' && ! (
2009-12-17 13:22:28 +00:00
( /(char|binary)$/ . test ( lastType ) && /(char|binary)$/ . test ( text ) )
|| ( /(enum|set)$/ . test ( lastType ) && /(enum|set)$/ . test ( text ) )
2009-12-17 12:54:17 +00:00
) ) {
el . value = '' ;
}
2010-04-02 13:44:23 +00:00
if ( lastType == 'timestamp' && el . name == name + '[has_default]' && /timestamp/i . test ( formField ( type . form , name + '[default]' ) . value ) ) {
el . checked = false ;
}
2009-07-03 12:38:21 +00:00
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' ) ;
}
2010-05-20 20:06:34 +00:00
if ( el . name == name + '[on_delete]' ) {
el . className = ( /`/ . test ( text ) ? '' : 'hidden' ) ;
}
2009-07-03 12:38:21 +00:00
}
}
2009-12-17 14:53:26 +00:00
/ * * E d i t e n u m o r s e t
* @ param HTMLInputElement
* /
2009-12-17 13:22:28 +00:00
function editingLengthFocus ( field ) {
2009-10-19 05:11:45 +00:00
var td = field . parentNode ;
2009-12-17 13:30:23 +00:00
if ( /(enum|set)$/ . test ( selectValue ( td . previousSibling . firstChild ) ) ) {
2009-10-19 05:11:45 +00:00
var edit = document . getElementById ( 'enum-edit' ) ;
var val = field . value ;
edit . value = ( /^'.+','.+'$/ . test ( val ) ? val . substr ( 1 , val . length - 2 ) . replace ( /','/g , "\n" ) . replace ( /''/g , "'" ) : val ) ;
td . appendChild ( edit ) ;
field . style . display = 'none' ;
edit . style . display = 'inline' ;
edit . focus ( ) ;
}
}
2009-12-17 14:53:26 +00:00
/ * * F i n i s h e d i t i n g o f e n u m o r s e t
* @ param HTMLTextAreaElement
* /
2009-12-17 13:22:28 +00:00
function editingLengthBlur ( edit ) {
2009-10-19 05:11:45 +00:00
var field = edit . parentNode . firstChild ;
var val = edit . value ;
field . value = ( /\n/ . test ( val ) ? "'" + val . replace ( /\n+$/ , '' ) . replace ( /'/g , "''" ) . replace ( /\n/g , "','" ) + "'" : val ) ;
field . style . display = 'inline' ;
edit . style . display = 'none' ;
}
2009-12-17 14:53:26 +00:00
/ * * S h o w o r h i d e s e l e c t e d t a b l e c o l u m n
* @ param boolean
* @ param number
* /
2009-12-17 13:22:28 +00:00
function columnShow ( checked , column ) {
2009-07-03 12:38:21 +00:00
var trs = document . getElementById ( 'edit-fields' ) . getElementsByTagName ( 'tr' ) ;
for ( var i = 0 ; i < trs . length ; i ++ ) {
2010-01-28 15:17:20 +00:00
trs [ i ] . getElementsByTagName ( 'td' ) [ column ] . className = ( checked ? '' : 'hidden' ) ;
2009-07-03 12:38:21 +00:00
}
}
2009-12-17 14:53:26 +00:00
/ * * D i s p l a y p a r t i t i o n o p t i o n s
* @ param HTMLSelectElement
* /
2009-12-17 13:22:28 +00:00
function partitionByChange ( el ) {
var partitionTable = /RANGE|LIST/ . test ( selectValue ( el ) ) ;
el . form [ 'partitions' ] . className = ( partitionTable || ! el . selectedIndex ? 'hidden' : '' ) ;
document . getElementById ( 'partition-table' ) . className = ( partitionTable ? '' : 'hidden' ) ;
2009-07-03 12:38:21 +00:00
}
2009-12-17 14:53:26 +00:00
/ * * A d d n e x t p a r t i t i o n r o w
* @ param HTMLInputElement
* /
2009-12-17 13:22:28 +00:00
function partitionNameChange ( el ) {
2009-07-03 12:38:21 +00:00
var row = el . parentNode . parentNode . cloneNode ( true ) ;
row . firstChild . firstChild . value = '' ;
el . parentNode . parentNode . parentNode . appendChild ( row ) ;
el . onchange = function ( ) { } ;
}
2009-12-17 14:53:26 +00:00
/ * * A d d r o w f o r f o r e i g n k e y
* @ param HTMLSelectElement
* /
2009-12-17 13:22:28 +00:00
function foreignAddRow ( field ) {
2010-10-18 23:06:30 +00:00
field . onchange = function ( ) { } ;
2009-07-03 12:38:21 +00:00
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 ) ;
}
2009-12-17 14:53:26 +00:00
/ * * A d d r o w f o r i n d e x e s
* @ param HTMLSelectElement
* /
2009-12-17 13:22:28 +00:00
function indexesAddRow ( field ) {
2010-10-18 23:06:30 +00:00
field . onchange = function ( ) { } ;
2009-07-03 12:38:21 +00:00
var row = field . parentNode . parentNode . cloneNode ( true ) ;
var spans = row . getElementsByTagName ( 'span' ) ;
2009-09-11 17:08:18 +00:00
for ( var i = 0 ; i < spans . length - 1 ; i ++ ) {
row . removeChild ( spans [ i ] ) ;
}
2009-07-03 12:38:21 +00:00
var selects = row . getElementsByTagName ( 'select' ) ;
for ( var i = 0 ; i < selects . length ; i ++ ) {
2010-10-22 22:02:24 +00:00
selects [ i ] . name = selects [ i ] . name . replace ( /indexes\[\d+/ , '$&1' ) ;
2009-07-03 12:38:21 +00:00
selects [ i ] . selectedIndex = 0 ;
}
var input = row . getElementsByTagName ( 'input' ) [ 0 ] ;
2010-10-22 22:02:24 +00:00
input . name = input . name . replace ( /indexes\[\d+/ , '$&1' ) ;
2009-07-03 12:38:21 +00:00
input . value = '' ;
field . parentNode . parentNode . parentNode . appendChild ( row ) ;
}
2009-12-17 14:53:26 +00:00
/ * * A d d c o l u m n f o r i n d e x
* @ param HTMLSelectElement
* /
2009-12-17 13:22:28 +00:00
function indexesAddColumn ( field ) {
2010-10-18 23:06:30 +00:00
field . onchange = function ( ) { } ;
2009-07-03 12:38:21 +00:00
var column = field . parentNode . cloneNode ( true ) ;
var select = column . getElementsByTagName ( 'select' ) [ 0 ] ;
2010-10-22 22:02:24 +00:00
select . name = select . name . replace ( /\]\[\d+/ , '$&1' ) ;
2009-07-03 12:38:21 +00:00
select . selectedIndex = 0 ;
var input = column . getElementsByTagName ( 'input' ) [ 0 ] ;
2010-10-22 22:02:24 +00:00
input . name = input . name . replace ( /\]\[\d+/ , '$&1' ) ;
2009-07-03 12:38:21 +00:00
input . value = '' ;
field . parentNode . parentNode . appendChild ( column ) ;
2010-12-10 00:16:01 +00:00
select = field . form [ field . name . replace ( /\].*/ , '][type]' ) ] ;
if ( ! select . selectedIndex ) {
select . selectedIndex = 3 ;
}
2009-07-03 12:38:21 +00:00
}
2009-12-17 13:22:28 +00:00
var that , x , y , em , tablePos ;
2009-07-03 12:38:21 +00:00
2009-12-17 14:53:26 +00:00
/ * * G e t m o u s e p o s i t i o n
* @ param HTMLElement
* @ param MouseEvent
* /
2009-12-17 13:22:28 +00:00
function schemaMousedown ( el , event ) {
2009-07-03 12:38:21 +00:00
that = el ;
x = event . clientX - el . offsetLeft ;
y = event . clientY - el . offsetTop ;
}
2009-12-17 14:53:26 +00:00
/ * * M o v e o b j e c t
* @ param MouseEvent
* /
2009-12-17 13:22:28 +00:00
function schemaMousemove ( ev ) {
2009-07-03 12:38:21 +00:00
if ( that !== undefined ) {
ev = ev || event ;
var left = ( ev . clientX - x ) / em ;
var top = ( ev . clientY - y ) / em ;
var divs = that . getElementsByTagName ( 'div' ) ;
2009-12-17 13:22:28 +00:00
var lineSet = { } ;
2009-07-03 12:38:21 +00:00
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 ) ) ;
2009-12-17 13:22:28 +00:00
var ref = ( tablePos [ divs [ i ] . title ] ? tablePos [ divs [ i ] . title ] : [ div2 . parentNode . offsetTop / em , 0 ] ) ;
2009-07-03 12:38:21 +00:00
var left1 = - 1 ;
2009-12-17 13:22:28 +00:00
var isTop = true ;
2009-07-03 12:38:21 +00:00
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' ;
2009-12-17 13:22:28 +00:00
isTop = ( div2 . offsetTop + ref [ 0 ] * em > divs [ i ] . offsetTop + top * em ) ;
2009-07-03 12:38:21 +00:00
}
2009-12-17 13:22:28 +00:00
if ( ! lineSet [ id ] ) {
2010-10-22 22:02:24 +00:00
var line = document . getElementById ( divs [ i ] . id . replace ( /^....(.+)-\d+$/ , 'refl$1' ) ) ;
2009-07-03 12:38:21 +00:00
var shift = ev . clientY - y - that . offsetTop ;
line . style . left = ( left + left1 ) + 'em' ;
2009-12-17 13:22:28 +00:00
if ( isTop ) {
2009-07-03 12:38:21 +00:00
line . style . top = ( line . offsetTop + shift ) / em + 'em' ;
}
if ( divs [ i ] . parentNode != div2 . parentNode ) {
line = line . getElementsByTagName ( 'div' ) [ 0 ] ;
2009-12-17 13:22:28 +00:00
line . style . height = ( line . offsetHeight + ( isTop ? - 1 : 1 ) * shift ) / em + 'em' ;
2009-07-03 12:38:21 +00:00
}
2009-12-17 13:22:28 +00:00
lineSet [ id ] = true ;
2009-07-03 12:38:21 +00:00
}
}
}
that . style . left = left + 'em' ;
that . style . top = top + 'em' ;
}
}
2009-12-17 14:53:26 +00:00
/ * * F i n i s h m o v e
* @ param MouseEvent
2011-02-08 19:58:31 +00:00
* @ param string
2009-12-17 14:53:26 +00:00
* /
2011-02-08 19:58:31 +00:00
function schemaMouseup ( ev , db ) {
2009-07-03 12:38:21 +00:00
if ( that !== undefined ) {
ev = ev || event ;
2009-12-17 13:22:28 +00:00
tablePos [ that . firstChild . firstChild . firstChild . data ] = [ ( ev . clientY - y ) / em , ( ev . clientX - x ) / em ] ;
2009-07-03 12:38:21 +00:00
that = undefined ;
var s = '' ;
2009-12-17 13:22:28 +00:00
for ( var key in tablePos ) {
s += '_' + key + ':' + Math . round ( tablePos [ key ] [ 0 ] * 10000 ) / 10000 + 'x' + Math . round ( tablePos [ key ] [ 1 ] * 10000 ) / 10000 ;
2009-07-03 12:38:21 +00:00
}
2010-12-29 17:08:34 +00:00
s = encodeURIComponent ( s . substr ( 1 ) ) ;
var link = document . getElementById ( 'schema-link' ) ;
link . href = link . href . replace ( /[^=]+$/ , '' ) + s ;
2011-02-08 19:58:31 +00:00
cookie ( 'adminer_schema-' + db + '=' + s , 30 ) ; //! special chars in db
2009-07-03 12:38:21 +00:00
}
}