@cycle/dom
Version:
The standard DOM Driver for Cycle.js, based on Snabbdom
85 lines • 3.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var adapt_1 = require("@cycle/run/lib/adapt");
var DocumentDOMSource_1 = require("./DocumentDOMSource");
var BodyDOMSource_1 = require("./BodyDOMSource");
var ElementFinder_1 = require("./ElementFinder");
var isolate_1 = require("./isolate");
var MainDOMSource = /** @class */ (function () {
function MainDOMSource(_rootElement$, _sanitation$, _namespace, _isolateModule, _eventDelegator, _name) {
if (_namespace === void 0) { _namespace = []; }
this._rootElement$ = _rootElement$;
this._sanitation$ = _sanitation$;
this._namespace = _namespace;
this._isolateModule = _isolateModule;
this._eventDelegator = _eventDelegator;
this._name = _name;
this.isolateSource = function (source, scope) {
return new MainDOMSource(source._rootElement$, source._sanitation$, source._namespace.concat(isolate_1.getScopeObj(scope)), source._isolateModule, source._eventDelegator, source._name);
};
this.isolateSink = isolate_1.makeIsolateSink(this._namespace);
}
MainDOMSource.prototype._elements = function () {
if (this._namespace.length === 0) {
return this._rootElement$.map(function (x) { return [x]; });
}
else {
var elementFinder_1 = new ElementFinder_1.ElementFinder(this._namespace, this._isolateModule);
return this._rootElement$.map(function () { return elementFinder_1.call(); });
}
};
MainDOMSource.prototype.elements = function () {
var out = adapt_1.adapt(this._elements().remember());
out._isCycleSource = this._name;
return out;
};
MainDOMSource.prototype.element = function () {
var out = adapt_1.adapt(this._elements()
.filter(function (arr) { return arr.length > 0; })
.map(function (arr) { return arr[0]; })
.remember());
out._isCycleSource = this._name;
return out;
};
Object.defineProperty(MainDOMSource.prototype, "namespace", {
get: function () {
return this._namespace;
},
enumerable: true,
configurable: true
});
MainDOMSource.prototype.select = function (selector) {
if (typeof selector !== 'string') {
throw new Error("DOM driver's select() expects the argument to be a " +
"string as a CSS selector");
}
if (selector === 'document') {
return new DocumentDOMSource_1.DocumentDOMSource(this._name);
}
if (selector === 'body') {
return new BodyDOMSource_1.BodyDOMSource(this._name);
}
var namespace = selector === ':root'
? []
: this._namespace.concat({ type: 'selector', scope: selector.trim() });
return new MainDOMSource(this._rootElement$, this._sanitation$, namespace, this._isolateModule, this._eventDelegator, this._name);
};
MainDOMSource.prototype.events = function (eventType, options, bubbles) {
if (options === void 0) { options = {}; }
if (typeof eventType !== "string") {
throw new Error("DOM driver's events() expects argument to be a " +
"string representing the event type to listen for.");
}
var event$ = this._eventDelegator.addEventListener(eventType, this._namespace, options, bubbles);
var out = adapt_1.adapt(event$);
out._isCycleSource = this._name;
return out;
};
MainDOMSource.prototype.dispose = function () {
this._sanitation$.shamefullySendNext(null);
//this._isolateModule.reset();
};
return MainDOMSource;
}());
exports.MainDOMSource = MainDOMSource;
//# sourceMappingURL=MainDOMSource.js.map