2006-06-09 15:52:40 +00:00
ScholarItemPane = new function ( )
2006-05-30 19:24:57 +00:00
{
var _dynamicFields ;
2006-06-07 16:19:56 +00:00
var _creatorTypeMenu ;
var _beforeRow ;
2006-06-26 12:58:22 +00:00
var _notesList ;
2006-07-20 23:19:31 +00:00
var _linksBox ;
2006-06-15 22:35:48 +00:00
var _notesLabel ;
2006-06-07 16:19:56 +00:00
var _creatorCount ;
2006-06-02 23:05:24 +00:00
2006-07-26 14:03:54 +00:00
var _loaded ;
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-07-26 14:03:54 +00:00
this . loadPane = loadPane ;
2006-06-19 15:45:32 +00:00
this . changeTypeTo = changeTypeTo ;
2006-06-07 16:19:56 +00:00
this . addCreatorRow = addCreatorRow ;
this . removeCreator = removeCreator ;
2006-06-06 19:53:27 +00:00
this . showEditor = showEditor ;
this . hideEditor = hideEditor ;
2006-06-09 15:52:40 +00:00
this . modifyField = modifyField ;
this . modifyCreator = modifyCreator ;
2006-06-26 13:54:05 +00:00
this . removeNote = removeNote ;
2006-06-15 22:35:48 +00:00
this . addNote = addNote ;
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
{
2006-07-26 14:03:54 +00:00
_tabs = document . getElementById ( 'scholar-view-tabs' ) ;
2006-05-30 19:24:57 +00:00
_dynamicFields = document . getElementById ( 'editpane-dynamic-fields' ) ;
2006-06-19 15:45:32 +00:00
_itemTypeMenu = document . getElementById ( 'editpane-type-menu' ) ;
2006-06-07 16:19:56 +00:00
_creatorTypeMenu = document . getElementById ( 'creatorTypeMenu' ) ;
2006-06-26 12:58:22 +00:00
_notesList = document . getElementById ( 'editpane-dynamic-notes' ) ;
_notesLabel = document . getElementById ( 'editpane-notes-label' ) ;
2006-07-26 14:03:54 +00:00
_tagsBox = document . getElementById ( 'editpane-tags' ) ;
_relatedBox = document . getElementById ( 'editpane-related' ) ;
2006-06-07 16:19:56 +00:00
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
2006-06-19 15:45:32 +00:00
var itemTypes = Scholar . ItemTypes . getTypes ( ) ;
for ( var i = 0 ; i < itemTypes . length ; i ++ )
2006-06-27 20:37:02 +00:00
if ( itemTypes [ i ] [ 'id' ] != 1 )
_itemTypeMenu . appendItem ( Scholar . getString ( "itemTypes." + itemTypes [ i ] [ 'name' ] ) , itemTypes [ i ] [ 'id' ] ) ;
2006-06-19 15:45:32 +00:00
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-07-17 14:09:12 +00:00
{
2006-06-05 13:56:59 +00:00
_itemBeingEdited = thisItem ;
2006-07-26 14:03:54 +00:00
_loaded = new Array ( 5 ) ;
loadPane ( _tabs . selectedIndex ) ;
2006-06-05 13:56:59 +00:00
}
2006-07-26 14:03:54 +00:00
function loadPane ( index )
2006-06-05 13:56:59 +00:00
{
2006-07-26 14:03:54 +00:00
if ( _loaded [ index ] )
return ;
_loaded [ index ] = true ;
if ( index == 0 )
{
Scholar . debug ( 'loading FIELDS' ) ;
while ( _dynamicFields . hasChildNodes ( ) )
_dynamicFields . removeChild ( _dynamicFields . firstChild ) ;
2006-06-02 23:05:24 +00:00
2006-07-26 14:03:54 +00:00
for ( var i = 0 , len = _itemTypeMenu . firstChild . childNodes . length ; i < len ; i ++ )
if ( _itemTypeMenu . firstChild . childNodes [ i ] . value == _itemBeingEdited . getType ( ) )
_itemTypeMenu . selectedIndex = i ;
2006-06-19 15:45:32 +00:00
2006-07-26 14:03:54 +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-07-26 14:03:54 +00:00
for ( var i = 0 ; i < fieldNames . length ; i ++ )
{
var editable = ( ! _itemBeingEdited . isPrimaryField ( fieldNames [ i ] ) || _itemBeingEdited . isEditableField ( fieldNames [ i ] ) ) ;
2006-06-15 22:35:48 +00:00
2006-07-26 14:03:54 +00:00
var valueElement = createValueElement ( _itemBeingEdited . getField ( fieldNames [ i ] ) , editable ? fieldNames [ i ] : null ) ;
2006-06-15 22:35:48 +00:00
2006-07-26 14:03:54 +00:00
var label = document . createElement ( "label" ) ;
label . setAttribute ( "value" , Scholar . getString ( "itemFields." + fieldNames [ i ] ) + ":" ) ;
label . setAttribute ( "onclick" , "this.nextSibling.blur();" ) ;
2006-06-15 22:35:48 +00:00
2006-07-26 14:03:54 +00:00
addDynamicRow ( label , valueElement ) ;
}
2006-06-05 13:56:59 +00:00
2006-07-26 14:03:54 +00:00
//CREATORS:
_beforeRow = _dynamicFields . firstChild . nextSibling ;
_creatorCount = 0 ;
if ( _itemBeingEdited . numCreators ( ) > 0 )
2006-06-02 23:05:24 +00:00
{
2006-07-26 14:03:54 +00:00
for ( var i = 0 , len = _itemBeingEdited . numCreators ( ) ; i < len ; i ++ )
{
var creator = _itemBeingEdited . getCreator ( i ) ;
addCreatorRow ( creator [ 'firstName' ] , creator [ 'lastName' ] , creator [ 'creatorTypeID' ] ) ;
}
}
else
{
addCreatorRow ( '' , '' , 1 ) ;
2006-06-02 23:05:24 +00:00
}
}
2006-07-26 14:03:54 +00:00
else if ( index == 1 )
2006-05-30 19:24:57 +00:00
{
2006-07-26 14:03:54 +00:00
Scholar . debug ( 'loading NOTES' ) ;
//NOTES:
while ( _notesList . hasChildNodes ( ) )
_notesList . removeChild ( _notesList . firstChild ) ;
2006-06-15 22:35:48 +00:00
2006-07-26 14:03:54 +00:00
var notes = Scholar . Items . get ( _itemBeingEdited . getNotes ( ) ) ;
if ( notes . length )
2006-06-26 12:58:22 +00:00
{
2006-07-26 14:03:54 +00:00
for ( var i = 0 ; i < notes . length ; i ++ )
{
var icon = document . createElement ( 'image' ) ;
icon . setAttribute ( 'src' , 'chrome://scholar/skin/treeitem-note.png' ) ;
2006-06-26 13:54:05 +00:00
2006-07-26 14:03:54 +00:00
var label = document . createElement ( 'label' ) ;
label . setAttribute ( 'value' , _noteToTitle ( notes [ i ] . getNote ( ) ) ) ;
label . setAttribute ( 'crop' , 'end' ) ;
2006-06-26 13:54:05 +00:00
2006-07-26 14:03:54 +00:00
var box = document . createElement ( 'box' ) ;
box . setAttribute ( 'onclick' , "ScholarPane.openNoteWindow(" + notes [ i ] . getID ( ) + ");" ) ;
box . setAttribute ( 'class' , 'clicky' ) ;
box . appendChild ( icon ) ;
box . appendChild ( label ) ;
2006-06-26 13:54:05 +00:00
2006-07-26 14:03:54 +00:00
var removeButton = document . createElement ( 'label' ) ;
removeButton . setAttribute ( "value" , "-" ) ;
removeButton . setAttribute ( "class" , "clicky" ) ;
removeButton . setAttribute ( "onclick" , "ScholarItemPane.removeNote(" + notes [ i ] . getID ( ) + ")" ) ;
2006-06-26 13:54:05 +00:00
2006-07-26 14:03:54 +00:00
var row = document . createElement ( 'row' ) ;
row . appendChild ( box ) ;
row . appendChild ( removeButton ) ;
2006-06-26 12:58:22 +00:00
2006-07-26 14:03:54 +00:00
_notesList . appendChild ( row ) ;
}
2006-06-26 12:58:22 +00:00
}
2006-07-05 13:09:58 +00:00
2006-07-26 14:03:54 +00:00
_updateNoteCount ( ) ;
}
else if ( index == 2 )
{
Scholar . debug ( 'loading FILES' ) ;
//FILES
}
else if ( index == 3 )
{
Scholar . debug ( 'loading TAGS' ) ;
//TAGS:
_tagsBox . item = _itemBeingEdited ;
}
else if ( index == 4 )
{
Scholar . debug ( 'loading RELATED' ) ;
//RELATED
_relatedBox . item = _itemBeingEdited ;
}
2006-05-30 19:24:57 +00:00
}
2006-06-19 15:45:32 +00:00
function changeTypeTo ( id )
{
2006-06-21 23:22:37 +00:00
if ( id != _itemBeingEdited . getType ( ) && confirm ( Scholar . getString ( 'pane.item.changeType' ) ) )
2006-06-19 15:45:32 +00:00
{
_itemBeingEdited . setType ( id ) ;
_itemBeingEdited . save ( ) ;
reloadFields ( ) ;
}
}
2006-06-06 19:53:27 +00:00
function addDynamicRow ( label , value , beforeElement )
2006-07-20 23:19:31 +00:00
{
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" ) ;
2006-06-25 04:35:11 +00:00
label . setAttribute ( "value" , Scholar . getString ( 'creatorTypes.' + Scholar . CreatorTypes . getName ( typeID ) ) + ":" ) ;
2006-06-07 16:19:56 +00:00
label . setAttribute ( "popup" , "creatorTypeMenu" ) ;
label . setAttribute ( "fieldname" , 'creator-' + _creatorCount + '-typeID' ) ;
2006-06-16 17:21:30 +00:00
label . className = 'clicky' ;
2006-06-07 16:19:56 +00:00
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 ) ;
2006-06-12 12:43:20 +00:00
var removeButton = document . createElement ( 'label' ) ;
removeButton . setAttribute ( "value" , "-" ) ;
2006-06-16 17:21:30 +00:00
removeButton . setAttribute ( "class" , "clicky" ) ;
2006-06-12 12:43:20 +00:00
removeButton . setAttribute ( "onclick" , "ScholarItemPane.removeCreator(" + _creatorCount + ")" ) ;
2006-06-07 16:19:56 +00:00
row . appendChild ( removeButton ) ;
2006-06-12 12:43:20 +00:00
var addButton = document . createElement ( 'label' ) ;
addButton . setAttribute ( "value" , "+" ) ;
2006-06-16 17:21:30 +00:00
addButton . setAttribute ( "class" , "clicky" ) ;
2006-06-12 12:43:20 +00:00
addButton . setAttribute ( "onclick" , "ScholarItemPane.addCreatorRow('','',1);" ) ;
2006-06-07 16:19:56 +00:00
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" ) ;
if ( fieldName )
{
valueElement . setAttribute ( 'fieldname' , fieldName ) ;
2006-06-09 15:52:40 +00:00
valueElement . setAttribute ( 'onclick' , 'ScholarItemPane.showEditor(this);' ) ;
2006-06-16 17:21:30 +00:00
valueElement . className = 'clicky' ;
2006-06-06 19:53:27 +00:00
}
2006-06-17 00:57:50 +00:00
var firstSpace ;
if ( typeof valueText == 'string' )
firstSpace = valueText . indexOf ( " " ) ;
2006-06-13 20:45:30 +00:00
if ( ( firstSpace == - 1 && valueText . length > 29 ) || firstSpace > 29 )
{
valueElement . setAttribute ( 'crop' , 'end' ) ;
valueElement . setAttribute ( 'value' , valueText ) ;
}
else
valueElement . appendChild ( document . createTextNode ( valueText ) ) ;
2006-06-06 19:53:27 +00:00
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-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-07-17 14:09:12 +00:00
t . className = 'fieldeditor' ;
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 ( ) ;
2006-06-09 15:52:40 +00:00
t . setAttribute ( 'onblur' , "ScholarItemPane.hideEditor(this, true);" ) ;
t . setAttribute ( 'onkeypress' , 'if(event.keyCode == event.DOM_VK_RETURN) document.commandDispatcher.focusedElement.blur(); else if(event.keyCode == event.DOM_VK_ESCAPE) ScholarItemPane.hideEditor(document.commandDispatcher.focusedElement, false);' ) ; //for some reason I can't just say this.blur();
2006-06-06 19:53:27 +00:00
}
2006-06-07 18:20:45 +00:00
function hideEditor ( t , saveChanges )
2006-06-06 19:53:27 +00:00
{
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' )
{
2006-06-07 18:20:45 +00:00
if ( saveChanges )
modifyCreator ( creatorFields [ 1 ] , creatorFields [ 2 ] , value ) ;
2006-06-07 16:19:56 +00:00
2006-06-07 18:20:45 +00:00
elem = createValueElement ( _itemBeingEdited . getCreator ( creatorFields [ 1 ] ) [ creatorFields [ 2 ] ] , fieldName ) ;
2006-06-07 16:19:56 +00:00
}
else
{
2006-06-07 18:20:45 +00:00
if ( saveChanges )
2006-06-09 15:52:40 +00:00
modifyField ( fieldName , value ) ;
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-09 15:52:40 +00:00
function modifyField ( field , value )
{
_itemBeingEdited . setField ( field , value ) ;
_itemBeingEdited . save ( ) ;
}
function modifyCreator ( index , field , value )
{
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-15 22:35:48 +00:00
2006-06-26 13:54:05 +00:00
function removeNote ( id )
2006-06-15 22:35:48 +00:00
{
2006-06-27 20:37:02 +00:00
var note = Scholar . Items . get ( id ) ;
if ( note )
if ( confirm ( Scholar . getString ( 'pane.item.notes.delete.confirm' ) ) )
note . erase ( ) ;
2006-06-15 22:35:48 +00:00
}
function addNote ( )
{
2006-06-27 22:47:17 +00:00
ScholarPane . openNoteWindow ( _itemBeingEdited . getID ( ) ) ;
2006-06-15 22:35:48 +00:00
}
function _noteToTitle ( text )
{
2006-06-16 14:39:18 +00:00
var MAX _LENGTH = 100 ;
var t = text . substring ( 0 , MAX _LENGTH ) ;
2006-06-16 07:44:55 +00:00
var ln = t . indexOf ( "\n" ) ;
2006-06-16 14:39:18 +00:00
if ( ln > - 1 && ln < MAX _LENGTH )
2006-06-16 07:44:55 +00:00
{
t = t . substring ( 0 , ln ) ;
}
2006-06-15 22:35:48 +00:00
if ( t == "" )
{
2006-06-21 23:22:37 +00:00
return Scholar . getString ( 'pane.item.notes.untitled' ) ;
2006-06-15 22:35:48 +00:00
}
else
{
return t ;
}
}
function _updateNoteCount ( )
{
2006-06-26 12:58:22 +00:00
var c = _notesList . childNodes . length ;
2006-06-15 22:35:48 +00:00
2006-06-21 23:22:37 +00:00
_notesLabel . value = Scholar . getString ( 'pane.item.notes.count.' + ( c != 1 ? "plural" : "singular" ) ) . replace ( '%1' , c ) + ":" ;
2006-06-15 22:35:48 +00:00
}
2006-05-30 19:24:57 +00:00
}
2006-06-09 15:52:40 +00:00
addEventListener ( "load" , function ( e ) { ScholarItemPane . onLoad ( e ) ; } , false ) ;