webamp/modern/src/runtime/MakiObject.ts
jberg 59281e056f Move all image lookups to XML tree and support directly specifying image file (#886)
* handle image lookup in initialize for zip files

* remove unused params

* get image paths from node separately

* do image lookups in initialize

* fix lint errors

* handle case insensitivity and small tweaks

* handle nodes without name before we call toLowerCase

* fix isString

* move image lookups to xml tree

* update function comment

* spell out lowercase

* add additional comment about why we return node itself

* remove extra variable

* rename imagePaths to imageAttributes

* early returns

* move all resolved images to js_assets attribute instead of overwriting attr

* missed on previous renaming path->attribute

* fix lint errors

* need node back after rebase

* dont need children param anymore
2019-11-30 13:42:53 -08:00

103 lines
2.1 KiB
TypeScript

import Emitter from "../Emitter";
import * as Utils from "../utils";
import { ModernStore, ResolvedXmlNode } from "../types";
class MakiObject {
name: string;
_uid: number;
_store: ModernStore;
attributes: Object;
parent: MakiObject;
_emitter: Emitter;
children: MakiObject[];
js_annotations: Object;
constructor(
node: ResolvedXmlNode,
parent: MakiObject,
annotations: Object = {},
store: ModernStore
) {
this._store = store;
if (node) {
this._uid = node.uid;
this.attributes = node.attributes || {};
this.name = node.name;
} else {
this._uid = Utils.getId();
// When dynamically creating an object with `new` we have no underlying node
this.attributes = {};
this.name = this.getclassname().toLowerCase();
}
this.parent = parent;
this.js_annotations = annotations;
this.children = [];
this._emitter = new Emitter();
}
js_addChild(child: MakiObject) {
this.children.push(child);
}
js_addChildren(children: MakiObject[]) {
this.children = this.children.concat(children);
}
js_removeChild(child: MakiObject) {
this.children = this.children.filter(item => item !== child);
}
js_delete() {
this.parent.js_removeChild(this);
this.parent.js_trigger("js_update");
}
js_trigger(eventName, ...args) {
this._emitter.trigger(eventName.toLowerCase(), ...args);
}
js_listen(eventName, cb) {
return this._emitter.listen(eventName, cb);
}
js_listenToAll(cb) {
return this._emitter.listenToAll(cb);
}
js_dispose() {
this._emitter.dispose();
}
js_groupdefLookup(id: string) {
const groupdef = Utils.findGroupDefById(this, id);
if (groupdef) {
return groupdef;
}
return null;
}
/**
* getclassname()
*
* Returns the class name for the object.
* @ret The class name.
*/
getclassname() {
return "Object";
}
/**
* getId()
*/
getid() {
throw new Error("getId not implemented");
}
onnotify(command: string, param: string, a: number, b: number): number {
this.js_trigger("onNotify", command, param, a, b);
return 0;
}
}
export default MakiObject;