main.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. var fetchedMovies;
  2. var searchResultMovies;
  3. var fetchedReviews;
  4. var currentMovieId;
  5. var currentMovieTitle;
  6. var RottenTomatoesAPIKey = "YourKeyHere";
  7. var queryTypeEnum = {"latestMovie" : 0, "movieReview" : 1, "searchMovies" : 2};
  8. var queryType=0;
  9. function init_script(){
  10. GetJSONData("http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=16&page=1&country=us&apikey=" +RottenTomatoesAPIKey);
  11. }
  12. function GetJSONData(url)
  13. {
  14. var jsonDataSource = {
  15. URL: url,
  16. init: function(){
  17. //URL, success callback, failure callback
  18. this.connect(this.URL, this.responseHandler, this.failureHandler);
  19. },
  20. /**
  21. * Handles the response, and displays device data in web app
  22. * @param json
  23. */
  24. responseHandler: function(json) {
  25. var obj = eval('(' + json + ')');
  26. if (queryType == queryTypeEnum.latestMovie)
  27. {
  28. var movies = obj.movies;
  29. fetchedMovies = movies;
  30. var markup = "";
  31. for(i=0; i < movies.length; i++){
  32. markup += this.generateHTMLMarkup(i, movies[i]);
  33. }
  34. document.getElementById("thumbnailPage").innerHTML = markup;
  35. mwl.switchClass('#thumbnailPage', 'hide', 'show');
  36. mwl.switchClass('#loadingPage', 'show', 'hide');
  37. }
  38. else if (queryType== queryTypeEnum.movieReview)
  39. {
  40. var reviews = obj.reviews;
  41. fetchedReviews = reviews;
  42. var markup = "";
  43. markup = "<h1>" + currentMovieTitle + "</h1><br>";
  44. markup += "<h2>Reviews</h2><br>"
  45. for(i=0; i < reviews.length; i++){
  46. markup += "<b>" + reviews[i]['critic'] + "</b> (" + reviews[i]['original_score'] +")<br>"
  47. +"<i>" + reviews[i]['date']+ "</i><br>"
  48. + reviews[i]['quote']+ "<br><br><hr>";
  49. }
  50. markup += "<input type=\"button\" class=\"ui-button\" value=\"Back to Movie\" onclick=\"mwl.switchClass('#reviewPage', 'show', 'hide');mwl.switchClass('#detailsPage', 'hide', 'show');queryType=0;\"/>"
  51. document.getElementById("reviewPage").innerHTML = markup;
  52. mwl.switchClass('#thumbnailPage', 'show', 'hide');
  53. mwl.switchClass('#detailsPage', 'show', 'hide');
  54. mwl.switchClass('#reviewPage', 'hide', 'show');
  55. }
  56. else if (queryType== queryTypeEnum.searchMovies)
  57. {
  58. var movies = obj.movies;
  59. searchResultMovies = movies;
  60. var markup = "";
  61. for(i=0; i < movies.length; i++){
  62. markup += this.generateHTMLMarkup(i, movies[i]);
  63. }
  64. document.getElementById("searchResults").innerHTML = markup;
  65. mwl.switchClass('#searchLoadingPage', 'show', 'hide');
  66. mwl.switchClass('#searchResults', 'hide', 'show');
  67. }
  68. },
  69. /**
  70. * Generates HTML markup to be inserted in to Web App DOM.
  71. * @index i, index of the device
  72. * @param device, device object
  73. */
  74. generateHTMLMarkup: function(i, movie){
  75. var str ="";
  76. if (queryType == queryTypeEnum.latestMovie){
  77. str += "<img width=70px height=100px src=\"" + movie['posters']['thumbnail']+ "\" alt=\""+ movie['id'] +"\" onclick=\"showMovie(alt);\"/>&nbsp;";
  78. }
  79. else if (queryType == queryTypeEnum.searchMovies){
  80. str += "<img width=70px height=100px src=\"" + movie['posters']['thumbnail']+ "\" alt=\""+ movie['id'] +"\" onclick=\"showMovieFromSearch(alt);\"/>&nbsp;";
  81. }
  82. str += "</br>";
  83. return str;
  84. },
  85. failureHandler: function(reason){
  86. document.getElementById("thumbnailPage").innerHTML = "Could not get JSON data.<br>"+ reason;
  87. },
  88. /**
  89. * Retrieves a JSON resource in given URL by using XMLHttpRequest.
  90. * @param url URL of the JSON resource to retrieve
  91. * @param successCb Called, when the JSON resourece is retrieved successfully. Retreived JSON formatted data is passed as argument.
  92. * @param failCb Called, if something goes wrong. Reason in text format, is passed as argument.
  93. */
  94. connect: function(url, successCb, failCb) {
  95. var xmlhttp = new XMLHttpRequest();
  96. xmlhttp.open("GET", url, true);
  97. xmlhttp.setRequestHeader("Accept","application/json");
  98. xmlhttp.setRequestHeader("Cache-Control", "no-cache");
  99. xmlhttp.setRequestHeader("Pragma", "no-cache");
  100. var that = this;
  101. xmlhttp.onreadystatechange= function() {
  102. if (xmlhttp.readyState == 4 ){
  103. if(xmlhttp.status == 200){
  104. if (xmlhttp.responseText != null) {
  105. successCb.call(that, xmlhttp.responseText);
  106. }else{
  107. failCb.call(that, "Empty response.");
  108. }
  109. }else{
  110. failCb.call(that, "Connection failed: Status "+xmlhttp.status);
  111. }
  112. }
  113. };
  114. xmlhttp.send();
  115. }
  116. };
  117. jsonDataSource.init();
  118. }
  119. function showMovie(id) {
  120. mwl.switchClass('#thumbnailPage', 'show', 'hide');
  121. selected_movie = getMovieObject(id);
  122. var details_page_text = "";
  123. details_page_text += "<h2>" + selected_movie['title']+ "</h2>";
  124. details_page_text += "<br/>";
  125. details_page_text += "<img src=\""+ selected_movie['posters']['profile']+ "\"/>";
  126. details_page_text += "<br/><br/>";
  127. details_page_text +="<b>Release date:</b> " + selected_movie['release_dates']['theater'];
  128. details_page_text += "<br/>";
  129. details_page_text += "<br/>";
  130. var cast = selected_movie['abridged_cast'];
  131. var cast_string = "";
  132. for(i=0; i < cast.length; i++){
  133. if (i===cast.length-1)
  134. {
  135. cast_string += cast[i]['name'];
  136. }
  137. else{
  138. cast_string += cast[i]['name'] + ",";
  139. }
  140. }
  141. details_page_text += "<b>Cast: </b><br>" + cast_string;
  142. details_page_text += "<br/>";
  143. details_page_text += "<br/>";
  144. details_page_text += "<b>Synopsis: </b><br>" + selected_movie['synopsis'];
  145. details_page_text += "<br/>";
  146. currentMovieId = selected_movie['id'];
  147. currentMovieTitle =selected_movie['title'];
  148. details_page_text += "<div style=\"text-align:center\"> <input type=\"button\" name=\"reviewButton\" class=\"ui-button\" value=\"Read Reviews\" onclick=\"getMovieReviews();\" />";
  149. details_page_text += "<input type=\"button\" name=\"backButton\" class=\"ui-button\" value=\"Back\" onclick=\"mwl.switchClass('#thumbnailPage', 'hide', 'show');mwl.switchClass('#detailsPage', 'show', 'hide');\" /> </div>";
  150. document.getElementById("detailsPage").innerHTML = details_page_text;
  151. mwl.switchClass('#detailsPage', 'hide', 'show');
  152. }
  153. function showMovieFromSearch(id) {
  154. mwl.switchClass('#searchResults', 'show', 'hide');
  155. selected_movie = getMovieObject(id);
  156. var details_page_text = "";
  157. details_page_text += "<h2>" + selected_movie['title']+ "</h2>";
  158. details_page_text += "<br/>";
  159. details_page_text += "<img src=\""+ selected_movie['posters']['profile']+ "\"/>";
  160. details_page_text += "<br/><br/>";
  161. details_page_text +="<b>Release date:</b> " + selected_movie['release_dates']['theater'];
  162. details_page_text += "<br/>";
  163. details_page_text += "<br/>";
  164. var cast = selected_movie['abridged_cast'];
  165. var cast_string = "";
  166. for(i=0; i < cast.length; i++){
  167. if (i===cast.length-1)
  168. {
  169. cast_string += cast[i]['name'];
  170. }
  171. else{
  172. cast_string += cast[i]['name'] + ",";
  173. }
  174. }
  175. details_page_text += "<b>Cast: </b><br>" + cast_string;
  176. details_page_text += "<br/>";
  177. details_page_text += "<br/>";
  178. details_page_text += "<b>Synopsis: </b><br>" + selected_movie['synopsis'];
  179. details_page_text += "<br/>";
  180. details_page_text += "<div style=\"text-align:center\"> <input type=\"button\" name=\"reviewButton\" class=\"ui-button\" value=\"Read Reviews\" />";
  181. details_page_text += "<input type=\"button\" name=\"backButton\" class=\"ui-button\" value=\"Back\" onclick=\"mwl.switchClass('#searchResults', 'hide', 'show');mwl.switchClass('#searchDetailsPage', 'show', 'hide');\" /> </div>";
  182. document.getElementById("searchDetailsPage").innerHTML = details_page_text;
  183. mwl.switchClass('#searchDetailsPage', 'hide', 'show');
  184. }
  185. function getMovieObject(id) {
  186. var movie_sets;
  187. if (queryType == queryTypeEnum.latestMovie)
  188. {
  189. movie_sets = fetchedMovies;
  190. }
  191. else if (queryType == queryTypeEnum.searchMovies)
  192. {
  193. movie_sets = searchResultMovies;
  194. }
  195. for (var i=0; i<movie_sets.length; i++)
  196. {
  197. if (id === movie_sets[i]['id']){
  198. return movie_sets[i];
  199. }
  200. }
  201. return -1;
  202. }
  203. function searchMovies() {
  204. mwl.switchClass('#searchLoadingPage', 'hide', 'show');
  205. queryType =2;
  206. var search_query = lineEdit.value;
  207. var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=" + search_query +
  208. "&apikey=" +RottenTomatoesAPIKey;
  209. GetJSONData(url);
  210. }
  211. function getMovieReviews() {
  212. ;
  213. var url = "http://api.rottentomatoes.com/api/public/v1.0/movies/" + currentMovieId + "/reviews.json?review_type=all&page_limit=20&page=1&country=us"+ "&apikey=" +RottenTomatoesAPIKey;
  214. queryType =1;
  215. GetJSONData(url);
  216. }