-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
223 lines (206 loc) · 6.92 KB
/
popup.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
window.addEventListener('load', function() {
// Our default error handler.
Asana.ServerModel.onError = function(response) {
showError(response.errors[0].message);
};
// Ah, the joys of asynchronous programming.
// To initialize, we've got to gather various bits of information.
// Starting with a reference to the window and tab that were active when
// the popup was opened ...
chrome.windows.getCurrent(function(w) {
chrome.tabs.query({
active: true,
windowId: w.id
}, function(tabs) {
// Now load our options ...
Asana.ServerModel.options(function(options) {
// And ensure the user is logged in ...
Asana.ServerModel.isLoggedIn(function(is_logged_in) {
if (is_logged_in) {
if (window.quick_add_request) {
// If this was a QuickAdd request (set by the code popping up
// the window in Asana.ExtensionServer), then we have all the
// info we need and should show the add UI right away.
showAddUi(
quick_add_request.url, quick_add_request.title,
quick_add_request.selected_text, options);
} else {
// Otherwise we want to get the selection from the tab that
// was active when we were opened. So we set up a listener
// to listen for the selection send event from the content
// window ...
var selection = "";
var listener = function(request, sender, sendResponse) {
if (request.type === "selection") {
chrome.extension.onRequest.removeListener(listener);
console.info("Asana popup got selection");
selection = "\n" + request.value;
}
};
chrome.extension.onRequest.addListener(listener);
// ... and then we make a request to the content window to
// send us the selection.
var tab = tabs[0];
chrome.tabs.executeScript(tab.id, {
code: "(Asana && Asana.SelectionClient) ? Asana.SelectionClient.sendSelection() : 0"
}, function() {
// The requests appear to be handled synchronously, so the
// selection should have been sent by the time we get this
// completion callback. If the timing ever changes, however,
// that could break and we would never show the add UI.
// So this could be made more robust.
showAddUi(tab.url, tab.title, selection, options);
});
}
} else {
// The user is not even logged in. Prompt them to do so!
showLogin(Asana.Options.loginUrl(options));
}
});
});
});
});
});
// Helper to show a named view.
var showView = function(name) {
["login", "add", "success"].forEach(function(view_name) {
$("#" + view_name + "_view").css("display", view_name === name ? "" : "none");
});
};
// Show the add UI
var showAddUi = function(url, title, selected_text, options) {
var self = this;
showView("add");
$("#notes").val(url + selected_text);
$("#name").val(title);
$("#name").focus();
$("#name").select();
Asana.ServerModel.me(function(user) {
// Just to cache result.
Asana.ServerModel.workspaces(function(workspaces) {
$("#workspace").html("");
workspaces.forEach(function(workspace) {
$("#workspace").append(
"<option value='" + workspace.id + "'>" + workspace.name + "</option>");
});
$("#workspace").val(options.default_workspace_id);
onWorkspaceChanged();
$("#workspace").change(onWorkspaceChanged);
});
});
};
// Enable/disable the add button.
var setAddEnabled = function(enabled) {
var button = $("#add_button");
if (enabled) {
button.removeClass("disabled");
button.addClass("enabled");
button.click(function() {
createTask();
return false;
});
button.keydown(function(e) {
if (e.keyCode === 13) {
createTask();
}
});
} else {
button.removeClass("enabled");
button.addClass("disabled");
button.unbind('click');
button.unbind('keydown');
}
};
// Set the add button as being "working", waiting for the Asana request
// to complete.
var setAddWorking = function(working) {
setAddEnabled(!working);
$("#add_button").find(".button-text").text(
working ? "Adding..." : "Add to Asana");
};
// When the user changes the workspace, update the list of users.
var onWorkspaceChanged = function() {
var workspace_id = readWorkspaceId();
$("#assignee").html("<option>Loading...</option>");
setAddEnabled(false);
Asana.ServerModel.users(workspace_id, function(users) {
$("#assignee").html("");
users = users.sort(function(a, b) {
return (a.name < b.name) ? -1 : ((a.name > b.name) ? 1 : 0);
});
users.forEach(function(user) {
$("#assignee").append(
"<option value='" + user.id + "'>" + user.name + "</option>");
});
Asana.ServerModel.me(function(user) {
$("#assignee").val(user.id);
});
setAddEnabled(true);
});
};
var readAssignee = function() {
return $("#assignee").val();
};
var readWorkspaceId = function() {
return $("#workspace").val();
};
var createTask = function() {
console.info("Creating task");
hideError();
setAddWorking(true);
Asana.ServerModel.createTask(
readWorkspaceId(),
{
name: $("#name").val(),
notes: $("#notes").val(),
assignee: readAssignee()
},
function(task) {
setAddWorking(false);
showSuccess(task);
},
function(response) {
setAddWorking(false);
showError(response.errors[0].message);
});
};
var showError = function(message) {
console.log("Error: " + message);
$("#error").css("display", "");
};
var hideError = function() {
$("#error").css("display", "none");
};
// Helper to show a success message after a task is added.
var showSuccess = function(task) {
Asana.ServerModel.taskViewUrl(task, function(url) {
var name = task.name.replace(/^\s*/, "").replace(/\s*$/, "");
$("#new_task_link").attr("href", url);
$("#new_task_link").text(name !== "" ? name : "unnamed task");
$("#new_task_link").unbind("click");
$("#new_task_link").click(function() {
chrome.tabs.create({url: url});
window.close();
return false;
});
showView("success");
});
};
// Helper to show the login page.
var showLogin = function(url) {
$("#login_link").attr("href", url);
$("#login_link").unbind("click");
$("#login_link").click(function() {
chrome.tabs.create({url: url});
window.close();
return false;
});
showView("login");
};
// Close the popup if the ESCAPE key is pressed.
window.addEventListener("keydown", function(e) {
if (e.keyCode === 27) {
window.close();
}
}, /*capture=*/false);
$("#close-banner").click(function() { window.close(); });