Skip to content

Commit 08e6baa

Browse files
Merge pull request #72 from mlakatkou/GS-3312
[update] custom-edit-form article
2 parents 1214c07 + 7b3b475 commit 08e6baa

1 file changed

Lines changed: 73 additions & 76 deletions

File tree

docs/guides/custom-edit-form.md

Lines changed: 73 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,48 @@ sidebar_label: "Custom Lightbox"
55

66
# Custom Lightbox
77

8-
## The ways of creating a custom lightbox
8+
## Ways of creating a custom lightbox
99

10-
You can create a fully custom lightbox for gantt and replace the default one with it. There are two possible ways to do it:
10+
You can create a fully custom lightbox for Gantt and replace the default one with it. There are two possible ways to do it:
1111

12-
1) By redefining the [showLightbox](api/method/showlightbox.md) method:
12+
1. By redefining the [`showLightbox()`](api/method/showlightbox.md) method:
1313

1414
~~~js
15-
gantt.showLightbox = function(id){
15+
gantt.showLightbox = (id) => {
1616
// code of the custom form
17-
}
17+
};
1818
~~~
1919

20-
- id - (string/number) - the task id
21-
22-
There's also the [hideLightbox](api/method/hidelightbox.md) method that will help in the lightbox implementation.
20+
- `id` - (string/number) - the task ID
2321

22+
There's also the [`hideLightbox()`](api/method/hidelightbox.md) method that will help in the lightbox implementation.
2423

2524
Let's create an HTML container "my-form" where we'll put a custom lightbox:
2625

2726
~~~html
2827
<div id="my-form">
29-
<label for="description">Task text
30-
<input type="text" name="description" value="" >
31-
</label>
32-
33-
34-
35-
<input type="button" name="save" value="Save">
36-
<input type="button" name="close" value="Close">
37-
<input type="button" name="delete" value="Delete">
28+
<label for="description">Task text
29+
<input type="text" name="description" value="">
30+
</label>
31+
<input type="button" name="save" value="Save">
32+
<input type="button" name="close" value="Close">
33+
<input type="button" name="delete" value="Delete">
3834
</div>
3935
~~~
4036

41-
4237
Then to make a custom lightbox, you can use the configuration similar to this:
4338

44-
4539
~~~js
46-
var taskId = null;
40+
let currentTaskId = null;
4741

48-
gantt.showLightbox = function(id) {
49-
taskId = id;
50-
var task = gantt.getTask(id);
42+
gantt.showLightbox = (id) => {
43+
currentTaskId = id;
44+
const task = gantt.getTask(id);
5145

52-
var form = getForm();
53-
var input = form.querySelector("[name='description']");
54-
input.focus();
55-
input.value = task.text;
46+
const form = getForm();
47+
const descriptionInput = form.querySelector("[name='description']");
48+
descriptionInput.focus();
49+
descriptionInput.value = task.text;
5650

5751
form.style.display = "block";
5852

@@ -61,105 +55,108 @@ gantt.showLightbox = function(id) {
6155
form.querySelector("[name='delete']").onclick = remove;
6256
};
6357

64-
gantt.hideLightbox = function(){
58+
gantt.hideLightbox = () => {
6559
getForm().style.display = "";
66-
taskId = null;
67-
}
68-
60+
currentTaskId = null;
61+
};
6962

7063
function getForm() {
7164
return document.getElementById("my-form");
72-
};
65+
}
7366

7467
function save() {
75-
var task = gantt.getTask(taskId);
68+
const task = gantt.getTask(currentTaskId);
7669

7770
task.text = getForm().querySelector("[name='description']").value;
7871

79-
if(task.$new){
72+
if (task.$new) {
8073
delete task.$new;
81-
gantt.addTask(task,task.parent);
82-
}else{
74+
gantt.addTask(task, task.parent);
75+
} else {
8376
gantt.updateTask(task.id);
8477
}
8578

8679
gantt.hideLightbox();
8780
}
8881

8982
function cancel() {
90-
var task = gantt.getTask(taskId);
83+
const task = gantt.getTask(currentTaskId);
84+
85+
if (task.$new) {
86+
gantt.deleteTask(task.id);
87+
}
9188

92-
if(task.$new)
93-
gantt.deleteTask(task.id);
9489
gantt.hideLightbox();
9590
}
9691

9792
function remove() {
98-
gantt.deleteTask(taskId);
93+
gantt.deleteTask(currentTaskId);
9994
gantt.hideLightbox();
10095
}
10196
~~~
10297

103-
2) Using the [onBeforeLightbox](api/event/onbeforelightbox.md) event. In this case the algorithm of actions is the following:
98+
2. Use the [`onBeforeLightbox`](api/event/onbeforelightbox.md) event. In this case, the algorithm of actions is the following:
10499

105100
- detect when the lightbox is about to be shown
106101
- block the default lightbox
107-
- show a custom form and fill-in the task data.
108-
102+
- show a custom form and fill in the task data
109103

110104
~~~js
111-
gantt.attachEvent("onBeforeLightbox", function(id) {
112-
var task = gantt.getTask(id);
113-
if(task.$new){
105+
gantt.attachEvent("onBeforeLightbox", (id) => {
106+
const task = gantt.getTask(id);
107+
108+
if (task.$new) {
114109
dhtmlx.confirm({
115-
text:"Create task?",
116-
callback: function(res){
117-
if(res){
110+
text: "Create task?",
111+
callback: (confirmed) => {
112+
if (confirmed) {
118113
//..apply values
119114
delete task.$new;
120115
gantt.addTask(task);
121-
}else{
116+
} else {
122117
gantt.deleteTask(task.id);
123118
}
124119
}
125120
});
121+
126122
return false;
127123
}
124+
128125
return true;
129126
});
130127
~~~
131128

132129
## Processing actions in the custom form
133130

134-
When user saves the form, you'll need to manually get form values and update the appropriate task using the public API: [addTask](api/method/addtask.md), [updateTask](api/method/updatetask.md) and [deleteTask](api/method/deletetask.md).
131+
When the user saves the form, you'll need to manually get form values and update the appropriate task using the public API: [`addTask()`](api/method/addtask.md), [`updateTask()`](api/method/updatetask.md), and [`deleteTask()`](api/method/deletetask.md).
135132

136-
Note that when a lightbox is triggered by a new task (on clicking the 'plus' button) which should be deleted if the user clicks 'Cancel' to revert the task creation,
137-
the task object will have the '$new' property set.
133+
Note that when a lightbox is triggered by a new task, for example by clicking the 'plus' button, and should be deleted if the user clicks 'Cancel' to revert task creation, the task object will have the '$new' property set.
138134

139-
You can process lightbox closing, as shown in the example below. The type of an action - 'save', 'cancel' or 'delete' is passed as the "action" parameter:
135+
You can process lightbox closing, as shown in the example below. The action type, "save", "cancel", or "delete", is passed as the "action" parameter:
140136

141137
~~~js
142-
switch(action){
143-
case "save":
144-
task.text = '';// apply values from form
145-
146-
// add new task or update already existing one
147-
if(task.$new){
148-
delete task.$new;
149-
gantt.addTask(task,task.parent)
150-
}else{
151-
gantt.updateTask(id);
152-
}
153-
154-
break;
155-
case "cancel":
156-
// if cancel popup for creating a new task - delete it, otherwise do nothing
157-
if(task.$new)
158-
gantt.deleteTask(id);
159-
break;
160-
case "delete":
161-
gantt.deleteTask(id);
162-
break;
138+
switch (action) {
139+
case "save":
140+
task.text = ""; // apply values from form
141+
142+
// Add a new task or update an existing one.
143+
if (task.$new) {
144+
delete task.$new;
145+
gantt.addTask(task, task.parent);
146+
} else {
147+
gantt.updateTask(id);
148+
}
149+
150+
break;
151+
case "cancel":
152+
// If the user cancels creation of a new task, delete it.
153+
if (task.$new) {
154+
gantt.deleteTask(id);
155+
}
156+
157+
break;
158+
case "delete":
159+
gantt.deleteTask(id);
160+
break;
163161
}
164162
~~~
165-

0 commit comments

Comments
 (0)