@cycle/dom
Version:
The standard DOM Driver for Cycle.js, based on Snabbdom
83 lines • 3.64 kB
JavaScript
import { adapt } from '@cycle/run/lib/adapt';
import { DocumentDOMSource } from './DocumentDOMSource';
import { BodyDOMSource } from './BodyDOMSource';
import { ElementFinder } from './ElementFinder';
import { makeIsolateSink, getScopeObj } from './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(getScopeObj(scope)), source._isolateModule, source._eventDelegator, source._name);
};
this.isolateSink = 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(this._namespace, this._isolateModule);
return this._rootElement$.map(function () { return elementFinder_1.call(); });
}
};
MainDOMSource.prototype.elements = function () {
var out = adapt(this._elements().remember());
out._isCycleSource = this._name;
return out;
};
MainDOMSource.prototype.element = function () {
var out = 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(this._name);
}
if (selector === 'body') {
return new 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(event$);
out._isCycleSource = this._name;
return out;
};
MainDOMSource.prototype.dispose = function () {
this._sanitation$.shamefullySendNext(null);
//this._isolateModule.reset();
};
return MainDOMSource;
}());
export { MainDOMSource };
//# sourceMappingURL=MainDOMSource.js.map