GridFieldBulkEditingTools/bulkUpload/javascript/GridFieldBulkUpload.js

390 lines
11 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($) {
2013-06-23 17:17:42 +02:00
// ============================================================================================
// start SS namespace overrides
// ============================================================================================
/*
* open/close edit form
*/
2012-07-16 22:39:01 +02:00
$('div.ss-upload .ss-uploadfield-item-edit, div.ss-upload .ss-uploadfield-item-name').entwine({
2013-06-23 17:17:42 +02:00
onclick: function(e)
{
2012-07-16 22:39:01 +02:00
this.closest('.ss-uploadfield-item').find('.ss-uploadfield-item-editform').toggleEditForm();
}
});
2013-06-23 17:17:42 +02:00
/*
* edit all button
* @TODO fix
*/
2012-07-16 22:39:01 +02:00
$('div.ss-upload .fileOverview .ss-uploadfield-item-edit-all').entwine({
2013-06-23 17:17:42 +02:00
onmatch: function()
{
2012-07-16 22:39:01 +02:00
if( !$(this).hasClass('opened') ){
$(this).addClass('opened');
}
2012-07-16 22:39:01 +02:00
},
2013-06-23 17:17:42 +02:00
onunmatch: function(){},
onclick: function(e)
{
if( $(this).hasClass('opened') )
{
2012-07-16 22:39:01 +02:00
$('.ss-uploadfield-files .ss-uploadfield-item-editform').hide();
$(this).removeClass('opened');
2013-06-23 17:17:42 +02:00
}
else{
2012-07-16 22:39:01 +02:00
$('.ss-uploadfield-files .ss-uploadfield-item-editform').show();
$(this).addClass('opened');
}
2013-06-23 17:17:42 +02:00
e.preventDefault();
2012-07-16 22:39:01 +02:00
}
});
2013-06-23 17:17:42 +02:00
/*
* show/hide edit form
* overrides default behaviour
*/
2012-07-16 22:39:01 +02:00
$('div.ss-upload .ss-uploadfield-item-editform').entwine({
2013-06-23 17:17:42 +02:00
toggleEditForm: function()
{
2012-07-16 22:39:01 +02:00
if( this.css('display') == 'none' ) {
this.show();
2013-06-23 17:17:42 +02:00
}
else{
2012-07-16 22:39:01 +02:00
this.hide();
}
}
});
2013-06-23 17:17:42 +02:00
/*
* prevent submitting of individual edit forms
*/
2013-06-23 14:08:26 +02:00
$('#Form_uploadForm, div.ss-upload .ss-uploadfield-item-editform form').entwine({
2013-06-23 17:17:42 +02:00
onsubmit: function(e)
{
return false;
}
});
2013-06-23 17:17:42 +02:00
/*
* initialise disabled state
*/
$('#bulkImageUploadUpdateBtn,#bulkImageUploadUpdateCancelBtn').entwine({
2013-06-23 17:17:42 +02:00
onmatch: function()
{
$(this).addClass('ui-state-disabled ssui-button-disabled');
$(this).attr('aria-disabled', 'true');
$(this).attr('disabled', 'true');
},
onunmatch: function(){}
});
2013-06-23 14:08:26 +02:00
2013-06-23 17:17:42 +02:00
/*
* finish/return button
*/
2013-06-23 14:08:26 +02:00
$('#bulkImageUploadFinishBtn').entwine({
onmatch: function(){},
onunmatch: function(){},
2013-06-23 17:17:42 +02:00
onclick: function(e)
{
var formsWithUpadtes = $('form.bulkImageUploadUpdateForm.hasUpdate').length,
confirmed = true;
2013-06-23 14:08:26 +02:00
if ( formsWithUpadtes > 0 )
{
2013-06-23 18:10:22 +02:00
confirmed = confirm( ss.i18n._t('GridFieldBulkTools.FINISH_CONFIRM') );
2013-06-23 14:08:26 +02:00
}
if (confirmed)
{
$('.cms-container').loadPanel(this.attr('href'), null, {});
}
}
});
2012-07-16 22:39:01 +02:00
2013-06-23 17:17:42 +02:00
// ============================================================================================
// end SS namespace overrides
// ============================================================================================
2012-07-16 22:39:01 +02:00
2013-06-23 17:17:42 +02:00
// ============================================================================================
// start add-on behaviours
// ============================================================================================
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);
/*
this.closest('.colymba-bulkupload-info').html(ss.i18n.sprintf(
ss.i18n._t('GRIDFIELD_BULK_UPLOAD.PROGRESS_INFO'),
total,
done,
total
));*/
}
});
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();
},
});
/**
* Track completed uploads
*//*
$('li.ss-uploadfield-item.done').entwine({
onmatch: function(){
this.parents('ul.ss-uploadfield-files').trackProgress();
},
onunmatch: function(){},
});*/
/**
* 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');
$finishBtn.removeClass('ui-state-disabled ssui-button-disabled').attr('aria-disabled', 'false').removeAttr('disabled');
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');
}
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{
this.css({display: 'none'});
$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
2013-06-23 17:17:42 +02:00
/*
* save changes button behaviour
* loop through edited forms and submit data
*/
$('#bulkImageUploadUpdateBtn:not(.ui-state-disabled)').entwine({
2013-06-23 17:17:42 +02:00
onmatch: function()
{
2012-07-16 22:39:01 +02:00
$(this).data('completedForms', 0);
},
2013-06-23 17:17:42 +02:00
onunmatch: function(){},
onclick: function(e)
{
var formsWithUpadtes,
url,
data,
cacheBuster
;
2012-07-16 22:39:01 +02:00
formsWithUpadtes = $('form.bulkImageUploadUpdateForm.hasUpdate');
2012-07-16 22:39:01 +02:00
$(this).data('formsToUpdate', $(formsWithUpadtes).length);
url = $(this).data('url');
2012-07-16 22:39:01 +02:00
2013-06-23 17:17:42 +02:00
if ( $(formsWithUpadtes).length > 0 )
{
$(this).addClass('loading');
}
2013-06-23 17:17:42 +02:00
$(formsWithUpadtes).each(function()
{
cacheBuster = new Date().getTime() + '_' + $(this).attr('name');
2013-06-23 17:17:42 +02:00
if ( url.indexOf('?') !== -1 )
{
cacheBuster = '&cacheBuster=' + cacheBuster;
}
else{
cacheBuster = '?cacheBuster=' + cacheBuster;
}
data = $(this).serialize();
2012-07-16 22:39:01 +02:00
$.ajax({
2013-06-23 17:17:42 +02:00
url: url + cacheBuster,
data: data,
type: "POST",
context: $(this)
}).done(function() {
var btn = $('#bulkImageUploadUpdateBtn'),
totalForms = parseInt( $(btn).data('formsToUpdate') ),
counter = parseInt( $(btn).data('completedForms') )
;
2012-07-16 22:39:01 +02:00
counter = counter + 1;
$(btn).data('completedForms', counter);
$(this).removeClass('hasUpdate');
2013-06-23 18:10:22 +02:00
$(this).parents('li').find('.ss-uploadfield-item-status').removeClass('dirty').addClass('updated').html(ss.i18n._t('GridFieldBulkTools.EDIT_UPDATED'));
$(this).parents('li').find('.ss-uploadfield-item-info').removeClass('dirty').addClass('updated');
2012-08-09 19:50:55 +02:00
$(this).parents('li').find('.ss-uploadfield-item-editform').css('display', 'none');
2012-07-16 22:39:01 +02:00
$(this).removeClass('hasUpdate');
2012-07-16 22:39:01 +02:00
2013-06-23 17:17:42 +02:00
if ( counter == totalForms )
{
$('#bulkImageUploadUpdateBtn').data('completedForms', 0);
$('#bulkImageUploadUpdateBtn').removeClass('loading');
$('#bulkImageUploadUpdateBtn').addClass('ui-state-disabled');
2013-06-23 17:17:42 +02:00
}
2012-07-16 22:39:01 +02:00
});
2013-06-23 17:17:42 +02:00
2013-06-02 20:07:00 +02:00
});
2013-06-23 17:17:42 +02:00
return false;
2012-07-16 22:39:01 +02:00
}
});
2013-06-23 17:17:42 +02:00
/*
* cancel button behaviour
* loop through edit forms and submit for deletion
*/
$('#bulkImageUploadUpdateCancelBtn:not(.ui-state-disabled)').entwine({
2013-06-23 17:17:42 +02:00
onclick: function(e)
{
var url = $(this).data('url'),
cacheBuster = new Date().getTime()
;
if ( url.indexOf('?') !== -1 )
{
cacheBuster = '&cacheBuster=' + cacheBuster;
}
else{
cacheBuster = '?cacheBuster=' + cacheBuster;
}
2012-07-16 22:39:01 +02:00
2013-06-23 17:17:42 +02:00
$('form.bulkImageUploadUpdateForm').each(function()
{
2012-07-16 22:39:01 +02:00
var data = $(this).serialize();
$.ajax({
2013-06-23 17:17:42 +02:00
url: url + cacheBuster,
data: data,
type: "POST",
context: $(this)
2012-07-16 22:39:01 +02:00
}).done(function() {
$(this).parents('li.ss-uploadfield-item').empty().remove();
2013-06-23 17:17:42 +02:00
if ( $('li.ss-uploadfield-item').length == 0 )
{
2012-07-16 22:39:01 +02:00
$('.ss-uploadfield-editandorganize').css('display', 'none');
$('#Form_bulkImageUploadForm').removeClass('loading');
$('#bulkImageUploadUpdateCancelBtn').addClass('ui-state-disabled');
2012-07-16 22:39:01 +02:00
}
});
2013-06-02 20:07:00 +02:00
});
2013-06-23 17:17:42 +02:00
return false;
2012-07-16 22:39:01 +02:00
}
});
2013-06-23 17:17:42 +02:00
2012-07-16 22:39:01 +02:00
});
2013-06-23 17:17:42 +02:00
// ============================================================================================
// end add-on behaviours
// ============================================================================================
2012-07-16 22:39:01 +02:00
2013-06-23 17:17:42 +02:00
});
2014-04-06 13:30:54 +02:00
}(jQuery));