0
0
mirror of https://github.com/Doodle3D/doodle3d-connect.git synced 2024-06-26 17:41:22 +02:00
doodle3d-connect/js/main.js

108 lines
2.8 KiB
JavaScript
Raw Normal View History

2013-10-02 17:24:40 +02:00
2013-10-03 16:59:28 +02:00
var retrieveListInterval = 5000;
2013-10-02 17:24:40 +02:00
var retrieveListDelay; // retry setTimout instance
2013-10-03 18:06:05 +02:00
var boxTimeoutTime = 500;
2013-10-02 17:24:40 +02:00
var numBoxesChecking = 0; // count how many boxes we are checking
var numBoxesFound = 0; // count how many boxes responded
var $list;
2013-10-02 17:24:40 +02:00
var $intro;
var $preloader;
var spinner;
$(function() {
//console.log("ready");
2013-10-02 17:24:40 +02:00
$intro = $("#intro");
$list = $("#list");
$preloader = $("#preloader");
var spinnerSettings = {
lines: 13, // The number of lines to draw
length: 5, // The length of each line
width: 3, // The line thickness
radius: 7, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
2013-10-03 16:59:28 +02:00
color: '#57BF42', // #rgb or #rrggbb or array of colors
2013-10-02 17:24:40 +02:00
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: false, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
zIndex: 2e9, // The z-index (defaults to 2000000000)
top: 'auto', // Top position relative to parent in px
left: 'auto' // Left position relative to parent in px
};
spinner = new Spinner(spinnerSettings);
spinner.spin($preloader[0]);
retrieveList();
})
function retrieveList() {
2013-10-02 17:24:40 +02:00
$preloader.show();
//spinner.spin($preloader[0]);
$.ajax({
2013-10-03 16:59:28 +02:00
url: "api/list.php",
2013-10-02 17:24:40 +02:00
dataType: 'json',
success: function(response){
//console.log("retrieveList response: ",response);
if(response.status == "success") {
updateList(response.data);
}
clearTimeout(retrieveListDelay);
2013-10-02 17:24:40 +02:00
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval);
}
}).fail(function() {
//console.log("retrieveList: failed");
clearTimeout(retrieveListDelay);
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval); // retry after delay
});
}
function updateList(boxes) {
$list.empty();
2013-10-02 17:24:40 +02:00
numBoxesChecking = 0;
numBoxesFound = 0;
jQuery.each(boxes, function (index,box) {
2013-09-30 17:34:27 +02:00
checkBox(box);
});
2013-10-02 17:24:40 +02:00
updateIntro();
2013-09-30 17:34:27 +02:00
}
function checkBox(box) {
2013-10-02 17:24:40 +02:00
numBoxesChecking++;
2013-09-30 17:34:27 +02:00
$.ajax({
2013-10-03 18:06:05 +02:00
url: "http://"+box.localip+"/d3dapi/network/alive",
dataType: "json",
2013-10-02 17:24:40 +02:00
timeout: boxTimeoutTime,
2013-09-30 17:34:27 +02:00
success: function(response){
if(response.status == "success") {
2013-10-02 17:24:40 +02:00
numBoxesFound++;
2013-09-30 17:34:27 +02:00
var url = "http://"+box.localip;
if(boxIsListed(url)) return;
2013-10-02 17:24:40 +02:00
2013-09-30 17:34:27 +02:00
$list.append("<li><a href='"+url+"'>"+box.wifiboxid+"</a></li>");
}
2013-10-02 17:24:40 +02:00
numBoxesChecking--;
updateIntro();
2013-09-30 17:34:27 +02:00
}
2013-10-02 17:24:40 +02:00
}).fail(function() {
numBoxesChecking--;
updateIntro();
2013-09-30 17:34:27 +02:00
});
}
function boxIsListed(url){
return $list.find("a[href|='"+url+"']").length > 0;
2013-10-02 17:24:40 +02:00
}
function updateIntro() {
if(numBoxesChecking <= 0) {
if(numBoxesFound > 0) {
$intro.html("Found the following boxes near you:");
} else {
$intro.html("No boxes found near you.");
}
$preloader.fadeOut(1000);
}
}