Remove all JS linting errors, use let/const for assignment, do not reassign callback arguments

This commit is contained in:
Robbie Averill 2018-07-27 16:26:00 +12:00
parent c3ffb821d5
commit b3906f4af4
6 changed files with 346 additions and 349 deletions

View File

@ -1,74 +1,86 @@
/**
* Handles Gridfield updates after a bulk action/upload
*/
window.bulkTools = {
/* global window */
import jQuery from 'jquery';
gridfieldRefresh($gridfield, data) {
if (!data.isError) {
if (data.isDestructive) {
this.removeGridFieldRows($gridfield, data.records.success);
} else {
this.updateGridFieldRows($gridfield, data.records.success);
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);
}
// 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}"]`);
},
// 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');
},
// 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 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);
},
// 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);
// 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');
}
}, 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');
}
} */
};
} */
};
});

View File

@ -1,249 +1,233 @@
(function ($) {
$.entwine('ss', ($) => {
$.entwine('colymba', ($) => {
/**
* Makes sure the component is above the headers
*/
$('.bulkManagerOptions').entwine({
onmatch() {
let $parent = this.parents('thead'),
$tr = $parent.find('tr'),
/* global window, alert, confirm */
import jQuery from 'jquery';
import i18n from 'i18n';
targets = ['.filter-header', '.sortable-header'],
$target = $parent.find(targets.join(',')),
jQuery.entwine('colymba', ($) => {
/**
* Makes sure the component is above the headers
*/
$('.bulkManagerOptions').entwine({
onmatch() {
const $parent = this.parents('thead');
const $tr = $parent.find('tr');
index = $tr.index(this),
newIndex = $tr.length - 1
;
const targets = ['.filter-header', '.sortable-header'];
const $target = $parent.find(targets.join(','));
$target.each((index, Element) => {
const idx = $tr.index(Element);
if (idx < newIndex) {
newIndex = idx;
}
});
const index = $tr.index(this);
let newIndex = $tr.length - 1;
if (index > newIndex) {
$tr.eq(newIndex).insertAfter($(this));
}
},
onunmatch() {}
$target.each((targetIndex, Element) => {
const idx = $tr.index(Element);
if (idx < newIndex) {
newIndex = idx;
}
});
/**
* Bulkselect table cell behaviours
*/
$('td.col-bulkSelect').entwine({
onmatch() {
},
onunmatch() {
},
onmouseover() {
// disable default row click behaviour -> avoid navigation to edit form when clicking the checkbox
$(this).parents('.ss-gridfield-item').find('.edit-link').removeClass('edit-link')
.addClass('tempDisabledEditLink');
},
onmouseout() {
// re-enable default row click behaviour
$(this).parents('.ss-gridfield-item').find('.tempDisabledEditLink').addClass('edit-link')
.removeClass('tempDisabledEditLink');
},
onclick(e) {
// check/uncheck checkbox when clicking cell
const cb = $(e.target).find('input');
if (!$(cb).prop('checked')) $(cb).prop('checked', true);
else $(cb).prop('checked', false);
}
});
if (index > newIndex) {
$tr.eq(newIndex).insertAfter($(this));
}
},
});
/**
* Individual select checkbox behaviour
*/
$('td.col-bulkSelect input').entwine({
onmatch() {
},
onunmatch() {
},
onclick(e) {
$(this).parents('.grid-field__table').find('input.bulkSelectAll').prop('checked', '');
}
});
/**
* Bulkselect table cell behaviours
*/
$('td.col-bulkSelect').entwine({
onmouseover() {
// disable default row click behaviour -> avoid navigation to edit form when
// clicking the checkbox
$(this).parents('.ss-gridfield-item').find('.edit-link').removeClass('edit-link')
.addClass('tempDisabledEditLink');
},
onmouseout() {
// re-enable default row click behaviour
$(this).parents('.ss-gridfield-item').find('.tempDisabledEditLink').addClass('edit-link')
.removeClass('tempDisabledEditLink');
},
onclick(e) {
// check/uncheck checkbox when clicking cell
const cb = $(e.target).find('input');
if (!$(cb).prop('checked')) {
$(cb).prop('checked', true);
} else {
$(cb).prop('checked', false);
}
}
});
/**
* Individual select checkbox behaviour
*/
$('td.col-bulkSelect input').entwine({
onmatch() {
},
onunmatch() {
},
onclick() {
$(this).parents('.grid-field__table').find('input.bulkSelectAll').prop('checked', '');
}
});
/**
* Bulkselect checkbox behaviours
*/
$('input.bulkSelectAll').entwine({
onclick() {
const state = $(this).prop('checked');
$(this).parents('.grid-field__table')
.find('td.col-bulkSelect input')
.prop('checked', state)
.trigger('change');
},
getSelectRecordsID() {
return $(this).parents('.grid-field__table')
.find('td.col-bulkSelect input:checked')
.map(function () {
return parseInt($(this).data('record'), 10);
})
.get();
}
});
/**
* Bulkselect checkbox behaviours
*/
$('input.bulkSelectAll').entwine({
onmatch() {
},
onunmatch() {
},
onclick() {
const state = $(this).prop('checked');
$(this).parents('.grid-field__table')
.find('td.col-bulkSelect input')
.prop('checked', state)
.trigger('change');
},
getSelectRecordsID() {
return $(this).parents('.grid-field__table')
.find('td.col-bulkSelect input:checked')
.map(function () {
return parseInt($(this).data('record'));
})
.get();
}
});
/**
* Bulk action dropdown behaviours
*/
$('select.bulkActionName').entwine({
onchange() {
const value = $(this).val();
const $parent = $(this).parents('.bulkManagerOptions');
const $btn = $parent.find('.doBulkActionButton');
const config = $btn.data('config');
$.each(config, (configKey, configData) => {
if (configKey !== value) {
$btn.removeClass(configData.buttonClasses);
}
});
if (!value) {
$btn.addClass('disabled');
return;
}
$btn.removeClass('disabled');
$btn.addClass(config[value].buttonClasses).addClass('btn-outline-secondary');
if (config[value].icon) {
const $img = $btn.find('img');
if ($img.length) {
$img.attr('src', config[value].icon);
} else {
$btn.prepend(`<img src="${config[value].icon}" alt="" />`);
}
} else {
$btn.find('img').remove();
}
/**
* Bulk action dropdown behaviours
*/
$('select.bulkActionName').entwine({
onmatch() {
},
onunmatch() {
},
onchange(e) {
let value = $(this).val(),
$parent = $(this).parents('.bulkManagerOptions'),
$btn = $parent.find('.doBulkActionButton'),
config = $btn.data('config');
$.each(config, (configKey, configData) => {
if (configKey != value) {
$btn.removeClass(configData.buttonClasses);
}
});
if (!value) {
$btn.addClass('disabled');
return;
}
$btn.removeClass('disabled');
if (config[value].destructive) {
$btn.addClass('btn-outline-danger');
} else {
$btn.removeClass('btn-outline-danger');
}
}
});
$btn.addClass(config[value].buttonClasses).addClass('btn-outline-secondary');
/**
* bulk action button behaviours
*/
$('.doBulkActionButton').entwine({
getActionURL(action, url) {
const cacheBuster = new Date().getTime();
let newUrl = url.split('?');
let newAction = '';
if (action) {
newAction = `/${action}`;
}
if (config[value].icon) {
const $img = $btn.find('img');
if (newUrl[1]) {
newUrl = `${newUrl[0] + newAction}?${newUrl[1]}&cacheBuster=${cacheBuster}`;
} else {
newUrl = `${newUrl[0] + newAction}?cacheBuster=${cacheBuster}`;
}
return newUrl;
},
onclick() {
const $parent = $(this).parents('.bulkManagerOptions');
const action = $parent.find('select.bulkActionName').val();
const ids = $(this).parents('.bulkManagerOptions').find('input.bulkSelectAll:first').getSelectRecordsID();
if ($img.length) {
$img.attr('src', config[value].icon);
} else {
$btn.prepend(`<img src="${config[value].icon}" alt="" />`);
}
} else {
$btn.find('img').remove();
}
this.doBulkAction(action, ids);
},
doBulkAction(action, ids) {
const { bulkTools } = window;
if (config[value].destructive) {
$btn.addClass('btn-outline-danger');
} else {
$btn.removeClass('btn-outline-danger');
}
}
});
const $parent = $(this).parents('.bulkManagerOptions');
const $btn = $parent.find('a.doBulkActionButton');
const $msg = $parent.find('.message');
const config = $btn.data('config');
let url = this.getActionURL(action, $(this).data('url'));
const inputData = { records: ids };
/**
* bulk action button behaviours
*/
$('.doBulkActionButton').entwine({
onmatch() {
},
onunmatch() {
},
getActionURL(action, url) {
const cacheBuster = new Date().getTime();
url = url.split('?');
if (ids.length <= 0) {
alert(i18n._t('GRIDFIELD_BULK_MANAGER.BULKACTION_EMPTY_SELECT'));
return false;
}
if (action) {
action = `/${action}`;
} else {
action = '';
}
// if ( $btn.hasClass('ss-ui-action-destructive') )
if (config[action].destructive) {
if (!confirm(i18n._t('GRIDFIELD_BULK_MANAGER.CONFIRM_DESTRUCTIVE_ACTION'))) {
return false;
}
}
if (url[1]) {
url = `${url[0] + action}?${url[1]}&` + `cacheBuster=${cacheBuster}`;
} else {
url = `${url[0] + action}?` + `cacheBuster=${cacheBuster}`;
}
return url;
},
onclick(e) {
let $parent = $(this).parents('.bulkManagerOptions'),
action = $parent.find('select.bulkActionName').val(),
ids = $(this).parents('.bulkManagerOptions').find('input.bulkSelectAll:first').getSelectRecordsID()
;
$btn.addClass('loading');
$msg.removeClass('static show error warning');
this.doBulkAction(action, ids);
},
if (config[action].xhr) {
$.ajax({
url,
data: inputData,
type: 'POST',
context: $(this)
}).always(function (data) {
let returnData = data;
$btn.removeClass('loading');
doBulkAction(action, ids) {
let $parent = $(this).parents('.bulkManagerOptions'),
$btn = $parent.find('a.doBulkActionButton'),
$msg = $parent.find('.message'),
// if request fail, return a +4xx status code, extract json response
if (data.responseText) {
returnData = JSON.parse(data.responseText);
}
config = $btn.data('config'),
url = this.getActionURL(action, $(this).data('url')),
data = { records: ids }
;
$msg.html(returnData.message);
if (ids.length <= 0) {
alert(ss.i18n._t('GRIDFIELD_BULK_MANAGER.BULKACTION_EMPTY_SELECT'));
return;
}
if (returnData.isError) {
$msg.addClass('static error');
} else if (returnData.isWarning) {
$msg.addClass('show warning');
} else {
$msg.addClass('show');
}
// if ( $btn.hasClass('ss-ui-action-destructive') )
if (config[action].destructive) {
if (!confirm(ss.i18n._t('GRIDFIELD_BULK_MANAGER.CONFIRM_DESTRUCTIVE_ACTION'))) {
return false;
}
}
bulkTools.gridfieldRefresh($(this).parents('.ss-gridfield'), returnData);
});
} else {
const records = `records[]=${ids.join('&records[]=')}`;
url = `${url}&${records}`;
$btn.addClass('loading');
$msg.removeClass('static show error warning');
window.location.href = url;
}
if (config[action].xhr) {
$.ajax({
url,
data,
type: 'POST',
context: $(this)
}).always(function (data, textStatus, jqXHR) {
$btn.removeClass('loading');
// if request fail, return a +4xx status code, extract json response
if (data.responseText) {
data = JSON.parse(data.responseText);
}
$msg.html(data.message);
if (data.isError) {
$msg.addClass('static error');
} else if (data.isWarning) {
$msg.addClass('show warning');
} else {
$msg.addClass('show');
}
bulkTools.gridfieldRefresh($(this).parents('.ss-gridfield'), data);
});
} else {
const records = `records[]=${ids.join('&records[]=')}`;
url = `${url}&${records}`;
window.location.href = url;
}
}
});
});
});
}(jQuery));
return true;
}
});
});

View File

@ -1,53 +1,49 @@
(function ($) {
$.entwine('colymba', ($) => {
/**
* Toggle all accordion forms
* open or closed
*/
$('#bulkEditToggle').entwine({
onmatch() {},
onunmatch() {},
onclick(e) {
let toggleFields = this.parents('form').find('.ss-toggle .ui-accordion-header'),
state = this.data('state')
;
/* global window */
import jQuery from 'jquery';
if (!state || state === 'close') {
state = 'open';
} else {
state = 'close';
jQuery.entwine('colymba', ($) => {
/**
* Toggle all accordion forms
* open or closed
*/
$('#bulkEditToggle').entwine({
onclick() {
const toggleFields = this.parents('form').find('.ss-toggle .ui-accordion-header');
let state = this.data('state');
if (!state || state === 'close') {
state = 'open';
} else {
state = 'close';
}
toggleFields.each(function () {
const $this = $(this);
if (state === 'open' && !$this.hasClass('ui-state-active')) {
$this.click();
}
toggleFields.each(function () {
const $this = $(this);
if (state === 'open' && !$this.hasClass('ui-state-active')) {
$this.click();
}
if (state === 'close' && $this.hasClass('ui-state-active')) {
$this.click();
}
});
this.data('state', state);
}
});
/**
* Contains each rocrds editing fields,
* tracks changes and updates...
*/
$('.bulkEditingFieldHolder').entwine({
onmatch() {},
onunmatch() {},
onchange() {
this.removeClass('updated');
if (!this.hasClass('hasUpdate')) {
this.addClass('hasUpdate');
if (state === 'close' && $this.hasClass('ui-state-active')) {
$this.click();
}
}
});
});
this.data('state', state);
}
});
}(jQuery));
/**
* Contains each rocrds editing fields,
* tracks changes and updates...
*/
$('.bulkEditingFieldHolder').entwine({
onchange() {
this.removeClass('updated');
if (!this.hasClass('hasUpdate')) {
this.addClass('hasUpdate');
}
}
});
});

View File

@ -1,24 +1,27 @@
/* global window */
/**
* A quick hack to catch the uploadfield add file event
* and send the file ID to the bulkUploader component
* @todo write actions
*/
import jQuery from 'jquery';
import Injector from 'lib/Injector';
const { bulkTools } = window;
const bulkUploadFieldAttach = (payload) => {
const $uploadField = jQuery(`#${payload.fieldId}`);
const $gridfield = $uploadField.parents('.ss-gridfield');
const $gridfield = $uploadField.parents('.ss-gridfield');
const schema = $uploadField.data('schema');
jQuery.ajax(schema.data.attachFileEndpoint.url, {
method: schema.data.attachFileEndpoint.method, // doesn't seem to change anything
data: {
fileID: payload.file.id
}
}).done((data, textStatus, jqXHR) => {
bulkTools.gridfieldRefresh($gridfield, data);
jQuery.ajax(schema.data.attachFileEndpoint.url, {
method: schema.data.attachFileEndpoint.method, // doesn't seem to change anything
data: {
fileID: payload.file.id
}
}).done((data) => {
bulkTools.gridfieldRefresh($gridfield, data);
// bulkTools.removeUploadItem($gridfield, null, payload.file.id);//bad! can't dispath in Reducer
});
});
};
const bulkUploadFieldUpload = (payload) => {
@ -27,12 +30,12 @@ const bulkUploadFieldUpload = (payload) => {
// bulkTools.removeUploadItem($gridfield, payload.queuedId, null);//bad! can't dispath in Reducer
};
const bulkUploadFieldReducer = (originalReducer) => (globalState) => (state, { type, payload }) => {
const bulkUploadFieldReducer = (originalReducer) => () => (state, { type, payload }) => {
switch (type) {
case 'UPLOADFIELD_ADD_FILE': {
// Needs to be a bulk upload field and have a file ID (no file ID = normal)
if (payload.fieldId.indexOf('_BU') !== -1 && payload.file.id) {
bulkUploadFieldAttach(payload);
bulkUploadFieldAttach(payload);
}
return originalReducer(state, { type, payload });
}

View File

@ -1,6 +1,6 @@
@keyframes bt_fade_out {
from {
opacity: 1;
opacity: 1;
}
to {
@ -10,7 +10,7 @@
@keyframes bt_loading {
from {
opacity: 1;
opacity: 1;
}
to {
@ -20,7 +20,7 @@
@keyframes bt_fade_border {
from {
border-left-width: 6px;
border-left-width: 6px;
}
to {
@ -126,7 +126,7 @@
{
&.bt-deleted
{
opacity: .5;
opacity: 0.5;
}
&.bt-failed

View File

@ -33,11 +33,13 @@
},
"homepage": "https://github.com/colymba/GridFieldBulkEditingTools",
"dependencies": {
"babel-polyfill": "6.7.4"
"babel-polyfill": "6.7.4",
"jquery": "^3.2.1"
},
"devDependencies": {
"@silverstripe/eslint-config": "^0.0.2",
"@silverstripe/webpack-config": "^0.7",
"expose-loader": "^0.7.4",
"sass-loader": "^6.0.7"
},
"babel": {