-
Notifications
You must be signed in to change notification settings - Fork 2
/
js.html
113 lines (91 loc) · 2.75 KB
/
js.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
<script>
/*globals google, accounts, Browser, reports */
function UserException(message) {
this.messsage = message;
this.name = 'UserException';
}
function appendOptionsToSelect(array, selector) {
var select = $(selector);
var isAccount = selector === '#account';
select.empty();
if (!array.length) {
if (isAccount) {
throw new UserException('We couldn\'t find any accounts. Please log in with another account.');
}
throw new UserException('We we\'re unable to locate the report list. Please try again.');
}
array.forEach(function (element, index) {
var setValue = isAccount ? index : element.id;
var option = $('<option>')
.attr('value', setValue)
.text(element.name);
select.append(option);
});
}
var audit = {
flag: false,
spinner: $('.spinner'),
form: $('.sidebar'),
init: function () {
this.showAccounts(accounts);
this.showReports(reports);
$('#submit').on('click', $.proxy(audit.submit, audit));
$('#close').on('click', $.proxy(audit.close, audit));
},
showAccounts: function (accounts) {
try {
appendOptionsToSelect(accounts, '#account');
} catch (error) {
this.error(error);
}
},
showReports: function (reports) {
try {
appendOptionsToSelect(reports, '#report');
} catch (error) {
this.error(error);
}
},
submit: function (e) {
var data = JSON.stringify({
ids: accounts[$('#account').val()],
report: $('#report').val()
});
e.preventDefault();
this.form.hide();
this.spinner.show();
google.script.run
.withSuccessHandler(this.success.bind(this))
.withFailureHandler(this.error.bind(this))
.saveReportDataFromSidebar(data);
},
success: function () {
this.close();
},
error: function (error) {
if (error.message === 'Quota Error: User Rate Limit Exceeded.') {
return this.errorHandleUserRateLimit();
}
this.close();
window.alert(error);
},
errorHandleUserRateLimit: function () {
if (!this.flag) {
this.flag = true;
window.alert('It looks like you\'ve you hit the user rate limit. ' +
'We\'ll continue to ask for the data, but feel free to cancel ' +
'the request and try again later.');
}
return setTimeout(function () {
$('#submit').trigger('click');
}, 1000);
},
close: function () {
google.script.host.close();
}
};
$(function () {
// onReady - initialized
audit.init();
});
</script>