Fix eslint rules

[MAILPOET-1348]
This commit is contained in:
Pavel Dohnal
2018-04-18 12:13:13 +01:00
parent 566db67e8c
commit 867e62d0e8
4 changed files with 76 additions and 99 deletions

View File

@@ -180,15 +180,14 @@ define('notice', ['mailpoet', 'jquery'], function (mp, jQuery) { // eslint-disab
} }
}, },
hide: function hide(all) { hide: function hide(all) {
var id;
if (all !== undefined && all === true) { if (all !== undefined && all === true) {
// all notices // all notices
jQuery('.mailpoet_notice:not([id])').trigger('close'); jQuery('.mailpoet_notice:not([id])').trigger('close');
} else if (all !== undefined && jQuery.isArray(all)) { } else if (all !== undefined && jQuery.isArray(all)) {
// array of ids // array of ids
for (id in all) { Object.keys(all).forEach(function close(id) {
jQuery('[data-id="' + all[id] + '"]').trigger('close'); jQuery('[data-id="' + all[id] + '"]').trigger('close');
} });
} if (all !== undefined) { } if (all !== undefined) {
// single id // single id
jQuery('[data-id="' + all + '"]').trigger('close'); jQuery('[data-id="' + all + '"]').trigger('close');

View File

@@ -2,12 +2,12 @@ define(
[ [
'mailpoet' 'mailpoet'
], ],
function ( // eslint-disable-line func-names function reinstallFromScratch(
MailPoet MailPoet
) { ) {
var element; var element;
function eventHandler() { function eventHandler() {
if (confirm(MailPoet.I18n.t('reinstallConfirmation'))) { if (confirm(MailPoet.I18n.t('reinstallConfirmation'))) { // eslint-disable-line no-alert
MailPoet.trackEvent( MailPoet.trackEvent(
'User has reinstalled MailPoet via Settings', 'User has reinstalled MailPoet via Settings',
{ 'MailPoet Free version': window.mailpoet_version } { 'MailPoet Free version': window.mailpoet_version }
@@ -18,14 +18,14 @@ define(
api_version: window.mailpoet_api_version, api_version: window.mailpoet_api_version,
endpoint: 'setup', endpoint: 'setup',
action: 'reset' action: 'reset'
}).always(function () { // eslint-disable-line func-names }).always(function alwaysCb() {
MailPoet.Modal.loading(false); MailPoet.Modal.loading(false);
}).done(function () { // eslint-disable-line func-names }).done(function doneCb() {
window.location = 'admin.php?page=mailpoet-newsletters'; window.location = 'admin.php?page=mailpoet-newsletters';
}).fail(function (response) { // eslint-disable-line func-names }).fail(function failCb(response) {
if (response.errors.length > 0) { if (response.errors.length > 0) {
MailPoet.Notice.error( MailPoet.Notice.error(
response.errors.map(function (error) { // eslint-disable-line func-names response.errors.map(function responseMapCb(error) {
return error.message; return error.message;
}), }),
{ scroll: true } { scroll: true }

View File

@@ -33,8 +33,7 @@ define(
var disabled = 'button-disabled'; var disabled = 'button-disabled';
if (condition === 'on') { if (condition === 'on') {
nextStepButton.removeClass(disabled); nextStepButton.removeClass(disabled);
} } else {
else {
nextStepButton.addClass(disabled); nextStepButton.addClass(disabled);
} }
} }
@@ -55,12 +54,12 @@ define(
width: '20em', width: '20em',
templateResult: function templateResult(item) { templateResult: function templateResult(item) {
return (item.subscriberCount > 0) return (item.subscriberCount > 0)
? item.name + ' (' + parseInt(item.subscriberCount).toLocaleString() + ')' ? item.name + ' (' + parseInt(item.subscriberCount, 10).toLocaleString() + ')'
: item.name; : item.name;
}, },
templateSelection: function templateSelection(item) { templateSelection: function templateSelection(item) {
return (item.subscriberCount > 0) return (item.subscriberCount > 0)
? item.name + ' (' + parseInt(item.subscriberCount).toLocaleString() + ')' ? item.name + ' (' + parseInt(item.subscriberCount, 10).toLocaleString() + ')'
: item.name; : item.name;
} }
}) })
@@ -94,8 +93,7 @@ define(
(!window.exportData.segments && subscriberFieldsContainerElement.select2('data').length) (!window.exportData.segments && subscriberFieldsContainerElement.select2('data').length)
) { ) {
toggleNextStepButton('on'); toggleNextStepButton('on');
} } else {
else {
toggleNextStepButton('off'); toggleNextStepButton('off');
} }
}); });
@@ -132,7 +130,7 @@ define(
MailPoet.Modal.loading(false); MailPoet.Modal.loading(false);
}).done(function done(response) { }).done(function done(response) {
var resultMessage = MailPoet.I18n.t('exportMessage') var resultMessage = MailPoet.I18n.t('exportMessage')
.replace('%1$s', '<strong>' + parseInt(response.data.totalExported).toLocaleString() + '</strong>') .replace('%1$s', '<strong>' + parseInt(response.data.totalExported, 133).toLocaleString() + '</strong>')
.replace('[link]', '<a href="' + response.data.exportFileURL + '" target="_blank" >') .replace('[link]', '<a href="' + response.data.exportFileURL + '" target="_blank" >')
.replace('[/link]', '</a>'); .replace('[/link]', '</a>');
jQuery('#export_result_notice').html('<p>' + resultMessage + '</p>').show(); jQuery('#export_result_notice').html('<p>' + resultMessage + '</p>').show();

View File

@@ -184,12 +184,10 @@ define(
complete: function (CSV) { complete: function (CSV) {
var email; var email;
var emailAddress; var emailAddress;
var column;
var rowCount;
var rowData; var rowData;
var rowColumnCount; var rowColumnCount;
var errorNotice; var errorNotice;
for (rowCount in CSV.data) { Object.keys(CSV.data).forEach(function csvDataEach(rowCount) {
rowData = CSV.data[rowCount].map(function (el) { rowData = CSV.data[rowCount].map(function (el) {
return el.trim(); return el.trim();
}); });
@@ -207,7 +205,7 @@ define(
// determine position of email address inside an array; this is // determine position of email address inside an array; this is
// done once and then email regex is run just on that element for each row // done once and then email regex is run just on that element for each row
if (emailColumnPosition === null) { if (emailColumnPosition === null) {
for (column in rowData) { Object.keys(rowData).forEach(function rowDataEach(column) {
emailAddress = detectAndCleanupEmail(rowData[column]); emailAddress = detectAndCleanupEmail(rowData[column]);
if (emailColumnPosition === null if (emailColumnPosition === null
&& window.mailpoet_email_regex.test(emailAddress)) { && window.mailpoet_email_regex.test(emailAddress)) {
@@ -217,32 +215,29 @@ define(
rowData[column] = emailAddress; rowData[column] = emailAddress;
processedSubscribers[emailAddress] = rowData; processedSubscribers[emailAddress] = rowData;
} }
} });
if (emailColumnPosition === null if (emailColumnPosition === null
&& advancedOptionHeader && advancedOptionHeader
&& parseInt(rowCount) === 0) { && parseInt(rowCount, 10) === 0) {
isHeaderFound = true; isHeaderFound = true;
processedSubscribers[0] = rowData; processedSubscribers[0] = rowData;
} }
} } else if (rowData[emailColumnPosition] !== '') {
else if (rowData[emailColumnPosition] !== '') {
email = detectAndCleanupEmail(rowData[emailColumnPosition]); email = detectAndCleanupEmail(rowData[emailColumnPosition]);
if (_.has(parsedEmails, email)) { if (_.has(parsedEmails, email)) {
duplicateEmails.push(email); duplicateEmails.push(email);
} } else if (!window.mailpoet_email_regex.test(email)) {
else if (!window.mailpoet_email_regex.test(email)) {
invalidEmails.push(rowData[emailColumnPosition]); invalidEmails.push(rowData[emailColumnPosition]);
} } else {
// if we haven't yet processed this e-mail and it passed // if we haven't yet processed this e-mail and it passed
// the regex test, then process the row // the regex test, then process the row
else {
parsedEmails[email] = true; parsedEmails[email] = true;
rowData[emailColumnPosition] = email; rowData[emailColumnPosition] = email;
processedSubscribers[email] = rowData; processedSubscribers[email] = rowData;
} }
} }
} }
} });
// reindex array to avoid non-numeric indices // reindex array to avoid non-numeric indices
processedSubscribers = _.values(processedSubscribers); processedSubscribers = _.values(processedSubscribers);
// if the header options is set, there should be at least // if the header options is set, there should be at least
@@ -271,8 +266,7 @@ define(
'MailPoet Free version': window.mailpoet_version 'MailPoet Free version': window.mailpoet_version
}); });
router.navigate('step2', { trigger: true }); router.navigate('step2', { trigger: true });
} } else {
else {
MailPoet.Modal.loading(false); MailPoet.Modal.loading(false);
errorNotice = MailPoet.I18n.t('noValidRecords'); errorNotice = MailPoet.I18n.t('noValidRecords');
errorNotice = errorNotice.replace('[link]', MailPoet.I18n.t('csvKBLink')); errorNotice = errorNotice.replace('[link]', MailPoet.I18n.t('csvKBLink'));
@@ -288,8 +282,7 @@ define(
if (listSelectElement.data('select2')) { if (listSelectElement.data('select2')) {
listSelectElement.select2('data', data); listSelectElement.select2('data', data);
listSelectElement.trigger('change'); listSelectElement.trigger('change');
} } else {
else {
listSelectElement listSelectElement
.select2({ .select2({
data: data, data: data,
@@ -304,8 +297,7 @@ define(
.change(function () { .change(function () {
if (jQuery(this).val() !== null) { if (jQuery(this).val() !== null) {
toggleNextStepButton(mailChimpProcessButtonElement, 'on'); toggleNextStepButton(mailChimpProcessButtonElement, 'on');
} } else {
else {
toggleNextStepButton(mailChimpProcessButtonElement, 'off'); toggleNextStepButton(mailChimpProcessButtonElement, 'off');
} }
}) })
@@ -474,7 +466,6 @@ define(
var fillerPosition; var fillerPosition;
var importResults; var importResults;
var duplicates; var duplicates;
var email;
if (typeof (window.importData.step1) === 'undefined') { if (typeof (window.importData.step1) === 'undefined') {
router.navigate('step1', { trigger: true }); router.navigate('step1', { trigger: true });
return; return;
@@ -521,14 +512,13 @@ define(
duplicates[subscriberEmail] = (duplicates[subscriberEmail] || 0) + 1; duplicates[subscriberEmail] = (duplicates[subscriberEmail] || 0) + 1;
}); });
subscribers.duplicate = []; subscribers.duplicate = [];
for (email in duplicates) { Object.keys(duplicates).forEach(function emailDuplicates(email) {
if (duplicates[email] > 1) { if (duplicates[email] > 1) {
subscribers.duplicate.push(email + ' (x' + duplicates[email] + ')'); subscribers.duplicate.push(email + ' (x' + duplicates[email] + ')');
} } else {
else {
subscribers.duplicate.push(email); subscribers.duplicate.push(email);
} }
} });
importResults = { importResults = {
notice: MailPoet.I18n.t('importNoticeSkipped').replace( notice: MailPoet.I18n.t('importNoticeSkipped').replace(
@@ -564,8 +554,7 @@ define(
// show available segments // show available segments
if (window.mailpoetSegments.length) { if (window.mailpoetSegments.length) {
jQuery('.mailpoet_segments').show(); jQuery('.mailpoet_segments').show();
} } else {
else {
jQuery('.mailpoet_no_segments').show(); jQuery('.mailpoet_no_segments').show();
} }
@@ -681,46 +670,46 @@ define(
function (helperSubscribers, options) { function (helperSubscribers, options) {
var displayedColumns = []; var displayedColumns = [];
var displayedColumnsIds = []; var displayedColumnsIds = [];
var i;
var columnData; var columnData;
var columnId; var columnId;
var headerName; var headerName;
var headerNameMatch; var headerNameMatch;
// go through all elements of the first row in subscribers data // go through all elements of the first row in subscribers data
for (i in helperSubscribers.subscribers[0]) { Object
columnData = helperSubscribers.subscribers[0][i]; .keys(helperSubscribers.subscribers[0])
columnId = 'ignore'; // set default column type .forEach(function helperSubscribersLoop(i) {
// if the column is not undefined and has a valid e-mail, set type as email columnData = helperSubscribers.subscribers[0][i];
if (columnData % 1 !== 0 && window.mailpoet_email_regex.test(columnData)) { columnId = 'ignore'; // set default column type
columnId = 'email'; // if the column is not undefined and has a valid e-mail, set type as email
} else if (helperSubscribers.header) { if (columnData % 1 !== 0 && window.mailpoet_email_regex.test(columnData)) {
headerName = helperSubscribers.header[i]; columnId = 'email';
headerNameMatch = window.mailpoetColumns.map(function (el) { } else if (helperSubscribers.header) {
return el.name; headerName = helperSubscribers.header[i];
}).indexOf(headerName); headerNameMatch = window.mailpoetColumns.map(function (el) {
// set column type using header return el.name;
if (headerNameMatch !== -1) { }).indexOf(headerName);
columnId = window.mailpoetColumns[headerNameMatch].id; // set column type using header
}// set column type using header name if (headerNameMatch !== -1) {
else if (headerName) { columnId = window.mailpoetColumns[headerNameMatch].id;
if (/first|first name|given name/i.test(headerName)) { } else if (headerName) { // set column type using header name
columnId = 'first_name'; if (/first|first name|given name/i.test(headerName)) {
} else if (/last|last name/i.test(headerName)) { columnId = 'first_name';
columnId = 'last_name'; } else if (/last|last name/i.test(headerName)) {
columnId = 'last_name';
}
} }
} }
} // make sure the column id has not been previously selected
// make sure the column id has not been previously selected // (e.g., subscriber_first_name shouldn't be autodetected twice),
// (e.g., subscriber_first_name shouldn't be autodetected twice), // except for "ignore"
// except for "ignore" columnId =
columnId = (columnId !== 'ignore'
(columnId !== 'ignore' && displayedColumnsIds.indexOf(columnId) === -1)
&& displayedColumnsIds.indexOf(columnId) === -1) ? columnId
? columnId : 'ignore';
: 'ignore'; displayedColumns[i] = { column_id: columnId };
displayedColumns[i] = { column_id: columnId }; displayedColumnsIds.push(columnId);
displayedColumnsIds.push(columnId); });
}
return options.fn(displayedColumns); return options.fn(displayedColumns);
}); });
@@ -733,15 +722,14 @@ define(
// start array index from 1 // start array index from 1
Handlebars.registerHelper('calculate_index', function (rawIndex) { Handlebars.registerHelper('calculate_index', function (rawIndex) {
var index = parseInt(rawIndex); var index = parseInt(rawIndex, 10);
// display filler data (e.g., ellipsis) if we've reached the maximum number of rows and // display filler data (e.g., ellipsis) if we've reached the maximum number of rows and
// subscribers count is greater than the maximum number of rows we're displaying // subscribers count is greater than the maximum number of rows we're displaying
if (index === maxRowsToShow && subscribers.subscribersCount > (maxRowsToShow + 1)) { if (index === maxRowsToShow && subscribers.subscribersCount > (maxRowsToShow + 1)) {
fillerPosition = index; fillerPosition = index;
return filler; return filler;
} } else if (index === (subscribers.subscribers.length - 1)) {
// if we're on the last line, show the total count of subscribers data // if we're on the last line, show the total count of subscribers data
else if (index === (subscribers.subscribers.length - 1)) {
return subscribers.subscribersCount.toLocaleString(); return subscribers.subscribersCount.toLocaleString();
} }
return index + 1; return index + 1;
@@ -781,7 +769,6 @@ define(
var firstRowData; var firstRowData;
var validationRule; var validationRule;
var testedFormat; var testedFormat;
var format;
var allowedDateFormats; var allowedDateFormats;
// check if the column id matches the selected id of one of the // check if the column id matches the selected id of one of the
// subscriber's data columns // subscriber's data columns
@@ -804,8 +791,7 @@ define(
id: 'invalidEmail' id: 'invalidEmail'
}); });
} }
} } else {
else {
MailPoet.Notice.hide('invalidEmail'); MailPoet.Notice.hide('invalidEmail');
} }
} }
@@ -832,9 +818,8 @@ define(
+ MailPoet.I18n.t('emptyFirstRowDate') + MailPoet.I18n.t('emptyFirstRowDate')
+ '</span> '; + '</span> ';
preventNextStep = true; preventNextStep = true;
} } else {
else { Object.keys(allowedDateFormats).forEach(function allowedDateFormatsLoop(format) {
for (format in allowedDateFormats) {
testedFormat = allowedDateFormats[format]; testedFormat = allowedDateFormats[format];
if (Moment(firstRowData, testedFormat, true).isValid()) { if (Moment(firstRowData, testedFormat, true).isValid()) {
validationRule = (typeof (testedFormat) === 'function') ? validationRule = (typeof (testedFormat) === 'function') ?
@@ -842,12 +827,12 @@ define(
testedFormat; testedFormat;
// set validation on the column element // set validation on the column element
jQuery(matchedColumn.element).data('validation-rule', validationRule); jQuery(matchedColumn.element).data('validation-rule', validationRule);
break; return;
} }
if (validationRule === 'datetime') { if (validationRule === 'datetime') {
validationRule = Moment.ISO_8601; validationRule = Moment.ISO_8601;
} }
} });
} }
jQuery.map(subscribersClone.subscribers, function (dataSubscribers, index) { jQuery.map(subscribersClone.subscribers, function (dataSubscribers, index) {
var data = dataSubscribers; var data = dataSubscribers;
@@ -863,8 +848,7 @@ define(
+ MailPoet.Date.format(date) + MailPoet.Date.format(date)
+ '</span> ' + '</span> '
); );
} } else {
else {
data[matchedColumn.index] = new Handlebars.SafeString( data[matchedColumn.index] = new Handlebars.SafeString(
Handlebars.Utils.escapeExpression(data[matchedColumn.index]) Handlebars.Utils.escapeExpression(data[matchedColumn.index])
+ '<span class="mailpoet_data_match mailpoet_import_error" title="' + '<span class="mailpoet_data_match mailpoet_import_error" title="'
@@ -891,8 +875,7 @@ define(
if (preventNextStep) { if (preventNextStep) {
toggleNextStepButton('off'); toggleNextStepButton('off');
} } else if (!jQuery('.mailpoet_notice.error:visible').length
else if (!jQuery('.mailpoet_notice.error:visible').length
&& segmentSelectElement.val()) { && segmentSelectElement.val()) {
toggleNextStepButton('on'); toggleNextStepButton('on');
} }
@@ -982,9 +965,8 @@ define(
}); });
return false; return false;
}); });
} } else {
// CHANGE COLUMN // CHANGE COLUMN
else {
// check for duplicate values in all select options // check for duplicate values in all select options
jQuery('select.mailpoet_subscribers_column_data_match') jQuery('select.mailpoet_subscribers_column_data_match')
.each(function () { .each(function () {
@@ -994,10 +976,9 @@ define(
// prompt user // prompt user
if (elementId === selectedOptionId if (elementId === selectedOptionId
&& elementId !== 'ignore') { && elementId !== 'ignore') {
if (confirm(MailPoet.I18n.t('selectedValueAlreadyMatched') + ' ' + MailPoet.I18n.t('confirmCorrespondingColumn'))) { if (confirm(MailPoet.I18n.t('selectedValueAlreadyMatched') + ' ' + MailPoet.I18n.t('confirmCorrespondingColumn'))) { // eslint-disable-line no-alert
jQuery(element).data('column-id', 'ignore'); jQuery(element).data('column-id', 'ignore');
} } else {
else {
selectEvent.preventDefault(); selectEvent.preventDefault();
jQuery(selectElement).select2('close'); jQuery(selectElement).select2('close');
} }
@@ -1101,8 +1082,7 @@ define(
) { ) {
MailPoet.Notice.error(_.flatten(clickImportResults.errors) MailPoet.Notice.error(_.flatten(clickImportResults.errors)
); );
} } else {
else {
window.mailpoetSegments = clickImportResults.segments; window.mailpoetSegments = clickImportResults.segments;
clickImportResults.segments = _.map(segmentSelectElement.select2('data'), clickImportResults.segments = _.map(segmentSelectElement.select2('data'),
function (data) { function (data) {