Popover.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (C) 2013 Apple 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.Popover = function(delegate) {
  26. WebInspector.Object.call(this);
  27. this.delegate = delegate;
  28. this._edge = null;
  29. this._frame = new WebInspector.Rect;
  30. this._content = null;
  31. this._targetFrame = new WebInspector.Rect;
  32. this._preferredEdges = null;
  33. this._contentNeedsUpdate = false;
  34. this._element = document.createElement("div");
  35. this._element.className = WebInspector.Popover.StyleClassName;
  36. this._canvasId = "popover-" + (WebInspector.Popover.canvasId++);
  37. this._element.style.backgroundImage = "-webkit-canvas(" + this._canvasId + ")";
  38. this._element.addEventListener("webkitTransitionEnd", this, true);
  39. this._container = this._element.appendChild(document.createElement("div"));
  40. this._container.className = "container";
  41. };
  42. WebInspector.Popover.StyleClassName = "popover";
  43. WebInspector.Popover.FadeOutClassName = "fade-out";
  44. WebInspector.Popover.canvasId = 0;
  45. WebInspector.Popover.CornerRadius = 5;
  46. WebInspector.Popover.MinWidth = 40;
  47. WebInspector.Popover.MinHeight = 40;
  48. WebInspector.Popover.ShadowPadding = 5;
  49. WebInspector.Popover.ContentPadding = 5;
  50. WebInspector.Popover.AnchorSize = new WebInspector.Size(22, 11);
  51. WebInspector.Popover.ShadowEdgeInsets = new WebInspector.EdgeInsets(WebInspector.Popover.ShadowPadding);
  52. WebInspector.Popover.prototype = {
  53. constructor: WebInspector.Popover,
  54. // Public
  55. get element()
  56. {
  57. return this._element;
  58. },
  59. get frame()
  60. {
  61. return this._frame;
  62. },
  63. get visible()
  64. {
  65. return this._element.parentNode === document.body && !this._element.classList.contains(WebInspector.Popover.FadeOutClassName);
  66. },
  67. set frame(frame)
  68. {
  69. this._element.style.left = frame.origin.x + "px";
  70. this._element.style.top = frame.origin.y + "px";
  71. this._element.style.width = frame.size.width + "px";
  72. this._element.style.height = frame.size.height + "px";
  73. this._element.style.backgroundSize = frame.size.width + "px " + frame.size.height + "px";
  74. this._frame = frame;
  75. },
  76. set content(content)
  77. {
  78. if (content === this._content)
  79. return;
  80. this._content = content;
  81. this._contentNeedsUpdate = true;
  82. if (this.visible)
  83. this._update();
  84. },
  85. /**
  86. * @param {WebInspector.Rect} targetFrame
  87. * @param {WebInspector.RectEdge}[] preferredEdges
  88. */
  89. present: function(targetFrame, preferredEdges)
  90. {
  91. this._targetFrame = targetFrame;
  92. this._preferredEdges = preferredEdges;
  93. if (!this._content)
  94. return;
  95. window.addEventListener("mousedown", this, true);
  96. window.addEventListener("scroll", this, true);
  97. this._update();
  98. },
  99. dismiss: function()
  100. {
  101. if (this._element.parentNode !== document.body)
  102. return;
  103. window.removeEventListener("mousedown", this, true);
  104. window.removeEventListener("scroll", this, true);
  105. this._element.classList.add(WebInspector.Popover.FadeOutClassName);
  106. if (this.delegate && typeof this.delegate.willDismissPopover === "function")
  107. this.delegate.willDismissPopover(this);
  108. },
  109. handleEvent: function(event)
  110. {
  111. switch (event.type) {
  112. case "mousedown":
  113. case "scroll":
  114. if (!this._element.contains(event.target))
  115. this.dismiss();
  116. break;
  117. case "webkitTransitionEnd":
  118. document.body.removeChild(this._element);
  119. this._element.classList.remove(WebInspector.Popover.FadeOutClassName);
  120. this._container.textContent = "";
  121. if (this.delegate && typeof this.delegate.didDismissPopover === "function")
  122. this.delegate.didDismissPopover(this);
  123. break;
  124. }
  125. },
  126. // Private
  127. _update: function(replaceContent)
  128. {
  129. var targetFrame = this._targetFrame;
  130. var preferredEdges = this._preferredEdges;
  131. // Ensure our element is on display so that its metrics can be resolved
  132. // or interrupt any pending transition to remove it from display.
  133. if (this._element.parentNode !== document.body)
  134. document.body.appendChild(this._element);
  135. else
  136. this._element.classList.remove(WebInspector.Popover.FadeOutClassName);
  137. if (this._contentNeedsUpdate) {
  138. // Reset CSS properties on element so that the element may be sized to fit its content.
  139. this._element.style.removeProperty("left");
  140. this._element.style.removeProperty("top");
  141. this._element.style.removeProperty("width");
  142. this._element.style.removeProperty("height");
  143. if (this._edge !== null)
  144. this._element.classList.remove(this._cssClassNameForEdge());
  145. // Add the content in place of the wrapper to get the raw metrics.
  146. this._element.replaceChild(this._content, this._container);
  147. // Get the ideal size for the popover to fit its content.
  148. var popoverBounds = this._element.getBoundingClientRect();
  149. this._preferredSize = new WebInspector.Size(Math.ceil(popoverBounds.width), Math.ceil(popoverBounds.height));
  150. }
  151. // The frame of the window with a little inset to make sure we have room for shadows.
  152. var containerFrame = new WebInspector.Rect(0, 0, window.innerWidth, window.innerHeight);
  153. containerFrame = containerFrame.inset(WebInspector.Popover.ShadowEdgeInsets);
  154. // Work out the metrics for all edges.
  155. var metrics = new Array(preferredEdges.length);
  156. for (var edgeName in WebInspector.RectEdge) {
  157. var edge = WebInspector.RectEdge[edgeName];
  158. var item = {
  159. edge: edge,
  160. metrics: this._bestMetricsForEdge(this._preferredSize, targetFrame, containerFrame, edge)
  161. };
  162. var preferredIndex = preferredEdges.indexOf(edge);
  163. if (preferredIndex !== -1)
  164. metrics[preferredIndex] = item;
  165. else
  166. metrics.push(item);
  167. }
  168. function area(size)
  169. {
  170. return size.width * size.height;
  171. }
  172. // Find if any of those fit better than the frame for the preferred edge.
  173. var bestEdge = metrics[0].edge;
  174. var bestMetrics = metrics[0].metrics;
  175. for (var i = 1; i < metrics.length; i++) {
  176. var itemMetrics = metrics[i].metrics;
  177. if (area(itemMetrics.contentSize) > area(bestMetrics.contentSize)) {
  178. bestEdge = metrics[i].edge;
  179. bestMetrics = itemMetrics;
  180. }
  181. }
  182. var anchorPoint;
  183. var bestFrame = bestMetrics.frame;
  184. var needsToDrawBackground = !this._frame.size.equals(bestFrame.size) || this._edge !== bestEdge;
  185. this.frame = bestFrame;
  186. this._edge = bestEdge;
  187. if (this.frame === WebInspector.Rect.ZERO_RECT) {
  188. // The target for the popover is offscreen.
  189. this.dismiss();
  190. } else {
  191. switch (bestEdge) {
  192. case WebInspector.RectEdge.MIN_X: // Displayed on the left of the target, arrow points right.
  193. anchorPoint = new WebInspector.Point(bestFrame.size.width - WebInspector.Popover.ShadowPadding, targetFrame.midY() - bestFrame.minY());
  194. break;
  195. case WebInspector.RectEdge.MAX_X: // Displayed on the right of the target, arrow points left.
  196. anchorPoint = new WebInspector.Point(WebInspector.Popover.ShadowPadding, targetFrame.midY() - bestFrame.minY());
  197. break;
  198. case WebInspector.RectEdge.MIN_Y: // Displayed above the target, arrow points down.
  199. anchorPoint = new WebInspector.Point(targetFrame.midX() - bestFrame.minX(), bestFrame.size.height - WebInspector.Popover.ShadowPadding);
  200. break;
  201. case WebInspector.RectEdge.MAX_Y: // Displayed below the target, arrow points up.
  202. anchorPoint = new WebInspector.Point(targetFrame.midX() - bestFrame.minX(), WebInspector.Popover.ShadowPadding);
  203. break;
  204. }
  205. this._element.classList.add(this._cssClassNameForEdge());
  206. if (needsToDrawBackground)
  207. this._drawBackground(bestEdge, anchorPoint);
  208. // Make sure content is centered in case either of the dimension is smaller than the minimal bounds.
  209. if (this._preferredSize.width < WebInspector.Popover.MinWidth || this._preferredSize.height < WebInspector.Popover.MinHeight)
  210. this._container.classList.add("center");
  211. else
  212. this._container.classList.remove("center");
  213. }
  214. // Wrap the content in the container so that it's located correctly.
  215. if (this._contentNeedsUpdate) {
  216. this._container.textContent = "";
  217. this._element.replaceChild(this._container, this._content);
  218. this._container.appendChild(this._content);
  219. }
  220. this._contentNeedsUpdate = false;
  221. },
  222. _cssClassNameForEdge: function()
  223. {
  224. switch (this._edge) {
  225. case WebInspector.RectEdge.MIN_X: // Displayed on the left of the target, arrow points right.
  226. return "arrow-right";
  227. case WebInspector.RectEdge.MAX_X: // Displayed on the right of the target, arrow points left.
  228. return "arrow-left";
  229. case WebInspector.RectEdge.MIN_Y: // Displayed above the target, arrow points down.
  230. return "arrow-down";
  231. case WebInspector.RectEdge.MAX_Y: // Displayed below the target, arrow points up.
  232. return "arrow-up";
  233. }
  234. console.error("Unknown edge.");
  235. return "arrow-up";
  236. },
  237. _drawBackground: function(edge, anchorPoint)
  238. {
  239. var scaleFactor = window.devicePixelRatio;
  240. var width = this._frame.size.width;
  241. var height = this._frame.size.height;
  242. var scaledWidth = width * scaleFactor;
  243. var scaledHeight = height * scaleFactor;
  244. // Create a scratch canvas so we can draw the popover that will later be drawn into
  245. // the final context with a shadow.
  246. var scratchCanvas = document.createElement("canvas");
  247. scratchCanvas.width = scaledWidth;
  248. scratchCanvas.height = scaledHeight;
  249. var ctx = scratchCanvas.getContext("2d");
  250. ctx.scale(scaleFactor, scaleFactor);
  251. // Bounds of the path don't take into account the arrow, but really only the tight bounding box
  252. // of the content contained within the frame.
  253. var bounds;
  254. var arrowHeight = WebInspector.Popover.AnchorSize.height;
  255. switch (edge) {
  256. case WebInspector.RectEdge.MIN_X: // Displayed on the left of the target, arrow points right.
  257. bounds = new WebInspector.Rect(0, 0, width - arrowHeight, height);
  258. break;
  259. case WebInspector.RectEdge.MAX_X: // Displayed on the right of the target, arrow points left.
  260. bounds = new WebInspector.Rect(arrowHeight, 0, width - arrowHeight, height);
  261. break;
  262. case WebInspector.RectEdge.MIN_Y: // Displayed above the target, arrow points down.
  263. bounds = new WebInspector.Rect(0, 0, width, height - arrowHeight);
  264. break;
  265. case WebInspector.RectEdge.MAX_Y: // Displayed below the target, arrow points up.
  266. bounds = new WebInspector.Rect(0, arrowHeight, width, height - arrowHeight);
  267. break;
  268. }
  269. bounds = bounds.inset(WebInspector.Popover.ShadowEdgeInsets);
  270. // Clip the frame.
  271. ctx.fillStyle = "black";
  272. this._drawFrame(ctx, bounds, edge, anchorPoint);
  273. ctx.clip();
  274. // Gradient fill, top-to-bottom.
  275. var fillGradient = ctx.createLinearGradient(0, 0, 0, height);
  276. fillGradient.addColorStop(0, "rgba(255, 255, 255, 0.95)");
  277. fillGradient.addColorStop(1, "rgba(235, 235, 235, 0.95)");
  278. ctx.fillStyle = fillGradient;
  279. ctx.fillRect(0, 0, width, height);
  280. // Stroke.
  281. ctx.strokeStyle = "rgba(0, 0, 0, 0.25)";
  282. ctx.lineWidth = 2;
  283. this._drawFrame(ctx, bounds, edge, anchorPoint);
  284. ctx.stroke();
  285. // Draw the popover into the final context with a drop shadow.
  286. var finalContext = document.getCSSCanvasContext("2d", this._canvasId, scaledWidth, scaledHeight);
  287. finalContext.clearRect(0, 0, scaledWidth, scaledHeight);
  288. finalContext.shadowOffsetX = 1;
  289. finalContext.shadowOffsetY = 1;
  290. finalContext.shadowBlur = 5;
  291. finalContext.shadowColor = "rgba(0, 0, 0, 0.5)";
  292. finalContext.drawImage(scratchCanvas, 0, 0, scaledWidth, scaledHeight);
  293. },
  294. _bestMetricsForEdge: function(preferredSize, targetFrame, containerFrame, edge)
  295. {
  296. var x, y;
  297. var width = preferredSize.width + (WebInspector.Popover.ShadowPadding * 2) + (WebInspector.Popover.ContentPadding * 2);
  298. var height = preferredSize.height + (WebInspector.Popover.ShadowPadding * 2) + (WebInspector.Popover.ContentPadding * 2);
  299. var arrowLength = WebInspector.Popover.AnchorSize.height;
  300. switch (edge) {
  301. case WebInspector.RectEdge.MIN_X: // Displayed on the left of the target, arrow points right.
  302. width += arrowLength;
  303. x = targetFrame.origin.x - width + WebInspector.Popover.ShadowPadding;
  304. y = targetFrame.origin.y - (height - targetFrame.size.height) / 2;
  305. break;
  306. case WebInspector.RectEdge.MAX_X: // Displayed on the right of the target, arrow points left.
  307. width += arrowLength;
  308. x = targetFrame.origin.x + targetFrame.size.width - WebInspector.Popover.ShadowPadding;
  309. y = targetFrame.origin.y - (height - targetFrame.size.height) / 2;
  310. break;
  311. case WebInspector.RectEdge.MIN_Y: // Displayed above the target, arrow points down.
  312. height += arrowLength;
  313. x = targetFrame.origin.x - (width - targetFrame.size.width) / 2;
  314. y = targetFrame.origin.y - height + WebInspector.Popover.ShadowPadding;
  315. break;
  316. case WebInspector.RectEdge.MAX_Y: // Displayed below the target, arrow points up.
  317. height += arrowLength;
  318. x = targetFrame.origin.x - (width - targetFrame.size.width) / 2;
  319. y = targetFrame.origin.y + targetFrame.size.height - WebInspector.Popover.ShadowPadding;
  320. break;
  321. }
  322. var preferredFrame = new WebInspector.Rect(x, y, width, height);
  323. var bestFrame = preferredFrame.intersectionWithRect(containerFrame);
  324. width = bestFrame.size.width - (WebInspector.Popover.ShadowPadding * 2);
  325. height = bestFrame.size.height - (WebInspector.Popover.ShadowPadding * 2);
  326. switch (edge) {
  327. case WebInspector.RectEdge.MIN_X: // Displayed on the left of the target, arrow points right.
  328. case WebInspector.RectEdge.MAX_X: // Displayed on the right of the target, arrow points left.
  329. width -= arrowLength;
  330. break;
  331. case WebInspector.RectEdge.MIN_Y: // Displayed above the target, arrow points down.
  332. case WebInspector.RectEdge.MAX_Y: // Displayed below the target, arrow points up.
  333. height -= arrowLength;
  334. break;
  335. }
  336. return {
  337. frame: bestFrame,
  338. contentSize: new WebInspector.Size(width, height)
  339. };
  340. },
  341. _drawFrame: function(ctx, bounds, anchorEdge, anchorPoint)
  342. {
  343. var r = WebInspector.Popover.CornerRadius;
  344. var arrowHalfLength = WebInspector.Popover.AnchorSize.width / 2;
  345. ctx.beginPath();
  346. switch (anchorEdge) {
  347. case WebInspector.RectEdge.MIN_X: // Displayed on the left of the target, arrow points right.
  348. ctx.moveTo(bounds.maxX(), bounds.minY() + r);
  349. ctx.lineTo(bounds.maxX(), anchorPoint.y - arrowHalfLength);
  350. ctx.lineTo(anchorPoint.x, anchorPoint.y);
  351. ctx.lineTo(bounds.maxX(), anchorPoint.y + arrowHalfLength);
  352. ctx.arcTo(bounds.maxX(), bounds.maxY(), bounds.minX(), bounds.maxY(), r);
  353. ctx.arcTo(bounds.minX(), bounds.maxY(), bounds.minX(), bounds.minY(), r);
  354. ctx.arcTo(bounds.minX(), bounds.minY(), bounds.maxX(), bounds.minY(), r);
  355. ctx.arcTo(bounds.maxX(), bounds.minY(), bounds.maxX(), bounds.maxY(), r);
  356. break;
  357. case WebInspector.RectEdge.MAX_X: // Displayed on the right of the target, arrow points left.
  358. ctx.moveTo(bounds.minX(), bounds.maxY() - r);
  359. ctx.lineTo(bounds.minX(), anchorPoint.y + arrowHalfLength);
  360. ctx.lineTo(anchorPoint.x, anchorPoint.y);
  361. ctx.lineTo(bounds.minX(), anchorPoint.y - arrowHalfLength);
  362. ctx.arcTo(bounds.minX(), bounds.minY(), bounds.maxX(), bounds.minY(), r);
  363. ctx.arcTo(bounds.maxX(), bounds.minY(), bounds.maxX(), bounds.maxY(), r);
  364. ctx.arcTo(bounds.maxX(), bounds.maxY(), bounds.minX(), bounds.maxY(), r);
  365. ctx.arcTo(bounds.minX(), bounds.maxY(), bounds.minX(), bounds.minY(), r);
  366. break;
  367. case WebInspector.RectEdge.MIN_Y: // Displayed above the target, arrow points down.
  368. ctx.moveTo(bounds.maxX() - r, bounds.maxY());
  369. ctx.lineTo(anchorPoint.x + arrowHalfLength, bounds.maxY());
  370. ctx.lineTo(anchorPoint.x, anchorPoint.y);
  371. ctx.lineTo(anchorPoint.x - arrowHalfLength, bounds.maxY());
  372. ctx.arcTo(bounds.minX(), bounds.maxY(), bounds.minX(), bounds.minY(), r);
  373. ctx.arcTo(bounds.minX(), bounds.minY(), bounds.maxX(), bounds.minY(), r);
  374. ctx.arcTo(bounds.maxX(), bounds.minY(), bounds.maxX(), bounds.maxY(), r);
  375. ctx.arcTo(bounds.maxX(), bounds.maxY(), bounds.minX(), bounds.maxY(), r);
  376. break;
  377. case WebInspector.RectEdge.MAX_Y: // Displayed below the target, arrow points up.
  378. ctx.moveTo(bounds.minX() + r, bounds.minY());
  379. ctx.lineTo(anchorPoint.x - arrowHalfLength, bounds.minY());
  380. ctx.lineTo(anchorPoint.x, anchorPoint.y);
  381. ctx.lineTo(anchorPoint.x + arrowHalfLength, bounds.minY());
  382. ctx.arcTo(bounds.maxX(), bounds.minY(), bounds.maxX(), bounds.maxY(), r);
  383. ctx.arcTo(bounds.maxX(), bounds.maxY(), bounds.minX(), bounds.maxY(), r);
  384. ctx.arcTo(bounds.minX(), bounds.maxY(), bounds.minX(), bounds.minY(), r);
  385. ctx.arcTo(bounds.minX(), bounds.minY(), bounds.maxX(), bounds.minY(), r);
  386. break;
  387. }
  388. ctx.closePath();
  389. }
  390. };
  391. WebInspector.Popover.prototype.__proto__ = WebInspector.Object.prototype;