Phi: nicer scroll animation for METAR widget
[fg:fgdata.git] / webgui / widgets / metar.js
1 define([
2         'jquery', 'knockout', 'text!./metar.html' 
3 ], function(jquery, ko, htmlString) {
4
5     function ViewModel(params) {
6       var NO_METAR = "no METAR";
7       self.scrolledMetar = ko.observable("");
8       self.textStart = 0;
9       self.metar = ko.observable(NO_METAR);
10       self.valid = ko.observable(false).extend({ fgprop: 'metar-valid' });
11       self.valid.subscribe(function(newValue) {
12         self.textStart = 0;
13         if( false == newValue ) {
14           self.metar(NO_METAR);
15           return;
16         }
17         self.metar("Wait..");
18         jquery.get('/json/environment/metar/data', null, function(data) {
19           self.textStart = 0;
20           self.metar(data.value);
21         });
22       });
23
24       self.textLength = 20;
25       self.timerId = 0;
26       self.longTimeout = 1500;
27       self.shortTimeout = 50;
28
29       function scrollText ( id ){
30           if( id != self.timerId )
31               return;
32
33           var t = self.metar() + " " + self.metar();
34           var a = self.textStart;
35           var b = a+self.textLength;
36           self.scrolledMetar( t.substring(a,b) );
37           var timeout = t.charAt(a) == ' ' ? self.longTimeout : self.shortTimeout;
38           if( ++a  >= self.metar().length )
39             a = 0;
40           self.textStart = a;
41           setTimeout(function() { scrollText(id); }, timeout );
42       }
43
44       scrollText( ++self.timerId );
45     }
46
47     ViewModel.prototype.dispose = function() {
48       self.timerId++;
49     }
50
51     // Return component definition
52     return {
53         viewModel : ViewModel,
54         template : htmlString
55     };
56 });