2010-05-08 04:45:05 +02:00
/ * *
* File : AssetTableField . js
* /
2009-11-21 04:20:03 +01:00
( function ( $ ) {
2010-04-13 07:55:56 +02:00
$ . entwine ( 'ss' , function ( $ ) {
2009-11-21 04:20:03 +01:00
2010-05-08 04:45:05 +02:00
/ * *
* Class : . AssetTableField
* /
2010-04-13 07:55:56 +02:00
$ ( '.AssetTableField' ) . entwine ( {
2010-05-08 04:45:05 +02:00
// Constructor: onmatch
2009-11-21 04:20:11 +01:00
onmatch : function ( ) {
var self = this ;
// search button
this . find ( 'input#FileFilterButton' ) . click ( function ( e ) {
var btn = $ ( this ) ;
$ ( this ) . addClass ( 'loading' ) ;
self . refresh ( function ( ) { btn . removeClass ( 'loading' ) ; } ) ;
return false ;
} ) ;
// clear button
this . find ( 'input#FileFilterClearButton' ) . click ( function ( e ) {
self . find ( 'input#FileSearch' ) . val ( '' ) ;
self . find ( 'input#FileFilterButton' ) . click ( ) ;
return false ;
} ) ;
// search field
this . find ( 'input#FileSearch' ) . keypress ( function ( e ) {
if ( e . keyCode == $ . ui . keyCode . ENTER ) {
self . find ( 'input#FileFilterButton' ) . click ( ) ;
}
} ) ;
this . _super ( ) ;
} ,
2010-05-08 04:45:05 +02:00
/ * *
* Function : refresh
*
* Parameters :
* ( Function ) callback
* /
2009-11-21 04:20:11 +01:00
refresh : function ( callback ) {
var self = this ;
this . load (
this . attr ( 'href' ) ,
this . find ( ':input' ) . serialize ( ) ,
function ( response , status , xmlhttp ) {
Behaviour . apply ( self [ 0 ] , true ) ;
if ( callback ) callback . apply ( arguments ) ;
}
) ;
}
} ) ;
/ * *
2010-05-08 04:45:05 +02:00
* Class : . AssetTableField : checkbox
*
2009-11-21 04:20:11 +01:00
* Checkboxes used to batch delete files
* /
2010-04-13 07:55:56 +02:00
$ ( '.AssetTableField :checkbox' ) . entwine ( {
2010-05-08 04:45:05 +02:00
// Function: onchange
2009-11-21 04:20:03 +01:00
onchange : function ( ) {
var container = this . parents ( '.AssetTableField' ) ;
var input = container . find ( 'input#deletemarked' ) ;
if ( container . find ( ':input[name=Files\[\]]:checked' ) . length ) {
input . removeAttr ( 'disabled' ) ;
} else {
input . attr ( 'disabled' , 'disabled' ) ;
}
}
} )
/ * *
2010-05-08 04:45:05 +02:00
* Class : . AssetTableField input # deletemarked
*
2009-11-21 04:20:03 +01:00
* Batch delete files marked by checkboxes in the table .
* Refreshes the form field afterwards via ajax .
* /
2010-04-13 07:55:56 +02:00
$ ( '.AssetTableField input#deletemarked' ) . entwine ( {
2010-05-08 04:45:05 +02:00
// Constructor: onmatch
2009-11-21 04:20:03 +01:00
onmatch : function ( ) {
this . attr ( 'disabled' , 'disabled' ) ;
this . _super ( ) ;
} ,
2010-05-08 04:45:05 +02:00
/ * *
* Function : onclick
*
* Parameters :
* ( Event ) e
* /
2009-11-21 04:20:03 +01:00
onclick : function ( e ) {
if ( ! confirm ( ss . i18n . _t ( 'AssetTableField.REALLYDELETE' ) ) ) return false ;
var container = this . parents ( '.AssetTableField' ) ;
var self = this ;
this . addClass ( 'loading' ) ;
$ . post (
container . attr ( 'href' ) + '/deletemarked' ,
this . parents ( 'form' ) . serialize ( ) ,
function ( data , status ) {
self . removeClass ( 'loading' ) ;
2009-11-21 04:20:11 +01:00
container . refresh ( ) ;
2009-11-21 04:20:03 +01:00
}
) ;
return false ;
}
} ) ;
} ) ;
2010-05-26 01:38:34 +02:00
} ( jQuery ) ) ;
// TODO Implementation in Behaviour instead of entwine is necessary to overload TableListField
var AssetTableField = Class . create ( ) ;
AssetTableField . applyTo ( '#Form_EditForm_Files' ) ;
AssetTableField . prototype = {
initialize : function ( ) {
var rules = { } ;
rules [ '#' + this . id + ' table.data a.deletelink' ] = { onclick : this . deleteRecord . bind ( this ) } ;
Behaviour . register ( 'ComplexTableField_' + this . id , rules ) ;
} ,
deleteRecord : function ( e ) {
var img = Event . element ( e ) ;
var link = Event . findElement ( e , "a" ) ;
var row = Event . findElement ( e , "tr" ) ;
var linkCount = row . getElementsByClassName ( 'linkCount' ) [ 0 ] ;
if ( linkCount ) linkCount = linkCount . innerHTML ;
var confirmMessage = ss . i18n . _t ( 'TABLEFIELD.DELETECONFIRMMESSAGE' , 'Are you sure you want to delete this record?' ) ;
if ( linkCount && linkCount > 0 ) confirmMessage += '\nThere are ' + linkCount + ' page(s) that use this file, please review the list of pages on the Links tab of the file before continuing.' ;
// TODO ajaxErrorHandler and loading-image are dependent on cms, but formfield is in sapphire
var confirmed = confirm ( confirmMessage ) ;
if ( confirmed )
{
img . setAttribute ( "src" , 'cms/images/network-save.gif' ) ; // TODO doesn't work
new Ajax . Request (
link . getAttribute ( "href" ) ,
{
method : 'post' ,
postBody : 'forceajax=1' + ( $ ( 'SecurityID' ) ? '&SecurityID=' + $ ( 'SecurityID' ) . value : '' ) ,
onComplete : function ( ) {
Effect . Fade (
row ,
{
afterFinish : function ( obj ) {
// remove row from DOM
obj . element . parentNode . removeChild ( obj . element ) ;
// recalculate summary if needed (assumes that TableListField.js is present)
// TODO Proper inheritance
if ( this . _summarise ) this . _summarise ( ) ;
// custom callback
if ( this . callback _deleteRecord ) this . callback _deleteRecord ( e ) ;
} . bind ( this )
}
) ;
} . bind ( this ) ,
onFailure : this . ajaxErrorHandler
}
) ;
}
Event . stop ( e ) ;
}
} ;