reclibrary.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. console.log("Loading...");
  2. function trx_startbut( code ) { return "startbutton-"+code; }
  3. function trx_stopbut( code ) { return "stopbutton-"+code; }
  4. function trx_downbut( code ) { return "downloadbutton-"+code; }
  5. function trx_endbut( code ) { return "endbutton-"+code; }
  6. function trx_logarea( code ) { return "logarea-"+code; }
  7. function rs_button( code ) { return "button"+code; }
  8. function rs_trxarea( code ) { return "recarea-"+code; }
  9. function rs_trxname( code ) { return "name-"+code; }
  10. function rs_buttonarea( code ) { return "butarea-"+code; }
  11. function rs_inputstart( code ) { return "starttime-"+code; }
  12. function rs_inputend( code ) { return "endtime-"+code; }
  13. function rs_formid(code) { return "form-"+code; }
  14. function rs_dellink(code) { return "dellink-"+code;}
  15. function rs_id(code) { return code; }
  16. var txt_start = "Inizia";
  17. var txt_stop = "Ferma";
  18. var txt_download = "Scarica";
  19. var srvaddr = "http://127.0.0.1:8000";
  20. var almostone = false;
  21. var noplusbotton = true;
  22. var rec_name_default = "";
  23. /*
  24. TODO: cambiare logica
  25. Quando premo il primo tasto, faccio la crazione,
  26. per ogni altro pulsante, faccio solo e sempre UPDATE
  27. */
  28. /**
  29. * Perform Ajax async loading
  30. **/
  31. function newformstr ( recid , butflag=false )
  32. {
  33. var formid = rs_formid( recid );
  34. var str = "<form id=\""+formid+"\" name=\""+formid+"\" action=\"#\">";
  35. if (butflag) {
  36. str = str + "<input type=\"button\" name=\""+trx_startbut(recid)+"\" id=\""+trx_startbut(recid)+"\" ";
  37. str = str + " class=\"recbutton\" value=\"Inizia\" />";
  38. str = str + "<input type=\"button\" name=\""+trx_stopbut(recid)+"\" id=\""+trx_stopbut(recid)+"\" ";
  39. str = str + " class=\"recbutton\" value=\"Stop\" />";
  40. str = str + "<input type=\"submit\" name=\""+trx_downbut(recid)+"\" id=\""+trx_downbut(recid)+"\" ";
  41. str = str + " class=\"recbutton\" value=\"Salva\" />";
  42. str = str + "<input type=\"submit\" name=\""+trx_endbut(recid)+"\" id=\""+trx_endbut(recid)+"\" ";
  43. str = str + " class=\"recbutton\" value=\"Download\" />";
  44. }
  45. str = str + "<input type=\"hidden\" id=\"recid\" name=\"recid\" value=\""+recid+"\" />";
  46. str = str + "<input type=\"text\" id=\""+rs_trxname(recid)+"\" name=\""+rs_trxname(recid)+"\" />";
  47. str = str + "<input type=\"text\" id=\""+rs_inputstart(recid)+"\" name=\""+rs_inputstart(recid)+"\" />";
  48. str = str + "<input type=\"text\" id=\""+rs_inputend(recid)+"\" name=\""+rs_inputend(recid)+"\" />";
  49. if (! butflag) {
  50. str = str + "<input type=\"button\" name=\""+trx_downbut(recid)+"\" id=\""+trx_downbut(recid)+"\" ";
  51. str = str + " class=\"downloadbutton\" value=\"scarica\" />";
  52. }
  53. /*
  54. str = str + "<input type=\"text\" id=\"name\" name=\"name\" />";
  55. str = str + "<input type=\"text\" id=\"starttime\" name=\"starttime\" />";
  56. str = str + "<input type=\"text\" id=\"endtime\" name=\"endtime\" /> ";
  57. */
  58. str = str + "</form>";
  59. return str;
  60. }
  61. /**
  62. * GetActive Recs
  63. **/
  64. function rec_active( recid ) {
  65. dataString = "";
  66. var request = RecAjax("search", dataString);
  67. request.done( function(data) {
  68. $.each(data, function(key, val) {
  69. console.log("Key " + key + " > VAL " + val );
  70. $("#"+trx_logarea( recid )).append( "Key " + key + " > VAL " + val + "<br>" );
  71. });
  72. console.log("Req OK: "+ data);
  73. // console.log("request"+ req);
  74. ChangeState(recid, trx_downbut(recid) , trx_endbut(recid));
  75. });
  76. }
  77. /**
  78. * New record
  79. **/
  80. function rec_new( )
  81. {
  82. var myDate = new Date()
  83. console.log("New ID "+ myDate.getTime());
  84. var recid = "rec-"+ myDate.getTime();
  85. console.log("[rec_new] New Rec " + recid);
  86. $("#buttonscontainer").append( "<div id=\""+rs_trxarea(recid)+"\" class=\"recarea\"> </div>" );
  87. $("#"+rs_trxarea(recid)).append( "<div id=\""+rs_buttonarea(recid)+"\" class=\"buttonarea\"> </div>" );
  88. console.log("[rec_new"+recid+"] add div (TRXArea, ButtonArea) ok " );
  89. var formid = rs_formid( recid );
  90. var str = newformstr(recid, butflag=true);
  91. $("#"+rs_buttonarea(recid)).append( str );
  92. $("#"+trx_stopbut(recid)).hide();
  93. $("#"+trx_downbut(recid)).hide();
  94. $("#"+trx_endbut(recid)).hide();
  95. console.log("[rec_new "+recid+"] Form OK");
  96. $("#"+rs_buttonarea(recid)).append( "<div class=\"dellinkarea\" > <a href=\"#\" id="+rs_dellink(recid)+"> cancella</a> </div>" );
  97. // INSERT AND POPULATE BUTTON AREA
  98. $("#"+rs_trxarea(recid)).append( "<div id=\""+trx_logarea(recid)+"\" class=\"logarea\"> Nuova trasmissione </div>" );
  99. // Bind the Delete Links
  100. $("#"+rs_dellink(recid)).click(function(){
  101. console.log("Remove " + rs_trxarea(recid) + "[ID"+recid+"]");
  102. $("#"+rs_trxarea(recid)).remove();
  103. alert("Premuto del link. Invio al server ?");
  104. });
  105. // FORM SUBMIT: THE REC IS STOPPEND AND MUST BE PROCESSED
  106. $("#"+formid).submit(function(event){
  107. // Immediately, mark the end time (stop action)
  108. ChangeState(recid, trx_stopbut(recid) , trx_downbut(recid));
  109. // Force a Name
  110. while (true) {
  111. if ( $("#"+rs_trxname(recid)).val() == "" )
  112. {
  113. var tmpname = prompt("Nessun nome di trasmissione!!!");
  114. $("#"+rs_trxname(recid)).val(tmpname);
  115. $("#"+trx_logarea(recid)).append("Titolo: <b>"+ tmpname +"</b> <br/>");
  116. }
  117. else { break; }
  118. }
  119. event.preventDefault();
  120. // Update data (send to server) in order to save some information
  121. recUpdate(recid);
  122. recStart(recid);
  123. }); // End of form SUBMIT
  124. // Bind the STOP button
  125. $("#"+trx_stopbut(recid)).click( function(event){
  126. event.preventDefault();
  127. ChangeState(recid, trx_stopbut(recid) , trx_downbut(recid));
  128. recUpdate(recid);
  129. }); // End of STOP button
  130. // Bind the START button
  131. $("#"+trx_startbut(recid)).click( function(event){
  132. // Immediately, mark the start time (start action) and send it to Server
  133. ChangeState(recid, trx_startbut(recid) , trx_stopbut(recid));
  134. event.preventDefault();
  135. recNew( recid );
  136. }); // End of START button
  137. console.log("New form has been built.");
  138. }
  139. /* New Record */
  140. function recNew ( recid ) {
  141. var formid = rs_formid( recid );
  142. var dataString = $("#"+formid).serialize();
  143. console.log("New rec: "+dataString);
  144. var request = RecAjax("create", dataString);
  145. request.done( function(data) {
  146. $.each(data, function(key, val) {
  147. console.log("Received (K:V) ("+key+":"+val+")") ;
  148. if (key == "msg") {
  149. $("#"+trx_logarea(recid)).html("Nuova Registrazione </br> (recid:"+recid+") </br>");
  150. $("#"+trx_logarea(recid)).append("Inizio: "+ $("#"+rs_inputstart(recid)).val() +"<br/>");
  151. }
  152. if (key == "error") {
  153. $("#"+trx_logarea( recid )).html("Errore: impossibile creare una nuova registrazione"+val+" </ br>");
  154. }
  155. });
  156. } );
  157. return request;
  158. }
  159. /* Update Record */
  160. function recUpdate( recid ) {
  161. var formid = rs_formid( recid );
  162. var dataString = $("#"+formid).serialize();
  163. console.log("Sending Ajax Update request: "+ dataString);
  164. //event.preventDefault();
  165. var request = RecAjax("update", dataString );
  166. request.done( function(data) {
  167. $.each(data, function(key, val) {
  168. console.log("recUpdate receive (k:v) ("+key+":"+val+")" );
  169. if (key == "message") {
  170. var str = "";
  171. str += "<b>RecID</b> "+ recid + "</br>"
  172. str += "<b>nome</b> "+ $("#"+rs_trxname(recid)).val() + "</br>"
  173. str += "<b>Inizio</b> "+ $("#"+rs_inputstart(recid)).val() + "</br>"
  174. str += "<b>Fine</b> "+ $("#"+rs_inputend(recid)).val() + "</br>"
  175. $("#"+trx_logarea(recid)).html( str );
  176. // if all elements have been recorded
  177. if ($("#"+rs_trxname(recid)).val() != "") {
  178. $("#"+trx_logarea(recid)).append( "<b>In Elaborazione</b>" );
  179. }
  180. }
  181. if (key == "error") {
  182. $("#"+trx_logarea( recid )).append( "Error:" + val +"<br>" );
  183. }
  184. }); // end of each
  185. }); // end of request.done
  186. }
  187. /*
  188. *
  189. * AJAX REQUEST
  190. *
  191. */
  192. function RecAjax(apipath, dataString ) {
  193. var srv = srvaddr + "/" + apipath ;
  194. var request = $.ajax({
  195. type: "POST",
  196. cache: false,
  197. url: srv,
  198. data: dataString,
  199. dataType: "json"
  200. });
  201. request.fail(function (jqXHR, textStatus, errorThrown){
  202. console.error("The following error occured: "+ jqXHR.status +"-"+ textStatus + "-" + errorThrown );
  203. if (jqXHR.status == 0 && jqXHR.readyState === 4)
  204. {
  205. alert("Errore di connessione, impossibile inviare i dati al server "+ srv);
  206. } else {
  207. alert("Error: "+jqXHR.status +"\nTextStatus: "+ textStatus + "\n Ready State "+jqXHR.readyState+"\n" + errorThrown );
  208. }
  209. });
  210. return request;
  211. }
  212. /*
  213. * GetNow (data parser)
  214. */
  215. function getnow()
  216. {
  217. var myDate = new Date()
  218. var displayDate = myDate.getFullYear() + '/' + (myDate.getMonth()+1) + '/' + myDate.getDate();
  219. displayDate = displayDate +' '+ myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
  220. return displayDate;
  221. }
  222. /*
  223. FUNCTION: CHANGE STATE (gui)
  224. */
  225. function ChangeState(recid, from, to) {
  226. console.log("ChangeState: " + from + " --> " + to );
  227. $("#"+from).css("display", "none");
  228. $("#"+to).css("display", "inline");
  229. // take the date
  230. var displayDate = getnow();
  231. if ( from == trx_startbut(recid) ) {
  232. $("#"+rs_inputstart(recid)).val( displayDate );
  233. console.log("ChangeState: set "+rs_inputstart(recid)+ " to "+ displayDate )
  234. }
  235. if ( from == trx_stopbut(recid) ) {
  236. $("#"+rs_inputend(recid)).val( displayDate );
  237. console.log("ChangeState: set '"+rs_inputend(recid)+ "' to "+ displayDate )
  238. }
  239. if ( from == trx_downbut(recid) ) {
  240. $("input[type=submit]").attr("disabled", "disabled");
  241. console.log("ChangeState: set '"+rs_inputend(recid)+ "' to "+ displayDate );
  242. }
  243. } // End function ChangeState