PageRenderTime 156ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/js/tasks.js

https://gitlab.com/clusterpress/clusterpress
JavaScript | 236 lines | 168 code | 43 blank | 25 comment | 32 complexity | 728d5f0bd9a039dcc4b0fc10eba0340c MD5 | raw file
  1. /* global wp, cp, ClusterPress_l10n */
  2. window.wp = window.wp || {};
  3. window.cp = window.cp || {};
  4. ( function( exports, $ ) {
  5. if ( typeof ClusterPress_l10n === 'undefined' ) {
  6. return;
  7. }
  8. _.extend( cp, _.pick( wp, 'Backbone', 'ajax', 'template' ) );
  9. // Init Models and Collections
  10. cp.Models = cp.Models || {};
  11. cp.Collections = cp.Collections || {};
  12. // Init Views
  13. cp.Views = cp.Views || {};
  14. /**
  15. * The Batch Processor
  16. */
  17. cp.batchProcessor = {
  18. /**
  19. * Launcher
  20. */
  21. start: function() {
  22. this.tasks = new cp.Collections.Tasks();
  23. this.completed = false;
  24. // Create the task list view
  25. var task_list = new cp.Views.Processor( { collection: this.tasks } );
  26. task_list.inject( '#clusterpress-batch' );
  27. this.setUpTasks();
  28. },
  29. /**
  30. * Populate the tasks collection
  31. */
  32. setUpTasks: function() {
  33. var self = this;
  34. _.each( ClusterPress_l10n.tasks, function( task, index ) {
  35. if ( ! _.isObject( task ) ) {
  36. return;
  37. }
  38. self.tasks.add( {
  39. id : task.callback,
  40. order : index,
  41. message : task.message,
  42. count : task.count,
  43. number : task.number,
  44. done : 0,
  45. active : false
  46. } );
  47. } );
  48. }
  49. };
  50. /**
  51. * The Tasks collection
  52. */
  53. cp.Collections.Tasks = Backbone.Collection.extend( {
  54. proceed: function( options ) {
  55. options = options || {};
  56. options.context = this;
  57. options.data = options.data || {};
  58. options.data = _.extend( options.data, {
  59. action : ClusterPress_l10n.action || 'cp_process_batch',
  60. '_clusterpress_batch_nonce' : ClusterPress_l10n.nonce
  61. } );
  62. return cp.ajax.send( options );
  63. }
  64. } );
  65. /**
  66. * Extend Backbone.View with .prepare() and .inject()
  67. */
  68. cp.View = cp.Backbone.View.extend( {
  69. inject: function( selector ) {
  70. this.render();
  71. $( selector ).html( this.el );
  72. this.views.ready();
  73. },
  74. prepare: function() {
  75. if ( ! _.isUndefined( this.model ) && _.isFunction( this.model.toJSON ) ) {
  76. return this.model.toJSON();
  77. } else {
  78. return {};
  79. }
  80. }
  81. } );
  82. /**
  83. * List of tasks view
  84. */
  85. cp.Views.Processor = cp.View.extend( {
  86. tagName : 'div',
  87. initialize: function() {
  88. this.views.add( new cp.View( { tagName: 'ul', id: 'clusterpress-tasks' } ) );
  89. this.collection.on( 'add', this.injectTask, this );
  90. this.collection.on( 'change:active', this.manageQueue, this );
  91. this.collection.on( 'change:done', this.manageQueue, this );
  92. },
  93. taskSuccess: function( response ) {
  94. var task, next, nextTask;
  95. if ( response.done && response.callback ) {
  96. task = this.collection.get( response.callback );
  97. task.set( 'done', Number( response.done ) + Number( task.get( 'done' ) ) );
  98. if ( Number( task.get( 'count' ) ) === Number( task.get( 'done' ) ) ) {
  99. task.set( 'active', false );
  100. next = Number( task.get( 'order' ) ) + 1;
  101. nextTask = this.collection.findWhere( { order: next } );
  102. if ( ! _.isUndefined( nextTask ) && _.isObject( nextTask ) ) {
  103. nextTask.set( 'active', true );
  104. } else {
  105. this.views.add( new cp.Views.Feedback( {
  106. value : ClusterPress_l10n.feedbacks.updated,
  107. type : 'updated'
  108. } ) );
  109. if ( $( '#toplevel_page_clusterpress-main a.current span.cluster-tasks' ).length ) {
  110. $( '#toplevel_page_clusterpress-main a.current span.cluster-tasks' ).remove();
  111. $( '#toplevel_page_clusterpress-main a.wp-menu-open span.cluster-tasks' ).remove();
  112. }
  113. if ( 'clusterpress' === task.get( 'id') && 'cp_process_updates' === ClusterPress_l10n.action ) {
  114. this.views.add( new cp.Views.Feedback( {
  115. value : ClusterPress_l10n.feedbacks.redirect,
  116. type : 'updated'
  117. } ) );
  118. window.setTimeout( function() {
  119. window.location.href = ClusterPress_l10n.coreUpgradeRedirect;
  120. }, 1500 );
  121. }
  122. }
  123. }
  124. }
  125. },
  126. taskError: function( response ) {
  127. if ( response.message && response.callback ) {
  128. if ( 'warning' === response.type ) {
  129. var task = this.collection.get( response.callback );
  130. response.message = response.message.replace( '%d', Number( task.get( 'count' ) ) - Number( task.get( 'done' ) ) );
  131. }
  132. $( '#' + response.callback + ' .clusterpress-task-progress' ).html( response.message ).addClass( response.type );
  133. } else {
  134. this.views.add( new cp.Views.Feedback( {
  135. value : response.message,
  136. type : 'error'
  137. } ) );
  138. }
  139. },
  140. injectTask: function( task ) {
  141. this.views.add( '#clusterpress-tasks', new cp.Views.Task( { model: task } ) );
  142. },
  143. manageQueue: function( task ) {
  144. if ( true === task.get( 'active' ) && Number( task.get( 'done' ) ) < Number( task.get( 'count' ) ) ) {
  145. this.collection.proceed( {
  146. data : _.pick( task.attributes, ['id', 'count', 'number', 'done'] ),
  147. success : _.bind( this.taskSuccess, this ),
  148. error : _.bind( this.taskError, this )
  149. } );
  150. }
  151. }
  152. } );
  153. /**
  154. * The task view
  155. */
  156. cp.Views.Task = cp.View.extend( {
  157. tagName : 'li',
  158. template : cp.template( 'progress-window' ),
  159. className : 'clusterpress-task',
  160. initialize: function() {
  161. this.model.on( 'change:done', this.taskProgress, this );
  162. this.model.on( 'change:active', this.addClass, this );
  163. if ( 0 === this.model.get( 'order' ) ) {
  164. this.model.set( 'active', true );
  165. }
  166. },
  167. addClass: function( task ) {
  168. if ( true === task.get( 'active' ) ) {
  169. $( this.$el ).addClass( 'active' );
  170. }
  171. },
  172. taskProgress: function( task ) {
  173. if ( ! _.isUndefined( task.get( 'done' ) ) && ! _.isUndefined( task.get( 'count' ) ) ) {
  174. var percent = ( Number( task.get( 'done' ) ) / Number( task.get( 'count' ) ) ) * 100;
  175. $( '#' + task.get( 'id' ) + ' .clusterpress-task-progress .clusterpress-task-bar' ).css( 'width', percent + '%' );
  176. }
  177. }
  178. } );
  179. cp.Views.Feedback = cp.View.extend( {
  180. tagName: 'div',
  181. className: 'notice is-dismissible',
  182. id: 'message',
  183. initialize: function() {
  184. this.el.className += ' ' + this.options.type;
  185. this.value = this.options.value;
  186. },
  187. render: function() {
  188. this.$el.html( $( '<p></p>' ).html( this.value ) );
  189. return this;
  190. }
  191. } );
  192. cp.batchProcessor.start();
  193. } )( cp, jQuery );