1 /* See license.txt for terms of usage */
  2 
  3 define([
  4     "firebug/firebug",
  5     "firebug/lib/domplate",
  6     "firebug/lib/locale",
  7     "firebug/lib/dom",
  8     "firebug/console/commandLineExposed",
  9     "firebug/chrome/window",
 10     "firebug/lib/xpcom",
 11     "firebug/lib/events",
 12 ],
 13 function(Firebug, Domplate, Locale, Dom, CommandLineExposed, Win, Xpcom, Events) {
 14 with (Domplate) {
 15 
 16 // ********************************************************************************************* //
 17 // Constants
 18 
 19 const Cc = Components.classes;
 20 const Ci = Components.interfaces;
 21 
 22 var CMD_TYPE_COMMAND = 1;
 23 var CMD_TYPE_SHORTCUT = 2;
 24 var CMD_TYPE_PROPERTY = 3;
 25 
 26 const prompts = Xpcom.CCSV("@mozilla.org/embedcomp/prompt-service;1", "nsIPromptService");
 27 
 28 // ********************************************************************************************* //
 29 // Domplates
 30 
 31 var HelpCaption = domplate(
 32 {
 33     tag:
 34         SPAN({"class": "helpTitle"},
 35             SPAN({"class": "helpCaption"},
 36                 Locale.$STR("console.cmd.help_title")
 37             ),
 38             SPAN({"class": "helpCaptionDesc"},
 39                 Locale.$STR("console.cmd.help_title_desc")
 40             )
 41         )
 42 });
 43 
 44 // The table UI should be based on tableRep
 45 var HelpTable = domplate(
 46 {
 47     tag:
 48         TABLE({"class": "helpTable", cellspacing: 0, cellpadding: 0, width: "100%",
 49             "role": "grid"},
 50             THEAD({"class": "helpThead", "role": "presentation"},
 51                 TR({"class": "headerRow focusRow helpRow subFocusRow", onclick: "$onClick",
 52                     "role": "row"},
 53                     TH({"class": "headerCell a11yFocus", "role": "columnheader", width: "10%"},
 54                         DIV({"class": "headerCellBox"},
 55                             Locale.$STR("Name")
 56                         )
 57                     ),
 58                     TH({"class": "headerCell a11yFocus", "role": "columnheader", width: "90%"},
 59                         DIV({"class": "headerCellBox"},
 60                             Locale.$STR("Description")
 61                         )
 62                     )
 63                 )
 64             ),
 65             TBODY({"class": "helpTbody", "role": "presentation"})
 66         ),
 67 
 68     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 69 
 70     onClick: function(event)
 71     {
 72     }
 73 });
 74 
 75 var HelpEntry = domplate(
 76 {
 77     tag:
 78         FOR("command", "$commands",
 79             TR({"class": "focusRow helpRow subFocusRow", "role": "row"},
 80                 TD({"class": "a11yFocus helpCell commandName", "role": "presentation"},
 81                     A({"class": "objectLink", onclick: "$onClick", _repObject: "$command"},
 82                         "$command|getName"
 83                     )
 84                 ),
 85                 TD({"class": "a11yFocus helpCell commandDesc", "role": "gridcell"},
 86                     "$command|getDesc"
 87                 )
 88             )
 89         ),
 90 
 91     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 92 
 93     onClick: function(event)
 94     {
 95         Events.cancelEvent(event);
 96 
 97         var object = Firebug.getRepObject(event.target);
 98 
 99         if (object.noUserHelpUrl)
100         {
101             prompts.alert(null, Locale.$STR("Firebug"),
102                 Locale.$STR("console.cmd.helpUrlNotAvailable"));
103             return;
104         }
105 
106         var helpUrl = "http://getfirebug.com/wiki/index.php/" + object.name;
107         if (object.helpUrl)
108             helpUrl = object.helpUrl;
109 
110         Win.openNewTab(helpUrl);
111     },
112 
113     getName: function(object)
114     {
115         var name = object.name;
116         if (object.type != CMD_TYPE_PROPERTY)
117             name = name + "()";
118         return name;
119     },
120 
121     getDesc: function(object)
122     {
123         if (object.nol10n)
124             return object.desc;
125 
126         return Locale.$STR(object.desc);
127     }
128 });
129 
130 // ********************************************************************************************* //
131 // Help Object
132 
133 var CommandLineHelp = domplate(
134 {
135     render: function(context)
136     {
137         var row = Firebug.Console.openGroup("help", context, "help",
138             HelpCaption, true, null, true);
139         Firebug.Console.closeGroup(context, true);
140 
141         var logGroupBody = row.lastChild;
142         var table = HelpTable.tag.replace({}, logGroupBody);
143         var tBody = table.lastChild;
144 
145         var commands = [];
146 
147         for (var i=0; i<CommandLineExposed.commands.length; i++)
148         {
149             commands.push({
150                 name: CommandLineExposed.commands[i],
151                 desc: "console.cmd.help." + CommandLineExposed.commands[i],
152                 type: CMD_TYPE_COMMAND,
153             })
154         }
155 
156         for (var i=0; i<CommandLineExposed.consoleShortcuts.length; i++)
157         {
158             commands.push({
159                 name: CommandLineExposed.consoleShortcuts[i],
160                 desc: "console.cmd.help." + CommandLineExposed.consoleShortcuts[i],
161                 type: CMD_TYPE_SHORTCUT,
162             })
163         }
164 
165         for (var i=0; i<CommandLineExposed.properties.length; i++)
166         {
167             commands.push({
168                 name: CommandLineExposed.properties[i],
169                 desc: "console.cmd.help." + CommandLineExposed.properties[i],
170                 type: CMD_TYPE_PROPERTY,
171             })
172         }
173 
174         for (var name in CommandLineExposed.userCommands)
175         {
176             var config = CommandLineExposed.userCommands[name];
177             commands.push({
178                 name: name,
179                 desc: config.description,
180                 nol10n: true,
181                 noUserHelpUrl: !config.helpUrl,
182                 helpUrl: config.helpUrl ? config.helpUrl: null,
183                 type: config.getter ? CMD_TYPE_PROPERTY : CMD_TYPE_COMMAND,
184             })
185         }
186 
187         // Sort commands
188         commands.sort(function sortName(a, b) { return a.name > b.name ? 1 : -1; });
189 
190         // Generate table
191         HelpEntry.tag.insertRows({commands: commands}, tBody);
192 
193         return row;
194     }
195 });
196 
197 // ********************************************************************************************* //
198 // Command Implementation
199 
200 function onExecuteCommand(context)
201 {
202     CommandLineHelp.render(context);
203     return Firebug.Console.getDefaultReturnValue(context.window);
204 }
205 
206 // ********************************************************************************************* //
207 // Registration
208 
209 Firebug.registerCommand("help", {
210     getter: true,
211     helpUrl: "http://getfirebug.com/wiki/index.php/help",
212     handler: onExecuteCommand.bind(this),
213     description: Locale.$STR("console.cmd.help.help")
214 });
215 
216 return CommandLineHelp;
217 
218 // ********************************************************************************************* //
219 }});
220