Skip to content

Commit 872a362

Browse files
author
Andreas Eisenkolb
committed
added ajax loader and improved error handling
1 parent ed3da43 commit 872a362

2 files changed

Lines changed: 65 additions & 15 deletions

File tree

Code.gs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/* Copyright 2014 University of Passau
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
115
/**
216
* Creates a menu entry in the Google Docs UI when the document is opened.
317
*
@@ -100,7 +114,9 @@ function getRecommendations() {
100114
}
101115
}
102116
// privacy proxy URL
103-
var url = "http://eexcess-dev.joanneum.at/eexcess-privacy-proxy/api/v1/recommend";
117+
var url = "http://eexcess.joanneum.at/eexcess-privacy-proxy/api/v1/recommend";
118+
// federated recommender
119+
//var url = "http://eexcess.joanneum.at/eexcess-federated-recommender-web-service-1.0-SNAPSHOT/recommender/recommend";
104120

105121
// POST payload
106122
var data = { "numResults" : 60, "contextKeywords" : [] };
@@ -111,19 +127,21 @@ function getRecommendations() {
111127
data["contextKeywords"].push({ "weight" : 1.0 / terms.length, "text" : terms[i] });
112128
}
113129

114-
Logger.log(data);
115-
116130
// Options object, that specifies the method, content type and payload of the HTTPRequest
117131
var options = {
118132
"method" : "POST",
119133
"contentType" : "application/json",
134+
"origin" : "gdocs",
135+
"headers" : {
136+
"Accept" : "application/json"
137+
},
120138
"payload" : JSON.stringify(data)
121139
};
122-
140+
123141
try {
124-
return UrlFetchApp.fetch(url, options).getContentText();
142+
var response = UrlFetchApp.fetch(url, options);
143+
return response.getContentText();
125144
} catch(err) {
126-
145+
throw err;
127146
}
128147
}
129-

Sidedbar.html

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
height: 75%;
2121
}
2222

23+
#ajax-loader-container {
24+
text-align: center;
25+
margin-top: 50px;
26+
}
2327
</style>
2428

2529

@@ -61,22 +65,36 @@
6165
*/
6266
function getRecommendations() {
6367
this.disabled = true;
64-
68+
// Clear previously loaded recommendations
69+
$("#eexcess-recommendations-list").empty();
70+
// Remove error messages
71+
removeError();
72+
// Insert Ajax loader icon
73+
$('<div id="ajax-loader-container"><img src="http://mics.fim.uni-passau.de/wp-content/uploads/2014/10/ajax-loader.gif" width="16px" height="16px" alt="ajax loader"/></div>').insertAfter("#eexcess-recommendations-list");
74+
6575
google.script.run
6676
.withSuccessHandler(
6777
function(recommendations, element) {
78+
79+
removeAjaxLoader();
80+
var container = $("#eexcess-recommendations-list");
6881
// Parsing the JSON string
6982
var o = JSON.parse(recommendations);
70-
var container = $("#eexcess-recommendations-list");
71-
// iterate the results and append the list elements
72-
$.each(o.result, function() {
73-
container.append("<li><a href='" + this.uri + "' target='_blank'>" + this.title + "</a></li>");
74-
})
75-
83+
84+
if(o.totalResults > 0) {
85+
// iterate the results and append the list elements
86+
$.each(o.result, function() {
87+
container.append("<li><a href='" + this.uri + "' target='_blank'>" + this.title + "</a></li>");
88+
})
89+
} else {
90+
showError("No results found", $('#button-bar'));
91+
}
92+
7693
element.disabled = false;
7794
})
7895
.withFailureHandler(
7996
function(msg, element) {
97+
removeAjaxLoader();
8098
showError(msg, $('#button-bar'));
8199
element.disabled = false;
82100
})
@@ -91,8 +109,22 @@
91109
* @param element The element after which to display the error.
92110
*/
93111
function showError(msg, element) {
94-
$("#error").remove();
112+
removeError();
95113
var div = $('<div id="error" class="error">' + msg + '</div>');
96114
$(element).after(div);
97115
}
116+
117+
/**
118+
* Removes previously added error messages.
119+
*/
120+
function removeError() {
121+
$("#error").remove();
122+
}
123+
124+
/**
125+
* Removes the ajax loader icon (spinner) from the DOM.
126+
*/
127+
function removeAjaxLoader() {
128+
$('#ajax-loader-container').remove();
129+
}
98130
</script>

0 commit comments

Comments
 (0)