Merge branch 'bugfix/js-don-t-use-var-self-this-10703'

fixes #10703
This commit is contained in:
Eric Lippmann 2016-09-09 13:26:18 +02:00
commit 972358532d
13 changed files with 119 additions and 120 deletions

View File

@ -69,10 +69,10 @@
*/
this.modules = {};
var self = this;
var _this = this;
$(document).ready(function () {
self.initialize();
self = null;
_this.initialize();
_this = null;
});
};
@ -94,9 +94,9 @@
this.loader = new Icinga.Loader(this);
this.events = new Icinga.Events(this);
this.history = new Icinga.History(this);
var self = this;
var _this = this;
$.each(Icinga.Behaviors, function(name, Behavior) {
self.behaviors[name.toLowerCase()] = new Behavior(self);
_this.behaviors[name.toLowerCase()] = new Behavior(_this);
});
this.timezone.initialize();

View File

@ -185,12 +185,12 @@
filter.addClass('active');
return;
}
var self = this;
var _this = this;
this.rowActions()
.filter(
function (i, el) {
var params = self.getRowData($(el));
if (self.icinga.utils.objectKeys(params).length !== self.icinga.utils.objectKeys(filter).length) {
var params = _this.getRowData($(el));
if (_this.icinga.utils.objectKeys(params).length !== _this.icinga.utils.objectKeys(filter).length) {
return false;
}
var equal = true;
@ -274,21 +274,21 @@
* @returns {String} The filter string
*/
toQuery: function() {
var self = this;
var _this = this;
var selections = this.selections();
var queries = [];
var utils = this.icinga.utils;
if (selections.length === 1) {
return $(selections[0]).attr('href');
} else if (selections.length > 1 && self.hasMultiselection()) {
} else if (selections.length > 1 && _this.hasMultiselection()) {
selections.each(function (i, el) {
var parts = [];
$.each(self.getRowData($(el)), function(key, value) {
$.each(_this.getRowData($(el)), function(key, value) {
parts.push(utils.fixedEncodeURIComponent(key) + '=' + utils.fixedEncodeURIComponent(value));
});
queries.push('(' + parts.join('&') + ')');
});
return self.getMultiselectionUrl() + '?(' + queries.join('|') + ')';
return _this.getMultiselectionUrl() + '?(' + queries.join('|') + ')';
} else {
return '';
}
@ -304,9 +304,9 @@
var query = parseSelectionQuery(hash);
if (query.length > 1 && this.hasMultiselectionUrl(this.icinga.utils.parseUrl(hash).path)) {
// select all rows with matching filters
var self = this;
var _this = this;
$.each(query, function(i, selection) {
self.select(selection);
_this.select(selection);
});
}
if (query.length > 1) {
@ -351,10 +351,10 @@
* Handle clicks on table rows and update selection and history
*/
ActionTable.prototype.onRowClicked = function (event) {
var self = event.data.self;
var _this = event.data.self;
var $target = $(event.target);
var $tr = $target.closest('tr');
var table = new Selection($tr.closest('table.action, table.table-row-selectable')[0], self.icinga);
var table = new Selection($tr.closest('table.action, table.table-row-selectable')[0], _this.icinga);
// some rows may contain form actions that trigger a different action, pass those through
if (!$target.hasClass('rowaction') && $target.closest('form').length &&
@ -390,18 +390,18 @@
var count = table.selections().length;
if (count > 0) {
var query = table.toQuery();
self.icinga.loader.loadUrl(query, self.icinga.events.getLinkTargetFor($tr));
_this.icinga.loader.loadUrl(query, _this.icinga.events.getLinkTargetFor($tr));
state += '#!' + query;
} else {
if (self.icinga.events.getLinkTargetFor($tr).attr('id') === 'col2') {
self.icinga.ui.layout1col();
if (_this.icinga.events.getLinkTargetFor($tr).attr('id') === 'col2') {
_this.icinga.ui.layout1col();
}
}
self.icinga.history.pushUrl(state);
_this.icinga.history.pushUrl(state);
// redraw all table selections
self.tables().each(function () {
new Selection(this, self.icinga).refresh();
_this.tables().each(function () {
new Selection(this, _this.icinga).refresh();
});
// update selection info
@ -414,7 +414,7 @@
*/
ActionTable.prototype.onRendered = function(evt) {
var container = evt.target;
var self = evt.data.self;
var _this = evt.data.self;
// initialize all rows with the correct row action
$('table.action tr, table.table-row-selectable tr', container).each(function(idx, el) {
@ -442,19 +442,19 @@
});
// draw all active selections that have disappeared on reload
self.tables().each(function(i, el) {
new Selection(el, self.icinga).refresh();
_this.tables().each(function(i, el) {
new Selection(el, _this.icinga).refresh();
});
// update displayed selection counter
var table = new Selection(self.tables(container).first());
var table = new Selection(_this.tables(container).first());
$(container).find('.selection-info-count').text(table.selections().size());
};
ActionTable.prototype.clearAll = function () {
var self = this;
var _this = this;
this.tables().each(function () {
new Selection(this, self.icinga).clear();
new Selection(this, _this.icinga).clear();
});
};

View File

@ -71,15 +71,15 @@
var origFocus = document.activeElement;
var containerId = $container.attr('id');
var icinga = this.icinga;
var self = this.icinga.behaviors.form;
var _this = this.icinga.behaviors.form;
var changed = false;
$container.find('form').each(function () {
var form = self.uniqueFormName(this);
var form = _this.uniqueFormName(this);
if (autorefresh) {
// check if an element in this container was changed
$(this).find('input').each(function () {
var name = this.name;
if (self.inputs[form] && self.inputs[form][name]) {
if (_this.inputs[form] && _this.inputs[form][name]) {
icinga.logger.debug(
'form input: ' + form + '.' + name + ' was changed and aborts reload...'
);
@ -88,7 +88,7 @@
});
} else {
// user-triggered reload, forget all changes to forms in this container
self.inputs[form] = null;
_this.inputs[form] = null;
}
});
if (changed) {

View File

@ -108,14 +108,14 @@
var $a = $(this);
var href = $a.attr('href');
var $li;
var self = event.data.self;
var icinga = self.icinga;
var _this = event.data.self;
var icinga = _this.icinga;
self.hovered = null;
_this.hovered = null;
if (href.match(/#/)) {
// ...it may be a menu section without a dedicated link.
// Switch the active menu item:
self.setActive($a);
_this.setActive($a);
$li = $a.closest('li');
if ($li.hasClass('hover')) {
$li.removeClass('hover');
@ -128,7 +128,7 @@
return;
}
} else {
self.setActive($(event.target));
_this.setActive($(event.target));
}
// update target url of the menu container to the clicked link
var $menu = $('#menu');
@ -276,9 +276,9 @@
var $li = $(this),
delay = 800,
self = event.data.self;
_this = event.data.self;
self.hovered = null;
_this.hovered = null;
if ($li.hasClass('active')) {
$li.siblings().removeClass('hover');
return;
@ -315,14 +315,14 @@
$sibling.removeClass('hover');
}
});
self.hoverElement($li);
_this.hoverElement($li);
}, delay);
};
Navigation.prototype.leaveSidebar = function (event) {
var $sidebar = $(this),
$li = $sidebar.find('li.hover'),
self = event.data.self;
_this = event.data.self;
if (! $li.length) {
$('#layout').removeClass('hoveredmenu');
return;
@ -337,7 +337,7 @@
$li.removeClass('hover');
$('#layout').removeClass('hoveredmenu');
}, 500);
self.hovered = null;
_this.hovered = null;
};
Navigation.prototype.hoverElement = function ($li) {
@ -356,7 +356,7 @@
Navigation.prototype.dropdownLeave = function (event) {
var $li = $(this),
self = event.data.self;
_this = event.data.self;
setTimeout(function () {
// TODO: make this behave well together with keyboard navigation
try {
@ -365,7 +365,7 @@
}
} catch(e) { /* Bypass because if IE8 */ }
}, 300);
self.hovered = null;
_this.hovered = null;
};
Icinga.Behaviors.Navigation = Navigation;

View File

@ -21,7 +21,7 @@
};
Tooltip.prototype.onRendered = function(evt) {
var self = evt.data.self, icinga = evt.data.icinga, el = evt.target;
var _this = evt.data.self, icinga = evt.data.icinga, el = evt.target;
$('[title]', el).each(function () {
var $el = $(this);
@ -63,7 +63,7 @@
return;
}
var title = $(this).find('.tipsy-inner').html();
var atMouse = document.elementFromPoint(self.mouseX, self.mouseY);
var atMouse = document.elementFromPoint(_this.mouseX, _this.mouseY);
var nearestTip = $(atMouse).closest('[original-title="' + title + '"]')[0];
if (nearestTip) {
var tipsy = $.data(nearestTip, 'tipsy');

View File

@ -13,7 +13,7 @@
Tristate.prototype = new Icinga.EventListener();
Tristate.prototype.clickTriState = function (event) {
var self = event.data.self;
var _this = event.data.self;
var $tristate = $(this);
var triState = parseInt($tristate.data('icinga-tristate'), 10);
@ -42,7 +42,7 @@
} else {
$tristate.parent().find('b.tristate-changed').css('visibility', 'hidden');
}
self.icinga.ui.setTriState(value.toString(), $tristate);
_this.icinga.ui.setTriState(value.toString(), $tristate);
};
Icinga.Behaviors.Tristate = Tristate;

View File

@ -41,14 +41,14 @@
* 'on' to register listeners
*/
EventListener.prototype.bind = function (emitter) {
var self = this;
var _this = this;
$.each(this.handlers, function(i, handler) {
self.icinga.logger.debug('bind: ' + handler.evt + '(' + handler.cond + ')');
_this.icinga.logger.debug('bind: ' + handler.evt + '(' + handler.cond + ')');
emitter.on(
handler.evt, handler.cond,
{
self: handler.scope || emitter,
icinga: self.icinga
icinga: _this.icinga
}, handler.fn
);
});
@ -61,9 +61,9 @@
* 'off' to un-register listeners.
*/
EventListener.prototype.unbind = function (emitter) {
var self = this;
var _this = this;
$.each(this.handlers, function(i, handler) {
self.icinga.logger.debug('unbind: ' + handler.evt + '(' + handler.cond + ')');
_this.icinga.logger.debug('unbind: ' + handler.evt + '(' + handler.cond + ')');
emitter.off(handler.evt, handler.cond, handler.fn);
});
};

View File

@ -33,8 +33,8 @@
// TODO: What's this?
applyHandlers: function (event) {
var $target = $(event.target);
var self = event.data.self;
var icinga = self.icinga;
var _this = event.data.self;
var icinga = _this.icinga;
if (! icinga) {
// Attempt to catch a rare error, race condition, whatever
@ -42,7 +42,7 @@
return;
}
if (self.initializeModules) {
if (_this.initializeModules) {
var loaded = false;
var moduleName = $target.data('icingaModule');
if (moduleName) {
@ -63,7 +63,7 @@
// so we need to ensure that it is called the first time they are
// initialized
event.stopImmediatePropagation();
self.initializeModules = false;
_this.initializeModules = false;
var $container = $target.closest('.container');
if (! $container.length) {
@ -79,7 +79,7 @@
return false;
}
} else {
self.initializeModules = true;
_this.initializeModules = true;
}
$('.dashboard > div', $target).each(function(idx, el) {
@ -93,7 +93,7 @@
var $searchField = $('#menu input.search', $target);
// Remember initial search field value if any
if ($searchField.length && $searchField.val().length) {
self.searchValue = $searchField.val();
_this.searchValue = $searchField.val();
}
},
@ -105,7 +105,7 @@
// Note: It is important that this is the first handler for this event!
$(document).on('rendered', { self: this }, this.applyHandlers);
$.each(self.icinga.behaviors, function (name, behavior) {
$.each(this.icinga.behaviors, function (name, behavior) {
behavior.bind($(document));
});
@ -190,12 +190,12 @@
},
autoSubmitSearch: function(event) {
var self = event.data.self;
if ($('#menu input.search').val() === self.searchValue) {
var _this = event.data.self;
if ($('#menu input.search').val() === _this.searchValue) {
return;
}
self.searchValue = $('#menu input.search').val();
return self.autoSubmitForm(event);
_this.searchValue = $('#menu input.search').val();
return _this.autoSubmitForm(event);
},
rememberSubmitButton: function(e) {
@ -212,8 +212,8 @@
*
*/
submitForm: function (event, autosubmit) {
var self = event.data.self;
var icinga = self.icinga;
var _this = event.data.self;
var icinga = _this.icinga;
// .closest is not required unless subelements to trigger this
var $form = $(event.currentTarget).closest('form');
var url = $form.attr('action');
@ -277,9 +277,9 @@
$button.addClass('active');
}
$target = self.getLinkTargetFor($button);
$target = _this.getLinkTargetFor($button);
} else {
$target = self.getLinkTargetFor($form);
$target = _this.getLinkTargetFor($form);
}
if (! url) {
@ -427,8 +427,8 @@
* Someone clicked a link or tr[href]
*/
linkClicked: function (event) {
var self = event.data.self;
var icinga = self.icinga;
var _this = event.data.self;
var icinga = _this.icinga;
var $a = $(this);
var $eventTarget = $(event.target);
var href = $a.attr('href');
@ -512,7 +512,7 @@
}
return false;
}
$target = self.getLinkTargetFor($a);
$target = _this.getLinkTargetFor($a);
formerUrl = $target.data('icingaUrl');
if (typeof formerUrl !== 'undefined' && formerUrl.split(/#/)[0] === href.split(/#/)[0]) {
@ -524,7 +524,7 @@
return false;
}
} else {
$target = self.getLinkTargetFor($a);
$target = _this.getLinkTargetFor($a);
}
// Load link URL
@ -577,11 +577,11 @@
} else if (targetId === '_main') {
targetId = 'col1';
$target = $('#' + targetId);
self.icinga.ui.layout1col();
this.icinga.ui.layout1col();
} else {
$target = $('#' + targetId);
if (! $target.length) {
self.icinga.logger.warn('Link target "#' + targetId + '" does not exist in DOM.');
this.icinga.logger.warn('Link target "#' + targetId + '" does not exist in DOM.');
}
}
@ -596,7 +596,7 @@
},
unbindGlobalHandlers: function () {
$.each(self.icinga.behaviors, function (name, behavior) {
$.each(this.icinga.behaviors, function (name, behavior) {
behavior.unbind($(document));
});
$(window).off('resize', this.onWindowResize);

View File

@ -144,8 +144,8 @@
*/
onHistoryChange: function (event) {
var self = event.data.self,
icinga = self.icinga;
var _this = event.data.self,
icinga = _this.icinga;
icinga.logger.debug('Got a history change');
@ -157,9 +157,9 @@
}
// keep the last pushed url in sync with history changes
self.lastPushUrl = location.href;
_this.lastPushUrl = location.href;
self.applyLocationBar();
_this.applyLocationBar();
// notify behaviors of the state change
$.each(this.icinga.behaviors, function (i, behavior) {

View File

@ -114,13 +114,13 @@
contentType = false;
}
var self = this;
var _this = this;
var req = $.ajax({
type : method,
url : url,
data : data,
headers: headers,
context: self,
context: _this,
contentType: contentType,
processData: ! isFormData
});
@ -153,9 +153,9 @@
* @param {object} $target The target container
*/
submitFormToIframe: function ($form, action, $target) {
var self = this;
var _this = this;
$form.prop('action', self.icinga.utils.addUrlParams(action, {
$form.prop('action', _this.icinga.utils.addUrlParams(action, {
'_frameUpload': true
}));
$form.prop('target', 'fileupload-frame-target');
@ -165,10 +165,10 @@
var $redirectMeta = $contents.find('meta[name="redirectUrl"]');
if ($redirectMeta.length) {
self.redirectToUrl($redirectMeta.attr('content'), $target);
_this.redirectToUrl($redirectMeta.attr('content'), $target);
} else {
// Fetch the frame's new content and paste it into the target
self.renderContentToContainer(
_this.renderContentToContainer(
$contents.find('body').html(),
$target,
'replace'
@ -208,16 +208,16 @@
},
autorefresh: function () {
var self = this;
if (self.autorefreshEnabled !== true) {
var _this = this;
if (_this.autorefreshEnabled !== true) {
return;
}
$('.container').filter(this.filterAutorefreshingContainers).each(function (idx, el) {
var $el = $(el);
var id = $el.attr('id');
if (typeof self.requests[id] !== 'undefined') {
self.icinga.logger.debug('No refresh, request pending for ', id);
if (typeof _this.requests[id] !== 'undefined') {
_this.icinga.logger.debug('No refresh, request pending for ', id);
return;
}
@ -225,12 +225,12 @@
var lastUpdate = $el.data('lastUpdate');
if (typeof interval === 'undefined' || ! interval) {
self.icinga.logger.info('No interval, setting default', id);
_this.icinga.logger.info('No interval, setting default', id);
interval = 10;
}
if (typeof lastUpdate === 'undefined' || ! lastUpdate) {
self.icinga.logger.info('No lastUpdate, setting one', id);
_this.icinga.logger.info('No lastUpdate, setting one', id);
$el.data('lastUpdate',(new Date()).getTime());
return;
}
@ -248,12 +248,12 @@
return;
}
if (self.loadUrl($el.data('icingaUrl'), $el, undefined, undefined, undefined, true) === false) {
self.icinga.logger.debug(
if (_this.loadUrl($el.data('icingaUrl'), $el, undefined, undefined, undefined, true) === false) {
_this.icinga.logger.debug(
'NOT autorefreshing ' + id + ', even if ' + interval + ' ms passed. Request pending?'
);
} else {
self.icinga.logger.debug(
_this.icinga.logger.debug(
'Autorefreshing ' + id + ' ' + interval + ' ms passed'
);
}
@ -277,12 +277,12 @@
processNotificationHeader: function(req) {
var header = req.getResponseHeader('X-Icinga-Notification');
var self = this;
var _this = this;
if (! header) return false;
var list = header.split('&');
$.each(list, function(idx, el) {
var parts = decodeURIComponent(el).split(' ');
self.createNotice(parts.shift(), parts.join(' '));
_this.createNotice(parts.shift(), parts.join(' '));
});
return true;
},
@ -406,15 +406,15 @@
// TODO: this is just a prototype, disabled for now
return;
var self = this;
var _this = this;
$('img.icon', $container).each(function(idx, img) {
var src = $(img).attr('src');
if (typeof self.iconCache[src] !== 'undefined') {
if (typeof _this.iconCache[src] !== 'undefined') {
return;
}
var cache = new Image();
cache.src = src
self.iconCache[src] = cache;
_this.iconCache[src] = cache;
});
},
@ -422,7 +422,7 @@
* Handle successful XHR response
*/
onResponse: function (data, textStatus, req) {
var self = this;
var _this = this;
if (this.failureNotice !== null) {
if (! this.failureNotice.hasClass('fading-out')) {
this.failureNotice.remove();
@ -540,7 +540,7 @@
var title = $('h1', $el).first();
$('h1', targets[i]).first().replaceWith(title);
self.loadUrl(url, targets[i]);
_this.loadUrl(url, targets[i]);
i++;
});
rendered = true;
@ -569,7 +569,7 @@
if (newBody) {
this.icinga.ui.fixDebugVisibility().triggerWindowResize();
}
self.cacheLoadedIcons(req.$target);
_this.cacheLoadedIcons(req.$target);
},
/**
@ -586,7 +586,7 @@
var url = req.url;
if (req.$target[0].id === 'col1') {
self.icinga.behaviors.navigation.trySetActiveByUrl(url);
this.icinga.behaviors.navigation.trySetActiveByUrl(url);
}
var $forms = $('[action="' + this.icinga.utils.parseUrl(url).path + '"]');
@ -735,7 +735,7 @@
renderContentToContainer: function (content, $container, action, autorefresh, forceFocus) {
// Container update happens here
var scrollPos = false;
var self = this;
var _this = this;
var containerId = $container.attr('id');
var activeElementPath = false;
@ -772,7 +772,7 @@
$container.trigger('beforerender');
var discard = false;
$.each(self.icinga.behaviors, function(name, behavior) {
$.each(_this.icinga.behaviors, function(name, behavior) {
if (behavior.renderHook) {
var changed = behavior.renderHook(content, $container, action, autorefresh);
if (!changed) {
@ -798,7 +798,7 @@
// });
$('.container', $container).each(function() {
self.stopPendingRequestsFor($(this));
_this.stopPendingRequestsFor($(this));
});
if (false &&

View File

@ -80,16 +80,16 @@
},
applyHandlers: function () {
var self = this;
var _this = this;
$.each(this.registeredHandlers, function (key, on) {
self.bindEventHandler(
_this.bindEventHandler(
on.event,
on.filter,
on.handler
);
});
self = null;
_this = null;
return this;
},
@ -98,10 +98,10 @@
* Effectively bind the given event handler
*/
bindEventHandler: function (event, filter, handler) {
var self = this;
var _this = this;
this.icinga.logger.debug('Bound ' + filter + ' .' + event + '()');
this.handlers.push([event, filter, handler]);
$(document).on(event, filter, handler.bind(self.object));
$(document).on(event, filter, handler.bind(_this.object));
},
/**

View File

@ -49,8 +49,8 @@
* The initialization function starts our ticker
*/
initialize: function () {
var self = this;
this.ticker = setInterval(function () { self.tick(); }, this.interval);
var _this = this;
this.ticker = setInterval(function () { _this.tick(); }, this.interval);
},
/**

View File

@ -211,16 +211,16 @@
* Our window got resized, let's fix our UI
*/
onWindowResize: function (event) {
var self = event.data.self;
var _this = event.data.self;
if (self.layoutHasBeenChanged()) {
self.icinga.logger.info(
if (_this.layoutHasBeenChanged()) {
_this.icinga.logger.info(
'Layout change detected, switching to',
self.currentLayout
_this.currentLayout
);
}
self.fixControls();
self.refreshDebug();
_this.fixControls();
_this.refreshDebug();
},
/**
@ -458,7 +458,6 @@
* Initialize all TriStateCheckboxes in the given html
*/
initializeTriStates: function ($html) {
var self = this;
$('div.tristate', $html).each(function(index, item) {
var $target = $(item);