This article explains how to create and use a JavaScript Network signal component in a Web Runtime widget. This example uses WRT 1.0 SystemInfo Service API, and is so compatible with both WRT 1.0 and 1.1 versions.
Contents |
The following steps are necessary in order to use the Network Signal component in a WRT Widget:
<script language="javascript" type="text/javascript" src="networksignal_component.js"></script>
<body>
[...]
<div id="signal_holder"></div>
[...]
</body>
<embed type="application/x-systeminfo-widget" hidden="yes"></embed>
Once instantiated, append the NetworkSignal instance to the parent element defined above:
var signal = new NetworkSignal('images/signal_empty.png', 'images/signal_full.png', 72, 168);
signal.appendTo(document.getElementById('signal_holder'));
Let's start implementing the NetworkSignal constructor, and a first method that will build up the component DOM structure.
/**
*
* @param {String} emptySignalSrc
* @param {String} fullSignalSrc
* @param {Number} signalWidth
* @param {Number} signalHeight
*/
function NetworkSignal(emptySignalSrc, fullSignalSrc, signalWidth, signalHeight)
{
this.signalHeight = signalHeight;
this.signalWidth = signalWidth;
this.emptySignalElement = null;
this.domElement = null;
this.interval = null;
this.init(emptySignalSrc, fullSignalSrc);
}
NetworkSignal.prototype.init = function(emptySignalSrc, fullSignalSrc)
{
var el = document.createElement('div');
el.style.position = 'relative';
var fullImage = document.createElement('img');
fullImage.style.position = 'absolute';
fullImage.src = fullSignalSrc;
this.emptySignalElement = document.createElement('div');
this.emptySignalElement.style.position = 'absolute';
this.emptySignalElement.style.overflow = 'hidden';
this.emptySignalElement.style.width = this.signalWidth + 'px';
this.emptySignalElement.style.height = '0px';
var emptyImage = document.createElement('img');
emptyImage.src = emptySignalSrc;
this.emptySignalElement.appendChild(emptyImage);
el.appendChild(fullImage);
el.appendChild(this.emptySignalElement);
this.domElement = el;
}
Apart from calling the init() method, that creates the component DOM structure, the constructor stores the images width and height for later usage, and defines an interval variable that will be used by methods defined below.
To access SystemInfo Service API objects and properties, the following HTML code snippet must be embedded in the Widget's HTML code:
<embed type="application/x-systeminfo-widget" hidden="yes"></embed>
Done this, it is possible to use SystemInfo Service API.
/**
*
* @param {Node} parentElement
*/
NetworkSignal.prototype.appendTo = function(parentElement)
{
parentElement.appendChild(this.domElement);
this.startService();
}
/* WRT 1.0 service */
NetworkSignal.prototype.startService = function()
{
var sysinfo = document.embeds[0];
if(sysinfo)
{
var self = this;
var signalHandler = function()
{
var bars = sysinfo.signalbars;
if(bars == undefined || bars < 0)
bars = 0;
var value = bars * 100 / 7;
self.updateSignalValue(value);
};
signalHandler();
this.interval = setInterval(signalHandler, 2000);
}
}
NetworkSignal.prototype.stopService = function()
{
if(this.interval != null)
{
clearInterval(this.interval);
this.interval = null;
}
}
In the startService() method, an anonymous function is defined and referenced by the signalHandler variable. This function is periodically called to check the network signal, and then it calls the updateSignalValue() method to update the component UI with the latest retrieved value:
NetworkSignal.prototype.updateSignalValue = function(value)
{
this.emptySignalElement.style.height = Math.round(((100 - value) * this.signalHeight) / 100) + 'px';
}
Here's a list of related downloads:
No related wiki articles found