Skip to content

Instantly share code, notes, and snippets.

@queerviolet
Last active August 29, 2015 14:16
Show Gist options
  • Save queerviolet/a4522c025a55a26e8efd to your computer and use it in GitHub Desktop.
Save queerviolet/a4522c025a55a26e8efd to your computer and use it in GitHub Desktop.
Javascript UI modules
<!doctype html>
<html>
<head>
<script src="using-onload.js"></script>
<script>
new Status.Error("Something went wrong");
new Status.Error("I feel confused");
new Status.Error("Rampancy emminent.");
</script>
<style>
.butter-bar {
background: fuchsia;
color: black;
}
x-error {
display: block;
}
</style>
</head>
<body>
<div class="status-bar"></div>
<script>
console.log(Status.getLog());
</script>
</body>
</html>
"use strict";
var Status = (function() {
var log = [];
var statusbar;
function append(node) {
log.push(node);
if (statusbar) {
statusbar.appendChild(node);
}
}
function Error(msg) {
this.msg = msg;
var node = document.createElement('x-error');
node.textContent = msg;
append(node);
}
function main() {
statusbar = document.querySelector('.status-bar');
// play any messages posted before the document was ready into
// the status bar.
for (var i = 0; i != log.length; ++i) {
statusbar.appendChild(log[i]);
}
}
window.addEventListener('load', main);
return {
Error: Error,
getLog: function() {
return log;
},
};
})();
<!doctype html>
<html>
<head>
<script src="using-register-element.js"></script>
<script>
new Status.Error("Something went wrong");
new Status.Error("I feel confused");
new Status.Error("Rampancy emminent.");
</script>
<style>
status-bar {
display: block;
background: fuchsia;
color: black;
}
x-error {
display: block;
}
</style>
</head>
<body>
<status-bar></status-bar>
<script>
console.log(Status.getLog());
</script>
</body>
</html>
"use strict";
var Status = (function() {
var log = [];
var statusbar;
function append(node) {
log.push(node);
if (statusbar) {
statusbar.appendChild(node);
}
}
function Error(msg) {
this.msg = msg;
var node = document.createElement('x-error');
node.textContent = msg;
append(node);
}
var StatusBar = Object.create(HTMLElement.prototype);
StatusBar.createdCallback = function() {
statusbar = this;
for (var i = 0; i != log.length; ++i) {
this.appendChild(log[i]);
}
}
document.registerElement('status-bar', {prototype: StatusBar});
return {
Error: Error,
getLog: function() {
return log;
},
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment