The code/**
* This class can be used to monitor changes in font-size. Instances will
* dispatch a "fontsizechange" event.
* Example usage:
* <pre>
* var fms = new goog.dom.FontSizeMonitor();
* goog.events.listen(fms, goog.dom.FontSizeMonitor.CHANGE_EVENT, function(e) {
* alert('Font-size was changed');
* });
* </pre>
* @param {goog.Timer} opt_timer Optional timer object that can be used instead
* of an interval. The monitor will listen on the TICK event.
* @constructor
* @extends goog.events.EventTarget
*/
goog.dom.FontSizeMonitor = function(opt_timer) {
// Create a new element and position it off screen. Use this so we can
// guarantee its not going to have a fixed size.
this.sizeElement_ = goog.dom.createDom(
'div', { 'style': 'position:absolute;left:-1000px;top:-100px;' }, 'X');
goog.dom.appendChild(goog.dom.getDocument().body, this.sizeElement_);
this.lastSize_ = this.sizeElement_.offsetWidth;
if (opt_timer) {
this.timerListener_ = goog.events.listen(
opt_timer, 'tick', this.checkFontSize_, false, this);
} else {
this.interval_ = goog.global.setInterval(
goog.bind(this.checkFontSize_, this), 50);
}
};
goog.dom.FontSizeMonitor.inherits(goog.events.EventTarget);
/**
* Constant for the fontsizechange event
* @type String
*/
goog.dom.FontSizeMonitor.CHANGE_EVENT = 'fontsizechange';
/**
* The key for the listener if a timer is used
* @type String
*/
goog.dom.FontSizeMonitor.prototype.timerListener_ = null;
/**
* Identifier for the interval
* @type Number
*/
goog.dom.FontSizeMonitor.prototype.interval_ = null;
/**
* Dispose the fontsize monitor
*/
goog.dom.FontSizeMonitor.prototype.dispose = function() {
if (!this.getDisposed()) {
goog.dom.FontSizeMonitor.superClass_.dispose.call(this);
goog.dom.removeNode(this.sizeElement_);
if (this.timerListener_) {
goog.events.unlistenByKey(this.timerListener_);
}
if (this.interval_) {
window.clearInterval(this.interval_);
}
}
};
/**
* Callback used to check the fontsize, if it has changed this will dispatch
* a "fontsizechange" event.
* @private
*/
goog.dom.FontSizeMonitor.prototype.checkFontSize_ = function() {
var curSize = this.sizeElement_.offsetWidth;
if (this.lastSize_ != curSize) {
this.lastSize_ = curSize;
this.dispatchEvent(goog.dom.FontSizeMonitor.CHANGE_EVENT);
}
};The code walkthroughTODO walkthrough Further readingTODO further reading
|