CSSNamedFlowView.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2012 Adobe Systems Incorporated. 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
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials
  14. * provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  21. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. * OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @constructor
  31. * @extends {WebInspector.View}
  32. * @param {WebInspector.NamedFlow} flow
  33. */
  34. WebInspector.CSSNamedFlowView = function(flow)
  35. {
  36. WebInspector.View.call(this);
  37. this.element.addStyleClass("css-named-flow");
  38. this.element.addStyleClass("outline-disclosure");
  39. this._treeOutline = new TreeOutline(this.element.createChild("ol"), true);
  40. this._contentTreeItem = new TreeElement(WebInspector.UIString("content"), null, true);
  41. this._treeOutline.appendChild(this._contentTreeItem);
  42. this._regionsTreeItem = new TreeElement(WebInspector.UIString("region chain"), null, true);
  43. this._regionsTreeItem.expand();
  44. this._treeOutline.appendChild(this._regionsTreeItem);
  45. this._flow = flow;
  46. var content = flow.content;
  47. for (var i = 0; i < content.length; ++i)
  48. this._insertContentNode(content[i]);
  49. var regions = flow.regions;
  50. for (var i = 0; i < regions.length; ++i)
  51. this._insertRegion(regions[i]);
  52. }
  53. WebInspector.CSSNamedFlowView.OversetTypeMessageMap = {
  54. empty: "empty",
  55. fit: "fit",
  56. overset: "overset"
  57. }
  58. WebInspector.CSSNamedFlowView.prototype = {
  59. /**
  60. * @param {WebInspector.DOMNode=} rootDOMNode
  61. * @return {?WebInspector.ElementsTreeOutline}
  62. */
  63. _createFlowTreeOutline: function(rootDOMNode)
  64. {
  65. if (!rootDOMNode)
  66. return null;
  67. var treeOutline = new WebInspector.ElementsTreeOutline(false, false, true);
  68. treeOutline.element.addStyleClass("named-flow-element");
  69. treeOutline.setVisible(true);
  70. treeOutline.rootDOMNode = rootDOMNode;
  71. treeOutline.wireToDomAgent();
  72. WebInspector.domAgent.removeEventListener(WebInspector.DOMAgent.Events.DocumentUpdated, treeOutline._elementsTreeUpdater._documentUpdated, treeOutline._elementsTreeUpdater);
  73. return treeOutline;
  74. },
  75. /**
  76. * @param {DOMAgent.NodeId} contentNodeId
  77. * @param {number=} index
  78. */
  79. _insertContentNode: function(contentNodeId, index)
  80. {
  81. var treeOutline = this._createFlowTreeOutline(WebInspector.domAgent.nodeForId(contentNodeId));
  82. var treeItem = new TreeElement(treeOutline.element, treeOutline);
  83. if (index === undefined) {
  84. this._contentTreeItem.appendChild(treeItem);
  85. return;
  86. }
  87. this._contentTreeItem.insertChild(treeItem, index);
  88. },
  89. /**
  90. * @param {CSSAgent.Region} region
  91. * @param {number=} index
  92. */
  93. _insertRegion: function(region, index)
  94. {
  95. var treeOutline = this._createFlowTreeOutline(WebInspector.domAgent.nodeForId(region.nodeId));
  96. treeOutline.element.addStyleClass("region-" + region.regionOverset);
  97. var treeItem = new TreeElement(treeOutline.element, treeOutline);
  98. var oversetText = WebInspector.UIString(WebInspector.CSSNamedFlowView.OversetTypeMessageMap[region.regionOverset]);
  99. treeItem.tooltip = WebInspector.UIString("Region is %s.", oversetText);
  100. if (index === undefined) {
  101. this._regionsTreeItem.appendChild(treeItem);
  102. return;
  103. }
  104. this._regionsTreeItem.insertChild(treeItem, index);
  105. },
  106. get flow()
  107. {
  108. return this._flow;
  109. },
  110. set flow(newFlow)
  111. {
  112. this._update(newFlow);
  113. },
  114. /**
  115. * @param {TreeElement} regionTreeItem
  116. * @param {string} newRegionOverset
  117. * @param {string} oldRegionOverset
  118. */
  119. _updateRegionOverset: function(regionTreeItem, newRegionOverset, oldRegionOverset)
  120. {
  121. var element = regionTreeItem.representedObject.element;
  122. element.removeStyleClass("region-" + oldRegionOverset);
  123. element.addStyleClass("region-" + newRegionOverset);
  124. var oversetText = WebInspector.UIString(WebInspector.CSSNamedFlowView.OversetTypeMessageMap[newRegionOverset]);
  125. regionTreeItem.tooltip = WebInspector.UIString("Region is %s." , oversetText);
  126. },
  127. /**
  128. * @param {Array.<DOMAgent.NodeId>} oldContent
  129. * @param {Array.<DOMAgent.NodeId>} newContent
  130. */
  131. _mergeContentNodes: function(oldContent, newContent)
  132. {
  133. var nodeIdSet = {};
  134. for (var i = 0; i < newContent.length; ++i)
  135. nodeIdSet[newContent[i]] = true;
  136. var oldContentIndex = 0;
  137. var newContentIndex = 0;
  138. var contentTreeChildIndex = 0;
  139. while(oldContentIndex < oldContent.length || newContentIndex < newContent.length) {
  140. if (oldContentIndex === oldContent.length) {
  141. this._insertContentNode(newContent[newContentIndex]);
  142. ++newContentIndex;
  143. continue;
  144. }
  145. if (newContentIndex === newContent.length) {
  146. this._contentTreeItem.removeChildAtIndex(contentTreeChildIndex);
  147. ++oldContentIndex;
  148. continue;
  149. }
  150. if (oldContent[oldContentIndex] === newContent[newContentIndex]) {
  151. ++oldContentIndex;
  152. ++newContentIndex;
  153. ++contentTreeChildIndex;
  154. continue;
  155. }
  156. if (nodeIdSet[oldContent[oldContentIndex]]) {
  157. this._insertContentNode(newContent[newContentIndex], contentTreeChildIndex);
  158. ++newContentIndex;
  159. ++contentTreeChildIndex;
  160. continue;
  161. }
  162. this._contentTreeItem.removeChildAtIndex(contentTreeChildIndex);
  163. ++oldContentIndex;
  164. }
  165. },
  166. /**
  167. * @param {Array.<CSSAgent.Region>} oldRegions
  168. * @param {Array.<CSSAgent.Region>} newRegions
  169. */
  170. _mergeRegions: function(oldRegions, newRegions)
  171. {
  172. var nodeIdSet = {};
  173. for (var i = 0; i < newRegions.length; ++i)
  174. nodeIdSet[newRegions[i].nodeId] = true;
  175. var oldRegionsIndex = 0;
  176. var newRegionsIndex = 0;
  177. var regionsTreeChildIndex = 0;
  178. while(oldRegionsIndex < oldRegions.length || newRegionsIndex < newRegions.length) {
  179. if (oldRegionsIndex === oldRegions.length) {
  180. this._insertRegion(newRegions[newRegionsIndex]);
  181. ++newRegionsIndex;
  182. continue;
  183. }
  184. if (newRegionsIndex === newRegions.length) {
  185. this._regionsTreeItem.removeChildAtIndex(regionsTreeChildIndex);
  186. ++oldRegionsIndex;
  187. continue;
  188. }
  189. if (oldRegions[oldRegionsIndex].nodeId === newRegions[newRegionsIndex].nodeId) {
  190. if (oldRegions[oldRegionsIndex].regionOverset !== newRegions[newRegionsIndex].regionOverset)
  191. this._updateRegionOverset(this._regionsTreeItem.children[regionsTreeChildIndex], newRegions[newRegionsIndex].regionOverset, oldRegions[oldRegionsIndex].regionOverset);
  192. ++oldRegionsIndex;
  193. ++newRegionsIndex;
  194. ++regionsTreeChildIndex;
  195. continue;
  196. }
  197. if (nodeIdSet[oldRegions[oldRegionsIndex].nodeId]) {
  198. this._insertRegion(newRegions[newRegionsIndex], regionsTreeChildIndex);
  199. ++newRegionsIndex;
  200. ++regionsTreeChildIndex;
  201. continue;
  202. }
  203. this._regionsTreeItem.removeChildAtIndex(regionsTreeChildIndex);
  204. ++oldRegionsIndex;
  205. }
  206. },
  207. /**
  208. * @param {WebInspector.NamedFlow} newFlow
  209. */
  210. _update: function(newFlow)
  211. {
  212. this._mergeContentNodes(this._flow.content, newFlow.content);
  213. this._mergeRegions(this._flow.regions, newFlow.regions);
  214. this._flow = newFlow;
  215. },
  216. __proto__: WebInspector.View.prototype
  217. }