GridFieldBulkEditingTools/bulkUpload/javascript/GridFieldBulkUpload.js

285 lines
9.2 KiB
JavaScript
Raw Normal View History

2013-06-23 17:17:42 +02:00
(function($) {
2012-07-16 22:39:01 +02:00
$.entwine('ss', function($) {
2014-04-06 14:51:15 +02:00
2014-04-05 18:54:50 +02:00
$.entwine('colymba', function($) {
/**
* Makes sure the component is at the top :)
*/
$('.bulkUpload').entwine({
onmatch: function(){
var $tr = this.parents('thead').find('tr'),
$component = this.clone(),
index = $tr.index(this)
;
if ( index > 1 )
{
$component.insertAfter($tr.eq(0));
this.remove();
}
},
onunmatch: function(){}
});
2014-04-06 13:30:54 +02:00
/**
* Track upload progress...
*/
$('ul.ss-uploadfield-files').entwine({
2013-06-23 17:17:42 +02:00
onmatch: function(){},
2014-04-06 13:30:54 +02:00
onunmatch: function(){},
trackProgress: function()
2013-06-23 17:17:42 +02:00
{
2014-04-06 13:30:54 +02:00
var $li = this.find('li.ss-uploadfield-item'),
total = $li.length,
$done = $li.filter('.done'),
done = $done.length,
$errors = $li.not($done).find('.ui-state-warning-text,.ui-state-error-text'),
errors = $errors.length
2013-06-23 17:17:42 +02:00
;
2014-04-06 13:30:54 +02:00
this.parents('.ss-uploadfield').find('.colymba-bulkupload-buttons').refresh(total, done, errors);
}
});
2014-04-06 13:30:54 +02:00
/**
* Track new and canceled updloads
*/
$('li.ss-uploadfield-item').entwine({
onmatch: function(){
this.parents('ul.ss-uploadfield-files').trackProgress();
},
onunmatch: function(){
$('ul.ss-uploadfield-files').trackProgress();
},
});
/**
* Track updload warning/errors
*/
$('li.ss-uploadfield-item .ui-state-warning-text,li.ss-uploadfield-item .ui-state-error-text').entwine({
onmatch: function(){
this.parents('ul.ss-uploadfield-files').trackProgress();
},
onunmatch: function(){
$('ul.ss-uploadfield-files').trackProgress();
},
});
/**
* Update buttons state and progress info...
*/
$('.colymba-bulkupload-buttons').entwine({
2013-06-23 17:17:42 +02:00
onmatch: function(){},
2014-04-06 13:30:54 +02:00
onunmatch: function(){},
refresh: function(total, done, error)
{
var $info = this.find('.colymba-bulkupload-info'),
$editBtn = this.find('.bulkUploadEditButton'),
$cancelBtn = this.find('.bulkUploadCancelButton'),
$finishBtn = this.find('.bulkUploadFinishButton'),
$clearErrorBtn = this.find('.bulkUploadClearErrorButton')
;
if ( total > 0 )
{
this.css({display: 'block'});
$info.html(ss.i18n.sprintf(
ss.i18n._t('GRIDFIELD_BULK_UPLOAD.PROGRESS_INFO'),
total,
done,
error
));
$cancelBtn.removeClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'false').removeAttr('disabled');
//if there are still uploads going
if ( (done + error) < total )
{
if ( !this.hasClass('loading') )
{
this.addClass('loading');
$finishBtn.addClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'true').attr('disabled', 'disabled');
}
}
else{
this.removeClass('loading');
$finishBtn.removeClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'false').removeAttr('disabled');
}
//if all done and OK, enable edit
2014-04-06 13:30:54 +02:00
if ( total === done )
{
$editBtn.removeClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'false').removeAttr('disabled');
}
else{
$editBtn.addClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'true').attr('disabled', 'true');
}
//toggle clear error button
2014-04-06 13:30:54 +02:00
if ( error > 0 )
{
$clearErrorBtn.removeClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'false').removeAttr('disabled');
}
else{
$clearErrorBtn.addClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'true').attr('disabled', 'true');
}
}
else{
//if not uploading, reset + hide
this.css({display: 'none'}).removeClass('loading');
2014-04-06 13:30:54 +02:00
$editBtn.addClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'true').attr('disabled', 'true');
$cancelBtn.addClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'true').attr('disabled', 'true');
$finishBtn.addClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'true').attr('disabled', 'true');
$clearErrorBtn.addClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'true').attr('disabled', 'true');
}
}
});
2014-04-06 13:30:54 +02:00
2014-04-06 19:04:12 +02:00
/**
* Clears all updloads with warning or error
*/
$('.bulkUploadClearErrorButton:not(.ui-state-disabled)').entwine({
2014-04-06 14:51:15 +02:00
onmatch: function(){
this.removeClass('action');
},
onunmatch: function(){},
onclick: function(e)
{
var $bulkUpload = this.parents('.bulkUpload'),
$errors = $bulkUpload.find('li.ss-uploadfield-item .ui-state-warning-text,li.ss-uploadfield-item .ui-state-error-text').parents('li')
;
$($errors.get().reverse()).each(function(index, Element){
$(this).remove();
});
}
});
2014-04-06 13:30:54 +02:00
2014-04-06 19:04:12 +02:00
/**
* Cancel all uploads
* Clear the ones with warnings/error and delete dataObjects from the successful ones
*/
$('.bulkUploadCancelButton:not(.ui-state-disabled)').entwine({
2014-04-06 14:51:15 +02:00
onmatch: function(){
this.removeClass('action');
},
onunmatch: function(){},
onclick: function()
{
var $bulkUpload = this.parents('.bulkUpload'),
$li = $bulkUpload.find('li.ss-uploadfield-item'),
$records = $li.filter('[data-recordid]'),
$other = $li.not($records),
$doBulkActionButton = $bulkUpload.parents('.ss-gridfield-table').find('.doBulkActionButton'),
recordsID
2014-04-06 14:51:15 +02:00
;
$other.each(function(index, Element){
// skip in progress
$(this).remove();
});
if ( $doBulkActionButton.length > 0 )
{
recordsID = $records.map(function() {
return parseInt( $(this).data('recordid') )
}).get();
this.addClass('loading');
$doBulkActionButton.doBulkAction('delete', recordsID, this.cancelCallback, this);
2014-04-06 14:51:15 +02:00
}
},
cancelCallback: function(data)
{
var $bulkUpload = this.parents('.bulkUpload'),
$li = $bulkUpload.find('li.ss-uploadfield-item'),
ids
;
if ( data )
{
ids = data.records;
$li.each(function(index, Element){
var $this = $(this),
recordID = parseInt( $this.data('recordid') )
;
if ( ids.indexOf(recordID) !== -1 )
{
$this.remove();
}
});
2014-04-07 23:14:08 +02:00
$(this).parents('.ss-gridfield').entwine('.').entwine('ss').reload();
}
this.removeClass('loading');
2014-04-06 14:51:15 +02:00
}
});
2014-04-06 19:04:12 +02:00
/**
* Clear all the warning/error/finished uploads
*/
$('.bulkUploadFinishButton:not(.ui-state-disabled)').entwine({
2014-04-06 14:51:15 +02:00
onmatch: function(){
this.removeClass('action');
},
onunmatch: function(){},
onclick: function()
{
var $bulkUpload = this.parents('.bulkUpload'),
$li = $bulkUpload.find('li.ss-uploadfield-item')
;
this.addClass('loading');
2014-04-06 14:51:15 +02:00
$li.each(function(index, Element){
// skip in progress
$(this).remove();
});
2014-04-07 23:14:08 +02:00
$(this).parents('.ss-gridfield').entwine('.').entwine('ss').reload();
this.removeClass('loading');
2014-04-06 14:51:15 +02:00
}
});
$('.bulkUploadEditButton:not(.ui-state-disabled)').entwine({
2014-04-06 14:51:15 +02:00
onmatch: function(){
this.removeClass('action');
},
onunmatch: function(){},
onclick: function()
{
var $bulkUpload = this.parents('.bulkUpload'),
$li = $bulkUpload.find('li.ss-uploadfield-item'),
$records = $li.filter('[data-recordid]'),
recordsID,
$doBulkActionButton = $bulkUpload.parents('.ss-gridfield-table').find('.doBulkActionButton')
;
if ( $doBulkActionButton.length > 0 )
{
this.addClass('loading');
2014-04-06 19:04:12 +02:00
2014-04-06 14:51:15 +02:00
recordsID = $records.map(function() {
return parseInt( $(this).data('recordid') )
}).get();
$doBulkActionButton.doBulkAction('bulkedit', recordsID);
2014-04-06 14:51:15 +02:00
}
}
});
2014-04-06 13:30:54 +02:00
2014-04-06 14:51:15 +02:00
}); // colymba namespace
2012-07-16 22:39:01 +02:00
2014-04-06 14:51:15 +02:00
}); // ss namespace
2014-05-11 12:32:21 +02:00
}(jQuery));