forked from mirrors/gecko-dev
		
	
		
			
				
	
	
		
			105 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/* This Source Code Form is subject to the terms of the Mozilla Public
 | 
						|
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 | 
						|
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
						|
 | 
						|
/* global loop:true */
 | 
						|
 | 
						|
var loop = loop || {};
 | 
						|
loop.shared = loop.shared || {};
 | 
						|
loop.shared.mixins = (function() {
 | 
						|
  "use strict";
 | 
						|
 | 
						|
  /**
 | 
						|
   * Root object, by default set to window.
 | 
						|
   * @type {DOMWindow|Object}
 | 
						|
   */
 | 
						|
  var rootObject = window;
 | 
						|
 | 
						|
  /**
 | 
						|
   * Sets a new root object. This is useful for testing native DOM events so we
 | 
						|
   * can fake them.
 | 
						|
   *
 | 
						|
   * @param {Object}
 | 
						|
   */
 | 
						|
  function setRootObject(obj) {
 | 
						|
    console.info("loop.shared.mixins: rootObject set to " + obj);
 | 
						|
    rootObject = obj;
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Dropdown menu mixin.
 | 
						|
   * @type {Object}
 | 
						|
   */
 | 
						|
  var DropdownMenuMixin = {
 | 
						|
    get documentBody() {
 | 
						|
      return rootObject.document.body;
 | 
						|
    },
 | 
						|
 | 
						|
    getInitialState: function() {
 | 
						|
      return {showMenu: false};
 | 
						|
    },
 | 
						|
 | 
						|
    _onBodyClick: function() {
 | 
						|
      this.setState({showMenu: false});
 | 
						|
    },
 | 
						|
 | 
						|
    componentDidMount: function() {
 | 
						|
      this.documentBody.addEventListener("click", this._onBodyClick);
 | 
						|
      this.documentBody.addEventListener("blur", this.hideDropdownMenu);
 | 
						|
    },
 | 
						|
 | 
						|
    componentWillUnmount: function() {
 | 
						|
      this.documentBody.removeEventListener("click", this._onBodyClick);
 | 
						|
      this.documentBody.removeEventListener("blur", this.hideDropdownMenu);
 | 
						|
    },
 | 
						|
 | 
						|
    showDropdownMenu: function() {
 | 
						|
      this.setState({showMenu: true});
 | 
						|
    },
 | 
						|
 | 
						|
    hideDropdownMenu: function() {
 | 
						|
      this.setState({showMenu: false});
 | 
						|
    },
 | 
						|
 | 
						|
    toggleDropdownMenu: function() {
 | 
						|
      this.setState({showMenu: !this.state.showMenu});
 | 
						|
    },
 | 
						|
  };
 | 
						|
 | 
						|
  /**
 | 
						|
   * Document visibility mixin. Allows defining the following hooks for when the
 | 
						|
   * document visibility status changes:
 | 
						|
   *
 | 
						|
   * - {Function} onDocumentVisible For when the document becomes visible.
 | 
						|
   * - {Function} onDocumentHidden  For when the document becomes hidden.
 | 
						|
   *
 | 
						|
   * @type {Object}
 | 
						|
   */
 | 
						|
  var DocumentVisibilityMixin = {
 | 
						|
    _onDocumentVisibilityChanged: function(event) {
 | 
						|
      var hidden = event.target.hidden;
 | 
						|
      if (hidden && typeof this.onDocumentHidden === "function") {
 | 
						|
        this.onDocumentHidden();
 | 
						|
      }
 | 
						|
      if (!hidden && typeof this.onDocumentVisible === "function") {
 | 
						|
        this.onDocumentVisible();
 | 
						|
      }
 | 
						|
    },
 | 
						|
 | 
						|
    componentDidMount: function() {
 | 
						|
      rootObject.document.addEventListener(
 | 
						|
        "visibilitychange", this._onDocumentVisibilityChanged);
 | 
						|
    },
 | 
						|
 | 
						|
    componentWillUnmount: function() {
 | 
						|
      rootObject.document.removeEventListener(
 | 
						|
        "visibilitychange", this._onDocumentVisibilityChanged);
 | 
						|
    }
 | 
						|
  };
 | 
						|
 | 
						|
  return {
 | 
						|
    setRootObject: setRootObject,
 | 
						|
    DropdownMenuMixin: DropdownMenuMixin,
 | 
						|
    DocumentVisibilityMixin: DocumentVisibilityMixin
 | 
						|
  };
 | 
						|
})();
 |