GridFieldBulkEditingTools/javascript/GridFieldBulkImageUpload.js

313 lines
9.1 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
// ============================================================================================
2012-07-16 22:39:01 +02:00
$.entwine('colymba', function($) {
2013-06-23 17:17:42 +02:00
/*
* handles individual edit forms changes
* updates buttons and visual styles
*/
$('.bulkImageUploadUpdateForm').entwine({
onmatch: function(){},
onunmatch: function(){},
haschanged: function()
{
var itemInfo,
itemStatus
;
2013-06-23 17:17:42 +02:00
itemStatus = $(this).parents('li').find('.ss-uploadfield-item-status');
itemInfo = $(this).parents('li').find('.ss-uploadfield-item-info');
2013-06-23 17:17:42 +02:00
if ( !$(this).hasClass('hasUpdate') )
{
$(this).addClass('hasUpdate');
2012-07-16 22:39:01 +02:00
}
2013-06-23 18:10:22 +02:00
$(itemStatus).removeClass('updated').addClass('dirty').html(ss.i18n._t('GridFieldBulkTools.EDIT_CHANGED'));
if ( $(itemInfo).hasClass('updated') ) $(itemInfo).removeClass('updated');
if ( !$(itemInfo).hasClass('dirty') ) $(itemInfo).addClass('dirty');
$('#bulkImageUploadUpdateBtn').removeClass('ui-state-disabled ssui-button-disabled');
$('#bulkImageUploadUpdateBtn').attr('aria-disabled', 'false');
$('#bulkImageUploadUpdateBtn').removeAttr('disabled');
}
});
2013-06-23 17:17:42 +02:00
/*
* catches edit form changes
*/
$('.bulkImageUploadUpdateForm input.text, .bulkImageUploadUpdateForm input.checkbox, .bulkImageUploadUpdateForm select, .bulkImageUploadUpdateForm textarea').entwine({
2013-06-23 17:17:42 +02:00
onchange: function()
{
this.closest('.bulkImageUploadUpdateForm').haschanged();
2012-07-16 22:39:01 +02:00
}
});
2013-06-23 17:17:42 +02:00
/*
* catches edit form changes
* HTMLEditorField hack
*/
//textarea node is being removed from the DOM when the HTMLEditorFieldChanges, not the best but works
$('.field.htmleditor textarea').entwine({
2013-06-23 17:17:42 +02:00
onmatch: function(){},
onunmatch: function()
{
//note sure why querying straight from the texarea doesn't work... maybe because it is already removed from DOM?
$('input[type="hidden"][name="'+$(this).attr('name')+'"]').parents('.bulkImageUploadUpdateForm').haschanged();
}
});
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
/*
* edit forms ovverides
*/
$('.ss-uploadfield-item-editform').entwine({
onmatch: function(e)
{
$('#bulkImageUploadUpdateCancelBtn').removeClass('ui-state-disabled ssui-button-disabled');
$('#bulkImageUploadUpdateCancelBtn').attr('aria-disabled', 'false');
$('#bulkImageUploadUpdateCancelBtn').removeAttr('disabled');
},
2013-06-23 17:17:42 +02:00
onunmatch: function(){}
});
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
});
2012-07-16 22:39:01 +02:00
}(jQuery));