Difference between revisions of "Widget:SpaceAPI"
m (added processing class) |
(renamed SpaceState class to SpaceAPI for future purpose. added (space API) data.) |
||
Line 38: | Line 38: | ||
"use strict"; | "use strict"; | ||
− | if ( typeof | + | if ( typeof SpaceAPI === "undefined" ) |
{ | { | ||
− | window. | + | window.SpaceAPI = function( _width, _height, _float, _padding, _url, _interval ) |
{ | { | ||
this._width = _width; | this._width = _width; | ||
Line 50: | Line 50: | ||
} | } | ||
− | + | SpaceAPI.prototype.data = null; | |
− | + | SpaceAPI.prototype._width = null; | |
− | + | SpaceAPI.prototype._height = null; | |
− | + | SpaceAPI.prototype._float = null; | |
− | + | SpaceAPI.prototype._url = null; | |
− | + | SpaceAPI.prototype._interval = null; | |
− | + | SpaceAPI.prototype._intervalId = null; | |
− | + | SpaceAPI.prototype._node = null; | |
− | + | SpaceAPI.prototype._msgLoading = "Loading.."; | |
− | + | SpaceAPI.prototype._msgError = "Error"; | |
− | + | SpaceAPI.prototype._msgParserError = "Failed to parse space state information"; | |
− | + | SpaceAPI.prototype._msgOpen = "Open"; | |
− | + | SpaceAPI.prototype._msgClosed = "Closed"; | |
− | + | SpaceAPI.prototype._msgUnknown = "Unknown"; | |
− | + | SpaceAPI.prototype._msgSince = "Since: "; | |
− | + | SpaceAPI.prototype._colorOpen = "#0f0"; | |
− | + | SpaceAPI.prototype._colorClosed = "#f00"; | |
+ | SpaceAPI.prototype._colorUnknown = "#f70"; | ||
− | + | SpaceAPI.prototype.start = function( ) | |
{ | { | ||
// Use interval timer id as image id | // Use interval timer id as image id | ||
this._intervalId = 0; | this._intervalId = 0; | ||
if ( this._interval > 0 ) | if ( this._interval > 0 ) | ||
− | this._intervalId = setInterval( this. | + | this._intervalId = setInterval( this._fetchState.bind( this ), this._interval ); |
var style = "width:" + this._width + ";height:" + this._height + ";padding:" + this._padding + ";text-align:center;-moz-box-shadow: 3px 3px 4px #000;-webkit-box-shadow: 3px 3px 4px rgba(0,0,0,0.2);box-shadow: 3px 3px 4px rgba(0,0,0,0.2);position:relative;"; | var style = "width:" + this._width + ";height:" + this._height + ";padding:" + this._padding + ";text-align:center;-moz-box-shadow: 3px 3px 4px #000;-webkit-box-shadow: 3px 3px 4px rgba(0,0,0,0.2);box-shadow: 3px 3px 4px rgba(0,0,0,0.2);position:relative;"; | ||
Line 82: | Line 83: | ||
this._node = document.getElementById( "spaceAPI" + this._intervalId ); | this._node = document.getElementById( "spaceAPI" + this._intervalId ); | ||
− | // Update the | + | // Update the space state immediately |
− | setTimeout( this. | + | setTimeout( this._fetchState.bind( this ), 1 ); |
}; | }; | ||
− | + | SpaceAPI.prototype._fetchState = function( ) | |
{ | { | ||
this._node.className = "processing"; | this._node.className = "processing"; | ||
Line 119: | Line 120: | ||
}; | }; | ||
− | + | SpaceAPI.prototype._updateState = function( _message, _color, _title ) | |
{ | { | ||
this._node.className = ""; | this._node.className = ""; | ||
Line 130: | Line 131: | ||
}; | }; | ||
− | + | SpaceAPI.prototype._xhr_onload = function( _event ) | |
{ | { | ||
− | |||
var open = null; | var open = null; | ||
var message = null; | var message = null; | ||
Line 139: | Line 139: | ||
try | try | ||
{ | { | ||
− | + | this.data = JSON.parse( _event.target.responseText ); | |
− | open = | + | open = this.data.state.open; |
− | message = | + | message = this.data.state.message; |
// Start as epoch timestamp (NOTE: check if timezone doesn't mess things up) | // Start as epoch timestamp (NOTE: check if timezone doesn't mess things up) | ||
var d = new Date( 0 ); | var d = new Date( 0 ); | ||
− | d.setUTCSeconds( | + | d.setUTCSeconds( this.data.state.lastchange ); |
title = this._msgSince + d.toLocaleString( ); | title = this._msgSince + d.toLocaleString( ); | ||
} | } | ||
Line 154: | Line 154: | ||
if ( open ) | if ( open ) | ||
− | this. | + | this._updateState( message || this._msgOpen, this._colorOpen, title ); |
else if ( open === false ) | else if ( open === false ) | ||
− | this. | + | this._updateState( message || this._msgClosed, this._colorClosed, title ); |
else | else | ||
− | this. | + | this._updateState( message || this._msgUnknown, this._colorUnknown, title ); |
}; | }; | ||
− | + | SpaceAPI.prototype._xhr_onerror = function( ) | |
{ | { | ||
// Something has failed, show the error | // Something has failed, show the error | ||
− | this. | + | this._updateState( this._msgError, this._colorUnknown ); |
} | } | ||
} | } | ||
− | var state = new | + | var state = new SpaceAPI( "<!--{$width|escape:html|default:auto}-->", "<!--{$height|escape:html|default:auto}-->", "<!--{$float|escape:html|default:none}-->", "<!--{$padding|escape:html|default:8px}-->", "<!--{$url|escape:urlpathinfo}-->", <!--{$interval|validate:int|default:0}--> ); |
state.start(); | state.start(); | ||
Revision as of 16:53, 10 May 2016
This widget allows you to display the Space API data (provided as JSON)
Created by Xopr
Using this widget
To insert this widget, use the following code:
{{#widget:SpaceAPI |url=/spaceAPI/ |width=260px |height=20px |padding=8px |interval=20 |float=right }}
This will give the following result:
Notes
- url is mandatory, the rest is optional (leave out interval to make the data static).
- it also must be written without protocol since colon (:) is not allowed, and may be relative, for example: //ackspace.nl/spaceAPI/ or /spaceAPI/
- You must provide a unit for the sizes (i.e. px, %, etc.)
Copy to your site
To use this widget on your site, just install MediaWiki Widgets extension and copy full source code of this page to your wiki as Widget:SpaceAPI article.