ui.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* global $ */
  2. function time_changer_dialog (d, changed_callback) {
  3. // d is a Date object
  4. var dlg_html = '<div> \
  5. <form action="#"> \
  6. <input name="h" size="2" maxlength="2" type="number" min="0" max="23"> \
  7. <input name="m" size="2" maxlength="2" type="number" min="0" max="59"> \
  8. <input name="s" size="2" maxlength="2" type="number" min="0" max="59"> \
  9. </form> \
  10. </div>'
  11. var $dlg = $($.parseHTML(dlg_html))
  12. $('[name=h]', $dlg).val(d.getHours())
  13. $('[name=m]', $dlg).val(d.getMinutes())
  14. $('[name=s]', $dlg).val(d.getSeconds())
  15. $dlg.dialog({
  16. title: 'Modifica inizio',
  17. buttons:
  18. {
  19. 'Ok': function () {
  20. $(this).dialog('close')
  21. if (changed_callback === undefined) {
  22. return
  23. }
  24. newd = new Date(d.getTime())
  25. newd.setHours($('[name=h]', $dlg).val())
  26. newd.setMinutes($('[name=m]', $dlg).val())
  27. newd.setSeconds($('[name=s]', $dlg).val())
  28. if (newd.getTime() === d.getTime()) {
  29. console.debug('Time not changed, discarding')
  30. return
  31. }
  32. changed_callback(newd) // FIXME: crea data a partire dal form
  33. },
  34. 'Annulla': function () {
  35. $(this).dialog('close')
  36. }
  37. }
  38. }).dialog('open')
  39. }
  40. $.widget('ror.thebutton', {
  41. options: {
  42. state: 'Create',
  43. filename: null,
  44. errormsg: null
  45. },
  46. _create: function () {
  47. 'use strict'
  48. // create an appropriate button
  49. var widget = this
  50. var state = this.options.rec
  51. widget.element.addClass('pure-button')
  52. widget.element.on('click', function (evt) {
  53. /* global error_dialog */
  54. if (widget.element.hasClass('rec-failed')) {
  55. error_dialog(widget.options.errormsg,
  56. function () {
  57. console.log('Should retry, TODO')
  58. $(this).dialog('close')
  59. widget._setOption('state', 'Create')
  60. widget.element.click()
  61. },
  62. function () {
  63. $(this).dialog('close')
  64. })
  65. }
  66. })
  67. this._update()
  68. },
  69. _setOption: function (key, value) {
  70. this.options[key] = value
  71. if (key === 'state') {
  72. if (this.options.state !== 'Download') {
  73. this.options.filename = null
  74. }
  75. }
  76. this._update()
  77. },
  78. _update: function () {
  79. this.element.removeClass('pure-button-disabled rec-run rec-create ' +
  80. 'rec-encoding rec-download rec-failed rec-stop')
  81. switch (this.options.state) {
  82. case 'Stop':
  83. this.element.addClass('rec-stop rec-run').html(
  84. $('<i/>').addClass('fa fa-stop')).append(' Stop')
  85. break
  86. case 'Create':
  87. this.element.addClass('rec-create rec-run').html(
  88. $('<i/>').addClass('fa fa-gear')).append(' Create')
  89. break
  90. case 'Failed':
  91. this.element.addClass('rec-failed').html(
  92. $('<i/>').addClass('fa fa-warning')).append(' Errori')
  93. break
  94. case 'Wait':
  95. this.element.addClass('pure-button-disabled rec-encoding').html(
  96. $('<i/>').addClass('fa fa-clock-o')).append(' Wait')
  97. break
  98. case 'Download':
  99. this.element
  100. .addClass('rec-download')
  101. .prop('href', this.options.filename)
  102. .html(
  103. $('<i/>').addClass('fa fa-download').css('color', 'green'))
  104. .append(' Scarica')
  105. break
  106. }
  107. }
  108. })
  109. function error_dialog (msg, retry, cancel) {
  110. $('<div/>').html($('<pre/>').text(msg))
  111. .dialog({modal: true, title: 'Dettaglio errori',
  112. buttons: {
  113. Retry: retry,
  114. Cancel: cancel
  115. }
  116. })
  117. }
  118. /* vim: set ts=2 sw=2 noet fdm=indent: */