Skip to content

Commit

Permalink
[fix] Destroy event and example
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Aug 6, 2019
1 parent 0b920d4 commit 15f3c26
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
59 changes: 59 additions & 0 deletions examples/destroy-object.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8" />
<title>Refresh object</title>
<meta name="viewport" content="width=device-width">
<style>
.container {
width: 450px;
margin: 50px auto 0 auto;
text-align: center;
}

.gauge {
width: 450px;
height: 450px;
}

a:link.button,
a:active.button,
a:visited.button,
a:hover.button {
margin: 30px 5px 0 2px;
padding: 7px 13px;
}
</style>
</head>

<body>
<div class="container">
<div id="g1" class="gauge"></div>
<a href="#" id="g1_destroy" class="button grey">Destroy Object</a>
</div>
<script src="../raphael-2.1.4.min.js"></script>
<script src="../justgage.js"></script>
<script>

document.addEventListener("DOMContentLoaded", function(event) {

var g1 = new JustGage({
id: "g1",
title: "Max is 100.",
value: 50,
min: 0,
max: 100,
decimals: 0,
gaugeWidthScale: 0.6
});

document.getElementById('g1_destroy').addEventListener('click', function() {
g1.destroy()
});

});
</script>
</body>

</html>
12 changes: 5 additions & 7 deletions justgage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ JustGage = function(config) {
return false;
}

var node;

if (config.id !== null && config.id !== undefined) {
node = document.getElementById(config.id);
if (!node) {
obj.node = document.getElementById(config.id);
if (!obj.node) {
console.log('* justgage: No element with id : %s found', config.id);
return false;
}
} else if (config.parentNode !== null && config.parentNode !== undefined) {
node = config.parentNode;
obj.node = config.parentNode;
} else {
console.log('* justgage: Make sure to pass the existing element id or parentNode to the constructor.');
return false;
}

var dataset = node.dataset ? node.dataset : {};
var dataset = obj.node.dataset ? obj.node.dataset : {};

// check for defaults
var defaults = (config.defaults !== null && config.defaults !== undefined) ? config.defaults : false;
Expand Down Expand Up @@ -905,7 +903,7 @@ JustGage.prototype.refresh = function(val, max) {

/** Destroy gauge object */
JustGage.prototype.destroy = function() {
document.getElementById(this.config.id).innerHTML = '';
if(this.node && this.node.parentNode) this.node.innerHTML = ''
};

/** Generate shadow */
Expand Down

0 comments on commit 15f3c26

Please sign in to comment.