Skip to content

Commit cc620a5

Browse files
devvaannshabose
authored andcommitted
feat: add dialogs quick start docs
1 parent ce5fb9a commit cc620a5

File tree

4 files changed

+323
-0
lines changed

4 files changed

+323
-0
lines changed
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
---
2+
title: Dialogs and Buttons
3+
---
4+
5+
## Adding a Dialog Box and Buttons
6+
7+
To add a dialog box, follow these steps:
8+
9+
1. **Import the required modules.**
10+
Import the `Dialogs` and `DefaultDialogs` modules along with other necessary modules:
11+
12+
```jsx
13+
const DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"),
14+
Dialogs = brackets.getModule("widgets/Dialogs");
15+
16+
// other modules you may require
17+
const AppInit = brackets.getModule("utils/AppInit"),
18+
CommandManager = brackets.getModule("command/CommandManager"),
19+
Menus = brackets.getModule("command/Menus");
20+
21+
```
22+
23+
2. **Create a function to show the dialog**
24+
25+
To create a dialog you can use the specialized dialog APIs such as `Dialogs.showConfirmDialog`, `Dialogs.showInfoDialog` and `Dialogs.showErrorDialog` provided by the `Dialogs` module:
26+
27+
28+
```jsx
29+
function handleHelloWorld() {
30+
Dialogs.showInfoDialog(
31+
"hello", // Title
32+
"world" // Message
33+
);
34+
}
35+
```
36+
37+
The `Dialogs.showInfoDialog()` method is the preferred way to display information messages.
38+
39+
Similarly, you can use `Dialogs.showErrorDialog()` for error messages:
40+
41+
```jsx
42+
function handleError() {
43+
Dialogs.showErrorDialog(
44+
"Error",
45+
"Something went wrong!"
46+
);
47+
}
48+
```
49+
50+
You can also close the dialog programmatically using the `Dialog.close()` method.
51+
52+
```jsx
53+
function handleHelloWorld() {
54+
const dialog = Dialogs.showInfoDialog(
55+
"hello",
56+
"world"
57+
);
58+
59+
// Close the dialog after 2 seconds
60+
setTimeout(() => {
61+
dialog.close();
62+
}, 2000);
63+
}
64+
```
65+
66+
This will automatically close the dialog after 2 seconds.
67+
68+
These specialized dialog methods handle the common use cases.
69+
70+
Click on the functions to read more about them : [showConfirmDialog()](https://docs.phcode.dev/api/API-Reference/widgets/Dialogs#showConfirmDialog), [showInfoDialog](https://docs.phcode.dev/api/API-Reference/widgets/Dialogs#showInfoDialog), [showErrorDialog](https://docs.phcode.dev/api/API-Reference/widgets/Dialogs#showErrorDialog).
71+
72+
If you require custom buttons or advanced functionality, you can use the generic `showModalDialog()` method.
73+
74+
[Click here](#creating-custom-dialog-boxes) to read more about creating custom dialog boxes.
75+
76+
3. **Register the command**
77+
Register a command that will trigger the dialog:
78+
79+
```jsx
80+
const MY_COMMAND_ID = "helloworld_sayhello";
81+
CommandManager.register("Hello World", MY_COMMAND_ID, handleHelloWorld);
82+
```
83+
84+
4. **Add the menu item**
85+
Add a menu item that will execute the command:
86+
87+
```jsx
88+
const menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
89+
menu.addMenuItem(MY_COMMAND_ID);
90+
```
91+
92+
Full Code Example:
93+
94+
```jsx
95+
define(function (require, exports, module) {
96+
"use strict";
97+
98+
// Brackets modules
99+
const AppInit = brackets.getModule("utils/AppInit"),
100+
DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"),
101+
Dialogs = brackets.getModule("widgets/Dialogs"),
102+
CommandManager = brackets.getModule("command/CommandManager"),
103+
Menus = brackets.getModule("command/Menus");
104+
105+
// Function to run when the menu item is clicked
106+
function handleHelloWorld() {
107+
Dialogs.showInfoDialog(
108+
"hello",
109+
"world"
110+
);
111+
}
112+
113+
// Register command
114+
const MY_COMMAND_ID = "helloworld_sayhello";
115+
CommandManager.register("Hello World", MY_COMMAND_ID, handleHelloWorld);
116+
117+
// Add menu item
118+
const menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
119+
menu.addMenuItem(MY_COMMAND_ID);
120+
121+
// Initialize extension
122+
AppInit.appReady(function () {
123+
console.log("hello world");
124+
});
125+
});
126+
```
127+
128+
Expected Output:
129+
130+
When the menu item is clicked, a dialog box appears:
131+
132+
![Dialog box](./images/dialog.png)
133+
134+
135+
## Creating Custom Dialog Boxes
136+
137+
While the specialized dialog methods like `showInfoDialog()`, `showConfirmDialog()` and `showErrorDialog()` cover the common use cases, you can also create more complex custom dialog boxes using `showModalDialog()`. Here's how:
138+
139+
```jsx
140+
const dialog = Dialogs.showModalDialog(
141+
DefaultDialogs.DIALOG_ID_INFO,
142+
"Custom Dialog",
143+
// Custom HTML content with CSS styling
144+
'<div class="custom-dialog">' +
145+
'<p style="text-align: center">This is a custom message</p>' +
146+
'<input style="width: 97%" type="text" id="custom-input" placeholder="Enter some text...">' +
147+
"</div>",
148+
[
149+
// For buttons
150+
{
151+
className: Dialogs.DIALOG_BTN_CLASS_PRIMARY,
152+
id: Dialogs.DIALOG_BTN_OK,
153+
text: "OK",
154+
},
155+
{
156+
className: Dialogs.DIALOG_BTN_CLASS_NORMAL,
157+
id: Dialogs.DIALOG_BTN_CANCEL,
158+
text: "Cancel",
159+
},
160+
]
161+
);
162+
```
163+
164+
The `showModalDialog()` method provides more flexibility, allowing you to create custom dialog boxes with HTML content and buttons. However, it's recommended to use the specialized dialog APIs like `showInfoDialog()`, `showConfirmDialog()` and `showErrorDialog()` whenever possible, as they provide a simpler and more standardized interface for the most common dialog types.
165+
166+
Visual Reference
167+
168+
![Custom Dialog Box](./images/custom-dialog.png)
169+
170+
[Click Here](https://docs.phcode.dev/api/API-Reference/widgets/Dialogs#showModalDialog) to read more about `showModalDialog()`.
171+
172+
→ Each button object can have:
173+
174+
- `className`: Button styling class
175+
- `id`: Button identifier
176+
- `text`: Button label text
177+
178+
→ Available Button Classes:
179+
180+
- `Dialogs.DIALOG_BTN_CLASS_PRIMARY`: Primary action button
181+
- `Dialogs.DIALOG_BTN_CLASS_NORMAL`: Normal button
182+
- `Dialogs.DIALOG_BTN_CLASS_LEFT`: Left-aligned button
183+
184+
→ Common Button IDs:
185+
186+
- `Dialogs.DIALOG_BTN_OK`
187+
- `Dialogs.DIALOG_BTN_CANCEL`
188+
- `Dialogs.DIALOG_BTN_SAVE_AS`
189+
- `Dialogs.DIALOG_BTN_DONTSAVE`
190+
- `Dialogs.DIALOG_BTN_DOWNLOAD`
191+
192+
## Handle Button Clicks
193+
194+
You can handle button clicks using the dialog's promise:
195+
196+
```jsx
197+
dialog.done(function (buttonId) {
198+
if (buttonId === Dialogs.DIALOG_BTN_OK) {
199+
const inputValue = $input.val();
200+
alert("Input value: " + inputValue);
201+
}
202+
});
203+
```
204+
205+
Complete Code Block with Custom Dialog Box and handling the button clicks.
206+
207+
```jsx
208+
define(function (require, exports, module) {
209+
"use strict";
210+
211+
const AppInit = brackets.getModule("utils/AppInit"),
212+
DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"),
213+
Dialogs = brackets.getModule("widgets/Dialogs"),
214+
CommandManager = brackets.getModule("command/CommandManager"),
215+
Menus = brackets.getModule("command/Menus");
216+
217+
function showCustomDialog() {
218+
const dialog = Dialogs.showModalDialog(
219+
DefaultDialogs.DIALOG_ID_INFO,
220+
"Custom Dialog",
221+
// Custom HTML content
222+
'<div class="custom-dialog">' +
223+
'<p style="text-align: center">This is a custom message</p>' +
224+
'<input style="width: 97%" type="text" id="custom-input" placeholder="Enter some text...">' +
225+
"</div>",
226+
[
227+
{
228+
className: Dialogs.DIALOG_BTN_CLASS_PRIMARY,
229+
id: Dialogs.DIALOG_BTN_OK,
230+
text: "OK"
231+
},
232+
{
233+
className: Dialogs.DIALOG_BTN_CLASS_NORMAL,
234+
id: Dialogs.DIALOG_BTN_CANCEL,
235+
text: "Cancel"
236+
}
237+
]
238+
);
239+
240+
// Get dialog element and ensure input is accessible
241+
const $dlg = dialog.getElement();
242+
const $input = $dlg.find("#custom-input");
243+
244+
if (!$input.length) {
245+
console.error("Failed to find input element in dialog");
246+
return;
247+
}
248+
249+
// Handle dialog button clicks
250+
dialog.done(function (buttonId) {
251+
if (buttonId === Dialogs.DIALOG_BTN_OK) {
252+
const inputValue = $input.val();
253+
alert("Input value: " + inputValue);
254+
}
255+
});
256+
}
257+
258+
// Register command
259+
const MY_COMMAND_ID = "test_customdialog";
260+
CommandManager.register("Show Custom Dialog", MY_COMMAND_ID, showCustomDialog);
261+
262+
// Add menu item
263+
const menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
264+
menu.addMenuItem(MY_COMMAND_ID);
265+
266+
// Initialize extension
267+
AppInit.appReady(function () {
268+
console.log("Custom dialog extension loaded");
269+
});
270+
});
271+
```
272+
273+
Visual Reference
274+
275+
![Custom-dialog-box-gif](./images/custom-dialog-box-gif.gif)
276+
277+
278+
## Adding a button on Status Bar
279+
280+
1. Import the `StatusBar` module.
281+
282+
```jsx
283+
const StatusBar = brackets.getModule("widgets/StatusBar");
284+
```
285+
286+
287+
2. Register the command.
288+
289+
Register the command that will trigger the clicking.
290+
291+
```jsx
292+
var MY_COMMAND_ID = "helloworld_sayhello";
293+
CommandManager.register("Hello World", MY_COMMAND_ID, handleHelloWorld);
294+
```
295+
296+
297+
3. Add the button to the StatusBar.
298+
299+
To add the button to StatusBar, use `addIndicator()` :-
300+
301+
302+
```jsx
303+
StatusBar.addIndicator(
304+
MY_COMMAND_ID, // unique ID for this indicator
305+
$("<div>Test</div>").click(handleHelloWorld), // Optional DOMNode for the indicator
306+
true, // show the indicator
307+
"hello-world-status", // CSS class
308+
"tooltip", // tooltip text
309+
);
310+
```
311+
312+
→ The parameters of the `addIndicator()` method :-
313+
314+
| Param | Type | Description |
315+
| --- | --- | --- |
316+
| id | `string` | Registration id of the indicator to be updated. |
317+
| [indicator] | `DOMNode` or `jQueryObject` | Optional DOMNode for the indicator |
318+
| [visible] | `boolean` | Shows or hides the indicator over the statusbar. |
319+
| [style] | `string` | Sets the attribute "class" of the indicator. |
320+
| [tooltip] | `string` | Sets the attribute "title" of the indicator. |
321+
| [insertBefore] | `string` | An id of an existing status bar indicator. The new indicator will be inserted before (i.e. to the left of) the indicator specified by this parameter. |
322+
323+
> For a detailed description, refer to [this link](https://docs.phcode.dev/api/API-Reference/widgets/StatusBar).
772 KB
Loading
13.8 KB
Loading
5.48 KB
Loading

0 commit comments

Comments
 (0)