Skip to content

Commit 6070c2f

Browse files
committed
Updated to 0.0.11
1 parent f779b0e commit 6070c2f

File tree

12 files changed

+1210
-67
lines changed

12 files changed

+1210
-67
lines changed

.github/workflows/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
registry-url: 'https://npm.pkg.github.com'
1414
scope: '@djthorpe'
1515
- run: npm install
16-
- run: npm run build
16+
- run: npm run all
1717
- run: npm publish
1818
env:
1919
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

config/jsdoc.config.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"source": {
3+
"include": [
4+
"js"
5+
],
6+
"exclude": [
7+
"node_modules"
8+
]
9+
},
10+
"opts": {
11+
"encoding": "utf8",
12+
"readme": "./README.md",
13+
"destination": "./dist/doc",
14+
"recurse": true,
15+
"template": "./node_modules/clean-jsdoc-theme",
16+
"theme_opts": {
17+
"theme": "light"
18+
}
19+
},
20+
"plugins": [
21+
"./node_modules/@ckeditor/jsdoc-plugins/lib/export-fixer/export-fixer.js"
22+
]
23+
}

config/webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ module.exports = {
2121
{
2222
test: /\.(woff|woff2|eot|ttf|otf)$/i,
2323
type: 'asset/resource',
24+
generator: {
25+
filename: 'assets/fonts/[name][ext][query]',
26+
},
2427
},
2528
],
2629
},

js/controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export default class Controller {
2222
copypaste.addEventListener('mvc.copypaste.click', (sender, target) => {
2323
copypaste.clipboard = target.innerText;
2424
});
25-
copypaste.addEventListener('mvc.copypaste.change', (sender) => {
26-
console.log('copied');
25+
copypaste.addEventListener('mvc.copypaste.change', () => {
26+
console.log('TODO: copied');
2727
});
2828
});
2929
}

js/emitter.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Class to emit events to listener callbacks
3+
* @class
4+
*/
5+
export default class Emitter {
6+
/**
7+
* Create an Emitter.
8+
*/
9+
constructor() {
10+
this.$listeners = new Map();
11+
}
12+
13+
/**
14+
* Add a listener for emitted events.
15+
* @param {string[]} types - An array or string of the events to listen for.
16+
* @param {function} fn - The callback when an event is emitted.
17+
*/
18+
addEventListener(types, fn) {
19+
if (Array.isArray(types)) {
20+
types.forEach((type) => {
21+
this.addEventListener(type, fn);
22+
});
23+
} else {
24+
if (!this.$listeners.has(types)) {
25+
this.$listeners.set(types, []);
26+
}
27+
this.$listeners.get(types).push(fn);
28+
}
29+
}
30+
31+
/**
32+
* Dispatch an event to listeners with arguments.
33+
* @param {string} type - The type of event to dispatch.
34+
* @param {...*} args - The arguments to pass to the listener callback.
35+
*/
36+
dispatchEvent(type, ...args) {
37+
const listeners = this.$listeners.get(type);
38+
if (listeners) {
39+
listeners.forEach((fn) => {
40+
fn(...args);
41+
});
42+
}
43+
}
44+
}

js/events.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Form from './form';
1515

1616
// Utils
1717
import Error from './error';
18-
import Emitter from './events';
18+
import Emitter from './emitter';
1919
import './string';
2020

2121
// CSS and Fonts

js/model.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,23 @@ export default class Model {
3232
this.$setall(data);
3333
}
3434

35-
static define(classConstructor, classProps) {
35+
static define(classConstructor, classProps, className) {
3636
if (typeof classConstructor !== 'function') {
3737
throw new Error('Called define without a class constructor');
3838
}
39-
const className = classConstructor.name;
40-
if (Model.constructors[className]) {
41-
throw new Error(`Class already defined ${className}`);
39+
const classKey = className || classConstructor.name;
40+
if (Model.constructors[classKey]) {
41+
throw new Error(`Class already defined ${classKey}`);
4242
}
43-
const proto = Model.$newproto(classConstructor, classProps);
43+
const proto = Model.$newproto(classKey, classProps);
4444
if (!proto) {
45-
throw new Error(`No prototype for ${className}`);
45+
throw new Error(`No prototype for ${classKey}`);
4646
}
47-
Model.constructors[className] = classConstructor;
48-
Model.models[className] = proto;
47+
Model.constructors[classKey] = classConstructor;
48+
Model.models[classKey] = proto;
4949
}
5050

51-
static $newproto(classConstructor, classProps) {
52-
const className = classConstructor.name;
51+
static $newproto(className, classProps) {
5352
const proto = {};
5453

5554
// $className property

js/provider.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Provider class to be subclassed by an actual provider
22

33
import Error from './error';
4-
import Emitter from './events';
4+
import Emitter from './emitter';
55

66
// ////////////////////////////////////////////////////////////////////////////
77
// CONSTANTS
@@ -71,8 +71,11 @@ export default class Provider extends Emitter {
7171
if (this.$array(data)) {
7272
changed = true;
7373
}
74-
} else if (this.$object(data)) {
75-
changed = true;
74+
} else {
75+
const result = this.$object(data);
76+
if (result[1]) {
77+
changed = true;
78+
}
7679
}
7780
})
7881
.then(() => {
@@ -87,44 +90,65 @@ export default class Provider extends Emitter {
8790
});
8891
}
8992

93+
static $key(obj) {
94+
return typeof obj === 'object' ? obj.key : null;
95+
}
96+
97+
static $equals(a, b) {
98+
return a.$equals ? a.$equals(b) : a === b;
99+
}
100+
90101
$object(data) {
91102
const obj = new this.$constructor(data);
92-
const key = typeof obj === 'object' ? obj.key : null;
93-
if (key) {
94-
if (this.$objs.has(key)) {
95-
const existing = this.$objs.get(key);
96-
this.$objs.set(key, obj);
97-
if (obj.$equals && obj.$equals(existing) === false) {
98-
this.dispatchEvent(EVENT_CHANGED, this, obj, existing);
99-
}
103+
const key = this.constructor.$key(obj);
104+
let changed = true;
105+
if (key && this.$objs.has(key)) {
106+
const existing = this.$objs.get(key);
107+
this.$objs.set(key, obj);
108+
if (this.constructor.$equals(obj, existing) === false) {
109+
this.dispatchEvent(EVENT_CHANGED, this, obj, existing);
100110
} else {
101-
this.$objs.set(key, obj);
102-
this.dispatchEvent(EVENT_ADDED, this, obj);
111+
changed = false;
103112
}
104113
} else {
114+
if (key) {
115+
this.$objs.set(key, obj);
116+
}
105117
this.dispatchEvent(EVENT_ADDED, this, obj);
106118
}
107-
return key;
119+
return [key, changed];
108120
}
109121

110122
$array(data) {
111-
const changed = false;
123+
let changed = false;
112124
const mark = new Map();
125+
126+
// Mark existing objects
113127
this.$objs.forEach((_, key) => {
114128
mark.set(key, true);
115129
});
130+
131+
// Add and change objects
116132
data.forEach((elem) => {
117-
const key = this.$object(elem);
118-
if (key) {
119-
mark.delete(key);
133+
const result = this.$object(elem);
134+
if (result[0]) {
135+
mark.delete(result[0]);
136+
}
137+
if (result[1]) {
138+
changed = true;
120139
}
121140
});
141+
142+
// Delete objects which are still marked
122143
this.$objs.forEach((elem, key) => {
123144
if (mark.get(key)) {
145+
changed = true;
124146
this.dispatchEvent(EVENT_DELETED, this, elem);
125147
this.$objs.delete(key);
126148
}
127149
});
150+
151+
// Return true if the items were changed
128152
return changed;
129153
}
130154

@@ -143,8 +167,8 @@ export default class Provider extends Emitter {
143167
return this.$objs.get(key);
144168
}
145169

146-
// removeAll resets the provider
147-
removeAll() {
170+
// clear removes all objects from the provider
171+
clear() {
148172
this.$objs.clear();
149173
}
150174
}

js/view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// View class to be subclassed by an actual view
22

3-
import Emitter from './events';
3+
import Emitter from './emitter';
44
import Error from './error';
55

66
/* eslint no-param-reassign: ["error", { "props": false }] */

0 commit comments

Comments
 (0)