2007-07-19 12:40:05 +02:00
/ * *
* Configuration for the left hand tree
* /
if ( typeof SiteTreeHandlers == 'undefined' ) SiteTreeHandlers = { } ;
SiteTreeHandlers . parentChanged _url = 'admin/assets/ajaxupdateparent' ;
SiteTreeHandlers . orderChanged _url = 'admin/assets/ajaxupdatesort' ;
SiteTreeHandlers . loadPage _url = 'admin/assets/getitem' ;
SiteTreeHandlers . loadTree _url = 'admin/assets/getsubtree' ;
SiteTreeHandlers . showRecord _url = 'admin/assets/show/' ;
2007-09-04 06:12:30 +02:00
SiteTreeHandlers . controller _url = 'admin/assets' ;
2007-07-19 12:40:05 +02:00
var _HANDLER _FORMS = {
addpage : 'addpage_options' ,
2009-01-06 03:18:33 +01:00
deletepage : 'Form_DeleteItemsForm' ,
2007-07-19 12:40:05 +02:00
sortitems : 'sortitems_options'
} ;
2007-09-15 22:47:35 +02:00
MarkingPropertiesButton = Class . create ( ) ;
MarkingPropertiesButton . applyTo ( '#Form_EditForm_deletemarked' , "Please select some files to delete!" , 'deletemarked' , 'Do you really want to delete the marked files?' ) ;
2007-07-19 12:40:05 +02:00
2007-09-15 22:47:35 +02:00
MarkingPropertiesButton . prototype = {
initialize : function ( noneCheckedError , action , confirmMessage ) {
2007-07-19 12:40:05 +02:00
this . noneCheckedError = noneCheckedError ;
2007-09-15 22:47:35 +02:00
this . action = action ;
this . confirmMessage = confirmMessage ;
2007-07-19 12:40:05 +02:00
} ,
2007-09-15 22:47:35 +02:00
onclick : function ( ) {
2007-07-19 12:40:05 +02:00
var i , list = "" , checkboxes = $ ( 'Form_EditForm' ) . elements [ 'Files[]' ] ;
if ( ! checkboxes ) checkboxes = [ ] ;
if ( ! checkboxes . length ) checkboxes = [ checkboxes ] ;
for ( i = 0 ; i < checkboxes . length ; i ++ ) {
if ( checkboxes [ i ] . checked ) list += ( list ? ',' : '' ) + checkboxes [ i ] . value ;
}
2007-09-15 22:47:35 +02:00
if ( list == "" ) {
2007-07-19 12:40:05 +02:00
alert ( this . noneCheckedError ) ;
return false ;
} else {
2007-09-15 22:47:35 +02:00
$ ( 'Form_EditForm_FileIDs' ) . value = list ;
}
// If there is a confirmation message, show it before submitting
if ( '' != this . confirmMessage ) {
// Only submit if OK button is clicked
if ( confirm ( this . confirmMessage ) ) {
$ ( 'Form_EditForm' ) . save ( false , null , this . action ) ;
}
} else {
$ ( 'Form_EditForm' ) . save ( false , null , this . action ) ;
}
return false ;
}
}
// CheckBoxRange adapted from: http://jroller.com/page/rmcmahon?entry=checkboxrange_with_prototype
var CheckBoxRange = Class . create ( ) ;
CheckBoxRange . prototype = {
currentBox : null ,
form : null ,
field : null ,
initialize : function ( form , field ) {
this . form = form ;
this . field = field ;
this . eventPossibleCheckHappened = this . possibleCheckHappened . bindAsEventListener ( this ) ;
2009-02-03 04:22:20 +01:00
if ( form ) {
Event . observe ( form , "click" , this . eventPossibleCheckHappened ) ;
Event . observe ( form , "keyup" , this . eventPossibleCheckHappened ) ;
}
2007-09-15 22:47:35 +02:00
} ,
possibleCheckHappened : function ( event ) {
var target = Event . element ( event ) ;
if ( ( event . button == 0 || event . keyCode == 32 || event . keyCode == 17 ) &&
this . isCheckBox ( target ) && target . form == $ ( this . form ) && target . name == this . field ) {
// If ctrl or shift is keys are pressed
if ( ( event . shiftKey || event . ctrlKey ) && this . currentBox )
this . updateCheckBoxRange ( this . currentBox , target ) ;
this . currentBox = target ;
}
} ,
isCheckBox : function ( e ) {
return ( e . tagName . toLowerCase ( ) == "input" && e . type . toLowerCase ( ) == "checkbox" ) ;
} ,
updateCheckBoxRange : function ( start , end ) {
var last _clicked = end ;
var checkboxes = Form . getInputs ( this . form , 'checkbox' , this . field ) ;
var checkbox ;
var last ;
for ( var i = 0 ; ( checkbox = checkboxes [ i ] ) ; ++ i ) {
if ( checkbox == end ) {
last = start ;
break ;
}
if ( checkbox == start ) {
last = end ;
break ;
}
}
for ( ; ( checkbox = checkboxes [ i ] ) ; ++ i ) {
if ( checkbox != last _clicked && checkbox . checked != last _clicked . checked )
checkbox . click ( ) ;
if ( checkbox == last )
break ;
2007-07-19 12:40:05 +02:00
}
}
}
2007-09-15 22:47:35 +02:00
// SubsDraggable adapted from http://dev.rubyonrails.org/ticket/5771
// extentions for scriptaculous dragdrop.js
Object . extend ( Class , {
superrise : function ( obj , names ) {
names . each ( function ( n ) { obj [ 'super_' + n ] = obj [ n ] } )
return obj ;
}
} )
// Draggable that allows substitution of draggable element
var SubsDraggable = Class . create ( ) ;
SubsDraggable . prototype = Object . extend ( { } , Draggable . prototype ) ;
Class . superrise ( SubsDraggable . prototype , [ 'initialize' , 'startDrag' , 'finishDrag' ] )
Object . extend ( SubsDraggable . prototype , {
initialize : function ( event ) {
this . super _initialize . apply ( this , arguments ) ;
if ( typeof ( this . options . dragelement ) == 'undefined' ) this . options . dragelement = false ;
} ,
startDrag : function ( event ) {
if ( this . options . dragelement ) {
this . _originalElement = this . element ;
// Get the id of the file being dragged
var beingDraggedId = this . element . id . replace ( 'drag-Files-' , '' ) ;
this . element = this . options . dragelement ( this . element ) ;
Position . absolutize ( this . element ) ;
Position . clone ( this . _originalElement , this . element ) ;
// Add # files being moved message
this . element . className = 'dragfile DraggedHandle' ;
// We are at least moving the 1 file being dragged
var numMoved = 1 ;
var i , checkboxes = $ ( 'Form_EditForm' ) . elements [ 'Files[]' ] ;
if ( ! checkboxes ) checkboxes = [ ] ;
if ( ! checkboxes . length ) checkboxes = [ checkboxes ] ;
for ( i = 0 ; i < checkboxes . length ; i ++ ) {
// Total up the other files that are checked
if ( checkboxes [ i ] . checked && checkboxes [ i ] . value != beingDraggedId ) {
numMoved ++ ;
}
}
numFilesIndicator = document . createElement ( 'span' ) ;
numFilesIndicator . innerHTML = 'Moving ' + numMoved + ' files' ;
numFilesIndicator . className = 'NumFilesIndicator' ;
this . element . appendChild ( numFilesIndicator ) ;
}
this . super _startDrag ( event ) ;
} ,
finishDrag : function ( event , success ) {
this . super _finishDrag ( event , success ) ;
if ( this . options . dragelement ) {
Element . remove ( this . element ) ;
this . element = this . _originalElement ;
this . _originalElement = null ;
}
}
} )
// gets element that should be dragged instead of original element
// returned element should be added to DOM tree, and will be deleted by dragdrop library
function getDragElement ( element ) {
var el = element . cloneNode ( true ) ;
el . id = '' ;
document . body . appendChild ( el ) ;
return el ;
}
// Set up DRAG handle
DragFileItem = Class . create ( ) ;
DragFileItem . prototype = {
initialize : function ( ) {
if ( this . id )
{
this . draggable = new SubsDraggable ( this . id , { revert : true , ghosting : false , dragelement : getDragElement } ) ;
}
} ,
destroy : function ( ) {
this . draggable = null ;
}
}
2009-07-05 23:18:35 +02:00
DragFileItem . applyTo ( '#Form_EditForm_Files tr td.dragfile' ) ;
2007-09-15 22:47:35 +02:00
// Set up folder drop target
DropFileItem = Class . create ( ) ;
DropFileItem . prototype = {
initialize : function ( ) {
// Get this.recordID from the last "-" separated chunk of the id HTML attribute
// eg: <li id="treenode-6"> would give a recordID of 6
if ( this . id && this . id . match ( /-([^-]+)$/ ) )
this . recordID = RegExp . $1 ;
2009-02-03 03:50:25 +01:00
this . droppable = Droppables . add ( this . id , { accept : 'dragfile' , hoverclass : 'filefolderhover' ,
onDrop : function ( droppedElement ) {
// Get this.recordID from the last "-" separated chunk of the id HTML attribute
// eg: <li id="treenode-6"> would give a recordID of 6
if ( this . element . id && this . element . id . match ( /-([^-]+)$/ ) )
this . recordID = RegExp . $1 ;
$ ( 'Form_EditForm' ) . elements [ 'DestFolderID' ] . value = this . recordID ;
2007-09-15 22:47:35 +02:00
2009-02-03 03:50:25 +01:00
// Add the dropped file to the list of files to move
var list = droppedElement . getElementsByTagName ( 'img' ) [ 0 ] . id . replace ( 'drag-img-Files-' , '' ) ;
var i , checkboxes = $ ( 'Form_EditForm' ) . elements [ 'Files[]' ] ;
if ( ! checkboxes ) checkboxes = [ ] ;
if ( ! checkboxes . length ) checkboxes = [ checkboxes ] ;
// Add each checked file to the list of ones to move
for ( i = 0 ; i < checkboxes . length ; i ++ ) {
if ( checkboxes [ i ] . checked ) list += ( list ? ',' : '' ) + checkboxes [ i ] . value ;
2007-09-15 22:47:35 +02:00
}
2009-02-03 03:50:25 +01:00
$ ( 'Form_EditForm_FileIDs' ) . value = list ;
$ ( 'Form_EditForm' ) . save ( false , null , 'movemarked' )
}
} ) ;
2007-09-15 22:47:35 +02:00
} ,
destroy : function ( ) {
this . droppable = null ;
this . recordID = null ;
}
}
DropFileItem . applyTo ( '#sitetree li' ) ;
2007-07-19 12:40:05 +02:00
/ * *
* Add File Action
* /
addfolder = Class . create ( ) ;
addfolder . applyTo ( '#addpage' ) ;
addfolder . prototype = {
initialize : function ( ) {
Observable . applyTo ( $ ( this . id + '_options' ) ) ;
2007-09-15 02:54:13 +02:00
this . getElementsByTagName ( 'button' ) [ 0 ] . onclick = returnFalse ;
2007-07-19 12:40:05 +02:00
$ ( this . id + '_options' ) . onsubmit = this . form _submit ;
} ,
onclick : function ( ) {
statusMessage ( 'Creating new folder...' ) ;
this . form _submit ( ) ;
/ *
if ( treeactions . toggleSelection ( this ) ) {
var selectedNode = $ ( 'sitetree' ) . firstSelected ( ) ;
if ( selectedNode ) {
while ( selectedNode . parentTreeNode && ! selectedNode . hints . defaultChild ) {
$ ( 'sitetree' ) . changeCurrentTo ( selectedNode . parentTreeNode ) ;
selectedNode = selectedNode . parentTreeNode ;
}
}
}
* /
return false ;
} ,
form _submit : function ( ) {
var st = $ ( 'sitetree' ) ;
$ ( 'addpage_options' ) . elements . ParentID . value = st . getIdxOf ( st . firstSelected ( ) ) ;
Ajax . SubmitForm ( 'addpage_options' , null , {
2007-09-15 22:47:35 +02:00
onSuccess : this . onSuccess ,
2007-07-19 12:40:05 +02:00
onFailure : this . showAddPageError
} ) ;
return false ;
} ,
2007-09-15 22:47:35 +02:00
onSuccess : function ( response ) {
Ajax . Evaluator ( response ) ;
// Make it possible to drop files into the new folder
DropFileItem . applyTo ( '#sitetree li' ) ;
} ,
2007-07-19 12:40:05 +02:00
showAddPageError : function ( response ) {
errorMessage ( 'Error adding folder' , response ) ;
}
}
2009-07-02 00:28:06 +02:00
/ * *
* Look for new files ( FilesystemSync ) action
* /
FilesystemSyncClass = Class . create ( ) ;
FilesystemSyncClass . applyTo ( '#filesystemsync' ) ;
FilesystemSyncClass . prototype = {
initialize : function ( ) {
this . getElementsByTagName ( 'button' ) [ 0 ] . onclick = returnFalse ;
} ,
onclick : function ( ) {
statusMessage ( 'Looking for new files' ) ;
2009-10-21 06:34:41 +02:00
new Ajax . Request ( 'dev/tasks/FilesystemSyncTask' , {
2009-07-02 00:28:06 +02:00
onSuccess : function ( t ) {
2009-10-21 06:34:41 +02:00
statusMessage ( t . responseText , "good" ) ;
2009-07-02 00:28:06 +02:00
} ,
onFailure : function ( t ) {
errorMessage ( "There was an error looking for new files" ) ;
}
} ) ;
return false ;
}
}
2007-07-19 12:40:05 +02:00
/ * *
* Delete folder action
* /
deletefolder = {
button _onclick : function ( ) {
if ( treeactions . toggleSelection ( this ) ) {
deletefolder . o1 = $ ( 'sitetree' ) . observeMethod ( 'SelectionChanged' , deletefolder . treeSelectionChanged ) ;
2009-01-06 03:18:33 +01:00
deletefolder . o2 = $ ( 'Form_DeleteItemsForm' ) . observeMethod ( 'Close' , deletefolder . popupClosed ) ;
2007-07-19 12:40:05 +02:00
2009-11-21 03:37:27 +01:00
jQuery ( '#sitetree' ) . addClass ( 'multiselect' ) ;
2007-07-19 12:40:05 +02:00
deletefolder . selectedNodes = { } ;
var sel = $ ( 'sitetree' ) . firstSelected ( )
if ( sel ) {
var selIdx = $ ( 'sitetree' ) . getIdxOf ( sel ) ;
deletefolder . selectedNodes [ selIdx ] = true ;
sel . removeNodeClass ( 'current' ) ;
sel . addNodeClass ( 'selected' ) ;
}
}
return false ;
} ,
treeSelectionChanged : function ( selectedNode ) {
var idx = $ ( 'sitetree' ) . getIdxOf ( selectedNode ) ;
if ( selectedNode . selected ) {
selectedNode . removeNodeClass ( 'selected' ) ;
selectedNode . selected = false ;
deletefolder . selectedNodes [ idx ] = false ;
} else {
selectedNode . addNodeClass ( 'selected' ) ;
selectedNode . selected = true ;
deletefolder . selectedNodes [ idx ] = true ;
}
return false ;
} ,
popupClosed : function ( ) {
2009-11-21 03:37:27 +01:00
jQuery ( '#sitetree' ) . removeClass ( 'multiselect' ) ;
2007-07-19 12:40:05 +02:00
$ ( 'sitetree' ) . stopObserving ( deletefolder . o1 ) ;
2009-01-06 03:18:33 +01:00
$ ( 'Form_DeleteItemsForm' ) . stopObserving ( deletefolder . o2 ) ;
2007-07-19 12:40:05 +02:00
for ( var idx in deletefolder . selectedNodes ) {
if ( deletefolder . selectedNodes [ idx ] ) {
node = $ ( 'sitetree' ) . getTreeNodeByIdx ( idx ) ;
if ( node ) {
node . removeNodeClass ( 'selected' ) ;
node . selected = false ;
}
}
}
} ,
form _submit : function ( ) {
var csvIDs = "" ;
for ( var idx in deletefolder . selectedNodes ) {
var selectedNode = $ ( 'sitetree' ) . getTreeNodeByIdx ( idx ) ;
var link = selectedNode . getElementsByTagName ( 'a' ) [ 0 ] ;
if ( deletefolder . selectedNodes [ idx ] && ( ! Element . hasClassName ( link , 'contents' ) || confirm ( "'" + link . firstChild . nodeValue + "' contains files. Would you like to delete the files and folder?" ) ) )
csvIDs += ( csvIDs ? "," : "" ) + idx ;
}
if ( csvIDs ) {
2009-01-06 03:18:33 +01:00
$ ( 'Form_DeleteItemsForm' ) . elements . csvIDs . value = csvIDs ;
2007-07-19 12:40:05 +02:00
statusMessage ( 'deleting pages' ) ;
2009-01-06 03:18:33 +01:00
Ajax . SubmitForm ( 'Form_DeleteItemsForm' , null , {
2007-07-19 12:40:05 +02:00
onSuccess : deletefolder . submit _success ,
onFailure : function ( response ) {
errorMessage ( 'Error deleting pages' , response ) ;
}
} ) ;
2007-09-15 03:01:24 +02:00
$ ( 'deletepage' ) . getElementsByTagName ( 'button' ) [ 0 ] . onclick ( ) ;
2007-07-19 12:40:05 +02:00
} else {
alert ( "Please select at least 1 page." ) ;
}
return false ;
} ,
submit _success : function ( response ) {
Ajax . Evaluator ( response ) ;
treeactions . closeSelection ( $ ( 'deletepage' ) ) ;
}
}
Behaviour . register ( {
'#Form_EditForm_Files' : {
removeFile : function ( fileID ) {
var record ;
if ( record = $ ( 'record-' + fileID ) ) {
record . parentNode . removeChild ( record ) ;
}
}
} ,
'#Form_EditForm_Files a.deletelink' : {
onclick : function ( event ) {
2009-11-21 03:35:54 +01:00
// Send request
new Ajax . Request ( this . href + ( this . href . indexOf ( "?" ) == - 1 ? "?" : "&" ) + "ajax=1" , {
method : 'get' ,
onSuccess : Ajax . Evaluator ,
onFailure : ajaxErrorHandler
} ) ;
2007-07-19 12:40:05 +02:00
Event . stop ( event ) ;
return false ;
}
} ,
'#Form_EditForm' : {
changeDetection _fieldsToIgnore : {
'Files[]' : true
}
}
} ) ;
/ * *
* We don ' t want hitting the enter key in the name field
* to submit the form .
* /
Behaviour . register ( {
'#Form_EditForm_Name' : {
onkeypress : function ( event ) {
event = ( event ) ? event : window . event ;
var kc = event . keyCode ? event . keyCode : event . charCode ;
if ( kc == 13 ) {
return false ;
}
}
}
} ) ;
/ * *
* Initialisation function to set everything up
* /
2009-11-21 03:37:27 +01:00
jQuery ( document ) . ready ( function ( ) {
2007-07-19 12:40:05 +02:00
// Set up delete page
2009-01-06 03:18:33 +01:00
Observable . applyTo ( $ ( 'Form_DeleteItemsForm' ) ) ;
2007-07-19 12:40:05 +02:00
if ( $ ( 'deletepage' ) ) {
$ ( 'deletepage' ) . onclick = deletefolder . button _onclick ;
2007-09-15 03:01:24 +02:00
$ ( 'deletepage' ) . getElementsByTagName ( 'button' ) [ 0 ] . onclick = function ( ) { return false ; } ;
2009-01-06 03:18:33 +01:00
$ ( 'Form_DeleteItemsForm' ) . onsubmit = deletefolder . form _submit ;
Element . hide ( 'Form_DeleteItemsForm' ) ;
2007-07-19 12:40:05 +02:00
}
2008-04-26 08:40:29 +02:00
new CheckBoxRange ( $ ( 'Form_EditForm' ) , 'Files[]' ) ;
2007-10-02 23:34:57 +02:00
} ) ;
Behaviour . register ( {
2009-04-29 03:44:28 +02:00
'#Form_EditForm_delete_unused_thumbnails' : {
2007-10-02 23:34:57 +02:00
onclick : function ( event ) {
Event . stop ( event ) ;
var options = {
method : 'get' ,
onSuccess : function ( t ) {
eval ( t . responseText ) ;
}
} ;
2009-02-03 04:46:15 +01:00
new Ajax . Request ( 'admin/assets/deleteunusedthumbnails' , options ) ;
2007-10-02 23:34:57 +02:00
}
}
2007-11-07 05:50:09 +01:00
} ) ;
function refreshAsset ( ) {
frames [ 0 ] . location . reload ( 0 ) ;
frames [ 1 ] . location . reload ( 1 ) ;
}