1 /* See license.txt for terms of usage */
  2 
  3 define([
  4     "firebug/lib/object",
  5     "firebug/firebug",
  6     "firebug/console/commandLine",
  7     "firebug/lib/css",
  8     "firebug/lib/dom",
  9     "firebug/lib/string",
 10     "firebug/lib/xml",
 11     "firebug/lib/events",
 12 ],
 13 function(Obj, Firebug, CommandLine, Css, Dom, Str, Xml, Events) {
 14 
 15 // ************************************************************************************************
 16 // Constants
 17 
 18 // ************************************************************************************************
 19 // Implementation
 20 
 21 /**
 22  * @module Command Line availability in other panels.
 23  */
 24 Firebug.CommandLine.Popup = Obj.extend(Firebug.Module,
 25 {
 26     dispatchName: "commandLinePopup",
 27     lastFocused : null,
 28 
 29     initializeUI: function()
 30     {
 31         Firebug.Module.initializeUI.apply(this, arguments);
 32 
 33         this.setPopupBrowserStyle(Firebug.chrome);
 34 
 35         this.onKeyPress = Obj.bind(this.onKeyPress, this);
 36 
 37         this.attachListeners();
 38     },
 39 
 40     shutdown: function()
 41     {
 42         var contentBox = Firebug.chrome.$("fbContentBox");
 43         Events.removeEventListener(contentBox, "keypress", this.onKeyPress, false);
 44     },
 45 
 46     initContext: function(context)
 47     {
 48         Firebug.Module.showContext.apply(this, arguments);
 49 
 50         var show = Firebug.Options.get("alwaysShowCommandLine");
 51         if (show && !this.isVisible())
 52             this.toggle(context);
 53     },
 54 
 55     showPanel: function(browser, panel)
 56     {
 57         if (FBTrace.DBG_COMMANDLINE)
 58             FBTrace.sysout("commandLine.Popup.showPanel; " + (panel?panel.name:"null panel"));
 59 
 60         var chrome = Firebug.chrome;
 61         var visible = this.isVisible();
 62         var isConsole = (panel && panel.name == "console");
 63         var showCommandEditor = Firebug.commandEditor;
 64 
 65         // Disable the console popup button (Firebug toolbar) if the Console panel
 66         // is disabled or selected.
 67         var consolePanelType = Firebug.getPanelType("console");
 68         var disabled = consolePanelType.prototype.isEnabled() ? "false" : "true";
 69         if (isConsole || !panel)
 70             disabled = "true";
 71 
 72         chrome.$("fbCommandPopupButton").setAttribute("disabled", disabled);
 73 
 74         if ((showCommandEditor && isConsole) || !panel)
 75         {
 76             Dom.collapse(chrome.$("fbPanelSplitter"), panel ? false : true);
 77             Dom.collapse(chrome.$("fbSidePanelDeck"), panel ? false : true);
 78             Dom.collapse(chrome.$("fbCommandBox"), true);
 79             chrome.$("fbSidePanelDeck").selectedPanel = chrome.$("fbCommandEditorBox");
 80         }
 81 
 82         // The console can't be multiline on other panels, so hide the toggle-to-multiline
 83         // button (displayed at the end of the one line command line)
 84         Dom.collapse(chrome.$("fbToggleCommandLine"), !isConsole);
 85 
 86         // Update visibility of the console-popup (hidden if the Console panel is selected).
 87         this.updateVisibility(visible && !isConsole && panel && disabled != "true");
 88 
 89         // Make sure the console panel is attached to the proper document
 90         // (the one used by all panels, or the one used by console popup and available
 91         // for all the panels).
 92         if (panel)
 93             this.reattach(panel.context);
 94 
 95         // If the the console panel is opened on another panel, simulate show event for it.
 96         if (panel && !isConsole && visible)
 97             this.showPopupPanel(panel.context);
 98     },
 99 
100     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
101 
102     setPopupBrowserStyle: function(chrome)
103     {
104         // Set additional style so we can make the panelNode-console node
105         // always visible regardless of the currently selected panel.
106         var doc = chrome.$("fbCommandPopupBrowser").contentDocument;
107         var body = Dom.getBody(doc);
108         Css.setClass(body, "commandPopup");
109     },
110 
111     attachListeners: function()
112     {
113         // Register event listeners.
114         var contentBox = Firebug.chrome.$("fbContentBox");
115         Events.addEventListener(contentBox, "keypress", this.onKeyPress, false);
116     },
117 
118     toggle: function(context)
119     {
120         var panel = Firebug.chrome.getSelectedPanel();
121         if (panel && panel.name == "console")
122             return;
123 
124         if (FBTrace.DBG_COMMANDLINE)
125             FBTrace.sysout("commandLine.Popup.toggle;");
126 
127         var newState = !this.isVisible();
128         Firebug.chrome.setGlobalAttribute("cmd_firebug_toggleCommandPopup", "checked", newState);
129         Firebug.Options.set("alwaysShowCommandLine", newState);
130 
131         this.updateVisibility(newState);
132 
133         this.reattach(context);
134         this.showPopupPanel(context);
135     },
136 
137     showPopupPanel: function(context)
138     {
139         // If the the console panel is opened on another panel, simulate show event for it.
140         if (this.isVisible())
141         {
142             var panel = context.getPanel("console", true);
143             if (panel)
144             {
145                 var state = Firebug.getPanelState(panel);
146                 panel.showPanel(state);
147             }
148         }
149     },
150 
151     updateVisibility: function(visible)
152     {
153         var chrome = Firebug.chrome;
154         var popup = chrome.$("fbCommandPopup");
155         var splitter = chrome.$("fbCommandPopupSplitter")
156         var cmdbox = chrome.$("fbCommandBox");
157         var toggle = chrome.$("fbToggleCommandLine");
158 
159         // If all the visual parts are already visible then bail out.
160         if (visible && !Dom.isCollapsed(popup) && !Dom.isCollapsed(splitter) &&
161             !Dom.isCollapsed(cmdbox) && Dom.isCollapsed(toggle))
162             return;
163 
164         Dom.collapse(popup, !visible);
165         Dom.collapse(splitter, !visible);
166         Dom.collapse(cmdbox, !visible);
167 
168         // The command line can't be multiline in other panels.
169         Dom.collapse(toggle, visible);
170 
171         var commandLine = Firebug.CommandLine.getSingleRowCommandLine();
172         var commandEditor = Firebug.CommandLine.getCommandEditor();
173 
174         // Focus the command line if it has been just displayed.
175         if (visible)
176         {
177             this.lastFocused = document.commandDispatcher.focusedElement;
178             commandLine.focus();
179         }
180         else if (this.lastFocused && Xml.isVisible(this.lastFocused) &&
181             typeof this.lastFocused.focus == "function")
182         {
183             this.lastFocused.focus();
184             this.lastFocused = null;
185         }
186 
187         if (Firebug.commandEditor)
188         {
189             if (visible)
190                 commandLine.value = Str.stripNewLines(commandEditor.value);
191             else
192                 commandEditor.value = Str.cleanIndentation(commandLine.value);
193         }
194     },
195 
196     isVisible: function()
197     {
198         var checked = Firebug.chrome.getGlobalAttribute("cmd_firebug_toggleCommandPopup", "checked");
199         return (checked == "true") ? true : false;
200     },
201 
202     reattach: function(context)
203     {
204         if (!context)
205         {
206             if (FBTrace.DBG_ERRORS)
207                 FBTrace.sysout("commandLinePopup.reattach; ERROR No context");
208             return;
209         }
210 
211         var consolePanelType = Firebug.getPanelType("console");
212         var doc = Firebug.chrome.getPanelDocument(consolePanelType);
213 
214         // Console doesn't have to be available (e.g. disabled)
215         var panel = context.getPanel("console", true);
216         if (panel)
217             panel.reattach(doc);
218     },
219 
220     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
221     // Event Listeners
222 
223     onKeyPress: function(event)
224     {
225         if (!Events.noKeyModifiers(event))
226             return false;
227 
228         // ESC
229         var target = event.target;
230         // prevent conflict with inline editors being closed
231         if (this.isVisible() && target && event.keyCode == KeyEvent.DOM_VK_ESCAPE
232             && !Css.hasClass(target, "textEditorInner"))
233             this.toggle(Firebug.currentContext);
234     }
235 });
236 
237 // ************************************************************************************************
238 // Registration
239 
240 Firebug.registerModule(Firebug.CommandLine.Popup);
241 
242 return Firebug.CommandLine.Popup;
243 
244 // ************************************************************************************************
245 });
246