forked from googleworkspace/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.html
127 lines (116 loc) · 3.4 KB
/
javascript.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!-- Load the jQuery and jQuery UI libraries. -->
<script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.min.js"></script>
<!-- Custom client-side JavaScript code. -->
<script>
// When the page loads.
$(function() {
$('#tasklist').bind('change', loadTasks);
$('#new-task').bind('submit', onNewTaskFormSubmit);
loadTaskLists();
});
/**
* Load the available task lists.
*/
function loadTaskLists() {
google.script.run.withSuccessHandler(showTaskLists)
.withFailureHandler(showError)
.getTaskLists();
}
/**
* Show the returned task lists in the dropdown box.
* @param {Array.<Object>} taskLists The task lists to show.
*/
function showTaskLists(taskLists) {
var select = $('#tasklist');
select.empty();
taskLists.forEach(function(taskList) {
var option = $('<option>')
.attr('value', taskList.id)
.text(taskList.name);
select.append(option);
});
loadTasks();
}
/**
* Load the tasks in the currently selected task list.
*/
function loadTasks() {
$('#tasks').text('Loading...');
var taskListId = $('#tasklist').val();
google.script.run.withSuccessHandler(showTasks)
.withFailureHandler(showError)
.getTasks(taskListId);
}
/**
* Show the returned tasks on the page.
* @param {Array.<Object>} tasks The tasks to show.
*/
function showTasks(tasks) {
var list = $('#tasks').empty();
if (tasks.length > 0) {
tasks.forEach(function(task) {
var item = $('<li>');
var checkbox = $('<input type="checkbox">')
.data('taskId', task.id)
.bind('change', onCheckBoxChange);
item.append(checkbox);
var title = $('<span>')
.text(task.title);
item.append(title);
if (task.completed) {
checkbox.prop('checked', true);
title.css('text-decoration', 'line-through')
}
list.append(item);
});
} else {
list.text('No tasks');
}
}
/**
* A callback function that runs when a task is checked or unchecked.
*/
function onCheckBoxChange() {
var checkbox = $(this);
var title = checkbox.siblings('span');
var isChecked = checkbox.prop('checked');
var taskListId = $('#tasklist').val();
var taskId = checkbox.data('taskId');
if (isChecked) {
title.css('text-decoration', 'line-through');
} else {
title.css('text-decoration', 'none');
}
google.script.run.withSuccessHandler(function() {
title.effect("highlight", {
duration: 1500
});
}).withFailureHandler(showError)
.setCompleted(taskListId, taskId, isChecked);
}
/**
* A callback function that runs when the new task form is submitted.
*/
function onNewTaskFormSubmit() {
var taskListId = $('#tasklist').val();
var titleTextBox = $('#task-title');
var title = titleTextBox.val();
google.script.run.withSuccessHandler(function() {
titleTextBox.val('');
titleTextBox.effect("highlight", {
duration: 1500
});
loadTasks();
}).withFailureHandler(showError)
.addTask(taskListId, title);
return false;
}
/**
* Logs an error message and shows an alert to the user.
*/
function showError(error) {
console.log(error);
window.alert('An error has occurred, please try again.');
}
</script>