Skip to content

Commit 7bfb562

Browse files
committed
u-toast implementation
1 parent b48153b commit 7bfb562

6 files changed

+166
-33
lines changed

elements.json

+13-11
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
},
2525

2626
"element": {
27-
"properties": ["loglevel", "title", "description"],
27+
"properties": ["title", "description", "room", "loglevel", "startup"],
2828
"actions": ["start", "stop"]
2929
},
3030

3131
"sensor": {
32-
"properties": ["warmuptime", "readtime", "resendtime", "restart"]
32+
"properties": ["readtime", "resendtime", "warmuptime", "restart"]
3333
},
3434

3535
"analog": { "extends": "sensor",
@@ -92,22 +92,24 @@
9292
"events": ["onvalue", "onhigh", "onlow"]
9393
},
9494

95-
"displaydot": {
95+
"displayoutput": {
9696
"sys": "false",
97-
"properties": ["page", "x", "y", "color"],
97+
"properties": ["x", "y", "w", "h", "page", "color", "background", "border"],
9898
"actions": ["value", "clear", "redraw"]
9999
},
100100

101-
"displaytext": {
101+
"displaydot": { "extends": "displayoutput" },
102+
103+
"displayrect": { "extends": "displayoutput" },
104+
105+
"displaytext": { "extends": "displayoutput",
102106
"sys": "false",
103-
"properties": ["page", "x", "y", "color", "fontsize", "prefix", "postfix"],
104-
"actions": ["value", "clear", "redraw"]
107+
"properties": ["fontsize", "prefix", "postfix"]
105108
},
106109

107-
"displayline": {
110+
"displayline": { "extends": "displayoutput",
108111
"sys": "false",
109-
"properties": ["page", "x", "y", "x1", "y1", "color"],
110-
"actions": ["redraw"]
112+
"properties": ["x1", "y1"]
111113
},
112114

113115
"displaybutton": {
@@ -237,7 +239,7 @@
237239
},
238240

239241
"dht": { "extends": "sensor", "ui": "air",
240-
"properties": ["pin"],
242+
"properties": ["pin", "type", "powerpin", "powerinvert"],
241243
"events": ["ontemperature", "onhumidity"]
242244
},
243245

elementsvg.js

+27-20
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,34 @@ function createBox(tName, txt) {
1414
pObj.appendChild(cn);
1515

1616
// document.documentElement.setAttribute('viewBox', '0 0 120 ' + (y + 20));
17-
document.documentElement.setAttribute('height', (2*(y + 18)));
17+
document.documentElement.setAttribute('height', (2 * (y + 18)));
1818
y += 12;
1919
}
2020

21-
function create(def) {
22-
// remove old blocks
23-
pObj.querySelectorAll('g.block').forEach(function (x) {
24-
pObj.removeChild(x)
25-
});
26-
y = 12;
21+
function create(def, bClean = false) {
22+
if (bClean) {
23+
// remove old blocks
24+
pObj.querySelectorAll('g.block').forEach(function(x) {
25+
pObj.removeChild(x);
26+
});
27+
y = 12;
28+
}
2729

2830
// header
2931
pObj.querySelector('text').textContent = def.name;
3032
var iconName = def.icon || def.name;
3133
pObj.querySelector('use').setAttribute("href", "/icons.svg#" + iconName);
3234

3335
if (def.properties)
34-
def.properties.forEach(function (e) {
36+
def.properties.forEach(function(e) {
3537
createBox("property", e);
3638
});
3739
if (def.events)
38-
def.events.forEach(function (e) {
40+
def.events.forEach(function(e) {
3941
createBox("event", e);
4042
});
4143
if (def.actions)
42-
def.actions.forEach(function (e) {
44+
def.actions.forEach(function(e) {
4345
createBox("action", e);
4446
});
4547
}
@@ -48,18 +50,23 @@ document.api = {
4850
create: create
4951
};
5052

51-
window.addEventListener("load", function () {
53+
window.addEventListener("load", async function() {
5254
var s = document.location.search;
5355
if (s.length > 1) {
5456
var qElem = s.substring(1);
55-
fetch('elements.json')
56-
.then(function (result) { return result.json(); })
57-
.then(function (e) {
58-
var def = e[qElem];
59-
if (def) {
60-
def.name = qElem;
61-
create(def);
62-
}
63-
});
57+
58+
let e = await fetch('elements.json');
59+
e = await e.json();
60+
var def = e[qElem];
61+
if (def) {
62+
// create element level
63+
create(e["element"], true);
64+
65+
if (def.extends)
66+
create(e[def.extends]);
67+
68+
def.name = qElem;
69+
create(def);
70+
}
6471
}
6572
});

packdist.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import yargs from 'yargs';
1010
import debug from 'debug';
1111

12-
import sass from 'sass';
12+
import * as sass from 'sass';
1313
import * as HTMLMinifier from 'html-minifier-terser';
1414
import * as JSMinifier from 'terser';
1515

@@ -129,6 +129,9 @@ const assets = [
129129
// { m: 'xml', src: 'i/no.svg' },
130130
{ m: 'css', src: 'iotstyle.scss', tar: 'iotstyle.css' },
131131

132+
{ m: 'm', src: 'u-toast.css', tar: 'u-toast.css' },
133+
{ m: 'js', src: 'u-toast.js' },
134+
132135
{ m: 'xml', src: 'chartline.svg' },
133136
{ m: 'js', src: 'chartline.js' },
134137
{ m: 'xml', src: 'chartpie.svg' },

packminimal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import yargs from 'yargs';
1010
import debug from 'debug';
1111

12-
import sass from 'sass';
12+
import * as sass from 'sass';
1313
import * as HTMLMinifier from 'html-minifier-terser';
1414
import * as JSMinifier from 'terser';
1515

u-toast.css

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* styles for u-toast */
2+
3+
u-toast {
4+
display: block;
5+
position: absolute;
6+
width: 320px;
7+
top: 1rem;
8+
right: 1rem;
9+
10+
.u-box {
11+
position: relative;
12+
transition: all 0.5s linear;
13+
overflow-y: hidden;
14+
padding-bottom: 1em;
15+
16+
&.u-fade {
17+
height: 0;
18+
padding-bottom: 0;
19+
}
20+
21+
>.u-text {
22+
padding: 0.25em;
23+
border-radius: 0.4em;
24+
color: black;
25+
background-color: white;
26+
border-left: 0.8em solid gray;
27+
}
28+
29+
>button {
30+
position: absolute;
31+
right: 0;
32+
top: 0.2em;
33+
padding: 0;
34+
border: 0;
35+
min-width: 1em;
36+
color: black;
37+
background-color: white;
38+
}
39+
}
40+
41+
.u-text.u-log {
42+
border-color: black;
43+
}
44+
45+
.u-text.u-info {
46+
border-color: #00cc00;
47+
}
48+
49+
.u-text.u-error {
50+
border-color: #cc0000;
51+
}
52+
}

u-toast.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// micro toast notification implementation
2+
3+
class ToastMessageComponent extends HTMLElement {
4+
5+
defaults = {
6+
design: "u-log",
7+
duration: 3000,
8+
close: false
9+
};
10+
11+
_closeToast(box) {
12+
box.style.height = '';
13+
box.classList.add('u-fade');
14+
}
15+
16+
// create a box for logs, alerts...
17+
_box(msg, options) {
18+
options = Object.assign({}, this.defaults, options);
19+
20+
const box = document.createElement('div');
21+
box.className = 'u-box';
22+
23+
const txt = document.createElement('div');
24+
txt.className = 'u-text';
25+
txt.classList.add(options.design);
26+
box.appendChild(txt);
27+
txt.innerText = msg;
28+
this.appendChild(box);
29+
30+
// set height of box to the full value
31+
window.setTimeout(() => {
32+
box.style.height = box.clientHeight + 'px';
33+
}, 100);
34+
35+
if (options.close) {
36+
// add close button to box
37+
const b = document.createElement('button');
38+
b.textContent = 'x';
39+
box.appendChild(b);
40+
b.addEventListener("click", () => {
41+
this._closeToast(box);
42+
}, box);
43+
}
44+
45+
// close box after duration ....
46+
if (options.duration) {
47+
window.setTimeout(() => { this._closeToast(box); }, options.duration);
48+
}
49+
50+
// finally remove box
51+
box.addEventListener("transitionend", () => { box.remove(); }, box);
52+
53+
}
54+
55+
// pre-configured toastr types.
56+
log(msg) {
57+
this._box(msg);
58+
}
59+
60+
info(msg) {
61+
this._box(msg, { design: 'u-info' });
62+
}
63+
64+
error(msg) {
65+
this._box(msg, { design: 'u-error', close: true, duration: 8000 });
66+
}
67+
}
68+
69+
customElements.define('u-toast', ToastMessageComponent);

0 commit comments

Comments
 (0)