2006-05-30 19:24:57 +00:00
MetadataPane = new function ( )
{
var _dynamicFields ;
2006-06-07 16:19:56 +00:00
var _creatorTypeMenu ;
var _beforeRow ;
var _creatorCount ;
2006-06-02 23:05:24 +00:00
2006-05-30 19:24:57 +00:00
var _itemBeingEdited ;
2006-06-02 23:05:24 +00:00
this . onLoad = onLoad ;
2006-05-30 19:24:57 +00:00
this . viewItem = viewItem ;
2006-06-07 16:19:56 +00:00
this . addCreatorRow = addCreatorRow ;
this . modifyCreator = modifyCreator ;
this . removeCreator = removeCreator ;
2006-06-06 19:53:27 +00:00
this . showEditor = showEditor ;
this . hideEditor = hideEditor ;
2006-05-30 19:24:57 +00:00
2006-06-02 23:05:24 +00:00
function onLoad ( )
2006-05-30 19:24:57 +00:00
{
_dynamicFields = document . getElementById ( 'editpane-dynamic-fields' ) ;
2006-06-07 16:19:56 +00:00
_creatorTypeMenu = document . getElementById ( 'creatorTypeMenu' ) ;
var creatorTypes = Scholar . CreatorTypes . getTypes ( ) ;
for ( var i = 0 ; i < creatorTypes . length ; i ++ )
{
var menuitem = document . createElement ( "menuitem" ) ;
menuitem . setAttribute ( "label" , Scholar . getString ( 'creatorTypes.' + creatorTypes [ i ] [ 'name' ] ) ) ;
menuitem . setAttribute ( "typeid" , creatorTypes [ i ] [ 'id' ] ) ;
if ( creatorTypes [ i ] [ 'id' ] == 0 )
menuitem . setAttribute ( "selected" , true ) ;
_creatorTypeMenu . appendChild ( menuitem ) ;
}
2006-05-30 19:24:57 +00:00
return true ;
}
2006-06-02 23:05:24 +00:00
/ *
2006-06-05 13:56:59 +00:00
* Loads an item
2006-06-02 23:05:24 +00:00
* /
2006-05-30 19:24:57 +00:00
function viewItem ( thisItem )
2006-06-06 19:53:27 +00:00
{
2006-06-05 13:56:59 +00:00
_itemBeingEdited = thisItem ;
reloadFields ( ) ;
}
function reloadFields ( )
{
2006-06-07 16:19:56 +00:00
while ( _dynamicFields . hasChildNodes ( ) )
_dynamicFields . removeChild ( _dynamicFields . firstChild ) ;
2006-06-02 23:05:24 +00:00
2006-06-07 16:19:56 +00:00
var fieldNames = new Array ( "title" , "dateAdded" , "dateModified" ) ;
var fields = Scholar . ItemFields . getItemTypeFields ( _itemBeingEdited . getField ( "itemTypeID" ) ) ;
for ( var i = 0 ; i < fields . length ; i ++ )
fieldNames . push ( Scholar . ItemFields . getName ( fields [ i ] ) ) ;
2006-06-02 23:05:24 +00:00
2006-05-30 19:24:57 +00:00
for ( var i = 0 ; i < fieldNames . length ; i ++ )
{
2006-06-07 16:19:56 +00:00
var editable = ( ! _itemBeingEdited . isPrimaryField ( fieldNames [ i ] ) || _itemBeingEdited . isEditableField ( fieldNames [ i ] ) ) ;
2006-06-06 19:53:27 +00:00
var label = document . createElement ( "label" ) ;
label . setAttribute ( "value" , Scholar . getString ( "itemFields." + fieldNames [ i ] ) + ":" ) ;
2006-06-07 16:19:56 +00:00
addDynamicRow ( label , createValueElement ( _itemBeingEdited . getField ( fieldNames [ i ] ) , editable ? fieldNames [ i ] : null ) ) ;
2006-05-30 19:24:57 +00:00
}
2006-06-05 13:56:59 +00:00
2006-06-07 16:19:56 +00:00
_beforeRow = _dynamicFields . firstChild . nextSibling ;
_creatorCount = 0 ;
if ( _itemBeingEdited . numCreators ( ) > 0 )
2006-06-02 23:05:24 +00:00
{
2006-06-07 16:19:56 +00:00
for ( var i = 0 , len = _itemBeingEdited . numCreators ( ) ; i < len ; i ++ )
2006-06-02 23:05:24 +00:00
{
2006-06-07 16:19:56 +00:00
var creator = _itemBeingEdited . getCreator ( i ) ;
addCreatorRow ( creator [ 'firstName' ] , creator [ 'lastName' ] , creator [ 'creatorTypeID' ] ) ;
2006-06-02 23:05:24 +00:00
}
}
2006-06-07 16:19:56 +00:00
else
2006-05-30 19:24:57 +00:00
{
2006-06-07 16:19:56 +00:00
addCreatorRow ( '' , '' , 1 ) ;
2006-05-30 19:24:57 +00:00
}
}
2006-06-06 19:53:27 +00:00
function addDynamicRow ( label , value , beforeElement )
{
2006-06-02 23:05:24 +00:00
var row = document . createElement ( "row" ) ;
row . appendChild ( label ) ;
2006-06-06 19:53:27 +00:00
row . appendChild ( value ) ;
if ( beforeElement )
2006-06-07 16:19:56 +00:00
_dynamicFields . insertBefore ( row , _beforeRow ) ;
2006-06-06 19:53:27 +00:00
else
_dynamicFields . appendChild ( row ) ;
2006-06-02 23:05:24 +00:00
}
2006-06-07 16:19:56 +00:00
function addCreatorRow ( firstName , lastName , typeID )
{
if ( ! firstName )
firstName = "(first)" ;
if ( ! lastName )
lastName = "(last)" ;
var label = document . createElement ( "label" ) ;
label . setAttribute ( "value" , Scholar . getString ( 'creatorTypes.' + Scholar . CreatorTypes . getTypeName ( typeID ) ) + ":" ) ;
label . setAttribute ( "popup" , "creatorTypeMenu" ) ;
label . setAttribute ( "fieldname" , 'creator-' + _creatorCount + '-typeID' ) ;
var row = document . createElement ( "hbox" ) ;
var firstlast = document . createElement ( "hbox" ) ;
firstlast . setAttribute ( "flex" , "1" ) ;
firstlast . appendChild ( createValueElement ( lastName + "," , 'creator-' + _creatorCount + '-lastName' ) ) ;
firstlast . appendChild ( createValueElement ( firstName , 'creator-' + _creatorCount + '-firstName' ) ) ;
row . appendChild ( firstlast ) ;
var removeButton = document . createElement ( 'toolbarbutton' ) ;
removeButton . setAttribute ( "label" , "-" ) ;
removeButton . setAttribute ( "oncommand" , "MetadataPane.removeCreator(" + _creatorCount + ")" ) ;
row . appendChild ( removeButton ) ;
var addButton = document . createElement ( 'toolbarbutton' ) ;
addButton . setAttribute ( "label" , "+" ) ;
addButton . setAttribute ( "oncommand" , "MetadataPane.addCreatorRow('','',1);" ) ;
row . appendChild ( addButton ) ;
_creatorCount ++ ;
addDynamicRow ( label , row , true ) ;
}
2006-06-06 19:53:27 +00:00
function createValueElement ( valueText , fieldName )
{
var valueElement = document . createElement ( "label" ) ;
valueElement . appendChild ( document . createTextNode ( valueText ) ) ;
if ( fieldName )
{
valueElement . setAttribute ( 'fieldname' , fieldName ) ;
valueElement . setAttribute ( 'onclick' , 'MetadataPane.showEditor(this);' ) ;
}
return valueElement ;
}
2006-06-07 16:19:56 +00:00
function removeCreator ( index )
2006-05-30 19:24:57 +00:00
{
2006-06-07 16:19:56 +00:00
_itemBeingEdited . removeCreator ( index ) ;
_itemBeingEdited . save ( ) ;
reloadFields ( ) ;
2006-05-30 19:24:57 +00:00
}
2006-06-07 16:19:56 +00:00
function modifyCreator ( index , field , value )
2006-05-30 19:24:57 +00:00
{
2006-06-07 16:19:56 +00:00
var creator = _itemBeingEdited . getCreator ( index ) ;
var firstName = creator [ 'firstName' ] ;
var lastName = creator [ 'lastName' ] ;
var typeID = creator [ 'typeID' ] ;
if ( field == 'firstName' )
firstName = value ;
else if ( field == 'lastName' )
lastName = value ;
else if ( field == 'typeID' )
typeID = value ;
_itemBeingEdited . setCreator ( index , firstName , lastName , typeID ) ;
_itemBeingEdited . save ( ) ;
}
2006-06-06 19:53:27 +00:00
function showEditor ( elem )
{
2006-06-07 16:19:56 +00:00
var fieldName = elem . getAttribute ( 'fieldname' ) ;
var value = '' ;
var creatorFields = fieldName . split ( '-' ) ;
if ( creatorFields [ 0 ] == 'creator' )
{
var c = _itemBeingEdited . getCreator ( creatorFields [ 1 ] ) ;
if ( c )
value = c [ creatorFields [ 2 ] ] ;
}
else
{
value = _itemBeingEdited . getField ( fieldName ) ;
}
2006-06-06 19:53:27 +00:00
var t = document . createElement ( "textbox" ) ;
2006-06-07 16:19:56 +00:00
t . setAttribute ( 'value' , value ) ;
t . setAttribute ( 'fieldname' , fieldName ) ;
t . setAttribute ( 'flex' , '1' ) ;
2006-06-06 19:53:27 +00:00
var box = elem . parentNode ;
2006-06-07 16:19:56 +00:00
box . replaceChild ( t , elem ) ;
2006-06-06 19:53:27 +00:00
t . select ( ) ;
t . setAttribute ( 'onblur' , "MetadataPane.hideEditor(this);" ) ;
t . setAttribute ( 'onkeypress' , 'if(event.keyCode == event.DOM_VK_RETURN) document.commandDispatcher.focusedElement.blur()' ) ; //for some reason I can't just say this.blur();
}
function hideEditor ( t )
{
var textbox = t . parentNode . parentNode ;
var fieldName = textbox . getAttribute ( 'fieldname' ) ;
var value = t . value ;
2006-06-07 16:19:56 +00:00
var elem ;
var creatorFields = fieldName . split ( '-' ) ;
if ( creatorFields [ 0 ] == 'creator' )
{
modifyCreator ( creatorFields [ 1 ] , creatorFields [ 2 ] , value ) ;
elem = createValueElement ( value , fieldName ) ;
}
else
{
_itemBeingEdited . setField ( fieldName , value ) ;
_itemBeingEdited . save ( ) ;
2006-06-06 19:53:27 +00:00
2006-06-07 16:19:56 +00:00
elem = createValueElement ( _itemBeingEdited . getField ( fieldName ) , fieldName ) ;
}
2006-06-06 19:53:27 +00:00
var box = textbox . parentNode ;
2006-06-07 16:19:56 +00:00
box . replaceChild ( elem , textbox ) ;
2006-06-06 19:53:27 +00:00
2006-05-30 19:24:57 +00:00
}
}
2006-06-02 23:05:24 +00:00
addEventListener ( "load" , function ( e ) { MetadataPane . onLoad ( e ) ; } , false ) ;