|
20 | 20 | height: 75%; |
21 | 21 | } |
22 | 22 |
|
| 23 | +#ajax-loader-container { |
| 24 | + text-align: center; |
| 25 | + margin-top: 50px; |
| 26 | +} |
23 | 27 | </style> |
24 | 28 |
|
25 | 29 |
|
|
61 | 65 | */ |
62 | 66 | function getRecommendations() { |
63 | 67 | 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 | + |
65 | 75 | google.script.run |
66 | 76 | .withSuccessHandler( |
67 | 77 | function(recommendations, element) { |
| 78 | + |
| 79 | + removeAjaxLoader(); |
| 80 | + var container = $("#eexcess-recommendations-list"); |
68 | 81 | // Parsing the JSON string |
69 | 82 | 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 | + |
76 | 93 | element.disabled = false; |
77 | 94 | }) |
78 | 95 | .withFailureHandler( |
79 | 96 | function(msg, element) { |
| 97 | + removeAjaxLoader(); |
80 | 98 | showError(msg, $('#button-bar')); |
81 | 99 | element.disabled = false; |
82 | 100 | }) |
|
91 | 109 | * @param element The element after which to display the error. |
92 | 110 | */ |
93 | 111 | function showError(msg, element) { |
94 | | - $("#error").remove(); |
| 112 | + removeError(); |
95 | 113 | var div = $('<div id="error" class="error">' + msg + '</div>'); |
96 | 114 | $(element).after(div); |
97 | 115 | } |
| 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 | + } |
98 | 130 | </script> |
0 commit comments