I created a ui-template node for my dashboard containing some code to create a clock with a clickable Home icon on the upper-right corner of my dashboard. The node is being added to the site header. The clock and Home icons display just fine but I"m having trouble making the home icon send you back to the homepage when clicked. In my code, I am able to fire off an event when the icon is clicked, but I get an error saying that send({payload:"Home"}) function is not defined
. here's the code for the template node below:
<script type="text/javascript">
var theScope = null;
$(function(scope)
{
theScope = scope;
if($('.md-toolbar-tools').length != 0)
loadClock();
else
setTimeout(loadClock, 500);
});
function loadClock(){
$('#clock').remove();
var toolbar = $('.md-toolbar-tools');
var div = $('<div/ class="clock-style">');
var p = $('<p/ id="clock">');
div.append(p);
div[0].style.margin = '5px 5px 5px auto';
toolbar.append(div);
// Add element for clickable home icon if not already present
var i = document.createElement('i');
i.classList.add("fa");
i.classList.add("fa-home");
i.classList.add("fa-2x");
i.classList.add("icon-padding");
i.addEventListener('click', sendPayload);
toolbar.append(i);
function displayTitle(lh) {
p.text(lh);
}
function upTime() {
var d = new Date();
p.text(d.toLocaleTimeString([], {hour: 'numeric', minute:'2-digit'}));
}
if(document.clockInterval){
clearInterval(document.clockInterval);
document.clockInterval = null;
}
document.clockInterval = setInterval(upTime, 1000);
function sendPayload(){
theScope.send({payload:"Home"});
}
}
</script>
Any idea what I'm doing wrong? I just want to send a message to the UI node to change the dashboard page.
NOTE: Above code edited to include missing assignment statement for the theScope
variable inside of the main function.