vmlelement.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Thin wrappers around the DOM element returned from
  16. * the different draw methods of the graphics. This is the VML implementation.
  17. * @author arv@google.com (Erik Arvidsson)
  18. */
  19. goog.provide('goog.graphics.VmlEllipseElement');
  20. goog.provide('goog.graphics.VmlGroupElement');
  21. goog.provide('goog.graphics.VmlImageElement');
  22. goog.provide('goog.graphics.VmlPathElement');
  23. goog.provide('goog.graphics.VmlRectElement');
  24. goog.provide('goog.graphics.VmlTextElement');
  25. goog.require('goog.dom');
  26. goog.require('goog.graphics.EllipseElement');
  27. goog.require('goog.graphics.GroupElement');
  28. goog.require('goog.graphics.ImageElement');
  29. goog.require('goog.graphics.PathElement');
  30. goog.require('goog.graphics.RectElement');
  31. goog.require('goog.graphics.TextElement');
  32. /**
  33. * Returns the VML element corresponding to this object. This method is added
  34. * to several classes below. Note that the return value of this method may
  35. * change frequently in IE8, so it should not be cached externally.
  36. * @return {Element} The VML element corresponding to this object.
  37. * @this {goog.graphics.VmlGroupElement|goog.graphics.VmlEllipseElement|
  38. * goog.graphics.VmlRectElement|goog.graphics.VmlPathElement|
  39. * goog.graphics.VmlTextElement|goog.graphics.VmlImageElement}
  40. * @private
  41. */
  42. goog.graphics.vmlGetElement_ = function() {
  43. this.element_ = this.getGraphics().getVmlElement(this.id_) || this.element_;
  44. return this.element_;
  45. };
  46. /**
  47. * Thin wrapper for VML group elements.
  48. * This is an implementation of the goog.graphics.GroupElement interface.
  49. * You should not construct objects from this constructor. The graphics
  50. * will return the object for you.
  51. * @param {Element} element The DOM element to wrap.
  52. * @param {goog.graphics.VmlGraphics} graphics The graphics creating
  53. * this element.
  54. * @constructor
  55. * @extends {goog.graphics.GroupElement}
  56. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  57. * differences before the canvas tag was widely supported. See
  58. * http://en.wikipedia.org/wiki/Canvas_element for details.
  59. * @final
  60. */
  61. goog.graphics.VmlGroupElement = function(element, graphics) {
  62. this.id_ = element.id;
  63. goog.graphics.GroupElement.call(this, element, graphics);
  64. };
  65. goog.inherits(goog.graphics.VmlGroupElement, goog.graphics.GroupElement);
  66. /** @override */
  67. goog.graphics.VmlGroupElement.prototype.getElement =
  68. goog.graphics.vmlGetElement_;
  69. /**
  70. * Remove all drawing elements from the group.
  71. * @override
  72. */
  73. goog.graphics.VmlGroupElement.prototype.clear = function() {
  74. goog.dom.removeChildren(this.getElement());
  75. };
  76. /**
  77. * @return {boolean} True if this group is the root canvas element.
  78. * @private
  79. */
  80. goog.graphics.VmlGroupElement.prototype.isRootElement_ = function() {
  81. return this.getGraphics().getCanvasElement() == this;
  82. };
  83. /**
  84. * Set the size of the group element.
  85. * @param {number|string} width The width of the group element.
  86. * @param {number|string} height The height of the group element.
  87. * @override
  88. * @suppress {missingRequire} goog.graphics.VmlGraphics
  89. */
  90. goog.graphics.VmlGroupElement.prototype.setSize = function(width, height) {
  91. var element = this.getElement();
  92. var style = element.style;
  93. style.width = goog.graphics.VmlGraphics.toSizePx(width);
  94. style.height = goog.graphics.VmlGraphics.toSizePx(height);
  95. element.coordsize = goog.graphics.VmlGraphics.toSizeCoord(width) + ' ' +
  96. goog.graphics.VmlGraphics.toSizeCoord(height);
  97. // Don't overwrite the root element's origin.
  98. if (!this.isRootElement_()) {
  99. element.coordorigin = '0 0';
  100. }
  101. };
  102. /**
  103. * Thin wrapper for VML ellipse elements.
  104. * This is an implementation of the goog.graphics.EllipseElement interface.
  105. * You should not construct objects from this constructor. The graphics
  106. * will return the object for you.
  107. * @param {Element} element The DOM element to wrap.
  108. * @param {goog.graphics.VmlGraphics} graphics The graphics creating
  109. * this element.
  110. * @param {number} cx Center X coordinate.
  111. * @param {number} cy Center Y coordinate.
  112. * @param {number} rx Radius length for the x-axis.
  113. * @param {number} ry Radius length for the y-axis.
  114. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  115. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  116. * @constructor
  117. * @extends {goog.graphics.EllipseElement}
  118. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  119. * differences before the canvas tag was widely supported. See
  120. * http://en.wikipedia.org/wiki/Canvas_element for details.
  121. * @final
  122. */
  123. goog.graphics.VmlEllipseElement = function(
  124. element, graphics, cx, cy, rx, ry, stroke, fill) {
  125. this.id_ = element.id;
  126. goog.graphics.EllipseElement.call(this, element, graphics, stroke, fill);
  127. // Store center and radius for future calls to setRadius or setCenter.
  128. /**
  129. * X coordinate of the ellipse center.
  130. * @type {number}
  131. */
  132. this.cx = cx;
  133. /**
  134. * Y coordinate of the ellipse center.
  135. * @type {number}
  136. */
  137. this.cy = cy;
  138. /**
  139. * Radius length for the x-axis.
  140. * @type {number}
  141. */
  142. this.rx = rx;
  143. /**
  144. * Radius length for the y-axis.
  145. * @type {number}
  146. */
  147. this.ry = ry;
  148. };
  149. goog.inherits(goog.graphics.VmlEllipseElement, goog.graphics.EllipseElement);
  150. /** @override */
  151. goog.graphics.VmlEllipseElement.prototype.getElement =
  152. goog.graphics.vmlGetElement_;
  153. /**
  154. * Update the center point of the ellipse.
  155. * @param {number} cx Center X coordinate.
  156. * @param {number} cy Center Y coordinate.
  157. * @override
  158. */
  159. goog.graphics.VmlEllipseElement.prototype.setCenter = function(cx, cy) {
  160. this.cx = cx;
  161. this.cy = cy;
  162. /** @suppress {missingRequire} */
  163. goog.graphics.VmlGraphics.setPositionAndSize(
  164. this.getElement(), cx - this.rx, cy - this.ry, this.rx * 2, this.ry * 2);
  165. };
  166. /**
  167. * Update the radius of the ellipse.
  168. * @param {number} rx Center X coordinate.
  169. * @param {number} ry Center Y coordinate.
  170. * @override
  171. */
  172. goog.graphics.VmlEllipseElement.prototype.setRadius = function(rx, ry) {
  173. this.rx = rx;
  174. this.ry = ry;
  175. /** @suppress {missingRequire} */
  176. goog.graphics.VmlGraphics.setPositionAndSize(
  177. this.getElement(), this.cx - rx, this.cy - ry, rx * 2, ry * 2);
  178. };
  179. /**
  180. * Thin wrapper for VML rectangle elements.
  181. * This is an implementation of the goog.graphics.RectElement interface.
  182. * You should not construct objects from this constructor. The graphics
  183. * will return the object for you.
  184. * @param {Element} element The DOM element to wrap.
  185. * @param {goog.graphics.VmlGraphics} graphics The graphics creating
  186. * this element.
  187. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  188. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  189. * @constructor
  190. * @extends {goog.graphics.RectElement}
  191. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  192. * differences before the canvas tag was widely supported. See
  193. * http://en.wikipedia.org/wiki/Canvas_element for details.
  194. * @final
  195. */
  196. goog.graphics.VmlRectElement = function(element, graphics, stroke, fill) {
  197. this.id_ = element.id;
  198. goog.graphics.RectElement.call(this, element, graphics, stroke, fill);
  199. };
  200. goog.inherits(goog.graphics.VmlRectElement, goog.graphics.RectElement);
  201. /** @override */
  202. goog.graphics.VmlRectElement.prototype.getElement =
  203. goog.graphics.vmlGetElement_;
  204. /**
  205. * Update the position of the rectangle.
  206. * @param {number} x X coordinate (left).
  207. * @param {number} y Y coordinate (top).
  208. * @override
  209. */
  210. goog.graphics.VmlRectElement.prototype.setPosition = function(x, y) {
  211. var style = this.getElement().style;
  212. style.left = /** @suppress {missingRequire} */
  213. goog.graphics.VmlGraphics.toPosPx(x);
  214. style.top = /** @suppress {missingRequire} */
  215. goog.graphics.VmlGraphics.toPosPx(y);
  216. };
  217. /**
  218. * Update the size of the rectangle.
  219. * @param {number} width Width of rectangle.
  220. * @param {number} height Height of rectangle.
  221. * @override
  222. * @suppress {missingRequire} goog.graphics.VmlGraphics
  223. */
  224. goog.graphics.VmlRectElement.prototype.setSize = function(width, height) {
  225. var style = this.getElement().style;
  226. style.width = goog.graphics.VmlGraphics.toSizePx(width);
  227. style.height = goog.graphics.VmlGraphics.toSizePx(height);
  228. };
  229. /**
  230. * Thin wrapper for VML path elements.
  231. * This is an implementation of the goog.graphics.PathElement interface.
  232. * You should not construct objects from this constructor. The graphics
  233. * will return the object for you.
  234. * @param {Element} element The DOM element to wrap.
  235. * @param {goog.graphics.VmlGraphics} graphics The graphics creating
  236. * this element.
  237. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  238. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  239. * @constructor
  240. * @extends {goog.graphics.PathElement}
  241. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  242. * differences before the canvas tag was widely supported. See
  243. * http://en.wikipedia.org/wiki/Canvas_element for details.
  244. * @final
  245. */
  246. goog.graphics.VmlPathElement = function(element, graphics, stroke, fill) {
  247. this.id_ = element.id;
  248. goog.graphics.PathElement.call(this, element, graphics, stroke, fill);
  249. };
  250. goog.inherits(goog.graphics.VmlPathElement, goog.graphics.PathElement);
  251. /** @override */
  252. goog.graphics.VmlPathElement.prototype.getElement =
  253. goog.graphics.vmlGetElement_;
  254. /**
  255. * Update the underlying path.
  256. * @param {!goog.graphics.Path} path The path object to draw.
  257. * @override
  258. */
  259. goog.graphics.VmlPathElement.prototype.setPath = function(path) {
  260. /** @suppress {missingRequire} */
  261. goog.graphics.VmlGraphics.setAttribute(
  262. this.getElement(), 'path',
  263. /** @suppress {missingRequire} */
  264. goog.graphics.VmlGraphics.getVmlPath(path));
  265. };
  266. /**
  267. * Thin wrapper for VML text elements.
  268. * This is an implementation of the goog.graphics.TextElement interface.
  269. * You should not construct objects from this constructor. The graphics
  270. * will return the object for you.
  271. * @param {Element} element The DOM element to wrap.
  272. * @param {goog.graphics.VmlGraphics} graphics The graphics creating
  273. * this element.
  274. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  275. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  276. * @constructor
  277. * @extends {goog.graphics.TextElement}
  278. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  279. * differences before the canvas tag was widely supported. See
  280. * http://en.wikipedia.org/wiki/Canvas_element for details.
  281. * @final
  282. */
  283. goog.graphics.VmlTextElement = function(element, graphics, stroke, fill) {
  284. this.id_ = element.id;
  285. goog.graphics.TextElement.call(this, element, graphics, stroke, fill);
  286. };
  287. goog.inherits(goog.graphics.VmlTextElement, goog.graphics.TextElement);
  288. /** @override */
  289. goog.graphics.VmlTextElement.prototype.getElement =
  290. goog.graphics.vmlGetElement_;
  291. /**
  292. * Update the displayed text of the element.
  293. * @param {string} text The text to draw.
  294. * @override
  295. */
  296. goog.graphics.VmlTextElement.prototype.setText = function(text) {
  297. /** @suppress {missingRequire} */
  298. goog.graphics.VmlGraphics.setAttribute(
  299. /** @type {!Element} */ (this.getElement().childNodes[1]), 'string',
  300. text);
  301. };
  302. /**
  303. * Thin wrapper for VML image elements.
  304. * This is an implementation of the goog.graphics.ImageElement interface.
  305. * You should not construct objects from this constructor. The graphics
  306. * will return the object for you.
  307. * @param {Element} element The DOM element to wrap.
  308. * @param {goog.graphics.VmlGraphics} graphics The graphics creating
  309. * this element.
  310. * @constructor
  311. * @extends {goog.graphics.ImageElement}
  312. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  313. * differences before the canvas tag was widely supported. See
  314. * http://en.wikipedia.org/wiki/Canvas_element for details.
  315. * @final
  316. */
  317. goog.graphics.VmlImageElement = function(element, graphics) {
  318. this.id_ = element.id;
  319. goog.graphics.ImageElement.call(this, element, graphics);
  320. };
  321. goog.inherits(goog.graphics.VmlImageElement, goog.graphics.ImageElement);
  322. /** @override */
  323. goog.graphics.VmlImageElement.prototype.getElement =
  324. goog.graphics.vmlGetElement_;
  325. /**
  326. * Update the position of the image.
  327. * @param {number} x X coordinate (left).
  328. * @param {number} y Y coordinate (top).
  329. * @override
  330. */
  331. goog.graphics.VmlImageElement.prototype.setPosition = function(x, y) {
  332. var style = this.getElement().style;
  333. style.left = /** @suppress {missingRequire} */
  334. goog.graphics.VmlGraphics.toPosPx(x);
  335. style.top = /** @suppress {missingRequire} */
  336. goog.graphics.VmlGraphics.toPosPx(y);
  337. };
  338. /**
  339. * Update the size of the image.
  340. * @param {number} width Width of rectangle.
  341. * @param {number} height Height of rectangle.
  342. * @override
  343. * @suppress {missingRequire} goog.graphics.VmlGraphics
  344. */
  345. goog.graphics.VmlImageElement.prototype.setSize = function(width, height) {
  346. var style = this.getElement().style;
  347. style.width = goog.graphics.VmlGraphics.toPosPx(width);
  348. style.height = goog.graphics.VmlGraphics.toPosPx(height);
  349. };
  350. /**
  351. * Update the source of the image.
  352. * @param {string} src Source of the image.
  353. * @override
  354. */
  355. goog.graphics.VmlImageElement.prototype.setSource = function(src) {
  356. /** @suppress {missingRequire} */
  357. goog.graphics.VmlGraphics.setAttribute(this.getElement(), 'src', src);
  358. };