video_browser.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Zend Framework
  3. *
  4. * LICENSE
  5. *
  6. * This source file is subject to the new BSD license that is bundled
  7. * with this package in the file LICENSE.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://framework.zend.com/license/new-bsd
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@zend.com so we can send you a copy immediately.
  13. *
  14. * @category Zend
  15. * @package Zend_Gdata
  16. * @subpackage Demos
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @fileoverview Provides functions for browsing and searching YouTube
  22. * data API feeds using a PHP backend powered by the Zend_Gdata component
  23. * of the Zend Framework.
  24. */
  25. /**
  26. * provides namespacing for the YouTube Video Browser PHP version (ytvbp)
  27. */
  28. var ytvbp = {};
  29. /**
  30. * maximum number of results to return for list of videos
  31. * @type Number
  32. */
  33. ytvbp.MAX_RESULTS_LIST = 5;
  34. /**
  35. * navigation button id used to page to the previous page of
  36. * results in the list of videos
  37. * @type String
  38. */
  39. ytvbp.PREVIOUS_PAGE_BUTTON = 'previousPageButton';
  40. /**
  41. * navigation button id used to page to the next page of
  42. * results in the list of videos
  43. * @type String
  44. */
  45. ytvbp.NEXT_PAGE_BUTTON = 'nextPageButton';
  46. /**
  47. * container div id used to hold list of videos
  48. * @type String
  49. */
  50. ytvbp.VIDEO_LIST_CONTAINER_DIV = 'searchResultsVideoList';
  51. /**
  52. * container div id used to hold the video player
  53. * @type String
  54. */
  55. ytvbp.VIDEO_PLAYER_DIV = 'videoPlayer';
  56. /**
  57. * container div id used to hold the search box which displays when the page
  58. * first loads
  59. * @type String
  60. */
  61. ytvbp.MAIN_SEARCH_CONTAINER_DIV = 'mainSearchBox';
  62. /**
  63. * container div id used to hold the search box displayed at the top of
  64. * the browser after one search has already been performed
  65. * @type String
  66. */
  67. ytvbp.TOP_SEARCH_CONTAINER_DIV = 'searchBox';
  68. /**
  69. * the page number to use for the next page navigation button
  70. * @type Number
  71. */
  72. ytvbp.nextPage = 2;
  73. /**
  74. * the page number to use for the previous page navigation button
  75. * @type Number
  76. */
  77. ytvbp.previousPage = 0;
  78. /**
  79. * the last search term used to query - allows for the navigation
  80. * buttons to know what string query to perform when clicked
  81. * @type String
  82. */
  83. ytvbp.previousSearchTerm = '';
  84. /**
  85. * the last query type used for querying - allows for the navigation
  86. * buttons to know what type of query to perform when clicked
  87. * @type String
  88. */
  89. ytvbp.previousQueryType = 'all';
  90. /**
  91. * Retrieves a list of videos matching the provided criteria. The list of
  92. * videos can be restricted to a particular standard feed or search criteria.
  93. * @param {String} queryType The type of query to be done - either 'all'
  94. * for querying all videos, or the name of a standard feed.
  95. * @param {String} searchTerm The search term(s) to use for querying as the
  96. * 'vq' query parameter value
  97. * @param {Number} page The 1-based page of results to return.
  98. */
  99. ytvbp.listVideos = function(queryType, searchTerm, page) {
  100. ytvbp.previousSearchTerm = searchTerm;
  101. ytvbp.previousQueryType = queryType;
  102. var maxResults = ytvbp.MAX_RESULTS_LIST;
  103. var startIndex = (((page - 1) * ytvbp.MAX_RESULTS_LIST) + 1);
  104. ytvbp.presentFeed(queryType, maxResults, startIndex, searchTerm);
  105. ytvbp.updateNavigation(page);
  106. };
  107. /**
  108. * Sends an AJAX request to the server to retrieve a list of videos or
  109. * the video player/metadata. Sends the request to the specified filePath
  110. * on the same host, passing the specified params, and filling the specified
  111. * resultDivName with the resutls upon success.
  112. * @param {String} filePath The path to which the request should be sent
  113. * @param {String} params The URL encoded POST params
  114. * @param {String} resultDivName The name of the DIV used to hold the results
  115. */
  116. ytvbp.sendRequest = function(filePath, params, resultDivName) {
  117. if (window.XMLHttpRequest) {
  118. var xmlhr = new XMLHttpRequest();
  119. } else {
  120. var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  121. }
  122. xmlhr.open('POST', filePath, true);
  123. xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  124. xmlhr.onreadystatechange = function() {
  125. var resultDiv = document.getElementById(resultDivName);
  126. if (xmlhr.readyState == 1) {
  127. resultDiv.innerHTML = '<b>Loading...</b>';
  128. } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
  129. if (xmlhr.responseText) {
  130. resultDiv.innerHTML = xmlhr.responseText;
  131. }
  132. } else if (xmlhr.readyState == 4) {
  133. alert('Invalid response received - Status: ' + xmlhr.status);
  134. }
  135. }
  136. xmlhr.send(params);
  137. }
  138. /**
  139. * Uses ytvbp.sendRequest to display a YT video player and metadata for the
  140. * specified video ID.
  141. * @param {String} videoId The ID of the YouTube video to show
  142. */
  143. ytvbp.presentVideo = function(videoId) {
  144. var params = 'queryType=show_video&videoId=' + videoId;
  145. var filePath = 'index.php';
  146. ytvbp.sendRequest(filePath, params, ytvbp.VIDEO_PLAYER_DIV);
  147. }
  148. /**
  149. * Uses ytvbp.sendRequest to display a list of of YT videos.
  150. * @param {String} queryType The name of a standard video feed or 'all'
  151. * @param {Number} maxResults The maximum number of videos to list
  152. * @param {Number} startIndex The first video to include in the list
  153. * @param {String} searchTerm The search terms to pass to the specified feed
  154. */
  155. ytvbp.presentFeed = function(queryType, maxResults, startIndex, searchTerm){
  156. var params = 'queryType=' + queryType +
  157. '&maxResults=' + maxResults +
  158. '&startIndex=' + startIndex +
  159. '&searchTerm=' + searchTerm;
  160. var filePath = 'index.php';
  161. ytvbp.sendRequest(filePath, params, ytvbp.VIDEO_LIST_CONTAINER_DIV);
  162. }
  163. /**
  164. * Updates the variables used by the navigation buttons and the 'enabled'
  165. * status of the buttons based upon the current page number passed in.
  166. * @param {Number} page The current page number
  167. */
  168. ytvbp.updateNavigation = function(page) {
  169. ytvbp.nextPage = page + 1;
  170. ytvbp.previousPage = page - 1;
  171. document.getElementById(ytvbp.NEXT_PAGE_BUTTON).style.display = 'inline';
  172. document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).style.display = 'inline';
  173. if (ytvbp.previousPage < 1) {
  174. document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).disabled = true;
  175. } else {
  176. document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).disabled = false;
  177. }
  178. document.getElementById(ytvbp.NEXT_PAGE_BUTTON).disabled = false;
  179. };
  180. /**
  181. * Hides the main (large) search form and enables one that's in the
  182. * title bar of the application. The main search form is only used
  183. * for the first load. Subsequent searches should use the version in
  184. * the title bar.
  185. */
  186. ytvbp.hideMainSearch = function() {
  187. document.getElementById(ytvbp.MAIN_SEARCH_CONTAINER_DIV).style.display =
  188. 'none';
  189. document.getElementById(ytvbp.TOP_SEARCH_CONTAINER_DIV).style.display =
  190. 'inline';
  191. };
  192. /**
  193. * Method called when the query type has been changed. Clears out the
  194. * value of the search term input box by default if one of the standard
  195. * feeds is selected. This is to improve usability, as many of the standard
  196. * feeds may not include results for even fairly popular search terms.
  197. * @param {String} queryType The type of query being done - either 'all'
  198. * for querying all videos, or the name of one of the standard feeds.
  199. * @param {Node} searchTermInputElement The HTML input element for the input
  200. * element.
  201. */
  202. ytvbp.queryTypeChanged = function(queryType, searchTermInputElement) {
  203. if (queryType != 'all') {
  204. searchTermInputElement.value = '';
  205. }
  206. };