new.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*global $, config, RecAPI, poll_job*/
  2. //TODO: move to a separate file(?)
  3. $.widget("ror.countclock", {
  4. options: {
  5. errormsg: null,
  6. since: null,
  7. editable:true,
  8. to: null
  9. },
  10. _create: function() {
  11. "use strict";
  12. this._update();
  13. //TODO: aggiungi conto secondi/minuti passati
  14. var widg = this;
  15. this.element.on('click', '.countclock-edit-time', function() {
  16. if(widg.options.editable === true) {
  17. widg._change_starttime(widg.options.since);
  18. }
  19. });
  20. },
  21. _change_starttime: function ( since ) {
  22. "use strict";
  23. var widget = this;
  24. time_changer_dialog(since, function(newsince) {
  25. widget._trigger("change", null, { since: newsince })
  26. });
  27. },
  28. _setOption: function(key, value) {
  29. this.options[key] = value;
  30. this._update();
  31. },
  32. _update: function() {
  33. "use strict";
  34. var text = "";
  35. if(this.options.since !== null) {
  36. if(this.options.to === null) {
  37. text = "Registrando da " + config.datetimeformat(this.options.since);
  38. } else {
  39. text = "Registrando da " +
  40. config.datetimeformat(this.options.since) +
  41. " a " +
  42. config.datetimeformat(this.options.to);
  43. this.options.editable = false;
  44. }
  45. }
  46. this.element.text(text);
  47. if(this.options.editable) {
  48. var btn = $('<span/>');
  49. btn.addClass('pure-button pure-button-compact countclock-edit-time');
  50. btn.css('margin-left', '0.5em');
  51. btn.append($('<i/>').addClass('fa fa-pencil'));
  52. this.element.append(btn);
  53. }
  54. }
  55. });
  56. $.widget("ror.ongoingrec", {
  57. options: {
  58. rec: null,
  59. state: 0,
  60. filename: null,
  61. /*0 = ongoing, 1 = encoding, 2 = ready to download, 9 = errors*/
  62. },
  63. _create: function() {
  64. "use strict";
  65. //convert a Rec into a <tr>
  66. var widget = this;
  67. var rec = this.options.rec;
  68. var view = this.element.data('rec', rec).addClass('ongoing-rec').append(
  69. $('<td/>').addClass('pure-form').append(
  70. $('<input/>').attr('type', 'text').attr('placeholder', 'Nome trasmissione')
  71. )
  72. ).append( $('<td class="ongoingrec-time"/>').countclock()).append(
  73. $('<td/>').append($('<a/>')
  74. .addClass('pure-button pure-button-large'))
  75. );
  76. this._update();
  77. view.on("change", "input", function(evt) {
  78. console.log('change', evt);
  79. var prevrec = widget.options.rec;
  80. prevrec.name = $(evt.target).val();
  81. $(evt.target).parents('tr.ongoing-rec').data('rec', prevrec);
  82. widget._trigger("change", evt,
  83. {rec: rec, widget: widget, changed: {name: rec.name}}
  84. );
  85. });
  86. view.on("click", ".rec-stop", function(evt) {
  87. widget._trigger("stop", evt, {rec: rec, widget: widget});
  88. });
  89. view.on("click", ".rec-failed", function(evt) {
  90. $('<div/>').html($('<pre/>').text(widget.options.errormsg))
  91. .dialog({modal: true, title: "Dettaglio errori",
  92. buttons: {
  93. Retry: function() {
  94. console.log("retrying");
  95. widget._setOption("state", 0);
  96. widget._trigger("retry", evt, {rec: rec, widget: widget});
  97. $(this).dialog("close");
  98. }, Cancel: function() {
  99. $(this).dialog("close");
  100. }
  101. }
  102. });
  103. });
  104. return view;
  105. },
  106. _setOption: function(key, value) {
  107. this.options[key] = value;
  108. if(key === 'state') {
  109. if(value !== 9) {
  110. this.options.errormsg = null;
  111. }
  112. if(value < 2) {
  113. this.options.filename = null;
  114. }
  115. }
  116. this._update();
  117. },
  118. _update: function() {
  119. var rec = this.options.rec;
  120. this.element.find('input').val(rec.name);
  121. this.element.find(':ror-countclock').countclock("option", "since",
  122. rec.starttime !== null ? config.date_read(rec.starttime) :
  123. null).on("countclockchange",
  124. function(evt, data) {
  125. count_widg = this;
  126. console.log(this);
  127. console.log(rec.starttime, data.since.getTime() / 1000);
  128. rec.starttime = data.since.getTime() / 1000;
  129. RecAPI.update(rec.id, rec).done(
  130. function() {
  131. $(count_widg).countclock('option', 'since', data.since);
  132. }).fail(console.error);
  133. });
  134. if(this.options.state > 0) {
  135. this.element.find(':ror-countclock').countclock("option", "to",
  136. rec.endtime !== null ? new Date(rec.endtime*1000) : null
  137. );
  138. } else {
  139. this.element.find(':ror-countclock').countclock("option", "to", null);
  140. }
  141. this.element.find('a').removeClass(
  142. 'pure-button-disabled rec-encoding rec-download rec-failed rec-stop');
  143. switch(this.options.state) {
  144. case 0:
  145. this.element.find('a').addClass("rec-stop").html(
  146. $('<i/>').addClass('fa fa-stop')).append(' Stop');
  147. break;
  148. case 1:
  149. this.element.find('a')
  150. .addClass("pure-button-disabled rec-encoding").html(
  151. $('<i/>').addClass('fa fa-clock-o')).append(' Aspetta');
  152. break;
  153. case 2:
  154. this.element.find('a').addClass("rec-download")
  155. .prop('href', this.options.filename)
  156. .html(
  157. $('<i/>').addClass('fa fa-download').css('color', 'green'))
  158. .append(' Scarica');
  159. break;
  160. case 9:
  161. this.element.find('a').addClass("rec-failed")
  162. .html(
  163. $('<i/>').addClass('fa fa-warning')).append(' Errori');
  164. break;
  165. }
  166. }
  167. });
  168. function add_new_rec() {
  169. //progress()
  170. return RecAPI.create()
  171. .done(function(res) {
  172. /*global show_ongoing*/
  173. //passa alla seconda schermata
  174. $('#rec-inizia').remove();
  175. $('#rec-normal').show();
  176. show_ongoing([res.rec]);
  177. })
  178. .fail(function() {
  179. /*global alert*/
  180. alert("C'e' stato qualche problema nella comunicazione col server");
  181. });
  182. }
  183. function gen_rec(rec, widget) {
  184. "use strict";
  185. var gen_xhr = RecAPI.generate(rec);
  186. gen_xhr.done(function(res_gen) {
  187. widget.option("state", 1);
  188. poll_job(res_gen.job_id, function(data) {
  189. if(data.job_status !== 'DONE') {
  190. console.error("Job failed!", data);
  191. widget.option("errormsg", "Generation failed");
  192. widget.option("state", 9);
  193. } else {
  194. widget.option("filename", res_gen.result);
  195. widget.option("state", 2);
  196. }
  197. });
  198. });
  199. gen_xhr.fail(function(res_gen) {
  200. var error = JSON.parse(res_gen.responseText).message;
  201. widget.option("errormsg", error);
  202. widget.option("state", 9);
  203. });
  204. return gen_xhr;
  205. }
  206. function stop_rec(rec, widget) {
  207. "use strict";
  208. var stop_xhr = RecAPI.stop(rec);
  209. stop_xhr.done(function(res_update) {
  210. widget.option("rec", res_update.rec);
  211. return gen_rec(rec, widget);
  212. });
  213. stop_xhr.fail(function(res_update) {
  214. var error = JSON.parse(res_update.responseText).message;
  215. widget.option("errormsg", error);
  216. widget.option("state", 9);
  217. });
  218. return stop_xhr; //RecAPI.stop
  219. }
  220. function show_ongoing(ongoing_recs) {
  221. return ongoing_recs.map(function(rec) {
  222. var viewrec = $('<tr/>').ongoingrec({rec: rec});
  223. viewrec.on("ongoingrecstop", function(evt, data) {
  224. stop_rec(data.rec, data.widget);
  225. }).on("ongoingrecretry", function(evt, data) {
  226. //FIXME: bisognerebbe solo generare, senza stoppare
  227. gen_rec(data.rec, data.widget);
  228. }).on("ongoingrecchange", function(evt, data) {
  229. //TODO: aggiorna nome sul server
  230. RecAPI.update(data.rec.id, data.rec);
  231. });
  232. $('#ongoing-recs-table tbody').prepend(viewrec);
  233. return viewrec;
  234. });
  235. }
  236. $(function() {
  237. "use strict";
  238. /*global getKeys*/
  239. //TODO: get-ongoing
  240. RecAPI.get_ongoing()
  241. .done(function(recs) {
  242. $('.add-new-rec').click(add_new_rec);
  243. console.log(recs);
  244. if(getKeys(recs).length !== 0) {
  245. $('#rec-inizia').remove();
  246. $('#rec-normal').show();
  247. show_ongoing(getKeys(recs).map(function(id) { console.log(id); return recs[id]; }));
  248. }
  249. });
  250. });
  251. //POLYFILL for Object.keys
  252. function getKeys(obj) {
  253. var keys = [];
  254. var key;
  255. for(key in obj) {
  256. if(obj.hasOwnProperty(key)) {
  257. keys.push(key);
  258. }
  259. }
  260. return keys;
  261. }
  262. /* vim: set ts=2 sw=2 noet fdm=indent: */