reclibrary.js 10 KB

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