Commit f1e33ca3 authored by webchick's avatar webchick

Issue #2925064 by drpal, dawehner, droplet, xjm, webchick, justafish: [1/2] JS...

Issue #2925064 by drpal, dawehner, droplet, xjm, webchick, justafish: [1/2] JS codestyle: no-restricted-syntax
parent 9d552cad
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
"rules": { "rules": {
"no-use-before-define": "off", "no-use-before-define": "off",
"no-shadow": "off", "no-shadow": "off",
"no-restricted-syntax": "off",
"no-new": "off", "no-new": "off",
"no-continue": "off", "no-continue": "off",
"new-cap": "off", "new-cap": "off",
......
...@@ -40,11 +40,7 @@ ...@@ -40,11 +40,7 @@
} }
// Load all Ajax behaviors specified in the settings. // Load all Ajax behaviors specified in the settings.
for (const base in settings.ajax) { Object.keys(settings.ajax || {}).forEach(base => loadAjaxBehavior(base));
if (settings.ajax.hasOwnProperty(base)) {
loadAjaxBehavior(base);
}
}
Drupal.ajax.bindAjaxLinks(document.body); Drupal.ajax.bindAjaxLinks(document.body);
...@@ -877,14 +873,14 @@ ...@@ -877,14 +873,14 @@
// Track if any command is altering the focus so we can avoid changing the // Track if any command is altering the focus so we can avoid changing the
// focus set by the Ajax command. // focus set by the Ajax command.
let focusChanged = false; let focusChanged = false;
for (const i in response) { Object.keys(response || {}).forEach((i) => {
if (response.hasOwnProperty(i) && response[i].command && this.commands[response[i].command]) { if (response[i].command && this.commands[response[i].command]) {
this.commands[response[i].command](this, response[i], status); this.commands[response[i].command](this, response[i], status);
if (response[i].command === 'invoke' && response[i].method === 'focus') { if (response[i].command === 'invoke' && response[i].method === 'focus') {
focusChanged = true; focusChanged = true;
} }
} }
} });
// If the focus hasn't be changed by the ajax commands, try to refocus the // If the focus hasn't be changed by the ajax commands, try to refocus the
// triggering element or one of its parents if that element does not exist // triggering element or one of its parents if that element does not exist
......
...@@ -21,11 +21,9 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr ...@@ -21,11 +21,9 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
}); });
} }
for (var base in settings.ajax) { Object.keys(settings.ajax || {}).forEach(function (base) {
if (settings.ajax.hasOwnProperty(base)) { return loadAjaxBehavior(base);
loadAjaxBehavior(base); });
}
}
Drupal.ajax.bindAjaxLinks(document.body); Drupal.ajax.bindAjaxLinks(document.body);
...@@ -397,6 +395,8 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr ...@@ -397,6 +395,8 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
}; };
Drupal.Ajax.prototype.success = function (response, status) { Drupal.Ajax.prototype.success = function (response, status) {
var _this = this;
if (this.progress.element) { if (this.progress.element) {
$(this.progress.element).remove(); $(this.progress.element).remove();
} }
...@@ -408,14 +408,14 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr ...@@ -408,14 +408,14 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
var elementParents = $(this.element).parents('[data-drupal-selector]').addBack().toArray(); var elementParents = $(this.element).parents('[data-drupal-selector]').addBack().toArray();
var focusChanged = false; var focusChanged = false;
for (var i in response) { Object.keys(response || {}).forEach(function (i) {
if (response.hasOwnProperty(i) && response[i].command && this.commands[response[i].command]) { if (response[i].command && _this.commands[response[i].command]) {
this.commands[response[i].command](this, response[i], status); _this.commands[response[i].command](_this, response[i], status);
if (response[i].command === 'invoke' && response[i].method === 'focus') { if (response[i].command === 'invoke' && response[i].method === 'focus') {
focusChanged = true; focusChanged = true;
} }
} }
} });
if (!focusChanged && this.element && !$(this.element).data('disable-refocus')) { if (!focusChanged && this.element && !$(this.element).data('disable-refocus')) {
var target = false; var target = false;
......
...@@ -152,8 +152,8 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -152,8 +152,8 @@ window.Drupal = { behaviors: {}, locale: {} };
settings = settings || drupalSettings; settings = settings || drupalSettings;
const behaviors = Drupal.behaviors; const behaviors = Drupal.behaviors;
// Execute all of them. // Execute all of them.
for (const i in behaviors) { Object.keys(behaviors || {}).forEach((i) => {
if (behaviors.hasOwnProperty(i) && typeof behaviors[i].attach === 'function') { if (typeof behaviors[i].attach === 'function') {
// Don't stop the execution of behaviors in case of an error. // Don't stop the execution of behaviors in case of an error.
try { try {
behaviors[i].attach(context, settings); behaviors[i].attach(context, settings);
...@@ -162,7 +162,7 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -162,7 +162,7 @@ window.Drupal = { behaviors: {}, locale: {} };
Drupal.throwError(e); Drupal.throwError(e);
} }
} }
} });
}; };
/** /**
...@@ -212,8 +212,8 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -212,8 +212,8 @@ window.Drupal = { behaviors: {}, locale: {} };
trigger = trigger || 'unload'; trigger = trigger || 'unload';
const behaviors = Drupal.behaviors; const behaviors = Drupal.behaviors;
// Execute all of them. // Execute all of them.
for (const i in behaviors) { Object.keys(behaviors || {}).forEach((i) => {
if (behaviors.hasOwnProperty(i) && typeof behaviors[i].detach === 'function') { if (typeof behaviors[i].detach === 'function') {
// Don't stop the execution of behaviors in case of an error. // Don't stop the execution of behaviors in case of an error.
try { try {
behaviors[i].detach(context, settings, trigger); behaviors[i].detach(context, settings, trigger);
...@@ -222,7 +222,7 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -222,7 +222,7 @@ window.Drupal = { behaviors: {}, locale: {} };
Drupal.throwError(e); Drupal.throwError(e);
} }
} }
} });
}; };
/** /**
...@@ -269,26 +269,24 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -269,26 +269,24 @@ window.Drupal = { behaviors: {}, locale: {} };
// Keep args intact. // Keep args intact.
const processedArgs = {}; const processedArgs = {};
// Transform arguments before inserting them. // Transform arguments before inserting them.
for (const key in args) { Object.keys(args || {}).forEach((key) => {
if (args.hasOwnProperty(key)) { switch (key.charAt(0)) {
switch (key.charAt(0)) { // Escaped only.
// Escaped only. case '@':
case '@': processedArgs[key] = Drupal.checkPlain(args[key]);
processedArgs[key] = Drupal.checkPlain(args[key]); break;
break;
// Pass-through.
// Pass-through. case '!':
case '!': processedArgs[key] = args[key];
processedArgs[key] = args[key]; break;
break;
// Escaped and placeholder.
// Escaped and placeholder. default:
default: processedArgs[key] = Drupal.theme('placeholder', args[key]);
processedArgs[key] = Drupal.theme('placeholder', args[key]); break;
break;
}
} }
} });
return Drupal.stringReplace(str, processedArgs, null); return Drupal.stringReplace(str, processedArgs, null);
}; };
...@@ -316,12 +314,7 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -316,12 +314,7 @@ window.Drupal = { behaviors: {}, locale: {} };
// If the array of keys is not passed then collect the keys from the args. // If the array of keys is not passed then collect the keys from the args.
if (!Array.isArray(keys)) { if (!Array.isArray(keys)) {
keys = []; keys = Object.keys(args || {});
for (const k in args) {
if (args.hasOwnProperty(k)) {
keys.push(k);
}
}
// Order the keys by the character length. The shortest one is the first. // Order the keys by the character length. The shortest one is the first.
keys.sort((a, b) => a.length - b.length); keys.sort((a, b) => a.length - b.length);
......
...@@ -19,15 +19,15 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -19,15 +19,15 @@ window.Drupal = { behaviors: {}, locale: {} };
settings = settings || drupalSettings; settings = settings || drupalSettings;
var behaviors = Drupal.behaviors; var behaviors = Drupal.behaviors;
for (var i in behaviors) { Object.keys(behaviors || {}).forEach(function (i) {
if (behaviors.hasOwnProperty(i) && typeof behaviors[i].attach === 'function') { if (typeof behaviors[i].attach === 'function') {
try { try {
behaviors[i].attach(context, settings); behaviors[i].attach(context, settings);
} catch (e) { } catch (e) {
Drupal.throwError(e); Drupal.throwError(e);
} }
} }
} });
}; };
Drupal.detachBehaviors = function (context, settings, trigger) { Drupal.detachBehaviors = function (context, settings, trigger) {
...@@ -36,15 +36,15 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -36,15 +36,15 @@ window.Drupal = { behaviors: {}, locale: {} };
trigger = trigger || 'unload'; trigger = trigger || 'unload';
var behaviors = Drupal.behaviors; var behaviors = Drupal.behaviors;
for (var i in behaviors) { Object.keys(behaviors || {}).forEach(function (i) {
if (behaviors.hasOwnProperty(i) && typeof behaviors[i].detach === 'function') { if (typeof behaviors[i].detach === 'function') {
try { try {
behaviors[i].detach(context, settings, trigger); behaviors[i].detach(context, settings, trigger);
} catch (e) { } catch (e) {
Drupal.throwError(e); Drupal.throwError(e);
} }
} }
} });
}; };
Drupal.checkPlain = function (str) { Drupal.checkPlain = function (str) {
...@@ -55,23 +55,21 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -55,23 +55,21 @@ window.Drupal = { behaviors: {}, locale: {} };
Drupal.formatString = function (str, args) { Drupal.formatString = function (str, args) {
var processedArgs = {}; var processedArgs = {};
for (var key in args) { Object.keys(args || {}).forEach(function (key) {
if (args.hasOwnProperty(key)) { switch (key.charAt(0)) {
switch (key.charAt(0)) { case '@':
case '@': processedArgs[key] = Drupal.checkPlain(args[key]);
processedArgs[key] = Drupal.checkPlain(args[key]); break;
break;
case '!': case '!':
processedArgs[key] = args[key]; processedArgs[key] = args[key];
break; break;
default: default:
processedArgs[key] = Drupal.theme('placeholder', args[key]); processedArgs[key] = Drupal.theme('placeholder', args[key]);
break; break;
}
} }
} });
return Drupal.stringReplace(str, processedArgs, null); return Drupal.stringReplace(str, processedArgs, null);
}; };
...@@ -82,12 +80,7 @@ window.Drupal = { behaviors: {}, locale: {} }; ...@@ -82,12 +80,7 @@ window.Drupal = { behaviors: {}, locale: {} };
} }
if (!Array.isArray(keys)) { if (!Array.isArray(keys)) {
keys = []; keys = Object.keys(args || {});
for (var k in args) {
if (args.hasOwnProperty(k)) {
keys.push(k);
}
}
keys.sort(function (a, b) { keys.sort(function (a, b) {
return a.length - b.length; return a.length - b.length;
......
...@@ -33,20 +33,16 @@ ...@@ -33,20 +33,16 @@
Drupal.behaviors.states = { Drupal.behaviors.states = {
attach(context, settings) { attach(context, settings) {
const $states = $(context).find('[data-drupal-states]'); const $states = $(context).find('[data-drupal-states]');
let config;
let state;
const il = $states.length; const il = $states.length;
for (let i = 0; i < il; i++) { for (let i = 0; i < il; i++) {
config = JSON.parse($states[i].getAttribute('data-drupal-states')); const config = JSON.parse($states[i].getAttribute('data-drupal-states'));
for (state in config) { Object.keys(config || {}).forEach((state) => {
if (config.hasOwnProperty(state)) { new states.Dependent({
new states.Dependent({ element: $($states[i]),
element: $($states[i]), state: states.State.sanitize(state),
state: states.State.sanitize(state), constraints: config[state],
constraints: config[state], });
}); });
}
}
} }
// Execute all postponed functions now. // Execute all postponed functions now.
...@@ -76,11 +72,9 @@ ...@@ -76,11 +72,9 @@
$.extend(this, { values: {}, oldValue: null }, args); $.extend(this, { values: {}, oldValue: null }, args);
this.dependees = this.getDependees(); this.dependees = this.getDependees();
for (const selector in this.dependees) { Object.keys(this.dependees || {}).forEach((selector) => {
if (this.dependees.hasOwnProperty(selector)) { this.initializeDependee(selector, this.dependees[selector]);
this.initializeDependee(selector, this.dependees[selector]); });
}
}
}; };
/** /**
...@@ -136,6 +130,7 @@ ...@@ -136,6 +130,7 @@
// Cache for the states of this dependee. // Cache for the states of this dependee.
this.values[selector] = {}; this.values[selector] = {};
// eslint-disable-next-line no-restricted-syntax
for (const i in dependeeStates) { for (const i in dependeeStates) {
if (dependeeStates.hasOwnProperty(i)) { if (dependeeStates.hasOwnProperty(i)) {
state = dependeeStates[i]; state = dependeeStates[i];
...@@ -267,6 +262,7 @@ ...@@ -267,6 +262,7 @@
// bogus, we don't want to end up with an infinite loop. // bogus, we don't want to end up with an infinite loop.
else if ($.isPlainObject(constraints)) { else if ($.isPlainObject(constraints)) {
// This constraint is an object (AND). // This constraint is an object (AND).
// eslint-disable-next-line no-restricted-syntax
for (const n in constraints) { for (const n in constraints) {
if (constraints.hasOwnProperty(n)) { if (constraints.hasOwnProperty(n)) {
result = ternary(result, this.checkConstraints(constraints[n], selector, n)); result = ternary(result, this.checkConstraints(constraints[n], selector, n));
...@@ -391,11 +387,9 @@ ...@@ -391,11 +387,9 @@
trigger.call(window, this.element); trigger.call(window, this.element);
} }
else { else {
for (const event in trigger) { Object.keys(trigger || {}).forEach((event) => {
if (trigger.hasOwnProperty(event)) { this.defaultTrigger(event, trigger[event]);
this.defaultTrigger(event, trigger[event]); });
}
}
} }
// Mark this trigger as initialized for this element. // Mark this trigger as initialized for this element.
......
...@@ -15,20 +15,21 @@ ...@@ -15,20 +15,21 @@
Drupal.behaviors.states = { Drupal.behaviors.states = {
attach: function attach(context, settings) { attach: function attach(context, settings) {
var $states = $(context).find('[data-drupal-states]'); var $states = $(context).find('[data-drupal-states]');
var config = void 0;
var state = void 0;
var il = $states.length; var il = $states.length;
var _loop = function _loop(i) {
var config = JSON.parse($states[i].getAttribute('data-drupal-states'));
Object.keys(config || {}).forEach(function (state) {
new states.Dependent({
element: $($states[i]),
state: states.State.sanitize(state),
constraints: config[state]
});
});
};
for (var i = 0; i < il; i++) { for (var i = 0; i < il; i++) {
config = JSON.parse($states[i].getAttribute('data-drupal-states')); _loop(i);
for (state in config) {
if (config.hasOwnProperty(state)) {
new states.Dependent({
element: $($states[i]),
state: states.State.sanitize(state),
constraints: config[state]
});
}
}
} }
while (states.postponed.length) { while (states.postponed.length) {
...@@ -38,14 +39,14 @@ ...@@ -38,14 +39,14 @@
}; };
states.Dependent = function (args) { states.Dependent = function (args) {
var _this = this;
$.extend(this, { values: {}, oldValue: null }, args); $.extend(this, { values: {}, oldValue: null }, args);
this.dependees = this.getDependees(); this.dependees = this.getDependees();
for (var selector in this.dependees) { Object.keys(this.dependees || {}).forEach(function (selector) {
if (this.dependees.hasOwnProperty(selector)) { _this.initializeDependee(selector, _this.dependees[selector]);
this.initializeDependee(selector, this.dependees[selector]); });
}
}
}; };
states.Dependent.comparisons = { states.Dependent.comparisons = {
...@@ -187,16 +188,16 @@ ...@@ -187,16 +188,16 @@
states.Trigger.prototype = { states.Trigger.prototype = {
initialize: function initialize() { initialize: function initialize() {
var _this2 = this;
var trigger = states.Trigger.states[this.state]; var trigger = states.Trigger.states[this.state];
if (typeof trigger === 'function') { if (typeof trigger === 'function') {
trigger.call(window, this.element); trigger.call(window, this.element);
} else { } else {
for (var event in trigger) { Object.keys(trigger || {}).forEach(function (event) {
if (trigger.hasOwnProperty(event)) { _this2.defaultTrigger(event, trigger[event]);
this.defaultTrigger(event, trigger[event]); });
}
}
} }
this.element.data('trigger:' + this.state, true); this.element.data('trigger:' + this.state, true);
......
...@@ -41,11 +41,9 @@ ...@@ -41,11 +41,9 @@
} }
} }
for (const base in settings.tableDrag) { Object.keys(settings.tableDrag || {}).forEach((base) => {
if (settings.tableDrag.hasOwnProperty(base)) { initTableDrag($(context).find(`#${base}`).once('tabledrag'), base);
initTableDrag($(context).find(`#${base}`).once('tabledrag'), base); });
}
}
}, },
}; };
...@@ -172,20 +170,16 @@ ...@@ -172,20 +170,16 @@
* @type {bool} * @type {bool}
*/ */
this.indentEnabled = false; this.indentEnabled = false;
for (const group in tableSettings) { Object.keys(tableSettings || {}).forEach((group) => {
if (tableSettings.hasOwnProperty(group)) { Object.keys(tableSettings[group] || {}).forEach((n) => {
for (const n in tableSettings[group]) { if (tableSettings[group][n].relationship === 'parent') {
if (tableSettings[group].hasOwnProperty(n)) { this.indentEnabled = true;
if (tableSettings[group][n].relationship === 'parent') {
this.indentEnabled = true;
}
if (tableSettings[group][n].limit > 0) {
this.maxDepth = tableSettings[group][n].limit;
}
}
} }
} if (tableSettings[group][n].limit > 0) {
} this.maxDepth = tableSettings[group][n].limit;
}
});
});
if (this.indentEnabled) { if (this.indentEnabled) {
/** /**
* Total width of indents, set in makeDraggable. * Total width of indents, set in makeDraggable.
...@@ -264,30 +258,29 @@ ...@@ -264,30 +258,29 @@
let hidden; let hidden;
let cell; let cell;
let columnIndex; let columnIndex;
for (const group in this.tableSettings) { Object.keys(this.tableSettings || {}).forEach((group) => {
if (this.tableSettings.hasOwnProperty(group)) { // Find the first field in this group.
// Find the first field in this group. // eslint-disable-next-line no-restricted-syntax
for (const d in this.tableSettings[group]) { for (const d in this.tableSettings[group]) {
if (this.tableSettings[group].hasOwnProperty(d)) { if (this.tableSettings[group].hasOwnProperty(d)) {
const field = $table.find(`.${this.tableSettings[group][d].target}`).eq(0); const field = $table.find(`.${this.tableSettings[group][d].target}`).eq(0);
if (field.length && this.tableSettings[group][d].hidden) { if (field.length && this.tableSettings[group][d].hidden) {
hidden = this.tableSettings[group][d].hidden; hidden = this.tableSettings[group][d].hidden;
cell = field.closest('td'); cell = field.closest('td');
break; break;
}
} }
} }
}
// Mark the column containing this field so it can be hidden. // Mark the column containing this field so it can be hidden.
if (hidden && cell[0]) { if (hidden && cell[0]) {
// Add 1 to our indexes. The nth-child selector is 1 based, not 0 // Add 1 to our indexes. The nth-child selector is 1 based, not 0
// based. Match immediate children of the parent element to allow // based. Match immediate children of the parent element to allow
// nesting. // nesting.
columnIndex = cell.parent().find('> td').index(cell.get(0)) + 1; columnIndex = cell.parent().find('> td').index(cell.get(0)) + 1;
$table.find('> thead > tr, > tbody > tr, > tr').each(this.addColspanClass(columnIndex)); $table.find('> thead > tr, > tbody > tr, > tr').each(this.addColspanClass(columnIndex));
}
} }
} });
this.displayColumns(showWeight); this.displayColumns(showWeight);
}; };
...@@ -419,12 +412,14 @@ ...@@ -419,12 +412,14 @@