forked from ento/asana-charts-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_model.js
192 lines (177 loc) · 5.26 KB
/
server_model.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
/**
* Library of functions for the "server" portion of an extension, which is
* loaded into the background and popup pages.
*
* Some of these functions are asynchronous, because they may have to talk
* to the Asana API to get results.
*/
Asana.ServerModel = {
_cached_user: null,
/**
* Called by the model whenever a request is made and error occurs.
* Override to handle in a context-appropriate way. Some requests may
* also take an `errback` parameter which will handle errors with
* that particular request.
*
* @param response {dict} Response from the server.
*/
onError: function(response) {},
/**
* Requests the user's preferences for the extension.
*
* @param callback {Function(options)} Callback on completion.
* workspaces {dict[]} See Asana.Options for details.
*/
options: function(callback) {
callback(Asana.Options.loadOptions());
},
/**
* Determine if the user is logged in.
*
* @param callback {Function(is_logged_in)} Called when request complete.
* is_logged_in {Boolean} True iff the user is logged in to Asana.
*/
isLoggedIn: function(callback) {
chrome.cookies.get({
url: Asana.ApiBridge.baseApiUrl(),
name: 'ticket'
}, function(cookie) {
callback(!!(cookie && cookie.value));
});
},
/**
* Get the URL of a task given some of its data.
*
* @param task {dict}
* @param callback {Function(url)}
*/
taskViewUrl: function(task, callback) {
// We don't know what pot to view it in so we just use the task ID
// and Asana will choose a suitable default.
var options = Asana.Options.loadOptions();
var pot_id = task.id;
var url = 'https://' + options.asana_host_port + '/0/' + pot_id + '/' + task.id;
callback(url);
},
/**
* Requests the set of workspaces the logged-in user is in.
*
* @param callback {Function(workspaces)} Callback on success.
* workspaces {dict[]}
*/
workspaces: function(callback, errback) {
var self = this;
Asana.ApiBridge.request("GET", "/workspaces", {},
function(response) {
self._makeCallback(response, callback, errback);
});
},
/**
* Requests the set of users in a workspace.
*
* @param callback {Function(users)} Callback on success.
* users {dict[]}
*/
users: function(workspace_id, callback) {
var self = this;
Asana.ApiBridge.request("GET", "/workspaces/" + workspace_id + "/users", {},
function(response) {
self._makeCallback(response, callback);
});
},
/**
* Requests the set of unarchived tasks in a workspace.
*
* @param callback {Function(tasks)} Callback on success.
* tasks {dict[]}
*/
workspaceTasks: function(workspace_id, callback) {
var self = this;
Asana.ApiBridge.request("GET", "/workspaces/" + workspace_id + "/tasks", {},
function(response) {
self._makeCallback(response, callback);
});
},
/**
* Requests the set of projects in a workspace.
*
* @param callback {Function(projects)} Callback on success.
* projects {dict[]}
*/
projects: function(workspace_id, callback) {
var self = this;
Asana.ApiBridge.request("GET", "/workspaces/" + workspace_id + "/projects", {},
function(response) {
self._makeCallback(response, callback);
});
},
/**
* Requests the set of tasks in a project.
*
* @param callback {Function(tasks)} Callback on success.
* tasks {dict[]}
*/
projectTasks: function(project_id, callback) {
var self = this;
Asana.ApiBridge.request("GET", "/projects/" + project_id + "/tasks", {},
function(response) {
self._makeCallback(response, callback);
});
},
/**
* Requests the task record for the given id.
*
* @param callback {Function(task)} Callback on success.
* task {dict}
*/
task: function(task_id, callback) {
var self = this;
return Asana.ApiBridge.request("GET", "/tasks/" + task_id, {},
function(response) {
self._makeCallback(response, callback);
});
},
/**
* Requests the user record for the logged-in user.
*
* @param callback {Function(user)} Callback on success.
* user {dict[]}
*/
me: function(callback) {
var self = this;
if (self._cached_user !== null) {
callback(self._cached_user);
} else {
Asana.ApiBridge.request("GET", "/users/me", {},
function(response) {
if (!response.errors) {
self._cached_user = response.data;
}
self._makeCallback(response, callback);
});
}
},
/**
* Makes an Asana API request to add a task in the system.
*
* @param task {dict} Task fields.
* @param callback {Function(response)} Callback on success.
*/
createTask: function(workspace_id, task, callback, errback) {
var self = this;
Asana.ApiBridge.request(
"POST",
"/workspaces/" + workspace_id + "/tasks",
task,
function(response) {
self._makeCallback(response, callback, errback);
});
},
_makeCallback: function(response, callback, errback) {
if (response.errors) {
(errback || this.onError).call(null, response);
} else {
callback(response.data);
}
}
};