Progress.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. */
  33. WebInspector.Progress = function()
  34. {
  35. }
  36. WebInspector.Progress.prototype = {
  37. /**
  38. * @param {number} totalWork
  39. */
  40. setTotalWork: function(totalWork) { },
  41. /**
  42. * @param {string} title
  43. */
  44. setTitle: function(title) { },
  45. /**
  46. * @param {number} worked
  47. * @param {string=} title
  48. */
  49. setWorked: function(worked, title) { },
  50. /**
  51. * @param {number=} worked
  52. */
  53. worked: function(worked) { },
  54. done: function() { },
  55. /**
  56. * @return {boolean}
  57. */
  58. isCanceled: function() { return false; }
  59. }
  60. /**
  61. * @constructor
  62. * @param {WebInspector.Progress} parent
  63. */
  64. WebInspector.CompositeProgress = function(parent)
  65. {
  66. this._parent = parent;
  67. this._children = [];
  68. this._childrenDone = 0;
  69. this._parent.setTotalWork(1);
  70. this._parent.setWorked(0);
  71. }
  72. WebInspector.CompositeProgress.prototype = {
  73. _childDone: function()
  74. {
  75. if (++this._childrenDone === this._children.length)
  76. this._parent.done();
  77. },
  78. /**
  79. * @param {number=} weight
  80. */
  81. createSubProgress: function(weight)
  82. {
  83. var child = new WebInspector.SubProgress(this, weight);
  84. this._children.push(child);
  85. return child;
  86. },
  87. _update: function()
  88. {
  89. var totalWeights = 0;
  90. var done = 0;
  91. for (var i = 0; i < this._children.length; ++i) {
  92. var child = this._children[i];
  93. if (child._totalWork)
  94. done += child._weight * child._worked / child._totalWork;
  95. totalWeights += child._weight;
  96. }
  97. this._parent.setWorked(done / totalWeights);
  98. }
  99. }
  100. /**
  101. * @constructor
  102. * @implements {WebInspector.Progress}
  103. * @param {WebInspector.CompositeProgress} composite
  104. * @param {number=} weight
  105. */
  106. WebInspector.SubProgress = function(composite, weight)
  107. {
  108. this._composite = composite;
  109. this._weight = weight || 1;
  110. this._worked = 0;
  111. }
  112. WebInspector.SubProgress.prototype = {
  113. /**
  114. * @return {boolean}
  115. */
  116. isCanceled: function()
  117. {
  118. return this._composite._parent.isCanceled();
  119. },
  120. /**
  121. * @param {string} title
  122. */
  123. setTitle: function(title)
  124. {
  125. this._composite._parent.setTitle(title);
  126. },
  127. done: function()
  128. {
  129. this.setWorked(this._totalWork);
  130. this._composite._childDone();
  131. },
  132. /**
  133. * @param {number} totalWork
  134. */
  135. setTotalWork: function(totalWork)
  136. {
  137. this._totalWork = totalWork;
  138. this._composite._update();
  139. },
  140. /**
  141. * @param {number} worked
  142. * @param {string=} title
  143. */
  144. setWorked: function(worked, title)
  145. {
  146. this._worked = worked;
  147. if (typeof title !== "undefined")
  148. this.setTitle(title);
  149. this._composite._update();
  150. },
  151. /**
  152. * @param {number=} worked
  153. */
  154. worked: function(worked)
  155. {
  156. this.setWorked(this._worked + (worked || 1));
  157. }
  158. }