2015-03-05 13:58:48 +00:00
|
|
|
define([
|
|
|
|
'jquery', 'knockout', 'text!./metar.html'
|
|
|
|
], function(jquery, ko, htmlString) {
|
|
|
|
|
|
|
|
function ViewModel(params) {
|
|
|
|
var NO_METAR = "no METAR";
|
|
|
|
self.scrolledMetar = ko.observable("");
|
|
|
|
self.textStart = 0;
|
|
|
|
self.metar = ko.observable(NO_METAR);
|
|
|
|
self.valid = ko.observable(false).extend({ fgprop: 'metar-valid' });
|
|
|
|
self.valid.subscribe(function(newValue) {
|
|
|
|
self.textStart = 0;
|
|
|
|
if( false == newValue ) {
|
|
|
|
self.metar(NO_METAR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.metar("Wait..");
|
|
|
|
jquery.get('/json/environment/metar/data', null, function(data) {
|
|
|
|
self.textStart = 0;
|
|
|
|
self.metar(data.value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
self.textLength = 20;
|
2015-03-06 08:36:09 +00:00
|
|
|
self.timerId = 0;
|
|
|
|
self.longTimeout = 1500;
|
|
|
|
self.shortTimeout = 50;
|
|
|
|
|
|
|
|
function scrollText ( id ){
|
|
|
|
if( id != self.timerId )
|
|
|
|
return;
|
|
|
|
|
2015-03-05 13:58:48 +00:00
|
|
|
var t = self.metar() + " " + self.metar();
|
|
|
|
var a = self.textStart;
|
|
|
|
var b = a+self.textLength;
|
|
|
|
self.scrolledMetar( t.substring(a,b) );
|
2015-03-06 08:36:09 +00:00
|
|
|
var timeout = t.charAt(a) == ' ' ? self.longTimeout : self.shortTimeout;
|
2015-03-05 13:58:48 +00:00
|
|
|
if( ++a >= self.metar().length )
|
|
|
|
a = 0;
|
|
|
|
self.textStart = a;
|
2015-03-06 08:36:09 +00:00
|
|
|
setTimeout(function() { scrollText(id); }, timeout );
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollText( ++self.timerId );
|
2015-03-05 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewModel.prototype.dispose = function() {
|
2015-03-06 08:36:09 +00:00
|
|
|
self.timerId++;
|
2015-03-05 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return component definition
|
|
|
|
return {
|
|
|
|
viewModel : ViewModel,
|
|
|
|
template : htmlString
|
|
|
|
};
|
|
|
|
});
|