Path: blob/trunk/third_party/closure/goog/graphics/ext/coordinates.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview Graphics utility functions for advanced coordinates.17*18* This file assists the use of advanced coordinates in goog.graphics. Coords19* can be specified as simple numbers which will correspond to units in the20* graphics element's coordinate space. Alternately, coords can be expressed21* in pixels, meaning no matter what tranformations or coordinate system changes22* are present, the number of pixel changes will remain constant. Coords can23* also be expressed as percentages of their parent's size.24*25* This file also allows for elements to have margins, expressable in any of26* the ways described above.27*28* Additional pieces of advanced coordinate functionality can (soon) be found in29* element.js and groupelement.js.30*31* @author [email protected] (Robby Walker)32*/3334goog.provide('goog.graphics.ext.coordinates');3536goog.require('goog.string');373839/**40* Cache of boolean values. For a given string (key), is it special? (value)41* @type {Object}42* @private43*/44goog.graphics.ext.coordinates.specialCoordinateCache_ = {};454647/**48* Determines if the given coordinate is a percent based coordinate or an49* expression with a percent based component.50* @param {string} coord The coordinate to test.51* @return {boolean} Whether the coordinate contains the string '%'.52* @private53*/54goog.graphics.ext.coordinates.isPercent_ = function(coord) {55return goog.string.contains(coord, '%');56};575859/**60* Determines if the given coordinate is a pixel based coordinate or an61* expression with a pixel based component.62* @param {string} coord The coordinate to test.63* @return {boolean} Whether the coordinate contains the string 'px'.64* @private65*/66goog.graphics.ext.coordinates.isPixels_ = function(coord) {67return goog.string.contains(coord, 'px');68};697071/**72* Determines if the given coordinate is special - i.e. not just a number.73* @param {string|number|null} coord The coordinate to test.74* @return {boolean} Whether the coordinate is special.75*/76goog.graphics.ext.coordinates.isSpecial = function(coord) {77var cache = goog.graphics.ext.coordinates.specialCoordinateCache_;7879if (!(coord in cache)) {80cache[coord] = goog.isString(coord) &&81(goog.graphics.ext.coordinates.isPercent_(coord) ||82goog.graphics.ext.coordinates.isPixels_(coord));83}8485return cache[coord];86};878889/**90* Returns the value of the given expression in the given context.91*92* Should be treated as package scope.93*94* @param {string|number} coord The coordinate to convert.95* @param {number} size The size of the parent element.96* @param {number} scale The ratio of pixels to units.97* @return {number} The number of coordinate space units that corresponds to98* this coordinate.99*/100goog.graphics.ext.coordinates.computeValue = function(coord, size, scale) {101var number = parseFloat(String(coord));102if (goog.isString(coord)) {103if (goog.graphics.ext.coordinates.isPercent_(coord)) {104return number * size / 100;105} else if (goog.graphics.ext.coordinates.isPixels_(coord)) {106return number / scale;107}108}109110return number;111};112113114/**115* Converts the given coordinate to a number value in units.116*117* Should be treated as package scope.118*119* @param {string|number} coord The coordinate to retrieve the value for.120* @param {boolean|undefined} forMaximum Whether we are computing the largest121* value this coordinate would be in a parent of no size. The container122* size in this case should be set to the size of the current element.123* @param {number} containerSize The unit value of the size of the container of124* this element. Should be set to the minimum width of this element if125* forMaximum is true.126* @param {number} scale The ratio of pixels to units.127* @param {Object=} opt_cache Optional (but highly recommend) object to store128* cached computations in. The calling class should manage clearing out129* the cache when the scale or containerSize changes.130* @return {number} The correct number of coordinate space units.131*/132goog.graphics.ext.coordinates.getValue = function(133coord, forMaximum, containerSize, scale, opt_cache) {134if (!goog.isNumber(coord)) {135var cacheString = opt_cache && ((forMaximum ? 'X' : '') + coord);136137if (opt_cache && cacheString in opt_cache) {138coord = opt_cache[cacheString];139} else {140if (goog.graphics.ext.coordinates.isSpecial(141/** @type {string} */ (coord))) {142coord = goog.graphics.ext.coordinates.computeValue(143coord, containerSize, scale);144} else {145// Simple coordinates just need to be converted from a string to a146// number.147coord = parseFloat(/** @type {string} */ (coord));148}149150// Cache the result.151if (opt_cache) {152opt_cache[cacheString] = coord;153}154}155}156157return coord;158};159160161