Progress.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @interface
  32. * @extends {WebInspector.EventTarget}
  33. */
  34. WebInspector.Progress = function()
  35. {
  36. }
  37. WebInspector.Progress.Events = {
  38. Canceled: "Canceled"
  39. }
  40. WebInspector.Progress.prototype = {
  41. /**
  42. * @param {number} totalWork
  43. */
  44. setTotalWork: function(totalWork) { },
  45. /**
  46. * @param {string} title
  47. */
  48. setTitle: function(title) { },
  49. /**
  50. * @param {number} worked
  51. * @param {string=} title
  52. */
  53. setWorked: function(worked, title) { },
  54. /**
  55. * @param {number=} worked
  56. */
  57. worked: function(worked) { },
  58. done: function() { },
  59. /**
  60. * @return {boolean}
  61. */
  62. isCanceled: function() { return false; },
  63. /**
  64. * @param {string} eventType
  65. * @param {function(WebInspector.Event)} listener
  66. * @param {Object=} thisObject
  67. */
  68. addEventListener: function(eventType, listener, thisObject) { }
  69. }
  70. /**
  71. * @constructor
  72. * @param {WebInspector.Progress} parent
  73. * @extends {WebInspector.Object}
  74. */
  75. WebInspector.CompositeProgress = function(parent)
  76. {
  77. this._parent = parent;
  78. this._children = [];
  79. this._childrenDone = 0;
  80. this._parent.setTotalWork(1);
  81. this._parent.setWorked(0);
  82. parent.addEventListener(WebInspector.Progress.Events.Canceled, this._parentCanceled.bind(this));
  83. }
  84. WebInspector.CompositeProgress.prototype = {
  85. _childDone: function()
  86. {
  87. if (++this._childrenDone === this._children.length)
  88. this._parent.done();
  89. },
  90. _parentCanceled: function()
  91. {
  92. this.dispatchEventToListeners(WebInspector.Progress.Events.Canceled);
  93. for (var i = 0; i < this._children.length; ++i) {
  94. this._children[i].dispatchEventToListeners(WebInspector.Progress.Events.Canceled);
  95. }
  96. },
  97. /**
  98. * @param {number=} weight
  99. */
  100. createSubProgress: function(weight)
  101. {
  102. var child = new WebInspector.SubProgress(this, weight);
  103. this._children.push(child);
  104. return child;
  105. },
  106. _update: function()
  107. {
  108. var totalWeights = 0;
  109. var done = 0;
  110. for (var i = 0; i < this._children.length; ++i) {
  111. var child = this._children[i];
  112. if (child._totalWork)
  113. done += child._weight * child._worked / child._totalWork;
  114. totalWeights += child._weight;
  115. }
  116. this._parent.setWorked(done / totalWeights);
  117. },
  118. __proto__: WebInspector.Object.prototype
  119. }
  120. /**
  121. * @constructor
  122. * @implements {WebInspector.Progress}
  123. * @extends {WebInspector.Object}
  124. * @param {WebInspector.CompositeProgress} composite
  125. * @param {number=} weight
  126. */
  127. WebInspector.SubProgress = function(composite, weight)
  128. {
  129. this._composite = composite;
  130. this._weight = weight || 1;
  131. this._worked = 0;
  132. }
  133. WebInspector.SubProgress.prototype = {
  134. /**
  135. * @return {boolean}
  136. */
  137. isCanceled: function()
  138. {
  139. return this._composite._parent.isCanceled();
  140. },
  141. /**
  142. * @param {string} title
  143. */
  144. setTitle: function(title)
  145. {
  146. this._composite._parent.setTitle(title);
  147. },
  148. done: function()
  149. {
  150. this.setWorked(this._totalWork);
  151. this._composite._childDone();
  152. },
  153. /**
  154. * @param {number} totalWork
  155. */
  156. setTotalWork: function(totalWork)
  157. {
  158. this._totalWork = totalWork;
  159. this._composite._update();
  160. },
  161. /**
  162. * @param {number} worked
  163. * @param {string=} title
  164. */
  165. setWorked: function(worked, title)
  166. {
  167. this._worked = worked;
  168. if (typeof title !== "undefined")
  169. this.setTitle(title);
  170. this._composite._update();
  171. },
  172. /**
  173. * @param {number=} worked
  174. */
  175. worked: function(worked)
  176. {
  177. this.setWorked(this._worked + (worked || 1));
  178. },
  179. __proto__: WebInspector.Object.prototype
  180. }