Path: blob/trunk/third_party/closure/goog/labs/style/pixeldensitymonitor.js
2868 views
// Copyright 2013 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 Utility class that monitors pixel density ratio changes.16*17* @see ../demos/pixeldensitymonitor.html18*/1920goog.provide('goog.labs.style.PixelDensityMonitor');21goog.provide('goog.labs.style.PixelDensityMonitor.Density');22goog.provide('goog.labs.style.PixelDensityMonitor.EventType');2324goog.require('goog.events');25goog.require('goog.events.EventTarget');26272829/**30* Monitors the window for changes to the ratio between device and screen31* pixels, e.g. when the user moves the window from a high density screen to a32* screen with normal density. Dispatches33* goog.labs.style.PixelDensityMonitor.EventType.CHANGE events when the density34* changes between the two predefined values NORMAL and HIGH.35*36* This class uses the window.devicePixelRatio value which is supported in37* WebKit and FF18. If the value does not exist, it will always return a38* NORMAL density. It requires support for MediaQueryList to detect changes to39* the devicePixelRatio.40*41* @param {!goog.dom.DomHelper=} opt_domHelper The DomHelper which contains the42* document associated with the window to listen to. Defaults to the one in43* which this code is executing.44* @constructor45* @extends {goog.events.EventTarget}46* @final47*/48goog.labs.style.PixelDensityMonitor = function(opt_domHelper) {49goog.labs.style.PixelDensityMonitor.base(this, 'constructor');5051/**52* @type {Window}53* @private54*/55this.window_ = opt_domHelper ? opt_domHelper.getWindow() : window;5657/**58* The last density that was reported so that changes can be detected.59* @type {goog.labs.style.PixelDensityMonitor.Density}60* @private61*/62this.lastDensity_ = this.getDensity();6364/**65* @type {function (MediaQueryList)}66* @private67*/68this.listener_ = goog.bind(this.handleMediaQueryChange_, this);6970/**71* The media query list for a query that detects high density, if supported72* by the browser. Because matchMedia returns a new object for every call, it73* needs to be saved here so the listener can be removed when disposing.74* @type {?MediaQueryList}75* @private76*/77this.mediaQueryList_ = this.window_.matchMedia ?78this.window_.matchMedia(79goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_QUERY_) :80null;81};82goog.inherits(goog.labs.style.PixelDensityMonitor, goog.events.EventTarget);838485/**86* The two different pixel density modes on which the various ratios between87* physical and device pixels are mapped.88* @enum {number}89*/90goog.labs.style.PixelDensityMonitor.Density = {91/**92* Mode for older portable devices and desktop screens, defined as having a93* device pixel ratio of less than 1.5.94*/95NORMAL: 1,9697/**98* Mode for newer portable devices with a high resolution screen, defined as99* having a device pixel ratio of more than 1.5.100*/101HIGH: 2102};103104105/**106* The events fired by the PixelDensityMonitor.107* @enum {string}108*/109goog.labs.style.PixelDensityMonitor.EventType = {110/**111* Dispatched when density changes between NORMAL and HIGH.112*/113CHANGE: goog.events.getUniqueId('change')114};115116117/**118* Minimum ratio between device and screen pixel needed for high density mode.119* @type {number}120* @private121*/122goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_RATIO_ = 1.5;123124125/**126* Media query that matches for high density.127* @type {string}128* @private129*/130goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_QUERY_ =131'(min-resolution: 1.5dppx), (-webkit-min-device-pixel-ratio: 1.5)';132133134/**135* Starts monitoring for changes in pixel density.136*/137goog.labs.style.PixelDensityMonitor.prototype.start = function() {138if (this.mediaQueryList_) {139this.mediaQueryList_.addListener(this.listener_);140}141};142143144/**145* @return {goog.labs.style.PixelDensityMonitor.Density} The density for the146* window.147*/148goog.labs.style.PixelDensityMonitor.prototype.getDensity = function() {149if (this.window_.devicePixelRatio >=150goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_RATIO_) {151return goog.labs.style.PixelDensityMonitor.Density.HIGH;152} else {153return goog.labs.style.PixelDensityMonitor.Density.NORMAL;154}155};156157158/**159* Handles a change to the media query and checks whether the density has160* changed since the last call.161* @param {MediaQueryList} mql The list of changed media queries.162* @private163*/164goog.labs.style.PixelDensityMonitor.prototype.handleMediaQueryChange_ =165function(mql) {166var newDensity = this.getDensity();167if (this.lastDensity_ != newDensity) {168this.lastDensity_ = newDensity;169this.dispatchEvent(goog.labs.style.PixelDensityMonitor.EventType.CHANGE);170}171};172173174/** @override */175goog.labs.style.PixelDensityMonitor.prototype.disposeInternal = function() {176if (this.mediaQueryList_) {177this.mediaQueryList_.removeListener(this.listener_);178}179goog.labs.style.PixelDensityMonitor.base(this, 'disposeInternal');180};181182183