complete.js: Properly manage enrichment persistence

This commit is contained in:
Johannes Meyer 2020-07-27 15:04:31 +02:00
parent d19a55311b
commit 3c41c14653

View File

@ -3,7 +3,7 @@
/** /**
* Complete - Behavior for forms with auto-completion of terms * Complete - Behavior for forms with auto-completion of terms
*/ */
(function(Icinga, $) { (function(Icinga) {
"use strict"; "use strict";
@ -13,20 +13,29 @@
* @param icinga * @param icinga
* @constructor * @constructor
*/ */
var Complete = function (icinga) { let Complete = function (icinga) {
Icinga.EventListener.call(this, icinga); Icinga.EventListener.call(this, icinga);
this.on('beforerender', '.container', this.onBeforeRender, this); this.on('beforerender', '.container', this.onBeforeRender, this);
this.on('rendered', '.container', this.onRendered, this); this.on('rendered', '.container', this.onRendered, this);
/** /**
* Cached completions * Enriched inputs
*
* @type {WeakMap<object, FilterInput>}
* @private
*/
this._enrichments = new WeakMap();
/**
* Cached enrichments
* *
* Holds values only during the time between `beforerender` and `rendered` * Holds values only during the time between `beforerender` and `rendered`
* *
* @type {{}} * @type {{}}
* @private
*/ */
this.cachedCompletions = {}; this._cachedEnrichments = {};
}; };
Complete.prototype = new Icinga.EventListener(); Complete.prototype = new Icinga.EventListener();
@ -38,20 +47,18 @@
* @param scripted * @param scripted
*/ */
Complete.prototype.onBeforeRender = function (event, content, action, autorefresh, scripted) { Complete.prototype.onBeforeRender = function (event, content, action, autorefresh, scripted) {
var _this = event.data.self; if (! autorefresh) {
return;
}
var $elements = $('input[data-term-completion]', event.currentTarget); let _this = event.data.self;
let inputs = event.currentTarget.querySelectorAll('input[data-term-completion]');
// Remember current instances // Remember current instances
$elements.each(function () { inputs.forEach((input) => {
var $input = $(this), let enrichment = _this._enrichments.get(input);
completion = $input.data('completion'); if (enrichment) {
if (completion) { _this._cachedEnrichments[_this.icinga.utils.getDomPath(input).join(' ')] = enrichment;
if (! completion.keepUsedTerms) {
completion.keepUsedTerms = autorefresh;
}
_this.cachedCompletions[_this.icinga.utils.getDomPath($input[0]).join(' ')] = completion;
} }
}); });
}; };
@ -62,31 +69,38 @@
* @param scripted * @param scripted
*/ */
Complete.prototype.onRendered = function (event, autorefresh, scripted) { Complete.prototype.onRendered = function (event, autorefresh, scripted) {
var _this = event.data.self; let _this = event.data.self;
let container = event.currentTarget;
// Apply remembered instances if (autorefresh) {
$.each(_this.cachedCompletions, function (inputPath) { // Apply remembered instances
var $input = $(inputPath); for (let inputPath in _this._cachedEnrichments) {
if ($input.length) { let enrichment = _this._cachedEnrichments[inputPath];
this.refresh($input[0]); let input = container.querySelector(inputPath);
} else { if (input !== null) {
this.destroy(); enrichment.refresh(input);
_this._enrichments.set(input, enrichment);
} else {
enrichment.destroy();
}
delete _this._cachedEnrichments[inputPath];
} }
}
delete _this.cachedCompletions[inputPath];
});
var $elements = $('input[data-term-completion]', event.currentTarget);
// Create new instances // Create new instances
$elements.each(function() { let inputs = container.querySelectorAll('input[data-term-completion]');
var $input = $(this); inputs.forEach((input) => {
if (! $input.data('completion')) { let enrichment = _this._enrichments.get(input);
(new Completion(_this.icinga, this)).bind().restoreTerms(); if (! enrichment) {
enrichment = (new FilterInput(input)).bind();
enrichment.restoreTerms();
_this._enrichments.set(input, enrichment);
} }
}); });
}; };
Icinga.Behaviors.Complete = Complete; Icinga.Behaviors.Complete = Complete;
})(Icinga, jQuery); })(Icinga);