github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/server/camlistored/ui/nav.js (about)

     1  /*
     2  Copyright 2013 The Camlistore Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  goog.provide('cam.Nav');
    18  goog.provide('cam.Nav.Item');
    19  goog.provide('cam.Nav.LinkItem');
    20  goog.provide('cam.Nav.SearchItem');
    21  
    22  goog.require('goog.dom');
    23  goog.require('goog.dom.classes');
    24  goog.require('goog.events.KeyCodes');
    25  goog.require('goog.ui.Container');
    26  goog.require('goog.ui.Component');
    27  goog.require('goog.ui.Control');
    28  goog.require('goog.ui.Button');
    29  
    30  goog.require('cam.style');
    31  goog.require('cam.Navigator');
    32  
    33  // A vertical, fixed-position expandy collapsy navigation bar thingy.
    34  cam.Nav = function(domHelper, opt_delegate) {
    35  	goog.base(this, null, null, domHelper);
    36  
    37  	this.delegate_ = opt_delegate;
    38  	this.expandTimer_ = 0;
    39  };
    40  goog.inherits(cam.Nav, goog.ui.Container);
    41  
    42  cam.Nav.prototype.createDom = function() {
    43  	this.setElementInternal(this.dom_.createDom('div'));
    44  	goog.dom.classes.add(this.element_, 'cam-nav');
    45  
    46  	this.closeButton_ = this.dom_.createDom('img', 'cam-nav-close');
    47  	this.closeButton_.src = 'close.svg';
    48  	this.getElement().appendChild(this.closeButton_);
    49  
    50  	this.close();
    51  };
    52  
    53  cam.Nav.prototype.enterDocument = function() {
    54  	goog.base(this, 'enterDocument');
    55  
    56  	this.getHandler().listen(this.getElement(), 'mouseover', this.handleMouseOver_);
    57  	this.getHandler().listen(this.getElement(), 'mouseout', this.handleMouseOut_);
    58  
    59  	this.getHandler().listen(this.closeButton_, 'click', function(e) {
    60  		e.stopPropagation();
    61  		this.close();
    62  	}.bind(this));
    63  
    64  	this.getHandler().listen(this.getElement(), 'keyup', function(e) {
    65  		if (e.keyCode == goog.events.KeyCodes.ESC) {
    66  			this.close();
    67  			e.preventDefault();
    68  		}
    69  	});
    70  };
    71  
    72  cam.Nav.prototype.open = function() {
    73  	if (this.delegate_) {
    74  		this.delegate_.onNavOpen();
    75  	}
    76  	goog.dom.classes.remove(this.getElement(), 'cam-nav-collapsed');
    77  };
    78  
    79  cam.Nav.prototype.close = function() {
    80  	if (this.delegate_) {
    81  		this.delegate_.onNavClose();
    82  	}
    83  
    84  	goog.dom.classes.add(this.getElement(), 'cam-nav-collapsed');
    85  };
    86  
    87  cam.Nav.prototype.isOpen = function() {
    88  	return !goog.dom.classes.has(this.getElement(), 'cam-nav-collapsed');
    89  };
    90  
    91  cam.Nav.prototype.toggle = function() {
    92  	if (this.isOpen()) {
    93  		this.close();
    94  		return false;
    95  	} else {
    96  		this.open();
    97  		return true;
    98  	}
    99  };
   100  
   101  cam.Nav.prototype.handleMouseOver_ = function() {
   102  	this.expandTimer_ = window.setTimeout(function() {
   103  		this.expandTimer_ = 0;
   104  		this.open();
   105  	}.bind(this), 250);
   106  };
   107  
   108  cam.Nav.prototype.handleMouseOut_ = function() {
   109  	if (this.expandTimer_) {
   110  		window.clearTimeout(this.expandTimer_);
   111  		this.expandTimer_ = 0;
   112  	}
   113  };
   114  
   115  
   116  cam.Nav.Item = function(domHelper, iconSrc, content) {
   117  	goog.base(this, content, null, domHelper);
   118  	this.iconSrc_ = iconSrc;
   119  	this.addClassName('cam-nav-item');
   120  };
   121  goog.inherits(cam.Nav.Item, goog.ui.Button);
   122  
   123  cam.Nav.Item.prototype.onClick = function() {};
   124  
   125  cam.Nav.Item.prototype.createDom = function() {
   126  	goog.base(this, 'createDom');
   127  	this.setIcon(this.iconSrc_);
   128  };
   129  
   130  cam.Nav.Item.prototype.enterDocument = function() {
   131  	this.getHandler().listen(this.getElement(), 'click', function(e) {
   132  		this.onClick();
   133  		e.stopPropagation();
   134  	});
   135  };
   136  
   137  cam.Nav.Item.prototype.setIcon = function(src) {
   138  	this.iconSrc_ = src;
   139  	if (this.element_) {
   140  		this.element_.style.backgroundImage = cam.style.getURLValue(src);
   141  	}
   142  };
   143  
   144  
   145  cam.Nav.SearchItem = function(domHelper, iconSrc, label) {
   146  	goog.base(this, domHelper, iconSrc, label);
   147  	this.setAllowTextSelection(true);
   148  	this.addClassName('cam-nav-searchitem');
   149  };
   150  goog.inherits(cam.Nav.SearchItem, cam.Nav.Item);
   151  
   152  cam.Nav.SearchItem.prototype.onSearch = function(value) {};
   153  
   154  cam.Nav.SearchItem.prototype.setText = function(text) {
   155  	if (this.input_) {
   156  		this.input_.value = text;
   157  	}
   158  };
   159  
   160  cam.Nav.SearchItem.prototype.focus = function() {
   161  	this.input_.focus();
   162  };
   163  
   164  cam.Nav.SearchItem.prototype.blur = function() {
   165  	this.input_.blur();
   166  };
   167  
   168  cam.Nav.SearchItem.prototype.createDom = function() {
   169  	this.setElementInternal(this.dom_.createDom('div', this.getExtraClassNames()));
   170  	this.form_ = this.dom_.createDom('form');
   171  	this.input_ = this.dom_.createDom('input', {'placeholder': this.getContent()});
   172  	this.form_.appendChild(this.input_);
   173  	this.getElement().appendChild(this.form_);
   174  	this.setIcon(this.iconSrc_);
   175  };
   176  
   177  cam.Nav.SearchItem.prototype.enterDocument = function() {
   178  	goog.base(this, 'enterDocument');
   179  
   180  	this.getHandler().listen(this.input_, 'mouseover', this.input_.focus.bind(this.input_));
   181  
   182  	this.getHandler().listen(this.getElement(), 'click', function(e) {
   183  		this.input_.focus();
   184  		e.stopPropagation();
   185  	}.bind(this));
   186  
   187  	this.getHandler().listen(this.form_, 'submit', function(e) {
   188  		this.onSearch(this.input_.value);
   189  		e.preventDefault();
   190  	});
   191  };
   192  
   193  
   194  cam.Nav.LinkItem = function(domHelper, iconSrc, label, linkUrl) {
   195  	goog.base(this, domHelper, iconSrc, label);
   196  	this.linkUrl_ = linkUrl;
   197  	this.addClassName('cam-nav-linkitem');
   198  };
   199  goog.inherits(cam.Nav.LinkItem, cam.Nav.Item);
   200  
   201  cam.Nav.LinkItem.prototype.onClick = function(url) {};
   202  
   203  cam.Nav.LinkItem.prototype.createDom = function() {
   204  	this.setElementInternal(this.dom_.createDom('a', this.getExtraClassNames(), this.getContent()));
   205  	this.getElement().href = this.linkUrl_;
   206  	this.setIcon(this.iconSrc_);
   207  };