Log updates
[beagleboard.org.git] / static / beagle-ui.js
blob3e7aa08cc3f61477e728221798a81f484ccecc5e
1 var name = "#floatMenu";  
2 var menuYloc = null;  
3   
4 $(document).ready(function(){
5     if($(name).length) {
6         menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
7         $(window).scroll(function () {
8             var offset = menuYloc+$(document).scrollTop()+"px";
9             $(name).animate({top:offset},{duration:500,queue:false});
10         });
11     }
12 });  
14 $(document).ready(function(){
15     $('.cloud9-url').each(function() {
16         this.href = 'http://' + window.location.host + ':3000';
17     });
18     $('.gateone-url').each(function() {
19         this.href = 'https://' + window.location.host;
20     });
21 });
23 $(function() {
24     if($('#accordian').length) {
25         $("#accordion").accordion({
26             collapsible: true
27         });
28     }
29 });
31 $(document).ready(function(){
32     if($('#side-menu').length) {
33         $.get('/Support/BoneScript/menu', function(data){
34             $('#side-menu').replaceWith(data);
35         });
36     }
37 });
39 $(document).ready(function(){
40     if($('#connect-status').length) {
41         var connectState = 'init';
42         var statusDisconnected = '' +
43             '<div id="connect-status">' +
44             '    <div class="browser-connect">' +
45             '        <img alt="Not Connected" src="/static/images/usb.png" border="0">' +
46             '        <div id="browser-content"><strong>Did you know?</strong>  This page can interact with your BeagleBone<br />' +
47             'Type in your BeagleBone&#39;s IP address here:<input id="connect-ip"></input>' +
48             '        </div>' +
49             '    </div>' +
50             '</div>';
51         var statusConnected = '' +
52             '<div id="connect-status">' +
53             '    <div class="browser-connected">' +
54             '        <img alt="Connected" src="/static/images/green_check.png" border="0">' +
55             '        <div id="browser-content"><strong>Your board is connected!</strong><br>' +
56             '            <div id="board-info"></div>' +
57             '        </div>' +
58             '    </div>' +
59             '</div>';
60         var i = 0;
61         var serversToTry = [
62             window.location.host,
63             '192.168.7.2',
64             'beaglebone.local',
65             'beaglebone-2.local'
66         ];
67         $('#connect-status').replaceWith(statusDisconnected);
69         // note, due to a bug in Firefox, the call is moved below
71         function testForConnection() {
72             var handlers = {};
73             handlers.callback = callback;
74             handlers.initialized = initialized;
75             handlers.connecting = disconnected;
76             handlers.connect_failed = connect_failed;
77             handlers.reconnect_failed = disconnected;
78             handlers.disconnect = disconnected;
79             handlers.connect = connected;
80             handlers.reconnect = connected;
81             handlers.reconnecting = connected;
82             $('#connect-ip').keypress(oninput);
83             setTargetAddress(serversToTry[i], handlers);
84             i++;
85             if(i >= serversToTry.length) i = 0;
87             function oninput(e) {
88                 if(e.which == 10 || e.which == 13) {
89                     var givenAddress = $('#connect-ip').val();
90                     setTargetAddress(givenAddress, handlers);
91                     serversToTry = [ givenAddress ];
92                 }
93             }
95             function callback() {
96                 if(typeof _bonescript == 'undefined') {
97                     setTimeout(testForConnection, 1000);
98                 }
99             }
100             function connected() {
101                 if(connectState == 'disconnected') {
102                     console.log('Bonescript: connected');
103                     connectState = 'reconnecting';
104                 }
105             }
106             function initialized() {
107                 console.log('Bonescript: initialized');
108                 $('#connect-status').replaceWith(statusConnected);
109                 updateBoardInfo();
110                 if(typeof onbonescriptinit == 'function') onbonescriptinit();
111                 connectState = 'connected';
112             }
113             function disconnected() {
114                 if(connectState == 'connected') {
115                     console.log('Bonescript: disconnected');
116                     $('#connect-status').replaceWith(statusDisconnected);
117                     connectState = 'disconnected';
118                 }
119             }
120             function connect_failed() {
121                 if(connectState == 'init') {
122                     _onSocketIOLoaded_workaround();
123                 } else {
124                     disconnected();
125                 }
126             }
127         }
129         testForConnection();
130     }
133 function updateBoardInfo() {
134     var b = require('bonescript');
135     b.getPlatform(function(x) {
136         var info = '<div id="board-info">' + x.name;
137         if(typeof x.version != 'undefined')
138             info += ' rev ' + x.version;
139         if(typeof x.serialNumber != 'undefined')
140             info += ' S/N ' + x.serialNumber;
141         if(typeof x.bonescript != 'undefined')
142             info += ' running BoneScript ' + x.bonescript;
143         if(typeof _bonescript.address != 'undefined')
144             info += ' at ' + _bonescript.address;
145         info += '</div>';
146         $('#board-info').replaceWith(info);
147     });
150 function _onSocketIOLoaded_workaround() {
151     //console.log("socket.io loaded");
152     var socket_addr = 'http://' + _bonescript.address + ':80';
153     var socket = io.connect(socket_addr);
154     socket.on('require', getRequireData);
155     socket.on('bonescript', _seqcall);
156     socket.on('connect', _bonescript.on.connect);
157     socket.on('connecting', _bonescript.on.connecting);
158     socket.on('disconnect', _bonescript.on.disconnect);
159     socket.on('connect_failed', _bonescript.on.connect_failed);
160     socket.on('error', _bonescript.on.error);
161     socket.on('reconnect', _bonescript.on.reconnect);
162     socket.on('reconnect_failed', _bonescript.on.reconnect_failed);
163     socket.on('reconnecting', _bonescript.on.reconnecting);
164     socket.on('initialized', _bonescript.on.initialized);
166     function getRequireData(m) {
167         if(!m.module || !m.data)
168             throw('Invalid "require" message sent for "' + m.module + '"');
169         //console.log('Initialized module: ' + m.module);
170         _bonescript.modules[m.module] = {};
171         for(var x in m.data) {
172             if(!m.data[x].type || !m.data[x].name || (typeof m.data[x].value == 'undefined'))
173                 throw('Invalid data in "require" message sent for "' + m.module + '.' + m.data[x] + '"');
174             if(m.data[x].type == 'function') {
175                 // define the function
176                 if(!m.data[x].value)
177                     throw('Missing args in "require" message sent for "' + m.module + '.' + m.data[x] + '"');
178                 var myargs = m.data[x].value;
180                 // eval of objString builds the call data out of arguments passed in
181                 var objString = '';
182                 for(var y in myargs) {
183                     if(isNaN(y)) continue;  // Need to find the source of this bug
184                     if(myargs[y] == 'callback') continue;
185                     objString += ' if(typeof ' + myargs[y] + ' == "function") {\n';
186                     objString += '  ' + myargs[y] + ' = ' + myargs[y] + '.toString();\n';
187                     objString += ' }\n';
188                     objString += ' calldata.' + myargs[y] + ' = ' + myargs[y] + ';\n';
189                 }
190                 var argsString = myargs.join(', ');
191                 var handyfunc = '_bonescript.modules["' + m.module + '"].' + m.data[x].name +
192                     ' = ' +
193                     'function (' + argsString + ') {\n' +
194                     ' var calldata = {};\n' +
195                     objString +
196                     ' if(callback) {\n' +
197                     '  _bonescript._callbacks[_bonescript._seqnum] = callback;\n' +
198                     '  calldata.seq = _bonescript._seqnum;\n' +
199                     '  _bonescript._seqnum++;\n' +
200                     ' }\n' +
201                     ' socket.emit("' + m.module + '$' + m.data[x].name + '", calldata);\n' +
202                     '};\n';
203                 eval(handyfunc);
204             } else {
205                 _bonescript.modules[m.module][m.data[x].name] = m.data[x].value;
206             }
207         }
209         // Work-around to add shell command
210         _bonescript.modules[m.module]["socket"] = socket;
211         _bonescript.modules[m.module]["shell"] = function(command) {
212             socket.emit('shell', command);
213         }
214         
215         // Call-back initialized function
216         _bonescript.on.initialized();
217     }