2011-11-10 02:09:18 -08:00
|
|
|
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Tilt: A WebGL-based 3D visualization of a webpage.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Victor Porof <vporof@mozilla.com> (original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the LGPL or the GPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
***** END LICENSE BLOCK *****/
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
// Tilt notifications dispatched through the nsIObserverService.
|
|
|
|
const TILT_NOTIFICATIONS = {
|
|
|
|
|
2012-01-19 07:48:22 -08:00
|
|
|
// Fires when Tilt starts the initialization.
|
|
|
|
INITIALIZING: "tilt-initializing",
|
|
|
|
|
|
|
|
// Fires immediately after initialization is complete.
|
|
|
|
// (when the canvas overlay is visible and the 3D mesh is completely created)
|
2011-11-10 02:09:18 -08:00
|
|
|
INITIALIZED: "tilt-initialized",
|
|
|
|
|
2012-01-19 07:48:22 -08:00
|
|
|
// Fires immediately before the destruction is started.
|
|
|
|
DESTROYING: "tilt-destroying",
|
|
|
|
|
|
|
|
// Fires immediately before the destruction is finished.
|
|
|
|
// (just before the canvas overlay is removed from its parent node)
|
|
|
|
BEFORE_DESTROYED: "tilt-before-destroyed",
|
|
|
|
|
|
|
|
// Fires when Tilt is completely destroyed.
|
2011-11-10 02:09:18 -08:00
|
|
|
DESTROYED: "tilt-destroyed",
|
|
|
|
|
|
|
|
// Fires when Tilt is shown (after a tab-switch).
|
|
|
|
SHOWN: "tilt-shown",
|
|
|
|
|
|
|
|
// Fires when Tilt is hidden (after a tab-switch).
|
2012-01-19 07:48:22 -08:00
|
|
|
HIDDEN: "tilt-hidden",
|
|
|
|
|
|
|
|
// Fires once Tilt highlights an element in the page.
|
|
|
|
HIGHLIGHTING: "tilt-highlighting",
|
|
|
|
|
|
|
|
// Fires once Tilt stops highlighting any element.
|
|
|
|
UNHIGHLIGHTING: "tilt-unhighlighting",
|
|
|
|
|
|
|
|
// Fires when a node is removed from the 3D mesh.
|
|
|
|
NODE_REMOVED: "tilt-node-removed"
|
2011-11-10 02:09:18 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource:///modules/devtools/TiltGL.jsm");
|
|
|
|
Cu.import("resource:///modules/devtools/TiltUtils.jsm");
|
|
|
|
Cu.import("resource:///modules/devtools/TiltVisualizer.jsm");
|
|
|
|
|
|
|
|
let EXPORTED_SYMBOLS = ["Tilt"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object managing instances of the visualizer.
|
|
|
|
*
|
|
|
|
* @param {Window} aWindow
|
|
|
|
* the chrome window used by each visualizer instance
|
|
|
|
*/
|
|
|
|
function Tilt(aWindow)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Save a reference to the top-level window.
|
|
|
|
*/
|
|
|
|
this.chromeWindow = aWindow;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All the instances of TiltVisualizer.
|
|
|
|
*/
|
|
|
|
this.visualizers = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for accessing notifications strings.
|
|
|
|
*/
|
|
|
|
this.NOTIFICATIONS = TILT_NOTIFICATIONS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tilt.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a visualizer for the current tab.
|
|
|
|
*/
|
|
|
|
initialize: function T_initialize()
|
|
|
|
{
|
|
|
|
let id = this.currentWindowId;
|
|
|
|
|
|
|
|
// if the visualizer for the current tab is already open, destroy it now
|
|
|
|
if (this.visualizers[id]) {
|
2012-01-15 23:00:56 -08:00
|
|
|
this.destroy(id, true);
|
2011-11-10 02:09:18 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a visualizer instance for the current tab
|
|
|
|
this.visualizers[id] = new TiltVisualizer({
|
2012-01-20 03:36:48 -08:00
|
|
|
chromeWindow: this.chromeWindow,
|
2011-11-10 02:09:18 -08:00
|
|
|
contentWindow: this.chromeWindow.gBrowser.selectedBrowser.contentWindow,
|
2012-01-20 03:36:48 -08:00
|
|
|
parentNode: this.chromeWindow.gBrowser.selectedBrowser.parentNode,
|
2012-01-19 07:48:22 -08:00
|
|
|
notifications: this.NOTIFICATIONS
|
2011-11-10 02:09:18 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
// make sure the visualizer object was initialized properly
|
|
|
|
if (!this.visualizers[id].isInitialized()) {
|
|
|
|
this.destroy(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:48:22 -08:00
|
|
|
Services.obs.notifyObservers(null, TILT_NOTIFICATIONS.INITIALIZING, null);
|
2011-11-10 02:09:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-02-07 01:44:55 -08:00
|
|
|
* Starts destroying a specific instance of the visualizer.
|
2011-11-10 02:09:18 -08:00
|
|
|
*
|
|
|
|
* @param {String} aId
|
|
|
|
* the identifier of the instance in the visualizers array
|
2012-01-15 23:00:56 -08:00
|
|
|
* @param {Boolean} aAnimateFlag
|
|
|
|
* optional, set to true to display a destruction transition
|
2011-11-10 02:09:18 -08:00
|
|
|
*/
|
2012-01-15 23:00:56 -08:00
|
|
|
destroy: function T_destroy(aId, aAnimateFlag)
|
2011-11-10 02:09:18 -08:00
|
|
|
{
|
2012-02-07 01:44:55 -08:00
|
|
|
// if the visualizer is destroyed or destroying, don't do anything
|
|
|
|
if (!this.visualizers[aId] || this._isDestroying) {
|
2011-11-10 02:09:18 -08:00
|
|
|
return;
|
|
|
|
}
|
2012-02-07 01:44:55 -08:00
|
|
|
this._isDestroying = true;
|
2011-11-10 02:09:18 -08:00
|
|
|
|
2012-02-07 01:44:55 -08:00
|
|
|
let controller = this.visualizers[aId].controller;
|
|
|
|
let presenter = this.visualizers[aId].presenter;
|
2012-01-15 23:00:56 -08:00
|
|
|
|
2012-02-07 01:44:55 -08:00
|
|
|
let content = presenter.contentWindow;
|
|
|
|
let pageXOffset = content.pageXOffset * presenter.transforms.zoom;
|
|
|
|
let pageYOffset = content.pageYOffset * presenter.transforms.zoom;
|
|
|
|
TiltUtils.setDocumentZoom(this.chromeWindow, presenter.transforms.zoom);
|
2012-01-15 23:00:56 -08:00
|
|
|
|
2012-02-07 01:44:55 -08:00
|
|
|
// if we're not doing any outro animation, just finish destruction directly
|
|
|
|
if (!aAnimateFlag) {
|
|
|
|
this._finish(aId);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-15 23:00:56 -08:00
|
|
|
|
2012-02-07 01:44:55 -08:00
|
|
|
// otherwise, trigger the outro animation and notify necessary observers
|
|
|
|
Services.obs.notifyObservers(null, TILT_NOTIFICATIONS.DESTROYING, null);
|
2012-01-15 23:00:57 -08:00
|
|
|
|
2012-02-07 01:44:55 -08:00
|
|
|
controller.removeEventListeners();
|
|
|
|
controller.arcball.reset([-pageXOffset, -pageYOffset]);
|
|
|
|
presenter.executeDestruction(this._finish.bind(this, aId));
|
|
|
|
},
|
2012-01-19 07:48:22 -08:00
|
|
|
|
2012-02-07 01:44:55 -08:00
|
|
|
/**
|
|
|
|
* Finishes detroying a specific instance of the visualizer.
|
|
|
|
*
|
|
|
|
* @param {String} aId
|
|
|
|
* the identifier of the instance in the visualizers array
|
|
|
|
*/
|
|
|
|
_finish: function T__finish(aId)
|
|
|
|
{
|
|
|
|
this.visualizers[aId].removeOverlay();
|
|
|
|
this.visualizers[aId].cleanup();
|
|
|
|
this.visualizers[aId] = null;
|
2012-01-15 23:00:56 -08:00
|
|
|
|
2012-02-07 01:44:55 -08:00
|
|
|
this._isDestroying = false;
|
|
|
|
this.chromeWindow.gBrowser.selectedBrowser.focus();
|
|
|
|
Services.obs.notifyObservers(null, TILT_NOTIFICATIONS.DESTROYED, null);
|
2011-11-10 02:09:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles any supplementary post-initialization work, done immediately
|
2012-01-19 07:48:22 -08:00
|
|
|
* after a TILT_NOTIFICATIONS.INITIALIZING notification.
|
2011-11-10 02:09:18 -08:00
|
|
|
*/
|
2012-01-19 07:48:22 -08:00
|
|
|
_whenInitializing: function T__whenInitializing()
|
2011-11-10 02:09:18 -08:00
|
|
|
{
|
|
|
|
this._whenShown();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles any supplementary post-destruction work, done immediately
|
|
|
|
* after a TILT_NOTIFICATIONS.DESTROYED notification.
|
|
|
|
*/
|
|
|
|
_whenDestroyed: function T__whenDestroyed()
|
|
|
|
{
|
|
|
|
this._whenHidden();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles any necessary changes done when the Tilt surface is shown,
|
|
|
|
* after a TILT_NOTIFICATIONS.SHOWN notification.
|
|
|
|
*/
|
|
|
|
_whenShown: function T__whenShown()
|
|
|
|
{
|
|
|
|
this.tiltButton.checked = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles any necessary changes done when the Tilt surface is hidden,
|
|
|
|
* after a TILT_NOTIFICATIONS.HIDDEN notification.
|
|
|
|
*/
|
|
|
|
_whenHidden: function T__whenHidden()
|
|
|
|
{
|
|
|
|
this.tiltButton.checked = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the event fired when a tab is selected.
|
|
|
|
*/
|
|
|
|
_onTabSelect: function T__onTabSelect()
|
|
|
|
{
|
2012-01-27 13:44:59 -08:00
|
|
|
if (this.currentInstance) {
|
2011-11-10 02:09:18 -08:00
|
|
|
Services.obs.notifyObservers(null, TILT_NOTIFICATIONS.SHOWN, null);
|
|
|
|
} else {
|
|
|
|
Services.obs.notifyObservers(null, TILT_NOTIFICATIONS.HIDDEN, null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A node was selected in the Inspector.
|
|
|
|
* Called from InspectorUI.
|
|
|
|
*
|
|
|
|
* @param {Element} aNode
|
|
|
|
* the newly selected node
|
|
|
|
*/
|
|
|
|
update: function T_update(aNode) {
|
2012-01-27 13:44:59 -08:00
|
|
|
if (this.currentInstance) {
|
2012-02-02 09:14:40 -08:00
|
|
|
this.currentInstance.presenter.highlightNode(aNode, "moveIntoView");
|
2011-11-10 02:09:18 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the browser event listeners to handle state changes.
|
|
|
|
* Called from InspectorUI.
|
|
|
|
*/
|
|
|
|
setup: function T_setup()
|
|
|
|
{
|
|
|
|
if (this._setupFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the preferences from the devtools.tilt branch
|
|
|
|
TiltVisualizer.Prefs.load();
|
|
|
|
|
|
|
|
// hide the button in the Inspector toolbar if Tilt is not enabled
|
|
|
|
this.tiltButton.hidden = !this.enabled;
|
|
|
|
|
|
|
|
// add the necessary observers to handle specific notifications
|
|
|
|
Services.obs.addObserver(
|
2012-01-19 07:48:22 -08:00
|
|
|
this._whenInitializing.bind(this), TILT_NOTIFICATIONS.INITIALIZING, false);
|
2011-11-10 02:09:18 -08:00
|
|
|
Services.obs.addObserver(
|
|
|
|
this._whenDestroyed.bind(this), TILT_NOTIFICATIONS.DESTROYED, false);
|
|
|
|
Services.obs.addObserver(
|
|
|
|
this._whenShown.bind(this), TILT_NOTIFICATIONS.SHOWN, false);
|
|
|
|
Services.obs.addObserver(
|
|
|
|
this._whenHidden.bind(this), TILT_NOTIFICATIONS.HIDDEN, false);
|
2012-01-27 13:44:59 -08:00
|
|
|
|
2011-11-10 02:09:18 -08:00
|
|
|
Services.obs.addObserver(function(aSubject, aTopic, aWinId) {
|
|
|
|
this.destroy(aWinId); }.bind(this),
|
|
|
|
this.chromeWindow.InspectorUI.INSPECTOR_NOTIFICATIONS.DESTROYED, false);
|
|
|
|
|
|
|
|
this.chromeWindow.gBrowser.tabContainer.addEventListener("TabSelect",
|
|
|
|
this._onTabSelect.bind(this), false);
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: this shouldn't be done here, see bug #705131
|
|
|
|
let onOpened = function() {
|
2012-01-29 10:00:10 -08:00
|
|
|
if (this.inspector && this.highlighter && this.currentInstance) {
|
|
|
|
this.inspector.stopInspecting();
|
|
|
|
this.inspector.inspectToolbutton.disabled = true;
|
|
|
|
this.highlighter.hide();
|
2011-11-10 02:09:18 -08:00
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let onClosed = function() {
|
2012-01-29 10:00:10 -08:00
|
|
|
if (this.inspector && this.highlighter) {
|
|
|
|
this.inspector.inspectToolbutton.disabled = false;
|
|
|
|
this.highlighter.show();
|
|
|
|
}
|
2011-11-10 02:09:18 -08:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
Services.obs.addObserver(onOpened,
|
|
|
|
this.chromeWindow.InspectorUI.INSPECTOR_NOTIFICATIONS.OPENED, false);
|
|
|
|
Services.obs.addObserver(onClosed,
|
|
|
|
this.chromeWindow.InspectorUI.INSPECTOR_NOTIFICATIONS.CLOSED, false);
|
|
|
|
Services.obs.addObserver(onOpened,
|
2012-01-19 07:48:22 -08:00
|
|
|
TILT_NOTIFICATIONS.INITIALIZING, false);
|
2011-11-10 02:09:18 -08:00
|
|
|
Services.obs.addObserver(onClosed,
|
|
|
|
TILT_NOTIFICATIONS.DESTROYED, false);
|
|
|
|
|
|
|
|
|
|
|
|
this._setupFinished = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this tool is enabled.
|
|
|
|
*/
|
|
|
|
get enabled()
|
|
|
|
{
|
|
|
|
return (TiltVisualizer.Prefs.enabled &&
|
2012-01-13 05:27:12 -08:00
|
|
|
(TiltGL.isWebGLForceEnabled() || TiltGL.isWebGLSupported()));
|
2011-11-10 02:09:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the ID of the current window object to identify the visualizer.
|
|
|
|
*/
|
|
|
|
get currentWindowId()
|
|
|
|
{
|
2012-01-27 13:44:59 -08:00
|
|
|
return TiltUtils.getWindowId(
|
|
|
|
this.chromeWindow.gBrowser.selectedBrowser.contentWindow);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the visualizer instance for the current tab.
|
|
|
|
*/
|
|
|
|
get currentInstance()
|
|
|
|
{
|
|
|
|
return this.visualizers[this.currentWindowId];
|
2011-11-10 02:09:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-01-29 10:00:10 -08:00
|
|
|
* Gets the current InspectorUI instance.
|
2011-11-10 02:09:18 -08:00
|
|
|
*/
|
2012-01-29 10:00:10 -08:00
|
|
|
get inspector()
|
2011-11-10 02:09:18 -08:00
|
|
|
{
|
2012-01-29 10:00:10 -08:00
|
|
|
return this.chromeWindow.InspectorUI;
|
2011-11-10 02:09:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-01-29 10:00:10 -08:00
|
|
|
* Gets the current Highlighter instance from the InspectorUI.
|
2011-11-10 02:09:18 -08:00
|
|
|
*/
|
2012-01-29 10:00:10 -08:00
|
|
|
get highlighter()
|
2011-11-10 02:09:18 -08:00
|
|
|
{
|
2012-01-29 10:00:10 -08:00
|
|
|
return this.inspector.highlighter;
|
2011-11-10 02:09:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-01-29 10:00:10 -08:00
|
|
|
* Gets the Tilt button in the Inspector toolbar.
|
2011-11-10 02:09:18 -08:00
|
|
|
*/
|
2012-01-29 10:00:10 -08:00
|
|
|
get tiltButton()
|
2011-11-10 02:09:18 -08:00
|
|
|
{
|
2012-02-07 01:44:55 -08:00
|
|
|
return this.chromeWindow.document.getElementById("inspector-3D-button");
|
2011-11-10 02:09:18 -08:00
|
|
|
}
|
|
|
|
};
|