old.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*global $, poll_job*/
  2. var form = {
  3. MAX_MINS: 5*60, // 5 hours
  4. get_values: function() {
  5. var name = $('#name').val();
  6. var start = $('#from-date').datepicker('getDate');
  7. if(start !== null) {
  8. start.setHours($('#from-hour').val());
  9. start.setMinutes($('#from-min').val());
  10. }
  11. var end = $('#to-date').datepicker('getDate');
  12. if(end !== null) {
  13. end.setHours($('#to-hour').val());
  14. end.setMinutes($('#to-min').val());
  15. }
  16. return { name: name, start: start, end: end };
  17. },
  18. check: function() {
  19. "use strict";
  20. var errs = [];
  21. function err(msg, element) {
  22. errs.unshift({ msg: msg, el: element});
  23. }
  24. var v = form.get_values();
  25. if(v.name === '') {
  26. err("Nome mancante", $('#name'));
  27. }
  28. if(v.start === null) {
  29. err("Start unspecified");
  30. }
  31. if(v.end === null) {
  32. err("End unspecified");
  33. }
  34. if(v.end <= v.start) {
  35. err("Inverted from/to ?");
  36. }
  37. if( (v.end - v.start) / (1000*60) > form.MAX_MINS) {
  38. err("Too long");
  39. }
  40. return errs;
  41. }
  42. };
  43. function click(widget) {
  44. /*global RecAPI*/
  45. var v = form.get_values();
  46. RecAPI.fullcreate(v.name, v.start, v.end)
  47. .done(function(res_create) {
  48. console.log("ok, created");
  49. RecAPI.generate(res_create.rec)
  50. .done(function(res_gen) {
  51. console.log("ok, generated", res_create);
  52. //TODO: start polling
  53. $('#download').thebutton('option', 'state', 'Wait');
  54. poll_job(res_gen.job_id, function(data) {
  55. if(data.job_status !== 'DONE') {
  56. console.error("Job failed!", data);
  57. widget.thebutton("option", "state", 'Failed');
  58. widget.thebutton("option", "errormsg", data.exception);
  59. } else {
  60. widget.thebutton("option", "filename", res_gen.result);
  61. widget.thebutton("option", "state", 'Download');
  62. }
  63. });
  64. })
  65. .fail(function() {
  66. console.error("Oh shit, generate failed", res_create.rec);
  67. });
  68. })
  69. .fail(function() {
  70. console.error("Oh shit, fullcreate failed");
  71. });
  72. }
  73. $(function() {
  74. "use strict";
  75. $( "#from-date" ).datepicker({
  76. defaultDate: "+0d",
  77. changeMonth: true,
  78. numberOfMonths: 1,
  79. maxDate: new Date(),
  80. onClose: function( selectedDate ) {
  81. if($('#to-date').val() === '') {
  82. $('#to-date').datepicker("setDate", selectedDate);
  83. }
  84. $("#to-date").datepicker("option", "minDate", selectedDate);
  85. }
  86. });
  87. $( "#to-date" ).datepicker({
  88. defaultDate: "+0d",
  89. changeMonth: true,
  90. numberOfMonths: 1,
  91. maxDate: new Date(),
  92. onClose: function( selectedDate ) {
  93. $("#from-date").datepicker("option", "maxDate", selectedDate);
  94. }
  95. });
  96. $('#to-date, #from-date').datepicker($.datepicker.regional.it);
  97. $('#download').thebutton();
  98. $('#download').click(function() {
  99. if(!$('#download').hasClass('rec-run')) {
  100. return;
  101. }
  102. var check = form.check();
  103. if(check.length > 0) {
  104. console.log("Errors in form", check);
  105. error_dialog(check.map(function(err) { return err.msg; }).join('\n'));
  106. return;
  107. }
  108. click($('#download'));
  109. });
  110. });
  111. /* vim: set ts=2 sw=2 noet fdm=indent: */