Skip to content

Commit 61ef7fe

Browse files
committed
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core into gh-pages
2 parents 3c4e7ee + 2a60328 commit 61ef7fe

File tree

2 files changed

+306
-23
lines changed

2 files changed

+306
-23
lines changed

src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11

2+
import { uuid } from 'utils';
3+
24
export class PIScheme {
35
static get isPIScheme() { return true; }
46

5-
static createRef(options, strings, ...expressions) {
7+
static createRef(options, strings, expressions) {
68
const reference = new this();
7-
reference.configure(options, strings, ...expressions);
9+
reference.configure(options, strings, expressions);
810
reference.initialize();
911
return reference;
1012
}
1113

12-
configure(options = {}, strings, ...expressions) {
14+
configure(options = {}, strings, expressions) {
1315
Object.assign(this, options);
1416
this.strings = strings;
1517
this.expressions = expressions;
@@ -28,10 +30,121 @@ export class PIScheme {
2830

2931
export function makeRef(Scheme, options) {
3032
if (Scheme && Scheme.isPIScheme) {
31-
return (strings, ...expressions) => Scheme.createRef(options, strings, ...expressions);
33+
return (strings, ...expressions) => Scheme.createRef(options, strings, expressions);
3234
}
3335

3436
return (...args) => ({
3537
access: Scheme(...args)
3638
});
3739
}
40+
41+
42+
/*MD # Basic Schemes MD*/
43+
44+
export class local extends PIScheme {
45+
initialize() {
46+
this.local = this.strings.first;
47+
}
48+
read() {
49+
return this.evalFunction(this.local);
50+
}
51+
write(value) {
52+
const id = uuid();
53+
self[id] = value;
54+
try {
55+
return this.evalFunction(this.local + ` = self['${id}']`);
56+
} finally {
57+
delete self[id];
58+
}
59+
}
60+
}
61+
62+
function saveJSON(key, json) {
63+
const stringValue = JSON.stringify(json, undefined, 2);
64+
return this.setItem(key, stringValue);
65+
}
66+
function loadJSON(key) {
67+
const stringValue = this.getItem(key);
68+
if (!stringValue) {
69+
return undefined;
70+
}
71+
return JSON.parse(stringValue);
72+
}
73+
74+
class ls extends PIScheme {
75+
initialize() {
76+
this.local = this.strings.first;
77+
}
78+
read() {
79+
return this.evalFunction(this.local);
80+
}
81+
write(value) {
82+
const id = uuid();
83+
self[id] = value;
84+
try {
85+
return this.evalFunction(this.local + ` = self['${id}']`);
86+
} finally {
87+
delete self[id];
88+
}
89+
}
90+
}
91+
export {ls as localStorage};
92+
93+
export class query extends PIScheme {
94+
read() {
95+
const { element, type, prop } = this.parse();
96+
97+
if (type === 'prop') {
98+
return element[prop];
99+
}
100+
101+
if (type === 'attr') {
102+
return element.getAttribute(prop);
103+
}
104+
105+
if (type === 'style') {
106+
return element.style[prop];
107+
}
108+
109+
if (type === 'html') {
110+
return element.innerHTML;
111+
}
112+
113+
return element;
114+
}
115+
write(v) {
116+
const { element, type, prop } = this.parse();
117+
118+
if (!type) {
119+
element.replaceWith(v);
120+
return v;
121+
}
122+
if (type === 'prop') {
123+
return element[prop] = v;
124+
}
125+
if (type === 'attr') {
126+
element.setAttribute(prop, v);
127+
return v;
128+
}
129+
if (type === 'style') {
130+
return element.style[prop] = v;
131+
}
132+
if (type === 'html') {
133+
return element.innerHTML = v;
134+
}
135+
136+
element.forEach(e => e.innerHTML = v)
137+
}
138+
139+
// helper
140+
parse() {
141+
const [selector, type, prop] = this.strings.first.split('/');
142+
const element = this.query(selector);
143+
return { selector, type, prop, element };
144+
}
145+
query(selector) {
146+
return document.querySelector(selector);
147+
}
148+
}
149+
150+
export class queryAll extends PIScheme {}

src/client/reactive/test/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.spec.js

Lines changed: 189 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
'pi'
1+
'pi';
22
"enable aexpr";
3-
import chai, {expect} from 'src/external/chai.js';
3+
4+
import chai, { expect } from 'src/external/chai.js';
45
import sinon from 'src/external/sinon-3.2.1.js';
56
import sinonChai from 'src/external/sinon-chai.js';
67
chai.use(sinonChai);
78

8-
import { PIScheme } from 'polymorphic-identifiers';
9+
import { PIScheme, local, localStorage as ls, query as q } from 'polymorphic-identifiers';
910
import { uuid } from 'utils';
1011

11-
describe("PI", function() {
12-
12+
describe("PI", function () {
13+
1314
it("is defined", () => {
1415
expect(PIScheme).to.be.ok;
1516
});
@@ -21,8 +22,7 @@ describe("PI", function() {
2122
}
2223
}
2324

24-
expect(fourtyTwo``).to.equal(42)
25-
25+
expect(fourtyTwo``).to.equal(42);
2626
});
2727

2828
it("can access the `this` reference", () => {
@@ -48,11 +48,11 @@ describe("PI", function() {
4848
prop`bar` << value;
4949
}
5050
};
51-
expect(o.func()).to.equal(17)
52-
51+
expect(o.func()).to.equal(17
52+
5353
// write
54-
o.write(23)
55-
expect(o.bar).to.equal(23)
54+
);o.write(23);
55+
expect(o.bar).to.equal(23);
5656
});
5757

5858
it("can access locals with eval", () => {
@@ -74,15 +74,16 @@ describe("PI", function() {
7474
}
7575
}
7676

77-
let v1 = 'v1', v2 = 42;
77+
let v1 = 'v1',
78+
v2 = 42;
7879

7980
// read
80-
expect(local`v1`).to.equal('v1')
81-
81+
expect(local`v1`).to.equal('v1'
82+
8283
// write
83-
local`v2` << "v2";
84+
);local`v2` << "v2";
8485
expect(v2).to.equal('v2');
85-
86+
8687
// read/write
8788
local`v2` << local`v2` + '.2';
8889
expect(v2).to.equal('v2.2');
@@ -92,8 +93,177 @@ describe("PI", function() {
9293
function foo() {
9394
return 'bar';
9495
}
95-
96-
expect(foo``).to.equal('bar')
97-
});
9896

97+
expect(foo``).to.equal('bar');
98+
});
9999
});
100+
101+
describe("PI Schemes", function () {
102+
103+
it("is defined", () => {
104+
expect(PIScheme).to.be.ok;
105+
});
106+
107+
it("detects accesses", () => {
108+
class fourtyTwo extends PIScheme {
109+
read() {
110+
return 42;
111+
}
112+
}
113+
114+
expect(fourtyTwo``).to.equal(42);
115+
});
116+
117+
it("can access the `this` reference", () => {
118+
class prop extends PIScheme {
119+
initialize() {
120+
this.prop = this.strings.first;
121+
}
122+
read() {
123+
return this.thisReference[this.prop];
124+
}
125+
write(value) {
126+
return this.thisReference[this.prop] = value;
127+
}
128+
}
129+
130+
// read
131+
const o = {
132+
foo: 17,
133+
func() {
134+
return prop`foo`;
135+
},
136+
write(value) {
137+
prop`bar` << value;
138+
}
139+
};
140+
expect(o.func()).to.equal(17
141+
142+
// write
143+
);o.write(23);
144+
expect(o.bar).to.equal(23);
145+
});
146+
147+
it("local: can access local variables with eval", () => {
148+
let v1 = 'v1',
149+
v2 = 42;
150+
151+
// read
152+
expect(local`v1`).to.equal('v1'
153+
154+
// write
155+
);local`v2` << "v2";
156+
expect(v2).to.equal('v2');
157+
158+
// read/write
159+
local`v2` << local`v2` + '.2';
160+
expect(v2).to.equal('v2.2');
161+
});
162+
163+
it("ls/localStorage", () => {
164+
localStorage.setItem;
165+
166+
let v1 = 'v1',
167+
v2 = 42;
168+
169+
// read
170+
expect(local`v1`).to.equal('v1'
171+
172+
// write
173+
);local`v2` << "v2";
174+
expect(v2).to.equal('v2');
175+
176+
// read/write
177+
local`v2` << local`v2` + '.2';
178+
expect(v2).to.equal('v2.2');
179+
});
180+
181+
describe("query", function () {
182+
183+
let testElement;
184+
beforeEach(() => {
185+
document.body.querySelectorAll('.query-test').forEach(e => e.remove());
186+
testElement = <div class="query-test">17</div>;
187+
document.body.appendChild(testElement);
188+
});
189+
afterEach(() => {
190+
testElement.remove();
191+
});
192+
193+
it("is defined", () => {
194+
expect(q).to.be.ok;
195+
});
196+
197+
it("return testElement", () => {
198+
expect(q`.query-test`).to.equal(testElement);
199+
});
200+
201+
it("return a property", () => {
202+
const expectedProp = testElement.myProp = {};
203+
expect(q`.query-test/prop/myProp`).to.equal(expectedProp);
204+
});
205+
206+
it("return an attribute", () => {
207+
testElement.setAttribute('myAttr', 24);
208+
expect(q`.query-test/attr/myAttr`).to.equal('24');
209+
});
210+
211+
it("return a style", () => {
212+
testElement.style.border = '3px solid red';
213+
expect(q`.query-test/style/border`).to.equal(testElement.style.border);
214+
});
215+
216+
it("return a style in camel- and kebabcase", () => {
217+
const expectedStyle = testElement.style.backgroundColor = 'red';
218+
expect(q`.query-test/style/background-color`).to.equal(expectedStyle);
219+
expect(q`.query-test/style/backgroundColor`).to.equal(expectedStyle);
220+
});
221+
222+
it("return innerHTML", () => {
223+
const expectedHTML = testElement.innerHTML = 'hello';
224+
expect(q`.query-test/html`).to.equal(expectedHTML);
225+
});
226+
227+
it("replace testElement", () => {
228+
const expectedResult = <div id={'this-is-the-expected-result'}></div>;
229+
230+
q`.query-test` << expectedResult;
231+
expect(document.body.contains(testElement)).to.be.false;
232+
expect(document.body.contains(expectedResult)).to.be.true;
233+
});
234+
235+
it("set a property", () => {
236+
const expectedProp = {};
237+
q`.query-test/prop/myProp` << expectedProp;
238+
expect(testElement.myProp).to.equal(expectedProp);
239+
});
240+
241+
it("set an attribute", () => {
242+
q`.query-test/attr/myAttr` << 24;
243+
expect(testElement.getAttribute('myAttr')).to.equal('24');
244+
});
245+
246+
it("set a style", () => {
247+
const expectedStyle = '3px solid red';
248+
q`.query-test/style/border` << expectedStyle;
249+
expect(testElement.style.border).to.equal(expectedStyle);
250+
});
251+
252+
it("set a style in camel- and kebabcase", () => {
253+
let expectedStyle = 'red';
254+
q`.query-test/style/background-color` << expectedStyle;
255+
expect(testElement.style.backgroundColor).to.equal(expectedStyle);
256+
257+
expectedStyle = 'blue';
258+
q`.query-test/style/backgroundColor` << expectedStyle;
259+
expect(testElement.style.backgroundColor).to.equal(expectedStyle);
260+
});
261+
262+
it("set innerHTML", () => {
263+
const expectedHTML = 'hello';
264+
q`.query-test/html` << expectedHTML;
265+
expect(testElement.innerHTML).to.equal(expectedHTML);
266+
});
267+
268+
});
269+
});

0 commit comments

Comments
 (0)