dataset-management.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. define(
  2. function( require ) {
  3. var Backbone = require( "backbone" ),
  4. _ = require( "underscore" ),
  5. fui = require( "app/fui" ),
  6. datasetManagementViewTpl = require( "plugins/text!app/templates/dataset-management.tpl" );
  7. var DataManagementView = Backbone.Marionette.ItemView.extend( {
  8. initialize: function(){
  9. _.bindAll( this, "onRemoveDataset", "onConfirmAction",
  10. "onDatasetRemoveSuccess", "onDatasetRemoveFail",
  11. "onTaskStatus", "onTaskFailed", "cleanup" );
  12. this.listenTo( this.model, "change", this.onModelChange, this );
  13. fui.vent.on( "action.delete.confirm", this.onConfirmRemoveDataset, this );
  14. fui.vent.on( "action.backup.confirm", this.onConfirmBackupDataset, this );
  15. fui.vent.on( "task.status", this.onTaskStatus, this );
  16. fui.vent.on( "task.failed", this.onTaskFailed, this );
  17. },
  18. template: _.template( datasetManagementViewTpl ),
  19. ui: {
  20. actionConfirmModal: "#actionConfirmModal"
  21. },
  22. el: "#dataset-management",
  23. events: {
  24. "click .action.remove": "onRemoveDataset",
  25. "click .action.confirm": "onConfirmAction",
  26. "click .action.backup": "onBackupDataset"
  27. },
  28. templateHelpers: {
  29. },
  30. cleanup: function() {
  31. this.unbind();
  32. this.undelegateEvents();
  33. this.model.unbind( 'change', this.onModelChange, this );
  34. fui.vent.unbind( 'action.delete.confirm', this.onConfirmRemoveDataset, this );
  35. },
  36. /** If the model changes, update the summary */
  37. onModelChange: function( event ) {
  38. this.cleanup();
  39. this.render();
  40. },
  41. /** User has requested a dataset be removed */
  42. onRemoveDataset: function( e ) {
  43. e.preventDefault();
  44. var elem = $(e.currentTarget);
  45. var dsId = elem.data( "ds-id" );
  46. var msg = sprintf( "Are you sure you want to delete dataset <code>%s</code>? This action cannot be reversed.", dsId );
  47. this.showConfirmationModal( msg, dsId, "action.delete.confirm" );
  48. },
  49. /** User has requested a dataset be backed up */
  50. onBackupDataset: function( e ) {
  51. e.preventDefault();
  52. var elem = $(e.currentTarget);
  53. var dsId = elem.data( "ds-id" );
  54. var msg = sprintf( "Are you sure you want to create a backup of dataset <code>%s</code>? This action may take some time to complete", dsId );
  55. this.showConfirmationModal( msg, dsId, "action.backup.confirm" );
  56. },
  57. /** Show a generic modal confirmation */
  58. showConfirmationModal: function( msg, dsId, eventId ) {
  59. this.ui.actionConfirmModal
  60. .find( ".modal-body p" )
  61. .html( msg );
  62. this.ui.actionConfirmModal
  63. .find( ".action.confirm" )
  64. .data( "ds-id", dsId )
  65. .data( "event-id", eventId );
  66. this.clearFeedback();
  67. this.ui.actionConfirmModal.modal( 'show' );
  68. },
  69. /** Generic response to confirming the current modal dialogue */
  70. onConfirmAction: function( e ) {
  71. e.preventDefault();
  72. var elem = $(e.currentTarget);
  73. var dsId = elem.data( "ds-id" );
  74. var eventId = elem.data( "event-id" );
  75. //this.ui.actionConfirmModal.modal( 'hide' );
  76. $('.modal.in').modal('hide');
  77. $('body').removeClass('modal-open');
  78. $('.modal-backdrop').remove();
  79. _.delay( function() {
  80. fui.vent.trigger( eventId, dsId );
  81. }, 100 );
  82. },
  83. /** User has confirmed that the dataset can be deleted */
  84. onConfirmRemoveDataset: function( dsId ) {
  85. var self = this;
  86. fui.models
  87. .fusekiServer
  88. .dataset( dsId )
  89. .deleteDataset()
  90. .done( function( data ) {self.onDatasetRemoveSuccess( data, dsId );} )
  91. .error( function( jqxhr, msg, err ) {self.onDatasetRemoveFail( jqxhr, msg, err, dsId );} );
  92. },
  93. /** Callback after successfully removing a dataset */
  94. onDatasetRemoveSuccess: function( data, dsId ) {
  95. this.model.loadServerDescription();
  96. },
  97. /** Removing the dataset did not work: notify the user */
  98. onDatasetRemoveFail: function( jqxhr, msg, err, dsId ) {
  99. this.feedbackArea( dsId )
  100. .html( sprintf( "<p class='text-warning'>Sorry, removing dataset %s did not work, because: '%s'</p>", dsId, err || msg ) );
  101. },
  102. /** User has confirmed backing up the dataset */
  103. onConfirmBackupDataset: function( dsId ) {
  104. var self = this;
  105. fui.models
  106. .fusekiServer
  107. .dataset( dsId )
  108. .backupDataset();
  109. },
  110. /** Remove any current feedback content */
  111. clearFeedback: function() {
  112. $(".action.feedback").empty();
  113. },
  114. /** Long running task has updated status */
  115. onTaskStatus: function( status ) {
  116. var task = status.task;
  117. var msg = sprintf( "<p>Task <strong>%s</strong> started at %s%s</p>",
  118. task.operationType,
  119. task.taskDescription.started,
  120. status.finished ? sprintf( ", finished at %s", status.finished ) : " &ndash; ongoing" );
  121. this.feedbackArea( status.dsId )
  122. .html( msg );
  123. },
  124. /** Long running task has failed to start */
  125. onTaskFailed: function( status ) {
  126. this.feedbackArea( status.dsId )
  127. .html( sprintf( "<p class='text-danger'>Task %s failed: '%s'</p>", task.operationType, task.errorMessage ));
  128. },
  129. /** Find the feedback area for a particular dataset */
  130. feedbackArea: function( dsId ) {
  131. return $(sprintf( ".action[data-ds-id='%s']", dsId ) )
  132. .parent()
  133. .siblings(".action.feedback");
  134. }
  135. });
  136. return DataManagementView;
  137. }
  138. );