Debian Codegraph @

closure-library/closure/goog/ui/roundedpanel.js

raw
1 // Copyright 2008 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 /**
16 * @fileoverview Class definition for a rounded corner panel.
17 * @supported IE 6.0+, Safari 2.0+, Firefox 1.5+, Opera 9.2+.
18 * @see ../demos/roundedpanel.html
19 */
20
21 goog.provide('goog.ui.BaseRoundedPanel');
22 goog.provide('goog.ui.CssRoundedPanel');
23 goog.provide('goog.ui.GraphicsRoundedPanel');
24 goog.provide('goog.ui.RoundedPanel');
25 goog.provide('goog.ui.RoundedPanel.Corner');
26
27 goog.require('goog.asserts');
28 goog.require('goog.dom');
29 goog.require('goog.dom.TagName');
30 goog.require('goog.dom.classlist');
31 goog.require('goog.graphics');
32 goog.require('goog.graphics.Path');
33 goog.require('goog.graphics.SolidFill');
34 goog.require('goog.graphics.Stroke');
35 goog.require('goog.math');
36 goog.require('goog.math.Coordinate');
37 goog.require('goog.style');
38 goog.require('goog.ui.Component');
39 goog.require('goog.userAgent');
40
41
42 /**
43 * Factory method that returns an instance of a BaseRoundedPanel.
44 * @param {number} radius The radius of the rounded corner(s), in pixels.
45 * @param {number} borderWidth The thickness of the border, in pixels.
46 * @param {string} borderColor The border color of the panel.
47 * @param {string=} opt_backgroundColor The background color of the panel.
48 * @param {number=} opt_corners The corners of the panel to be rounded. Any
49 * corners not specified will be rendered as square corners. Will default
50 * to all square corners if not specified.
51 * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
52 * document we want to render in.
53 * @return {!goog.ui.BaseRoundedPanel} An instance of a
54 * goog.ui.BaseRoundedPanel subclass.
55 * TODO(user): deprecate this class, which has <5 usages and only really
56 * matters for IE8, and then only stylistically.
57 */
58 goog.ui.RoundedPanel.create = function(
59 radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
60 opt_domHelper) {
61 // This variable checks for the presence of Safari 3.0+ or Gecko 1.9+,
62 // which can leverage special CSS styles to create rounded corners.
63 var isCssReady =
64 (goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('500')) ||
65 (goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9a')) ||
66 goog.userAgent.EDGE;
67
68 if (isCssReady) {
69 // Safari 3.0+ and Firefox 3.0+ support this instance.
70 return new goog.ui.CssRoundedPanel(
71 radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
72 opt_domHelper);
73 } else {
74 return new goog.ui.GraphicsRoundedPanel(
75 radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
76 opt_domHelper);
77 }
78 };
79
80
81 /**
82 * Enum for specifying which corners to render.
83 * @enum {number}
84 */
85 goog.ui.RoundedPanel.Corner = {
86 NONE: 0,
87 BOTTOM_LEFT: 2,
88 TOP_LEFT: 4,
89 LEFT: 6, // BOTTOM_LEFT | TOP_LEFT
90 TOP_RIGHT: 8,
91 TOP: 12, // TOP_LEFT | TOP_RIGHT
92 BOTTOM_RIGHT: 1,
93 BOTTOM: 3, // BOTTOM_LEFT | BOTTOM_RIGHT
94 RIGHT: 9, // TOP_RIGHT | BOTTOM_RIGHT
95 ALL: 15 // TOP | BOTTOM
96 };
97
98
99 /**
100 * CSS class name suffixes for the elements comprising the RoundedPanel.
101 * @enum {string}
102 * @private
103 */
104 goog.ui.RoundedPanel.Classes_ = {
105 BACKGROUND: goog.getCssName('goog-roundedpanel-background'),
106 PANEL: goog.getCssName('goog-roundedpanel'),
107 CONTENT: goog.getCssName('goog-roundedpanel-content')
108 };
109
110
111
112 /**
113 * Base class for the hierarchy of RoundedPanel classes. Do not
114 * instantiate directly. Instead, call goog.ui.RoundedPanel.create().
115 * The HTML structure for the RoundedPanel is:
116 * <pre>
117 * - div (Contains the background and content. Class name: goog-roundedpanel)
118 * - div (Contains the background/rounded corners. Class name:
119 * goog-roundedpanel-bg)
120 * - div (Contains the content. Class name: goog-roundedpanel-content)
121 * </pre>
122 * @param {number} radius The radius of the rounded corner(s), in pixels.
123 * @param {number} borderWidth The thickness of the border, in pixels.
124 * @param {string} borderColor The border color of the panel.
125 * @param {string=} opt_backgroundColor The background color of the panel.
126 * @param {number=} opt_corners The corners of the panel to be rounded. Any
127 * corners not specified will be rendered as square corners. Will default
128 * to all square corners if not specified.
129 * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
130 * document we want to render in.
131 * @extends {goog.ui.Component}
132 * @constructor
133 */
134 goog.ui.BaseRoundedPanel = function(
135 radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
136 opt_domHelper) {
137 goog.ui.Component.call(this, opt_domHelper);
138
139 /**
140 * The radius of the rounded corner(s), in pixels.
141 * @type {number}
142 * @private
143 */
144 this.radius_ = radius;
145
146 /**
147 * The thickness of the border, in pixels.
148 * @type {number}
149 * @private
150 */
151 this.borderWidth_ = borderWidth;
152
153 /**
154 * The border color of the panel.
155 * @type {string}
156 * @private
157 */
158 this.borderColor_ = borderColor;
159
160 /**
161 * The background color of the panel.
162 * @type {?string}
163 * @private
164 */
165 this.backgroundColor_ = opt_backgroundColor || null;
166
167 /**
168 * The corners of the panel to be rounded; defaults to
169 * goog.ui.RoundedPanel.Corner.NONE
170 * @type {number}
171 * @private
172 */
173 this.corners_ = opt_corners || goog.ui.RoundedPanel.Corner.NONE;
174 };
175 goog.inherits(goog.ui.BaseRoundedPanel, goog.ui.Component);
176 goog.tagUnsealableClass(goog.ui.BaseRoundedPanel);
177
178
179 /**
180 * The element containing the rounded corners and background.
181 * @type {Element}
182 * @private
183 */
184 goog.ui.BaseRoundedPanel.prototype.backgroundElement_;
185
186
187 /**
188 * The element containing the actual content.
189 * @type {Element}
190 * @private
191 */
192 goog.ui.BaseRoundedPanel.prototype.contentElement_;
193
194
195 /**
196 * This method performs all the necessary DOM manipulation to create the panel.
197 * Overrides {@link goog.ui.Component#decorateInternal}.
198 * @param {Element} element The element to decorate.
199 * @protected
200 * @override
201 */
202 goog.ui.BaseRoundedPanel.prototype.decorateInternal = function(element) {
203 goog.ui.BaseRoundedPanel.superClass_.decorateInternal.call(this, element);
204 goog.dom.classlist.add(
205 goog.asserts.assert(this.getElement()),
206 goog.ui.RoundedPanel.Classes_.PANEL);
207
208 // Create backgroundElement_, and add it to the DOM.
209 this.backgroundElement_ =
210 this.getDomHelper().createElement(goog.dom.TagName.DIV);
211 this.backgroundElement_.className = goog.ui.RoundedPanel.Classes_.BACKGROUND;
212 this.getElement().appendChild(this.backgroundElement_);
213
214 // Set contentElement_ by finding a child node within element_ with the
215 // proper class name. If none exists, create it and add it to the DOM.
216 this.contentElement_ = goog.dom.getElementsByTagNameAndClass(
217 null, goog.ui.RoundedPanel.Classes_.CONTENT, this.getElement())[0];
218 if (!this.contentElement_) {
219 this.contentElement_ = this.getDomHelper().createDom(goog.dom.TagName.DIV);
220 this.contentElement_.className = goog.ui.RoundedPanel.Classes_.CONTENT;
221 this.getElement().appendChild(this.contentElement_);
222 }
223 };
224
225
226 /** @override */
227 goog.ui.BaseRoundedPanel.prototype.disposeInternal = function() {
228 if (this.backgroundElement_) {
229 this.getDomHelper().removeNode(this.backgroundElement_);
230 this.backgroundElement_ = null;
231 }
232 this.contentElement_ = null;
233 goog.ui.BaseRoundedPanel.superClass_.disposeInternal.call(this);
234 };
235
236
237 /**
238 * Returns the DOM element containing the actual content.
239 * @return {Element} The element containing the actual content (null if none).
240 * @override
241 */
242 goog.ui.BaseRoundedPanel.prototype.getContentElement = function() {
243 return this.contentElement_;
244 };
245
246
247
248 /**
249 * RoundedPanel class specifically for browsers that support CSS attributes
250 * for elements with rounded borders (ex. Safari 3.0+, Firefox 3.0+). Do not
251 * instantiate directly. Instead, call goog.ui.RoundedPanel.create().
252 * @param {number} radius The radius of the rounded corner(s), in pixels.
253 * @param {number} borderWidth The thickness of the border, in pixels.
254 * @param {string} borderColor The border color of the panel.
255 * @param {string=} opt_backgroundColor The background color of the panel.
256 * @param {number=} opt_corners The corners of the panel to be rounded. Any
257 * corners not specified will be rendered as square corners. Will
258 * default to all square corners if not specified.
259 * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
260 * document we want to render in.
261 * @extends {goog.ui.BaseRoundedPanel}
262 * @constructor
263 * @final
264 */
265 goog.ui.CssRoundedPanel = function(
266 radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
267 opt_domHelper) {
268 goog.ui.BaseRoundedPanel.call(
269 this, radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
270 opt_domHelper);
271 };
272 goog.inherits(goog.ui.CssRoundedPanel, goog.ui.BaseRoundedPanel);
273
274
275 /**
276 * This method performs all the necessary DOM manipulation to create the panel.
277 * Overrides {@link goog.ui.Component#decorateInternal}.
278 * @param {Element} element The element to decorate.
279 * @protected
280 * @override
281 */
282 goog.ui.CssRoundedPanel.prototype.decorateInternal = function(element) {
283 goog.ui.CssRoundedPanel.superClass_.decorateInternal.call(this, element);
284
285 // Set the border width and background color, if needed.
286 this.backgroundElement_.style.border =
287 this.borderWidth_ + 'px solid ' + this.borderColor_;
288 if (this.backgroundColor_) {
289 this.backgroundElement_.style.backgroundColor = this.backgroundColor_;
290 }
291
292 // Set radii of the appropriate rounded corners.
293 if (this.corners_ == goog.ui.RoundedPanel.Corner.ALL) {
294 var styleName = this.getStyle_(goog.ui.RoundedPanel.Corner.ALL);
295 this.backgroundElement_.style[styleName] = this.radius_ + 'px';
296 } else {
297 var topLeftRadius =
298 this.corners_ & goog.ui.RoundedPanel.Corner.TOP_LEFT ? this.radius_ : 0;
299 var cornerStyle = this.getStyle_(goog.ui.RoundedPanel.Corner.TOP_LEFT);
300 this.backgroundElement_.style[cornerStyle] = topLeftRadius + 'px';
301 var topRightRadius = this.corners_ & goog.ui.RoundedPanel.Corner.TOP_RIGHT ?
302 this.radius_ :
303 0;
304 cornerStyle = this.getStyle_(goog.ui.RoundedPanel.Corner.TOP_RIGHT);
305 this.backgroundElement_.style[cornerStyle] = topRightRadius + 'px';
306 var bottomRightRadius =
307 this.corners_ & goog.ui.RoundedPanel.Corner.BOTTOM_RIGHT ?
308 this.radius_ :
309 0;
310 cornerStyle = this.getStyle_(goog.ui.RoundedPanel.Corner.BOTTOM_RIGHT);
311 this.backgroundElement_.style[cornerStyle] = bottomRightRadius + 'px';
312 var bottomLeftRadius =
313 this.corners_ & goog.ui.RoundedPanel.Corner.BOTTOM_LEFT ? this.radius_ :
314 0;
315 cornerStyle = this.getStyle_(goog.ui.RoundedPanel.Corner.BOTTOM_LEFT);
316 this.backgroundElement_.style[cornerStyle] = bottomLeftRadius + 'px';
317 }
318 };
319
320
321 /**
322 * This method returns the CSS style based on the corner of the panel, and the
323 * user-agent.
324 * @param {number} corner The corner whose style name to retrieve.
325 * @private
326 * @return {string} The CSS style based on the specified corner.
327 */
328 goog.ui.CssRoundedPanel.prototype.getStyle_ = function(corner) {
329 // Determine the proper corner to work with.
330 var cssCorner, suffixLeft, suffixRight;
331 if (goog.userAgent.WEBKIT) {
332 suffixLeft = 'Left';
333 suffixRight = 'Right';
334 } else {
335 suffixLeft = 'left';
336 suffixRight = 'right';
337 }
338 switch (corner) {
339 case goog.ui.RoundedPanel.Corner.ALL:
340 cssCorner = '';
341 break;
342 case goog.ui.RoundedPanel.Corner.TOP_LEFT:
343 cssCorner = 'Top' + suffixLeft;
344 break;
345 case goog.ui.RoundedPanel.Corner.TOP_RIGHT:
346 cssCorner = 'Top' + suffixRight;
347 break;
348 case goog.ui.RoundedPanel.Corner.BOTTOM_LEFT:
349 cssCorner = 'Bottom' + suffixLeft;
350 break;
351 case goog.ui.RoundedPanel.Corner.BOTTOM_RIGHT:
352 cssCorner = 'Bottom' + suffixRight;
353 break;
354 }
355
356 return goog.userAgent.WEBKIT ? 'WebkitBorder' + cssCorner + 'Radius' :
357 'MozBorderRadius' + cssCorner;
358 };
359
360
361
362 /**
363 * RoundedPanel class that uses goog.graphics to create the rounded corners.
364 * Do not instantiate directly. Instead, call goog.ui.RoundedPanel.create().
365 * @param {number} radius The radius of the rounded corner(s), in pixels.
366 * @param {number} borderWidth The thickness of the border, in pixels.
367 * @param {string} borderColor The border color of the panel.
368 * @param {string=} opt_backgroundColor The background color of the panel.
369 * @param {number=} opt_corners The corners of the panel to be rounded. Any
370 * corners not specified will be rendered as square corners. Will
371 * default to all square corners if not specified.
372 * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
373 * document we want to render in.
374 * @extends {goog.ui.BaseRoundedPanel}
375 * @constructor
376 * @final
377 */
378 goog.ui.GraphicsRoundedPanel = function(
379 radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
380 opt_domHelper) {
381 goog.ui.BaseRoundedPanel.call(
382 this, radius, borderWidth, borderColor, opt_backgroundColor, opt_corners,
383 opt_domHelper);
384 };
385 goog.inherits(goog.ui.GraphicsRoundedPanel, goog.ui.BaseRoundedPanel);
386
387
388 /**
389 * A 4-element array containing the circle centers for the arcs in the
390 * bottom-left, top-left, top-right, and bottom-right corners, respectively.
391 * @type {Array<goog.math.Coordinate>}
392 * @private
393 */
394 goog.ui.GraphicsRoundedPanel.prototype.arcCenters_;
395
396
397 /**
398 * A 4-element array containing the start coordinates for rendering the arcs
399 * in the bottom-left, top-left, top-right, and bottom-right corners,
400 * respectively.
401 * @type {Array<goog.math.Coordinate>}
402 * @private
403 */
404 goog.ui.GraphicsRoundedPanel.prototype.cornerStarts_;
405
406
407 /**
408 * A 4-element array containing the arc end angles for the bottom-left,
409 * top-left, top-right, and bottom-right corners, respectively.
410 * @type {Array<number>}
411 * @private
412 */
413 goog.ui.GraphicsRoundedPanel.prototype.endAngles_;
414
415
416 /**
417 * Graphics object for rendering the background.
418 * @type {goog.graphics.AbstractGraphics}
419 * @private
420 */
421 goog.ui.GraphicsRoundedPanel.prototype.graphics_;
422
423
424 /**
425 * A 4-element array containing the rounded corner radii for the bottom-left,
426 * top-left, top-right, and bottom-right corners, respectively.
427 * @type {Array<number>}
428 * @private
429 */
430 goog.ui.GraphicsRoundedPanel.prototype.radii_;
431
432
433 /**
434 * A 4-element array containing the arc start angles for the bottom-left,
435 * top-left, top-right, and bottom-right corners, respectively.
436 * @type {Array<number>}
437 * @private
438 */
439 goog.ui.GraphicsRoundedPanel.prototype.startAngles_;
440
441
442 /**
443 * Thickness constant used as an offset to help determine where to start
444 * rendering.
445 * @type {number}
446 * @private
447 */
448 goog.ui.GraphicsRoundedPanel.BORDER_WIDTH_FACTOR_ = 1 / 2;
449
450
451 /**
452 * This method performs all the necessary DOM manipulation to create the panel.
453 * Overrides {@link goog.ui.Component#decorateInternal}.
454 * @param {Element} element The element to decorate.
455 * @protected
456 * @override
457 */
458 goog.ui.GraphicsRoundedPanel.prototype.decorateInternal = function(element) {
459 goog.ui.GraphicsRoundedPanel.superClass_.decorateInternal.call(this, element);
460
461 // Calculate the points and angles for creating the rounded corners. Then
462 // instantiate a Graphics object for drawing purposes.
463 var elementSize = goog.style.getSize(this.getElement());
464 this.calculateArcParameters_(elementSize);
465 this.graphics_ = goog.graphics.createGraphics(
466 /** @type {number} */ (elementSize.width),
467 /** @type {number} */ (elementSize.height),
468 /** @type {number} */ (elementSize.width),
469 /** @type {number} */ (elementSize.height), this.getDomHelper());
470 this.graphics_.createDom();
471
472 // Create the path, starting from the bottom-right corner, moving clockwise.
473 // End with the top-right corner.
474 var path = new goog.graphics.Path();
475 for (var i = 0; i < 4; i++) {
476 if (this.radii_[i]) {
477 // If radius > 0, draw an arc, moving to the first point and drawing
478 // a line to the others.
479 var cx = this.arcCenters_[i].x;
480 var cy = this.arcCenters_[i].y;
481 var rx = this.radii_[i];
482 var ry = rx;
483 var fromAngle = this.startAngles_[i];
484 var extent = this.endAngles_[i] - fromAngle;
485 var startX = cx + goog.math.angleDx(fromAngle, rx);
486 var startY = cy + goog.math.angleDy(fromAngle, ry);
487 if (i > 0) {
488 var currentPoint = path.getCurrentPoint();
489 if (!currentPoint || startX != currentPoint[0] ||
490 startY != currentPoint[1]) {
491 path.lineTo(startX, startY);
492 }
493 } else {
494 path.moveTo(startX, startY);
495 }
496 path.arcTo(rx, ry, fromAngle, extent);
497 } else if (i == 0) {
498 // If we're just starting out (ie. i == 0), move to the starting point.
499 path.moveTo(this.cornerStarts_[i].x, this.cornerStarts_[i].y);
500 } else {
501 // Otherwise, draw a line to the starting point.
502 path.lineTo(this.cornerStarts_[i].x, this.cornerStarts_[i].y);
503 }
504 }
505
506 // Close the path, create a stroke object, and fill the enclosed area, if
507 // needed. Then render the path.
508 path.close();
509 var stroke = this.borderWidth_ ?
510 new goog.graphics.Stroke(this.borderWidth_, this.borderColor_) :
511 null;
512 var fill = this.backgroundColor_ ?
513 new goog.graphics.SolidFill(this.backgroundColor_, 1) :
514 null;
515 this.graphics_.drawPath(path, stroke, fill);
516 this.graphics_.render(this.backgroundElement_);
517 };
518
519
520 /** @override */
521 goog.ui.GraphicsRoundedPanel.prototype.disposeInternal = function() {
522 goog.ui.GraphicsRoundedPanel.superClass_.disposeInternal.call(this);
523 this.graphics_.dispose();
524 delete this.graphics_;
525 delete this.radii_;
526 delete this.cornerStarts_;
527 delete this.arcCenters_;
528 delete this.startAngles_;
529 delete this.endAngles_;
530 };
531
532
533 /**
534 * Calculates the start coordinates, circle centers, and angles, for the rounded
535 * corners at each corner of the panel.
536 * @param {goog.math.Size} elementSize The size of element_.
537 * @private
538 */
539 goog.ui.GraphicsRoundedPanel.prototype.calculateArcParameters_ = function(
540 elementSize) {
541 // Initialize the arrays containing the key points and angles.
542 this.radii_ = [];
543 this.cornerStarts_ = [];
544 this.arcCenters_ = [];
545 this.startAngles_ = [];
546 this.endAngles_ = [];
547
548 // Set the start points, circle centers, and angles for the bottom-right,
549 // bottom-left, top-left and top-right corners, in that order.
550 var angleInterval = 90;
551 var borderWidthOffset =
552 this.borderWidth_ * goog.ui.GraphicsRoundedPanel.BORDER_WIDTH_FACTOR_;
553 var radius, xStart, yStart, xCenter, yCenter, startAngle, endAngle;
554 for (var i = 0; i < 4; i++) {
555 var corner = Math.pow(2, i); // Determines which corner we're dealing with.
556 var isLeft = corner & goog.ui.RoundedPanel.Corner.LEFT;
557 var isTop = corner & goog.ui.RoundedPanel.Corner.TOP;
558
559 // Calculate the radius and the start coordinates.
560 radius = corner & this.corners_ ? this.radius_ : 0;
561 switch (corner) {
562 case goog.ui.RoundedPanel.Corner.BOTTOM_LEFT:
563 xStart = borderWidthOffset + radius;
564 yStart = elementSize.height - borderWidthOffset;
565 break;
566 case goog.ui.RoundedPanel.Corner.TOP_LEFT:
567 xStart = borderWidthOffset;
568 yStart = radius + borderWidthOffset;
569 break;
570 case goog.ui.RoundedPanel.Corner.TOP_RIGHT:
571 xStart = elementSize.width - radius - borderWidthOffset;
572 yStart = borderWidthOffset;
573 break;
574 case goog.ui.RoundedPanel.Corner.BOTTOM_RIGHT:
575 xStart = elementSize.width - borderWidthOffset;
576 yStart = elementSize.height - radius - borderWidthOffset;
577 break;
578 }
579
580 // Calculate the circle centers and start/end angles.
581 xCenter = isLeft ? radius + borderWidthOffset :
582 elementSize.width - radius - borderWidthOffset;
583 yCenter = isTop ? radius + borderWidthOffset :
584 elementSize.height - radius - borderWidthOffset;
585 startAngle = angleInterval * i;
586 endAngle = startAngle + angleInterval;
587
588 // Append the radius, angles, and coordinates to their arrays.
589 this.radii_[i] = radius;
590 this.cornerStarts_[i] = new goog.math.Coordinate(xStart, yStart);
591 this.arcCenters_[i] = new goog.math.Coordinate(xCenter, yCenter);
592 this.startAngles_[i] = startAngle;
593 this.endAngles_[i] = endAngle;
594 }
595 };