-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pilot implementation of view presets
- Loading branch information
Showing
7 changed files
with
154 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-env browser */ | ||
|
||
import Emitter from './emitter.js'; | ||
|
||
const entries = new WeakMap(); | ||
|
||
export default class Dictionary extends Emitter { | ||
constructor() { | ||
super(); | ||
|
||
entries.set(this, Object.create(null)); | ||
} | ||
|
||
define(key, value) { | ||
entries.get(this)[key] = value; | ||
|
||
this.emit('define', key, value); | ||
} | ||
|
||
isDefined(key) { | ||
return key in entries.get(this); | ||
} | ||
|
||
get(key) { | ||
return entries.get(this)[key]; | ||
} | ||
|
||
get names() { | ||
return Object.keys(entries.get(this)).sort(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-env browser */ | ||
|
||
import Dict from './dict.js'; | ||
|
||
export default class PresetRenderer extends Dict { | ||
constructor(view) { | ||
super(); | ||
|
||
this.view = view; | ||
} | ||
|
||
define(name, config) { | ||
// FIXME: add check that config is serializable object | ||
config = JSON.parse(JSON.stringify(config)); | ||
|
||
super.define(name, Object.freeze({ | ||
name, | ||
render: (el, _, data, context) => this.view.render(el, config, data, context), | ||
config | ||
})); | ||
} | ||
|
||
render(container, name, data, context) { | ||
let preset = this.get(name); | ||
|
||
if (!preset) { | ||
const errorMsg = 'Preset `' + name + '` is not found'; | ||
console.error(errorMsg, name); | ||
|
||
const el = container.appendChild(document.createElement('div')); | ||
el.style.cssText = 'color:#a00;border:1px dashed #a00;font-size:12px;padding:4px'; | ||
el.innerText = errorMsg; | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
return preset.render(container, null, data, context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters