forked from 15001217168/mojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface.babel.js
More file actions
83 lines (72 loc) · 1.93 KB
/
Copy pathsurface.babel.js
File metadata and controls
83 lines (72 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Html } from './html.babel.js';
/* -------------------- */
/* The `Surface` class */
/* -------------------- */
// It wextends `Html` module, create an HTMLElement and adds it to the DOM,
// after that it passes the newely create element to `el` option of the
// Html module and declares `width`/`height` defaults.
// Thus it cretes a `Surface` that is controlled by `Html` module.
const Super = Html.__mojsClass;
const Surface = Object.create(Super);
/**
* `init` - function init the class.
*
* @public
* @extends @Html
*/
Surface.init = function (o = {}) {
// create an Html element
this._createElement();
// add element and custom properties definition to the options
o.el = this.el;
o.customProperties = {
...o.customProperties,
width: { type: 'unit' },
height: { type: 'unit' },
};
// super call on HTML
Super.init.call(this, o);
// add element to DOM - we have `_props` available now
this._props.parent.appendChild(this.el);
};
/**
* `_createElement` - function to create `Html` element.
*/
Surface._createElement = function () {
this.el = document.createElement('div');
};
/**
* `_declareDefaults` - Method to declare `_defaults`.
*
* @private
* @overrides @ ClassProto
*/
Surface._declareDefaults = function () {
// super call
Super._declareDefaults.call(this);
// save html related defaults
this._htmlDefaults = { ...this._defaults };
// declare surface defaults
this._defaults = {
...this._htmlDefaults,
// add surface properties
parent: document.body,
// `width` of the surface, fallbacks to `size`
width: 100,
// `height` of the surface, fallbacks to `size`
height: 100,
};
};
/**
* Imitate `class` with wrapper.
*
* @param {Object} Options object.
* @returns {Object} `Html` instance.
*/
const wrap = (o) => {
const instance = Object.create(Surface);
instance.init(o);
return instance;
};
wrap.__mojsClass = Surface;
export { wrap as Surface };