xlsx-export.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // jQuery UI style "widget" for managing the "xlsx-exporter"
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "xlsxexporter" the widget name
  6. $.widget( "itop.xlsxexporter",
  7. {
  8. // default options
  9. options:
  10. {
  11. filter: '',
  12. ajax_page_url: '',
  13. labels: {dialog_title: 'Excel Export', export_button: 'Export', cancel_button: 'Cancel', download_button: 'Download', complete: 'Complete', cancelled: 'Cancelled' }
  14. },
  15. // the constructor
  16. _create: function()
  17. {
  18. this.element
  19. .addClass('itop-xlsxexporter');
  20. this.sToken = null;
  21. this.ajaxCall = null;
  22. this.oProgressBar = $('.progress-bar', this.element);
  23. this.oStatusMessage = $('.status-message', this.element);
  24. $('.progress', this.element).hide();
  25. $('.statistics', this.element).hide();
  26. var me = this;
  27. this.element.dialog({
  28. title: this.options.labels.dialog_title,
  29. modal: true,
  30. width: 500,
  31. height: 300,
  32. buttons: [
  33. { text: this.options.labels.export_button, 'class': 'export-button', click: function() {
  34. me._start();
  35. } },
  36. { text: this.options.labels.cancel_button, 'class': 'cancel-button', click: function() {
  37. $(this).dialog( "close" );
  38. } },
  39. ],
  40. close: function() { me._abort(); $(this).remove(); }
  41. });
  42. },
  43. // events bound via _bind are removed automatically
  44. // revert other modifications here
  45. destroy: function()
  46. {
  47. this.element
  48. .removeClass('itop-xlsxexporter');
  49. },
  50. // _setOptions is called with a hash of all options that are changing
  51. _setOptions: function()
  52. {
  53. this._superApply(arguments);
  54. },
  55. // _setOption is called for each individual option that is changing
  56. _setOption: function( key, value )
  57. {
  58. this._superApply(arguments);
  59. },
  60. _start: function()
  61. {
  62. var me = this;
  63. $('.export-options', this.element).hide();
  64. $('.progress', this.element).show();
  65. var bAdvanced = $('#export-advanced-mode').prop('checked');
  66. this.bAutoDownload = $('#export-auto-download').prop('checked');
  67. $('.export-button', this.element.parent()).button('disable');
  68. this.oProgressBar.progressbar({
  69. value: 0,
  70. change: function() {
  71. var progressLabel = $('.progress-label', me.element);
  72. progressLabel.text( $(this).progressbar( "value" ) + "%" );
  73. },
  74. complete: function() {
  75. var progressLabel = $('.progress-label', me.element);
  76. progressLabel.text( me.options.labels['complete'] );
  77. }
  78. });
  79. //TODO disable the "export" button
  80. this.ajaxCall = $.post(this.options.ajax_page_url, {filter: this.options.filter, operation: 'xlsx_start', advanced: bAdvanced}, function(data) {
  81. this.ajaxCall = null;
  82. if (data && data.status == 'ok')
  83. {
  84. me.sToken = data.token;
  85. me._run();
  86. }
  87. else
  88. {
  89. if (data == null)
  90. {
  91. me.oStatusMessage.html('Unexpected error (operation=xlsx_start).');
  92. me.oProgressBar.progressbar({value: 100});
  93. }
  94. else
  95. {
  96. me.oStatusMessage.html(data.message);
  97. }
  98. }
  99. }, 'json');
  100. },
  101. _abort: function()
  102. {
  103. $('.cancel-button', this.element.parent()).button('disable');
  104. this.oStatusMessage.html(this.options.labels['cancelled']);
  105. this.oProgressBar.progressbar({value: 100});
  106. if (this.sToken != null)
  107. {
  108. // Cancel the operation in progress... or cleanup a completed export
  109. // TODO
  110. if (this.ajaxCall)
  111. {
  112. this.ajaxCall.abort();
  113. this.ajaxClass = null;
  114. }
  115. var me = this;
  116. $.post(this.options.ajax_page_url, {token: this.sToken, operation: 'xlsx_abort'}, function(data) {
  117. me.sToken = null;
  118. });
  119. }
  120. },
  121. _run: function()
  122. {
  123. var me = this;
  124. this.ajaxCall = $.post(this.options.ajax_page_url, {token: this.sToken, operation: 'xlsx_run'}, function(data) {
  125. this.ajaxCall = null;
  126. if (data == null)
  127. {
  128. me.oStatusMessage.html('Unexpected error (operation=xlsx_run).');
  129. me.oProgressBar.progressbar({value: 100});
  130. }
  131. else if (data.status == 'error')
  132. {
  133. me.oStatusMessage.html(data.message);
  134. me.oProgressBar.progressbar({value: 100});
  135. }
  136. else if (data.status == 'done')
  137. {
  138. me.oStatusMessage.html(data.message);
  139. me.oProgressBar.progressbar({value: 100});
  140. $('.stats-data', this.element).html(data.statistics);
  141. me._on_completion();
  142. }
  143. else
  144. {
  145. // continue running the export in the background
  146. me.oStatusMessage.html(data.message);
  147. me.oProgressBar.progressbar({value: data.percentage});
  148. me._run();
  149. }
  150. }, 'json');
  151. },
  152. _on_completion: function()
  153. {
  154. var me = this;
  155. $('.progress', this.element).html('<form class="download-form" method="post" action="'+this.options.ajax_page_url+'"><input type="hidden" name="operation" value="xlsx_download"/><input type="hidden" name="token" value="'+this.sToken+'"/><button type="submit">'+this.options.labels['download_button']+'</button></form>');
  156. $('.download-form button', this.element).button().click(function() { me.sToken = null; window.setTimeout(function() { me.element.dialog('close'); }, 100); return true;});
  157. if (this.bAutoDownload)
  158. {
  159. me.sToken = null;
  160. $('.download-form').submit();
  161. this.element.dialog('close');
  162. }
  163. else
  164. {
  165. $('.statistics', this.element).show();
  166. $('.statistics .stats-toggle', this.element).click(function() { $(this).toggleClass('closed'); });
  167. }
  168. }
  169. });
  170. });
  171. function XlsxExportDialog(sFilter)
  172. {
  173. var sUrl = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php';
  174. $.post(sUrl, {operation: 'xlsx_export_dialog', filter: sFilter}, function(data) {
  175. $('body').append(data);
  176. });
  177. }