diff --git a/packages/electrode-react-webapp/lib/async-template.js b/packages/electrode-react-webapp/lib/async-template.js index d0530ea40..519b1c426 100644 --- a/packages/electrode-react-webapp/lib/async-template.js +++ b/packages/electrode-react-webapp/lib/async-template.js @@ -68,6 +68,8 @@ class AsyncTemplate { const pos = template.indexOf(tokenOpenTag, pt); if (pos >= pt) { const str = template.substring(pt, pos).trim(); + // if there are text between a close tag and an open tag, then consider + // that as plain HTML string if (str) tokens.push({ str }); const ex = template.indexOf(tokenCloseTag, pos); @@ -81,7 +83,8 @@ class AsyncTemplate { .trim() .split("\n") .map(x => x.trim()) - .filter(x => x) + // remove empty and comment lines that start with "//" + .filter(x => x && !x.startsWith("//")) .join(" "); const token = remain.split(" ", 1)[0]; diff --git a/packages/electrode-react-webapp/lib/token.js b/packages/electrode-react-webapp/lib/token.js index 891bf3f76..06ba797f0 100644 --- a/packages/electrode-react-webapp/lib/token.js +++ b/packages/electrode-react-webapp/lib/token.js @@ -15,6 +15,9 @@ class Token { this.custom = undefined; this.wantsNext = undefined; this.props = props || {}; + if (this.props._call) { + this._modCall = [].concat(this.props._call); + } } // if token is a module, then load it @@ -29,7 +32,18 @@ class Token { viewTokenModules[this.id] = tokenMod; } - this.custom = tokenMod(options || {}); // call setup function to get an instance + if (this._modCall) { + // call setup function to get an instance + const params = [options || {}, this].concat(this._modCall[1] || []); + assert( + tokenMod[this._modCall[0]], + `electrode-react-webapp: _call of token ${this.id} - '${this._modCall[0]}' not found` + ); + this.custom = tokenMod[this._modCall[0]].apply(undefined, params); + } else { + this.custom = tokenMod(options || {}, this); + } + assert( this.custom && this.custom.process, `custom token ${this.id} module doesn't have process method` diff --git a/packages/electrode-react-webapp/test/data/template3.html b/packages/electrode-react-webapp/test/data/template3.html index def3b7cb6..41e93a033 100644 --- a/packages/electrode-react-webapp/test/data/template3.html +++ b/packages/electrode-react-webapp/test/data/template3.html @@ -30,3 +30,7 @@ "test": [1,2,3] } }--> + diff --git a/packages/electrode-react-webapp/test/fixtures/custom-call.js b/packages/electrode-react-webapp/test/fixtures/custom-call.js new file mode 100644 index 000000000..3016cd4b8 --- /dev/null +++ b/packages/electrode-react-webapp/test/fixtures/custom-call.js @@ -0,0 +1,14 @@ +"use strict"; + +function setup() { + return { + name: "custom-call", + process: function() { + return `_call`; + } + }; +} + +module.exports = { + setup +}; diff --git a/packages/electrode-react-webapp/test/spec/parse-template.spec.js b/packages/electrode-react-webapp/test/spec/parse-template.spec.js index 2dd66b2d9..e1f4098ee 100644 --- a/packages/electrode-react-webapp/test/spec/parse-template.spec.js +++ b/packages/electrode-react-webapp/test/spec/parse-template.spec.js @@ -3,6 +3,7 @@ const AsyncTemplate = require("../../lib/async-template"); const Path = require("path"); const expect = require("chai").expect; +const _ = require("lodash"); describe("AsyncTemplate._parseTemplate", function() { it("should parse template into tokens", () => { @@ -211,8 +212,23 @@ describe("AsyncTemplate._parseTemplate", function() { test: [1, 2, 3] }, wantsNext: undefined + }, + { + _modCall: ["setup"], + custom: { + name: "custom-call" + }, + id: "#./test/fixtures/custom-call", + isModule: true, + pos: 364, + props: { + _call: "setup" + }, + wantsNext: false } ]; + expect(typeof _.last(asyncTemplate.tokens).custom.process).to.equal("function"); + delete _.last(asyncTemplate.tokens).custom.process; expect(asyncTemplate.tokens).to.deep.equal(expected); }); diff --git a/packages/electrode-react-webapp/test/spec/token.spec.js b/packages/electrode-react-webapp/test/spec/token.spec.js index 0627ca8bd..68e460875 100644 --- a/packages/electrode-react-webapp/test/spec/token.spec.js +++ b/packages/electrode-react-webapp/test/spec/token.spec.js @@ -14,6 +14,14 @@ describe("token", function() { expect(tk.custom).to.equal(undefined); }); + it("should invoke _call of a module", () => { + const tk = new Token("#./test/fixtures/custom-call", 0, { _call: "setup" }); + expect(tk.id).to.equal("#./test/fixtures/custom-call"); + expect(tk.isModule).to.equal(true); + tk.load(); + expect(tk.process()).to.equal("_call"); + }); + it("should create token as custom", () => { const tk = new Token("#./test/fixtures/custom-count"); expect(tk.id).to.equal("#./test/fixtures/custom-count");