1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * Base class to represent a "view". A view is an absolutely positioned box on 7 * the page. 8 * 9 * @constructor 10 */ 11function View() { 12 this.isVisible_ = true; 13} 14 15/** 16 * Called to reposition the view on the page. Measurements are in pixels. 17 */ 18View.prototype.setGeometry = function(left, top, width, height) { 19 this.left_ = left; 20 this.top_ = top; 21 this.width_ = width; 22 this.height_ = height; 23}; 24 25/** 26 * Called to show/hide the view. 27 */ 28View.prototype.show = function(isVisible) { 29 this.isVisible_ = isVisible; 30}; 31 32View.prototype.isVisible = function() { 33 return this.isVisible_; 34}; 35 36/** 37 * Method of the observer class. 38 * 39 * Called to check if an observer needs the data it is 40 * observing to be actively updated. 41 */ 42View.prototype.isActive = function() { 43 return this.isVisible(); 44}; 45 46View.prototype.getLeft = function() { 47 return this.left_; 48}; 49 50View.prototype.getTop = function() { 51 return this.top_; 52}; 53 54View.prototype.getWidth = function() { 55 return this.width_; 56}; 57 58View.prototype.getHeight = function() { 59 return this.height_; 60}; 61 62View.prototype.getRight = function() { 63 return this.getLeft() + this.getWidth(); 64}; 65 66View.prototype.getBottom = function() { 67 return this.getTop() + this.getHeight(); 68}; 69 70View.prototype.setParameters = function(params) {}; 71 72//----------------------------------------------------------------------------- 73 74/** 75 * DivView is an implementation of View that wraps a DIV. 76 * 77 * @constructor 78 */ 79function DivView(divId) { 80 View.call(this); 81 82 this.node_ = document.getElementById(divId); 83 if (!this.node_) 84 throw new Error('Element ' + divId + ' not found'); 85 86 // Initialize the default values to those of the DIV. 87 this.width_ = this.node_.offsetWidth; 88 this.height_ = this.node_.offsetHeight; 89 this.isVisible_ = this.node_.style.display != 'none'; 90} 91 92inherits(DivView, View); 93 94DivView.prototype.setGeometry = function(left, top, width, height) { 95 DivView.superClass_.setGeometry.call(this, left, top, width, height); 96 97 this.node_.style.position = 'absolute'; 98 setNodePosition(this.node_, left, top, width, height); 99}; 100 101DivView.prototype.show = function(isVisible) { 102 DivView.superClass_.show.call(this, isVisible); 103 setNodeDisplay(this.node_, isVisible); 104}; 105 106/** 107 * Returns the wrapped DIV 108 */ 109DivView.prototype.getNode = function() { 110 return this.node_; 111}; 112 113//----------------------------------------------------------------------------- 114 115/** 116 * Implementation of View that sizes its child to fit the entire window. 117 * 118 * @param {!View} childView 119 * 120 * @constructor 121 */ 122function WindowView(childView) { 123 View.call(this); 124 this.childView_ = childView; 125 window.addEventListener('resize', this.resetGeometry.bind(this), true); 126} 127 128inherits(WindowView, View); 129 130WindowView.prototype.setGeometry = function(left, top, width, height) { 131 WindowView.superClass_.setGeometry.call(this, left, top, width, height); 132 this.childView_.setGeometry(left, top, width, height); 133}; 134 135WindowView.prototype.show = function() { 136 WindowView.superClass_.show.call(this, isVisible); 137 this.childView_.show(isVisible); 138}; 139 140WindowView.prototype.resetGeometry = function() { 141 this.setGeometry(0, 0, window.innerWidth, window.innerHeight); 142}; 143 144