kui-shell
Version:
This is the monorepo for Kui, the hybrid command-line/GUI electron-based Kubernetes tool
172 lines • 4.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = require("debug");
const debug = debug_1.default('core/util/mimic-dom');
debug('loading');
const store_1 = require("../main/store");
class ClassList {
constructor() {
this.classList = [];
}
get length() {
return this.classList.length;
}
forEach(fn) {
this.classList.forEach(fn);
}
add(_) {
return this.classList.push(_);
}
contains(_) {
return this.classList.indexOf(_) >= 0;
}
remove(_) {
const idx = this.classList.findIndex((x) => x === _);
if (idx >= 0) {
this.classList.splice(idx, 1);
}
}
}
function clone(instance) {
const copy = new instance.constructor();
Object.assign(copy, instance);
return copy;
}
class ElementMimic {
constructor() {
this._isFakeDom = true;
this.value = '';
this.innerText = '';
this.innerHTML = '';
this.className = '';
this.classList = new ClassList();
this.nodeType = '';
this.style = {};
this.children = [];
this.cells = [];
this.rows = [];
this._attrs = {};
}
focus() {
}
appendChild(c) {
return this.children.push(c);
}
getAttribute(k) {
return this._attrs[k] || '';
}
setAttribute(k, v) {
this._attrs[k] = v;
return v;
}
remove() {
}
removeAttribute(k) {
const attr = this._attrs[k];
delete this._attrs[k];
return attr;
}
cloneNode() {
return clone(this);
}
querySelectorAll(selector) {
return [];
}
querySelector() {
return new ElementMimic();
}
addEventListener() {
return true;
}
hasStyle(style, desiredValue) {
const actualValue = this.style && this.style[style];
if (desiredValue)
return desiredValue == actualValue;
else
return actualValue;
}
recursiveInnerTextLength() {
return (this.innerText.length + this.children.reduce((sum, child) => sum + child.recursiveInnerTextLength(), 0));
}
insertRow(idx) {
const row = new ElementMimic();
row.nodeType = 'tr';
row.cells = [];
if (idx === -1)
this.rows.push(row);
else
this.rows.splice(idx, 0, row);
return row;
}
insertCell(idx) {
const cell = new ElementMimic();
cell.nodeType = 'td';
if (idx === -1)
this.cells.push(cell);
else
this.cells.splice(idx, 0, cell);
return cell;
}
static isFakeDom(dom) {
return dom && dom._isFakeDom;
}
}
exports.ElementMimic = ElementMimic;
function default_1() {
debug('mimicDom');
global.window = {};
try {
global.localStorage = store_1.default();
global.sessionStorage = store_1.default();
debug('successfully initialized persistent localStorage');
}
catch (err) {
debug('error initializing persistent localStorage', err);
const _localStorage = {};
const _sessionStorage = {};
global.localStorage = {
setItem: (k, v) => {
_localStorage[k] = v;
return v;
},
getItem: (k) => _localStorage[k] || null
};
global.sessionStorage = {
setItem: (k, v) => {
_sessionStorage[k] = v;
return v;
},
getItem: (k) => _sessionStorage[k] || null
};
}
finally {
global.window.localStorage = localStorage;
global.window.sessionStorage = sessionStorage;
}
window.addEventListener = () => true;
const dom0 = () => {
return new ElementMimic();
};
global.document = {
body: dom0(),
createElement: (tag) => {
const element = dom0();
element.nodeType = tag;
if (tag === 'table') {
element.rows = [];
}
return element;
},
addEventListener: () => true,
createTextNode: (text) => {
const element = dom0();
element.innerText = text;
return element;
},
querySelector: (selector) => {
return dom0();
}
};
}
exports.default = default_1;
//# sourceMappingURL=mimic-dom.js.map