1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * @constructor
33 * @param {string=} title
34 * @extends {WebInspector.View}
35 */
36WebInspector.HelpScreen = function(title)
37{
38    WebInspector.View.call(this);
39    this.markAsRoot();
40    this.registerRequiredCSS("helpScreen.css");
41
42    this.element.className = "help-window-outer";
43    this.element.addEventListener("keydown", this._onKeyDown.bind(this), false);
44    this.element.tabIndex = 0;
45    this.element.addEventListener("focus", this._onBlur.bind(this), false);
46
47    if (title) {
48        var mainWindow = this.element.createChild("div", "help-window-main");
49        var captionWindow = mainWindow.createChild("div", "help-window-caption");
50        captionWindow.appendChild(this._createCloseButton());
51        this.contentElement = mainWindow.createChild("div", "help-content");
52        captionWindow.createChild("h1", "help-window-title").textContent = title;
53    }
54}
55
56/**
57 * @type {WebInspector.HelpScreen}
58 */
59WebInspector.HelpScreen._visibleScreen = null;
60
61WebInspector.HelpScreen.prototype = {
62    _createCloseButton: function()
63    {
64        var closeButton = document.createElement("button");
65        closeButton.className = "help-close-button";
66        closeButton.textContent = "\u2716"; // Code stands for HEAVY MULTIPLICATION X.
67        closeButton.addEventListener("click", this.hide.bind(this), false);
68        return closeButton;
69    },
70
71    showModal: function()
72    {
73        var visibleHelpScreen = WebInspector.HelpScreen._visibleScreen;
74        if (visibleHelpScreen === this)
75            return;
76
77        if (visibleHelpScreen)
78            visibleHelpScreen.hide();
79        WebInspector.HelpScreen._visibleScreen = this;
80        this.show(document.body);
81        this.focus();
82    },
83
84    hide: function()
85    {
86        if (!this.isShowing())
87            return;
88
89        WebInspector.HelpScreen._visibleScreen = null;
90
91        WebInspector.restoreFocusFromElement(this.element);
92        this.detach();
93    },
94
95    /**
96     * @param {number} keyCode
97     * @return {boolean}
98     */
99    isClosingKey: function(keyCode)
100    {
101        return [
102            WebInspector.KeyboardShortcut.Keys.Enter.code,
103            WebInspector.KeyboardShortcut.Keys.Esc.code,
104            WebInspector.KeyboardShortcut.Keys.Space.code,
105        ].indexOf(keyCode) >= 0;
106    },
107
108    _onKeyDown: function(event)
109    {
110        if (this.isShowing() && this.isClosingKey(event.keyCode)) {
111            this.hide();
112            event.consume();
113        }
114    },
115
116    _onBlur: function(event)
117    {
118        // Pretend we're modal, grab focus back if we're still shown.
119        if (this.isShowing() && !this.element.isSelfOrAncestor(event.target))
120            WebInspector.setCurrentFocusElement(this.element);
121    },
122
123    __proto__: WebInspector.View.prototype
124}
125
126/**
127 * @constructor
128 * @param {string=} title
129 * @param {string=} message
130 * @extends {WebInspector.HelpScreen}
131 */
132WebInspector.HelpScreenUntilReload = function(title, message)
133{
134    WebInspector.HelpScreen.call(this, title);
135    var p = this.contentElement.createChild("p");
136    p.addStyleClass("help-section");
137    p.textContent = message;
138    WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this.hide, this);
139}
140
141WebInspector.HelpScreenUntilReload.prototype = {
142    /**
143     * @override
144     */
145    willHide: function()
146    {
147        WebInspector.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this.hide, this);
148        WebInspector.HelpScreen.prototype.willHide.call(this);
149    },
150
151    __proto__: WebInspector.HelpScreen.prototype
152}
153
154