2013-09-08 17:26:05 +02:00
|
|
|
console.log("Loading...");
|
|
|
|
|
|
|
|
function trx_startbut( code ) { return "startbutton-"+code; }
|
|
|
|
function trx_stopbut( code ) { return "stopbutton-"+code; }
|
|
|
|
function trx_downbut( code ) { return "downloadbutton-"+code; }
|
2013-11-24 23:43:56 +01:00
|
|
|
function trx_endbut( code ) { return "endbutton-"+code; }
|
2013-09-08 17:26:05 +02:00
|
|
|
|
|
|
|
function trx_logarea( code ) { return "logarea-"+code; }
|
|
|
|
|
|
|
|
function rs_button( code ) { return "button"+code; }
|
|
|
|
|
|
|
|
function rs_trxarea( code ) { return "recarea-"+code; }
|
2013-11-24 23:43:56 +01:00
|
|
|
function rs_trxname( code ) { return "name-"+code; }
|
|
|
|
function rs_buttonarea( code ) { return "butarea-"+code; }
|
|
|
|
function rs_inputstart( code ) { return "starttime-"+code; }
|
|
|
|
function rs_inputend( code ) { return "endtime-"+code; }
|
2013-09-08 17:26:05 +02:00
|
|
|
function rs_formid(code) { return "form-"+code; }
|
2013-11-24 23:43:56 +01:00
|
|
|
function rs_dellink(code) { return "dellink-"+code;}
|
2013-09-08 17:26:05 +02:00
|
|
|
function rs_id(code) { return code; }
|
|
|
|
|
|
|
|
var txt_start = "Inizia";
|
|
|
|
var txt_stop = "Ferma";
|
|
|
|
var txt_download = "Scarica";
|
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
var srvaddr = "/";
|
2013-09-08 17:26:05 +02:00
|
|
|
|
|
|
|
var almostone = false;
|
|
|
|
var noplusbotton = true;
|
|
|
|
|
|
|
|
var rec_name_default = "";
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
/*
|
|
|
|
TODO: cambiare logica
|
2013-11-26 14:45:31 +01:00
|
|
|
Quando premo il primo tasto, faccio la crazione,
|
2013-11-24 23:43:56 +01:00
|
|
|
per ogni altro pulsante, faccio solo e sempre UPDATE
|
|
|
|
*/
|
2013-11-26 14:45:31 +01:00
|
|
|
/**
|
|
|
|
* Perform Ajax async loading
|
2013-09-08 17:26:05 +02:00
|
|
|
**/
|
2013-09-15 16:13:06 +02:00
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
function newformstr ( recid , butflag=false )
|
2013-11-24 23:43:56 +01:00
|
|
|
{
|
|
|
|
var formid = rs_formid( recid );
|
|
|
|
var str = "<form id=\""+formid+"\" name=\""+formid+"\" action=\"#\">";
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
if (butflag) {
|
|
|
|
str = str + "<input type=\"button\" name=\""+trx_startbut(recid)+"\" id=\""+trx_startbut(recid)+"\" ";
|
|
|
|
str = str + " class=\"recbutton\" value=\"Inizia\" />";
|
|
|
|
str = str + "<input type=\"button\" name=\""+trx_stopbut(recid)+"\" id=\""+trx_stopbut(recid)+"\" ";
|
|
|
|
str = str + " class=\"recbutton\" value=\"Stop\" />";
|
|
|
|
str = str + "<input type=\"submit\" name=\""+trx_downbut(recid)+"\" id=\""+trx_downbut(recid)+"\" ";
|
|
|
|
str = str + " class=\"recbutton\" value=\"Salva\" />";
|
|
|
|
str = str + "<input type=\"submit\" name=\""+trx_endbut(recid)+"\" id=\""+trx_endbut(recid)+"\" ";
|
|
|
|
str = str + " class=\"recbutton\" value=\"Download\" />";
|
|
|
|
}
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
str = str + "<input type=\"hidden\" id=\"recid\" name=\"recid\" value=\""+recid+"\" />";
|
|
|
|
str = str + "<input type=\"text\" id=\""+rs_trxname(recid)+"\" name=\""+rs_trxname(recid)+"\" />";
|
|
|
|
str = str + "<input type=\"text\" id=\""+rs_inputstart(recid)+"\" name=\""+rs_inputstart(recid)+"\" />";
|
|
|
|
str = str + "<input type=\"text\" id=\""+rs_inputend(recid)+"\" name=\""+rs_inputend(recid)+"\" />";
|
2013-09-08 17:26:05 +02:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
if (! butflag) {
|
|
|
|
str = str + "<input type=\"button\" name=\""+trx_downbut(recid)+"\" id=\""+trx_downbut(recid)+"\" ";
|
|
|
|
str = str + " class=\"downloadbutton\" value=\"scarica\" />";
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
str = str + "<input type=\"text\" id=\"name\" name=\"name\" />";
|
|
|
|
str = str + "<input type=\"text\" id=\"starttime\" name=\"starttime\" />";
|
|
|
|
str = str + "<input type=\"text\" id=\"endtime\" name=\"endtime\" /> ";
|
|
|
|
*/
|
|
|
|
str = str + "</form>";
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
return str;
|
|
|
|
}
|
2013-11-26 14:45:31 +01:00
|
|
|
|
|
|
|
/**
|
2013-11-24 23:43:56 +01:00
|
|
|
* GetActive Recs
|
|
|
|
**/
|
|
|
|
|
|
|
|
function rec_active( recid ) {
|
|
|
|
dataString = "";
|
|
|
|
var request = RecAjax("search", dataString);
|
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
request.done( function(data) {
|
2013-11-24 23:43:56 +01:00
|
|
|
$.each(data, function(key, val) {
|
|
|
|
console.log("Key " + key + " > VAL " + val );
|
|
|
|
$("#"+trx_logarea( recid )).append( "Key " + key + " > VAL " + val + "<br>" );
|
|
|
|
});
|
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
console.log("Req OK: "+ data);
|
2013-11-24 23:43:56 +01:00
|
|
|
// console.log("request"+ req);
|
|
|
|
ChangeState(recid, trx_downbut(recid) , trx_endbut(recid));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
/**
|
|
|
|
* New record
|
2013-09-08 17:26:05 +02:00
|
|
|
**/
|
2013-11-26 14:45:31 +01:00
|
|
|
function rec_new( )
|
2013-11-24 23:43:56 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
var myDate = new Date()
|
2013-11-26 14:45:31 +01:00
|
|
|
console.log("New ID "+ myDate.getTime());
|
2013-11-24 23:43:56 +01:00
|
|
|
var recid = "rec-"+ myDate.getTime();
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-08 17:26:05 +02:00
|
|
|
console.log("[rec_new] New Rec " + recid);
|
|
|
|
|
|
|
|
$("#buttonscontainer").append( "<div id=\""+rs_trxarea(recid)+"\" class=\"recarea\"> </div>" );
|
|
|
|
$("#"+rs_trxarea(recid)).append( "<div id=\""+rs_buttonarea(recid)+"\" class=\"buttonarea\"> </div>" );
|
2013-11-24 23:43:56 +01:00
|
|
|
console.log("[rec_new"+recid+"] add div (TRXArea, ButtonArea) ok " );
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-08 17:26:05 +02:00
|
|
|
var formid = rs_formid( recid );
|
2013-11-26 14:45:31 +01:00
|
|
|
|
|
|
|
var str = newformstr(recid, butflag=true);
|
2013-09-08 17:26:05 +02:00
|
|
|
$("#"+rs_buttonarea(recid)).append( str );
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-08 17:26:05 +02:00
|
|
|
$("#"+trx_stopbut(recid)).hide();
|
|
|
|
$("#"+trx_downbut(recid)).hide();
|
2013-11-24 23:43:56 +01:00
|
|
|
$("#"+trx_endbut(recid)).hide();
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
console.log("[rec_new "+recid+"] Form OK");
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
$("#"+rs_buttonarea(recid)).append( "<div class=\"dellinkarea\" > <a href=\"#\" id="+rs_dellink(recid)+"> cancella</a> </div>" );
|
2013-09-08 17:26:05 +02:00
|
|
|
|
|
|
|
// INSERT AND POPULATE BUTTON AREA
|
2013-11-24 23:43:56 +01:00
|
|
|
$("#"+rs_trxarea(recid)).append( "<div id=\""+trx_logarea(recid)+"\" class=\"logarea\"> Nuova trasmissione </div>" );
|
2013-09-08 17:26:05 +02:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
// Bind the Delete Links
|
|
|
|
$("#"+rs_dellink(recid)).click(function(){
|
|
|
|
console.log("Remove " + rs_trxarea(recid) + "[ID"+recid+"]");
|
2013-11-25 00:29:24 +01:00
|
|
|
// $("#"+rs_trxarea(recid)).remove();
|
2013-11-26 14:45:31 +01:00
|
|
|
recDelete (recid,rs_trxarea(recid));
|
2013-09-08 17:26:05 +02:00
|
|
|
});
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
// FORM SUBMIT: THE REC IS STOPPEND AND MUST BE PROCESSED
|
2013-09-08 17:26:05 +02:00
|
|
|
$("#"+formid).submit(function(event){
|
2013-11-24 23:43:56 +01:00
|
|
|
// Immediately, mark the end time (stop action)
|
2013-11-26 14:45:31 +01:00
|
|
|
ChangeState(recid, trx_downbut(recid) , trx_endbut(recid));
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
// Force a Name
|
|
|
|
while (true) {
|
2013-11-26 14:45:31 +01:00
|
|
|
if ( $("#"+rs_trxname(recid)).val() == "" )
|
2013-11-24 23:43:56 +01:00
|
|
|
{
|
|
|
|
var tmpname = prompt("Nessun nome di trasmissione!!!");
|
|
|
|
$("#"+rs_trxname(recid)).val(tmpname);
|
|
|
|
$("#"+trx_logarea(recid)).append("Titolo: <b>"+ tmpname +"</b> <br/>");
|
|
|
|
}
|
2013-11-26 14:45:31 +01:00
|
|
|
else { break; }
|
2013-11-24 23:43:56 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
event.preventDefault();
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
// Update data (send to server) in order to save some information
|
2013-11-26 14:45:31 +01:00
|
|
|
recUpdate(recid);
|
|
|
|
|
|
|
|
recStart(recid);
|
2013-11-24 23:43:56 +01:00
|
|
|
|
|
|
|
}); // End of form SUBMIT
|
|
|
|
|
|
|
|
// Bind the STOP button
|
2013-11-26 14:45:31 +01:00
|
|
|
$("#"+trx_stopbut(recid)).click( function(event){
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
ChangeState(recid, trx_stopbut(recid) , trx_downbut(recid));
|
2013-11-24 23:43:56 +01:00
|
|
|
recUpdate(recid);
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
}); // End of STOP button
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
// Bind the START button
|
2013-11-26 14:45:31 +01:00
|
|
|
$("#"+trx_startbut(recid)).click( function(event){
|
|
|
|
|
|
|
|
// Immediately, mark the start time (start action) and send it to Server
|
2013-11-24 23:43:56 +01:00
|
|
|
ChangeState(recid, trx_startbut(recid) , trx_stopbut(recid));
|
2013-11-26 14:45:31 +01:00
|
|
|
event.preventDefault();
|
2013-11-24 23:43:56 +01:00
|
|
|
recNew( recid );
|
|
|
|
|
|
|
|
}); // End of START button
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
console.log("New form has been built.");
|
|
|
|
}
|
|
|
|
|
2013-11-25 00:29:24 +01:00
|
|
|
/* Delete Record */
|
|
|
|
function recDelete ( recid, targetarea ) {
|
|
|
|
var formid = rs_formid( recid );
|
|
|
|
var dataString = "recid="+recid
|
|
|
|
|
|
|
|
console.log("Del rec: "+dataString);
|
|
|
|
var req_del = RecAjax("delete", dataString);
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-25 00:29:24 +01:00
|
|
|
req_del.done (function(data) {
|
|
|
|
$.each(data, function(del_key, del_val) {
|
2013-11-26 14:45:31 +01:00
|
|
|
console.log("K:V " + del_key +":"+del_val );
|
|
|
|
|
2013-11-25 00:29:24 +01:00
|
|
|
if (del_key == "message") {
|
|
|
|
$("#"+targetarea).fadeOut( 200, function() { $(this).remove(); });
|
2013-11-26 14:45:31 +01:00
|
|
|
console.log("delete area "+rs_trxarea(key));
|
|
|
|
|
|
|
|
}
|
2013-11-25 00:29:24 +01:00
|
|
|
|
|
|
|
if (del_key == "error") {
|
|
|
|
alert("Impossibile cancellare elemento:\n" + del_val );
|
|
|
|
}
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-25 00:29:24 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
/* New Record */
|
|
|
|
function recNew ( recid ) {
|
|
|
|
var formid = rs_formid( recid );
|
|
|
|
var dataString = $("#"+formid).serialize();
|
|
|
|
|
|
|
|
console.log("New rec: "+dataString);
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
var request = RecAjax("create", dataString);
|
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
request.done( function(data) {
|
2013-11-24 23:43:56 +01:00
|
|
|
$.each(data, function(key, val) {
|
|
|
|
console.log("Received (K:V) ("+key+":"+val+")") ;
|
|
|
|
if (key == "msg") {
|
|
|
|
$("#"+trx_logarea(recid)).html("Nuova Registrazione </br> (recid:"+recid+") </br>");
|
|
|
|
$("#"+trx_logarea(recid)).append("Inizio: "+ $("#"+rs_inputstart(recid)).val() +"<br/>");
|
|
|
|
}
|
|
|
|
if (key == "error") {
|
|
|
|
$("#"+trx_logarea( recid )).html("Errore: impossibile creare una nuova registrazione"+val+" </ br>");
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
} );
|
|
|
|
return request;
|
2013-09-08 17:26:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
/* Update Record */
|
|
|
|
function recUpdate( recid ) {
|
|
|
|
var formid = rs_formid( recid );
|
|
|
|
var dataString = $("#"+formid).serialize();
|
|
|
|
console.log("Sending Ajax Update request: "+ dataString);
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
//event.preventDefault();
|
|
|
|
var request = RecAjax("update", dataString );
|
2013-11-26 14:45:31 +01:00
|
|
|
request.done( function(data) {
|
2013-11-24 23:43:56 +01:00
|
|
|
$.each(data, function(key, val) {
|
|
|
|
console.log("recUpdate receive (k:v) ("+key+":"+val+")" );
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
if (key == "message") {
|
|
|
|
var str = "";
|
|
|
|
str += "<b>RecID</b> "+ recid + "</br>"
|
|
|
|
str += "<b>nome</b> "+ $("#"+rs_trxname(recid)).val() + "</br>"
|
|
|
|
str += "<b>Inizio</b> "+ $("#"+rs_inputstart(recid)).val() + "</br>"
|
|
|
|
str += "<b>Fine</b> "+ $("#"+rs_inputend(recid)).val() + "</br>"
|
|
|
|
|
|
|
|
$("#"+trx_logarea(recid)).html( str );
|
|
|
|
// if all elements have been recorded
|
|
|
|
if ($("#"+rs_trxname(recid)).val() != "") {
|
|
|
|
$("#"+trx_logarea(recid)).append( "<b>In Elaborazione</b>" );
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
if (key == "error") {
|
|
|
|
$("#"+trx_logarea( recid )).append( "Error:" + val +"<br>" );
|
|
|
|
}
|
2013-11-26 14:45:31 +01:00
|
|
|
}); // end of each
|
2013-11-24 23:43:56 +01:00
|
|
|
}); // end of request.done
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-26 14:45:31 +01:00
|
|
|
*
|
2013-11-24 23:43:56 +01:00
|
|
|
* AJAX REQUEST
|
2013-11-26 14:45:31 +01:00
|
|
|
*
|
2013-11-24 23:43:56 +01:00
|
|
|
*/
|
|
|
|
function RecAjax(apipath, dataString ) {
|
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
var srv = srvaddr + apipath ;
|
2013-11-24 23:43:56 +01:00
|
|
|
|
2013-09-15 16:33:38 +02:00
|
|
|
var request = $.ajax({
|
|
|
|
type: "POST",
|
|
|
|
cache: false,
|
2013-11-24 23:43:56 +01:00
|
|
|
url: srv,
|
2013-09-15 16:33:38 +02:00
|
|
|
data: dataString,
|
|
|
|
dataType: "json"
|
|
|
|
});
|
|
|
|
|
|
|
|
request.fail(function (jqXHR, textStatus, errorThrown){
|
2013-11-24 23:43:56 +01:00
|
|
|
console.error("The following error occured: "+ jqXHR.status +"-"+ textStatus + "-" + errorThrown );
|
2013-11-26 14:45:31 +01:00
|
|
|
if (jqXHR.status == 0 && jqXHR.readyState === 4)
|
2013-11-24 23:43:56 +01:00
|
|
|
{
|
|
|
|
alert("Errore di connessione, impossibile inviare i dati al server "+ srv);
|
|
|
|
} else {
|
|
|
|
alert("Error: "+jqXHR.status +"\nTextStatus: "+ textStatus + "\n Ready State "+jqXHR.readyState+"\n" + errorThrown );
|
|
|
|
}
|
|
|
|
});
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
return request;
|
2013-09-15 16:33:38 +02:00
|
|
|
}
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
/*
|
|
|
|
* GetNow (data parser)
|
|
|
|
*/
|
|
|
|
function getnow()
|
|
|
|
{
|
|
|
|
var myDate = new Date()
|
|
|
|
var displayDate = myDate.getFullYear() + '/' + (myDate.getMonth()+1) + '/' + myDate.getDate();
|
|
|
|
displayDate = displayDate +' '+ myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
|
|
|
|
return displayDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
FUNCTION: CHANGE STATE (gui)
|
|
|
|
*/
|
|
|
|
function ChangeState(recid, from, to) {
|
|
|
|
|
2013-09-08 17:26:05 +02:00
|
|
|
console.log("ChangeState: " + from + " --> " + to );
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-08 17:26:05 +02:00
|
|
|
$("#"+from).css("display", "none");
|
|
|
|
$("#"+to).css("display", "inline");
|
|
|
|
|
|
|
|
// take the date
|
2013-11-24 23:43:56 +01:00
|
|
|
var displayDate = getnow();
|
|
|
|
|
|
|
|
if ( from == trx_startbut(recid) ) {
|
|
|
|
$("#"+rs_inputstart(recid)).val( displayDate );
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
console.log("ChangeState: set "+rs_inputstart(recid)+ " to "+ displayDate )
|
2013-09-08 17:26:05 +02:00
|
|
|
}
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
if ( from == trx_stopbut(recid) ) {
|
2013-11-26 14:45:31 +01:00
|
|
|
$("#"+rs_inputend(recid)).val( displayDate );
|
2013-11-24 23:43:56 +01:00
|
|
|
console.log("ChangeState: set '"+rs_inputend(recid)+ "' to "+ displayDate )
|
2013-09-08 17:26:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
if ( from == trx_downbut(recid) ) {
|
|
|
|
$("input[type=submit]").attr("disabled", "disabled");
|
|
|
|
console.log("ChangeState: set '"+rs_inputend(recid)+ "' to "+ displayDate );
|
|
|
|
}
|
2013-09-08 17:26:05 +02:00
|
|
|
} // End function ChangeState
|
2013-11-25 00:29:24 +01:00
|
|
|
|
|
|
|
|