muigui 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Gregg Tavares
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # muigui
2
+
3
+ > Nothing to see here...yet?
4
+
5
+ <img src="./images/muigui.png" style="max-width: 640px">
6
+
7
+ A simple Web UI library.
8
+
9
+ muigui is a simple UI library in the spirit of
10
+ [dat.gui](https://github.com/dataarts/dat.gui).
11
+
12
+ It is not a general purpose library for every type of GUI.
13
+ Rather, it is a small, easy to use library for small apps.
14
+ Basically I liked how simple it was to use dat.gui to add
15
+ a few sliders and options to a demo. What I didn't like is
16
+ that dat.gui is very opinionated on style. I thought I'd
17
+ try to make a DOM based UI with DOM elements only and then
18
+ require CSS to style it and see how far I got.
19
+
20
+ ### Not invented here syndrome
21
+
22
+ It's possible this already exists but if so I couldn't find it.
23
+ Most UI libraries seem to be giant and require a build step.
24
+ I wanted something hopefully not to big and something I could
25
+ easily add to any example with 1 file (or 2 if you add CSS).
26
+
27
+ ## License
28
+
29
+ [MIT](https://github.com/greggman/muigui/blob/main/LICENSE.md)
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "muigui",
3
+ "version": "0.0.1",
4
+ "description": "A Simple GUI",
5
+ "main": "muigui.js",
6
+ "module": "src/muigui.js",
7
+ "scripts": {
8
+ "build": "rollup -c",
9
+ "eslint": "eslint \"**/*.js\"",
10
+ "pre-push": "npm run eslint",
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/greggman/muigui.git"
16
+ },
17
+ "keywords": [
18
+ "muigui",
19
+ "gui",
20
+ "ui"
21
+ ],
22
+ "author": "Gregg Tavares",
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/greggman/muigui/issues"
26
+ },
27
+ "files": [
28
+ "muigui.js",
29
+ "src/**/*"
30
+ ],
31
+ "homepage": "https://github.com/greggman/muiguiy#readme",
32
+ "devDependencies": {
33
+ "eslint": "^7.5.0",
34
+ "eslint-plugin-html": "^6.0.2",
35
+ "eslint-plugin-optional-comma-spacing": "0.0.4",
36
+ "eslint-plugin-require-trailing-comma": "0.0.1",
37
+ "rollup": "^2.23.0",
38
+ "rollup-plugin-node-resolve": "^5.2.0"
39
+ }
40
+ }
@@ -0,0 +1,27 @@
1
+ export function createElem(tag, attrs = {}, children = []) {
2
+ const elem = document.createElement(tag);
3
+ for (const [key, value] of Object.entries(attrs)) {
4
+ if (typeof value === 'function' && key.startsWith('on')) {
5
+ const eventName = key.substring(2).toLowerCase();
6
+ elem.addEventListener(eventName, value, {passive: false});
7
+ } else if (typeof value === 'object') {
8
+ for (const [k, v] of Object.entries(value)) {
9
+ elem[key][k] = v;
10
+ }
11
+ } else if (elem[key] === undefined) {
12
+ elem.setAttribute(key, value);
13
+ } else {
14
+ elem[key] = value;
15
+ }
16
+ }
17
+ for (const child of children) {
18
+ elem.appendChild(child);
19
+ }
20
+ return elem;
21
+ }
22
+
23
+ export function addElem(tag, parent, attrs = {}, children = []) {
24
+ const elem = createElem(tag, attrs, children);
25
+ parent.appendChild(elem);
26
+ return elem;
27
+ }
@@ -0,0 +1,5 @@
1
+ let id = 0;
2
+
3
+ export function makeId() {
4
+ return `muigui-${++id}`;
5
+ }
@@ -0,0 +1,19 @@
1
+ export function removeElem(array, value) {
2
+ const ndx = array.indexOf(value);
3
+ if (ndx) {
4
+ array.splice(ndx, 1);
5
+ }
6
+ return array;
7
+ }
8
+
9
+ /**
10
+ * Converts an camelCase or snake_case id to "camel case" or "snake case"
11
+ * @param {string} id
12
+ */
13
+ const underscoreRE = /_/g;
14
+ const upperLowerRE = /([A-Z])([a-z])/g;
15
+ export function idToLabel(id) {
16
+ return id.replace(underscoreRE, ' ')
17
+ .replace(upperLowerRE, (m, m1, m2) => `${m1.toLowerCase()} ${m2}`);
18
+ }
19
+
package/src/muigui.js ADDED
@@ -0,0 +1,27 @@
1
+ import { createElem } from './libs/elem.js';
2
+ import Slider from './widgets/slider.js';
3
+ import Widget from './widgets/widget.js';
4
+
5
+ export function createWidget(object, property, ...args) {
6
+ const t = typeof object[property];
7
+ switch (t) {
8
+ case 'number':
9
+ return new Slider(object, property, ...args);
10
+ }
11
+ }
12
+
13
+ export class GUI extends Widget {
14
+ constructor() {
15
+ super({}, 'Controls', 'muigui-root');
16
+ document.body.appendChild(this.elem);
17
+ this._widgetsElem = createElem('div');
18
+ this.elem.appendChild(this._widgetsElem);
19
+ }
20
+ add(object, property, ...args) {
21
+ const widget = createWidget(object, property, ...args);
22
+ this._widgetsElem.appendChild(widget.elem);
23
+ return this;
24
+ }
25
+ }
26
+
27
+ export default GUI;
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ export function createSlider()
@@ -0,0 +1,41 @@
1
+ import {
2
+ createElem,
3
+ } from '../libs/elem.js';
4
+ import Widget from './widget.js';
5
+
6
+ export default class Slider extends Widget {
7
+ constructor(object, property, min = 0, max = 1, step = 0.01) {
8
+ super(object, property, 'muigui-slider');
9
+ const root = this.elem;
10
+ const id = this.id;
11
+
12
+ const value = this.getValue();
13
+ this._rangeElem = createElem('input', {
14
+ type: 'range',
15
+ min,
16
+ max,
17
+ value,
18
+ step,
19
+ id,
20
+ onInput: (e) => {
21
+ this.setValue(this._rangeElem.value);
22
+ }
23
+ });
24
+ root.appendChild(this._rangeElem);
25
+
26
+ this._textElem = createElem('input', {
27
+ type: 'number',
28
+ value: value,
29
+ onInput: (e) => {
30
+ this.setValue(parseFloat(this._textElem.value));
31
+ },
32
+ });
33
+ root.appendChild(this._textElem);
34
+ }
35
+ setValue(v) {
36
+ super.setValue(v);
37
+ const newV = super.getValue();
38
+ this._textElem.value = newV;
39
+ this._rangeElem.value = newV;
40
+ }
41
+ }
File without changes
@@ -0,0 +1,51 @@
1
+ import { createElem } from '../libs/elem.js';
2
+ import { makeId } from '../libs/ids.js';
3
+ import { idToLabel } from '../libs/utils.js';
4
+
5
+ export default class Widget {
6
+ constructor(object, property, className = '') {
7
+ this._object = object;
8
+ this._property = property;
9
+ this._changeFns = [];
10
+ this._id = makeId();
11
+ this._nameElem = createElem('label', {textContent: property, for: this._id});
12
+ this._root = createElem('div', {className: `muigui-widget`}, [this._nameElem]);
13
+ // we need the specialization to come last so it takes precedence.
14
+ if (className) {
15
+ this._root.classList.add(className);
16
+ }
17
+ }
18
+ get elem() {
19
+ return this._root;
20
+ }
21
+ get id() {
22
+ return this._id;
23
+ }
24
+ setJustValue(v) {
25
+ this._object[this._property] = v;
26
+ }
27
+ setValue(v) {
28
+ this._object[this._property] = v;
29
+ }
30
+ getValue(v) {
31
+ return this._object[this._property];
32
+ }
33
+ value(v) {
34
+ this.setValue(v);
35
+ return this;
36
+ }
37
+ onChange(fn) {
38
+ this.removeChange(fn);
39
+ this._changeFns.push(fn);
40
+ return this;
41
+ }
42
+ removeChange(fn) {
43
+ this._changeFns.remove(fn);
44
+ return this;
45
+ }
46
+ name(name) {
47
+ this._nameElem.textContent = name;
48
+ return this;
49
+ }
50
+ }
51
+