forked from googlearchive/cloud-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.js
More file actions
318 lines (269 loc) · 7.52 KB
/
Copy pathservices.js
File metadata and controls
318 lines (269 loc) · 7.52 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
'use strict';
/* Services */
angular.module('playgroundApp.services', [])
// TODO: test
.factory('$exceptionHandler', function($log, Alert) {
// borrowed from app/lib/angular/angular.js
function formatError(arg) {
if (arg instanceof Error) {
if (arg.stack) {
arg = (arg.message && arg.stack.indexOf(arg.message) === -1) ?
'Error: ' + arg.message + '\n' + arg.stack :
arg.stack;
} else if (arg.sourceURL) {
arg = arg.message + '\n' + arg.sourceURL + ':' + arg.line;
}
}
return arg;
}
return function(exception, cause) {
$log.error.apply($log, arguments);
// borrowed from app/lib/angular/angular.js
var args = [];
angular.forEach(arguments, function(arg) {
args.push(formatError(arg));
});
var msg = args[0];
if (args.length > 1) {
msg += '\ncaused by:\n' + args[1];
}
Alert.error(msg);
};
})
// TODO: test
.factory('CookieFinder', function($q, $log, $window, $location) {
var deferred = $q.defer();
$window.document.cookie = "foo=bar; Path=/";
if ($window.document.cookie) {
deferred.resolve($window.document.cookie);
$window.document.cookie = "foo=bar; Path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT;";
} else {
if ($window.iframed) {
deferred.reject('IFRAMED_NO_COOKIE');
} else {
deferred.reject('NO_BROWSER_COOKIE');
}
}
return deferred.promise
.catch(function(rejection) {
// track and pass on rejection
track('cookie-problem', rejection);
return $q.reject(rejection);
});
})
// TODO: test
.factory('ConfigService', function($http, $log, $q) {
return $http.get('/playground/getconfig')
.then(function(resolved) {
return {
config: resolved.data,
};
});
})
// TODO: test
.factory('ProjectsFactory', function($http, $log, $q) {
var projects = [];
return $http.get('/playground/getprojects')
.then(function(resolved) {
angular.forEach(resolved.data, function(project) {
projects.push(project);
});
return {
projects: projects,
remove: function(project) {
for (var i in projects) {
if (projects[i] == project) {
projects.splice(i, 1);
break;
}
}
$http.post('/playground/p/' + encodeURI(project.key) + '/delete')
.catch(function(rejection) {
// TODO: handle failure
$log.log('error deleting project:', rejection);
});
},
};
});
})
// TODO: test
.factory('Alert', function() {
var alert_list = [];
var Alert = {
alert_list: alert_list,
clear: function() {
alert_list = [];
},
handle_exception: function(exception, cause) {
var msg = '' + exception;
if (cause) {
msg += ' caused by ' + cause;
}
track('alert', 'handle-exception', msg);
alert_list.push({type: 'error', icon: 'icon-exclamation-sign', msg: msg});
},
note: function(msg) {
alert_list.push({icon: 'icon-hand-right', msg: msg});
},
info: function(msg) {
alert_list.push({type: 'info', icon: 'icon-info-sign', msg: msg});
},
success: function(msg) {
alert_list.push({type: 'success', icon: 'icon-ok', msg: msg});
},
error: function(msg) {
track('alert', 'error', msg);
alert_list.push({type: 'error', icon: 'icon-exclamation-sign', msg: msg});
},
alerts: function() {
return alert_list;
},
remove_alert: function(idx) {
alert_list.splice(idx, 1);
},
};
return Alert;
})
// TODO: improve upon flushDoSerial(); allow one step to be executed at a time
.factory('DoSerial', function($timeout, $log, $exceptionHandler, Alert) {
var work_items = [];
var pending_promise;
var on_promised_satisfied_success = function() {
pending_promise = undefined;
maybe_next();
};
var on_promised_satisfied_error = function(rejection) {
Alert.error('Execution step failed\n:' + angular.toJson(rejection));
pending_promise = undefined;
maybe_next();
};
var maybe_next = function() {
if (pending_promise) return;
if (!work_items.length) return;
var result;
try {
result = work_items.shift()();
} catch (err) {
$exceptionHandler(err);
}
if (result && result.then) {
pending_promise = result.then(on_promised_satisfied_success,
on_promised_satisfied_error);
} else {
maybe_next();
}
};
// TODO: rename to 'Queue'
var DoSerial = {
// yield execution until next tick from the event loop
tick: function() {
return this.then(function() {
return $timeout(angular.noop);
});
},
// schedule action to perform next
then: function(func) {
work_items.push(func);
maybe_next();
return DoSerial;
}
};
return DoSerial;
})
.factory('pgHttpInterceptor', function($q, $log, $window, Alert) {
return {
'request': function(config) {
return config || $q.when(config);
},
'requestError': function(response) {
return $q.reject(response);
},
'response': function(response) {
return response || $q.when(response);
},
'responseError': function(response) {
if (response.headers('X-Cloud-Playground-Error')) {
if (response.status == 401) {
// can occur if XRF cookie is deleted, but session cookie is still present
Alert.error('UNAUTHORIZED. Please clear your browser cookies and try again.');
}
}
return $q.reject(response);
}
};
})
// TODO: if want to focus() element create Focus service
.factory('DomElementById', function($window) {
return function(id) {
return $window.document.getElementById(id);
};
})
// TODO: move output iframe into directive with a template.html
// TODO: get rid of other uses
.factory('WrappedElementById', function(DomElementById) {
return function(id) {
return angular.element(DomElementById(id));
};
})
// TODO: test
// TODO: DETERMINE if there's a better way
.factory('Backoff', function($timeout) {
// Exponential backoff service.
var INIITAL_BACKOFF_MS = 1000;
var backoff_ms;
var timer = undefined;
var Backoff = {
reset: function() {
backoff_ms = INIITAL_BACKOFF_MS;
},
backoff: function() {
backoff_ms = Math.min(120 * 1000, (backoff_ms || 1000) * 2);
return backoff_ms;
},
schedule: function(func) {
if (timer) {
return;
}
timer = $timeout(function() {
timer = undefined;
func();
}, backoff_ms);
},
};
Backoff.reset();
return Backoff;
})
.factory('WindowService', function($window) {
var WindowService = {
'go': function(url) {
// avoid $window.location which assumes paths are routes
window.location = url;
},
'reload': function() {
// TODO: don't access 'document' directly
document.documentElement.scrollTop = 0;
$window.location.reload();
},
'open': function(url, name, specs, replace) {
$window.open(url, name, specs, replace);
},
};
return WindowService;
})
// Prompt service. Used only for prompts that do not require text input.
// TODO: test
.factory('ConfirmDialog', function($dialog) {
return function(title, msg, okButtonText, okButtonClass, callback) {
// TODO: autofocus primary button
var btns = [{result: false, label: 'Cancel'},
{result: true, label: okButtonText,
cssClass: okButtonClass}];
$dialog.messageBox(title, msg, btns)
.open()
.then(function(result) {
if (result) {
callback();
}
});
}
});