Path: blob/trunk/third_party/closure/goog/dom/fontsizemonitor.js
2868 views
// Copyright 2005 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.1314/**15* @fileoverview A class that can be used to listen to font size changes.16* @author [email protected] (Erik Arvidsson)17*/1819goog.provide('goog.dom.FontSizeMonitor');20goog.provide('goog.dom.FontSizeMonitor.EventType');2122goog.require('goog.dom');23goog.require('goog.dom.TagName');24goog.require('goog.events');25goog.require('goog.events.EventTarget');26goog.require('goog.events.EventType');27goog.require('goog.userAgent');282930// TODO(arv): Move this to goog.events instead.31323334/**35* This class can be used to monitor changes in font size. Instances will36* dispatch a {@code goog.dom.FontSizeMonitor.EventType.CHANGE} event.37* Example usage:38* <pre>39* var fms = new goog.dom.FontSizeMonitor();40* goog.events.listen(fms, goog.dom.FontSizeMonitor.EventType.CHANGE,41* function(e) {42* alert('Font size was changed');43* });44* </pre>45* @param {goog.dom.DomHelper=} opt_domHelper DOM helper object that is used to46* determine where to insert the DOM nodes used to determine when the font47* size changes.48* @constructor49* @extends {goog.events.EventTarget}50* @final51*/52goog.dom.FontSizeMonitor = function(opt_domHelper) {53goog.events.EventTarget.call(this);5455var dom = opt_domHelper || goog.dom.getDomHelper();5657/**58* Offscreen iframe which we use to detect resize events.59* @type {HTMLElement}60* @private61*/62this.sizeElement_ = /** @type {!HTMLElement} */ (63dom.createDom(64// The size of the iframe is expressed in em, which are font size65// relative66// which will cause the iframe to be resized when the font size67// changes.68// The actual values are not relevant as long as we can ensure that69// the70// iframe has a non zero size and is completely off screen.71goog.userAgent.IE ? goog.dom.TagName.DIV : goog.dom.TagName.IFRAME, {72'style': 'position:absolute;width:9em;height:9em;top:-99em',73'tabIndex': -1,74'aria-hidden': 'true'75}));76var p = dom.getDocument().body;77p.insertBefore(this.sizeElement_, p.firstChild);7879/**80* The object that we listen to resize events on.81* @type {Element|Window}82* @private83*/84var resizeTarget = this.resizeTarget_ = goog.userAgent.IE ?85this.sizeElement_ :86goog.dom.getFrameContentWindow(87/** @type {HTMLIFrameElement} */ (this.sizeElement_));8889// We need to open and close the document to get Firefox 2 to work. We must90// not do this for IE in case we are using HTTPS since accessing the document91// on an about:blank iframe in IE using HTTPS raises a Permission Denied92// error.93if (goog.userAgent.GECKO) {94var doc = resizeTarget.document;95doc.open();96doc.close();97}9899// Listen to resize event on the window inside the iframe.100goog.events.listen(101resizeTarget, goog.events.EventType.RESIZE, this.handleResize_, false,102this);103104/**105* Last measured width of the iframe element.106* @type {number}107* @private108*/109this.lastWidth_ = this.sizeElement_.offsetWidth;110};111goog.inherits(goog.dom.FontSizeMonitor, goog.events.EventTarget);112113114/**115* The event types that the FontSizeMonitor fires.116* @enum {string}117*/118goog.dom.FontSizeMonitor.EventType = {119// TODO(arv): Change value to 'change' after updating the callers.120CHANGE: 'fontsizechange'121};122123124/**125* Constant for the change event.126* @type {string}127* @deprecated Use {@code goog.dom.FontSizeMonitor.EventType.CHANGE} instead.128*/129goog.dom.FontSizeMonitor.CHANGE_EVENT =130goog.dom.FontSizeMonitor.EventType.CHANGE;131132133/** @override */134goog.dom.FontSizeMonitor.prototype.disposeInternal = function() {135goog.dom.FontSizeMonitor.superClass_.disposeInternal.call(this);136137goog.events.unlisten(138this.resizeTarget_, goog.events.EventType.RESIZE, this.handleResize_,139false, this);140this.resizeTarget_ = null;141142// Firefox 2 crashes if the iframe is removed during the unload phase.143if (!goog.userAgent.GECKO || goog.userAgent.isVersionOrHigher('1.9')) {144goog.dom.removeNode(this.sizeElement_);145}146delete this.sizeElement_;147};148149150/**151* Handles the onresize event of the iframe and dispatches a change event in152* case its size really changed.153* @param {goog.events.BrowserEvent} e The event object.154* @private155*/156goog.dom.FontSizeMonitor.prototype.handleResize_ = function(e) {157// Only dispatch the event if the size really changed. Some newer browsers do158// not really change the font-size, instead they zoom the whole page. This159// does trigger window resize events on the iframe but the logical pixel size160// remains the same (the device pixel size changes but that is irrelevant).161var currentWidth = this.sizeElement_.offsetWidth;162if (this.lastWidth_ != currentWidth) {163this.lastWidth_ = currentWidth;164this.dispatchEvent(goog.dom.FontSizeMonitor.EventType.CHANGE);165}166};167168169