mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
148d5efcea
* Update webpack-config to more closely align with SilverStripe core modules * Automated linting * Remove all JS linting errors, use let/const for assignment, do not reassign callback arguments * Rebuild dist files, remove yarn lock from build command, remove source map files
87 lines
3.0 KiB
JavaScript
87 lines
3.0 KiB
JavaScript
/* global window */
|
|
import jQuery from 'jquery';
|
|
|
|
jQuery.entwine('ss', ($) => {
|
|
/**
|
|
* Handles Gridfield updates after a bulk action/upload
|
|
*/
|
|
window.bulkTools = {
|
|
gridfieldRefresh($gridfield, data) {
|
|
if (!data.isError) {
|
|
if (data.isDestructive) {
|
|
this.removeGridFieldRows($gridfield, data.records.success);
|
|
} else {
|
|
this.updateGridFieldRows($gridfield, data.records.success);
|
|
}
|
|
// failed rows
|
|
this.failedGridFieldRows($gridfield, data.records.failed);
|
|
}
|
|
},
|
|
|
|
// return the gridfield row node for a specific record
|
|
getGridFieldRow($gridfield, record) {
|
|
return $gridfield.find(`.ss-gridfield-item[data-id="${record.id}"][data-class="${record.class}"]`);
|
|
},
|
|
|
|
// remove all bulk tools class and attributes from the row
|
|
cleanGridFieldRow($row) {
|
|
return $row.removeClass('bt-deleted bt-failed bt-updated').removeAttr('bt-error');
|
|
},
|
|
|
|
// mark all affected gridfield rows as deleted
|
|
removeGridFieldRows($gridfield, records) {
|
|
records.forEach(function (record) {
|
|
const $row = this.getGridFieldRow($gridfield, record);
|
|
$row.addClass('bt-deleted').fadeOut(2000);
|
|
}, this);
|
|
$gridfield.entwine('.').entwine('ss').delay(2000).reload();
|
|
},
|
|
|
|
// mark all affected gridfield rows as failed
|
|
failedGridFieldRows($gridfield, records) {
|
|
records.forEach(function (record) {
|
|
const $row = this.getGridFieldRow($gridfield, record);
|
|
$row.addClass('bt-failed').attr('bt-error', record.message);
|
|
// pseudo element absolute pos don't work in TR, so we'll have to find some
|
|
// JS way to show the bt-error content..
|
|
}, this);
|
|
},
|
|
|
|
// update rows with new content
|
|
updateGridFieldRows($gridfield, records) {
|
|
$gridfield.find('.ss-gridfield-item.ss-gridfield-no-items').remove();
|
|
records.forEach(function (record) {
|
|
const $row = this.getGridFieldRow($gridfield, record);
|
|
const $newRow = $(record.row).addClass('bt-updated');
|
|
// replace existing or add new?
|
|
if ($row.length === 1) {
|
|
$row.replaceWith($newRow);
|
|
} else {
|
|
$gridfield.find('.ss-gridfield-items').prepend($newRow);
|
|
}
|
|
}, this);
|
|
},
|
|
|
|
// removes an item from the upload item
|
|
// this is bad! can't dispatch within Reducer
|
|
// so it's commentted until actions are written
|
|
/*
|
|
removeUploadItem: function ($gridfield, queueIndex, fileID)
|
|
{
|
|
// we use queueIndex for uploads since the input value will not have been changed yet
|
|
// and we can't watch for change event on hidden input
|
|
if (fileID) {
|
|
$gridfield.find('.uploadfield-item')
|
|
.find('input[value="'+fileID+'"]')
|
|
.siblings('button.uploadfield-item__remove-btn')
|
|
.trigger('click');
|
|
} else if (queueIndex) {
|
|
$gridfield.find('.uploadfield-item')
|
|
.eq(queueIndex-1)
|
|
.find('button.uploadfield-item__remove-btn')
|
|
.trigger('click');
|
|
}
|
|
} */
|
|
};
|
|
});
|