My static website generator using poole https://www.xythobuz.de
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

youtube.source.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*!
  2. * Yox YouTube plugin
  3. * http://yoxigen.com/yoxview/
  4. *
  5. * Copyright (c) 2010 Yossi Kolesnicov
  6. *
  7. * Licensed under the MIT license.
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * Date: 13th November, 2010
  11. * Version : 1.0
  12. */
  13. function yox_youtube()
  14. {
  15. var $ = jQuery,
  16. youtubeRegex = /^http:\/\/(?:www\.)?youtube.com\//,
  17. self = this,
  18. ytRegex = {
  19. singleVideo: /^http:\/\/(?:www\.)?youtube.com\/watch\?v=([^\&]+)(.*)?/,
  20. playlist: /^http:\/\/(?:www\.)?youtube.com\/(?:view_play_list|my_playlists)\?p=([^\&]+)(.*)?/,
  21. user: /^http:\/\/(?:www\.)?youtube.com\/user\/([^\?]+)(?:\?(.*))?/,
  22. search: /^http:\/\/(?:www\.)?youtube.com\/results\?(.*)/
  23. };
  24. this.getImagesData = function(options, callback)
  25. {
  26. var defaults = {
  27. url: "http://gdata.youtube.com/feeds/api/videos",
  28. setThumbnails: true,
  29. setSingleAlbumThumbnails: true,
  30. alt: 'jsonc',
  31. thumbsize: 64,
  32. v: 2,
  33. format: 5,
  34. hqThumbnails: false,
  35. aspectRatio: "auto"
  36. },
  37. isSingleVideo = false,
  38. datasourceOptions = jQuery.extend({}, defaults, options.dataSourceOptions);
  39. function getDimensionsCalc(){
  40. var widescreenDimensions,
  41. defaultDimensions,
  42. widescreenAspectRatio = 16/9,
  43. defaultIsWidescreen = false;
  44. if (!datasourceOptions.width && !datasourceOptions.height)
  45. datasourceOptions.width = 720;
  46. if ((datasourceOptions.height && !datasourceOptions.width) || (datasourceOptions.width && !datasourceOptions.height)){
  47. if (typeof(datasourceOptions.aspectRatio) === "string"){
  48. if (datasourceOptions.aspectRatio === "auto")
  49. datasourceOptions.aspectRatio = 4/3;
  50. else{
  51. var ratioValues = datasourceOptions.aspectRatio.split(":");
  52. datasourceOptions.aspectRatio = parseInt(ratioValues[0], 10) / parseInt(ratioValues[1], 10);
  53. }
  54. }
  55. defaultIsWidescreen = datasourceOptions.aspectRatio === 16/9;
  56. if (datasourceOptions.height){
  57. widescreenDimensions = { height: datasourceOptions.height, width: datasourceOptions.height * widescreenAspectRatio };
  58. if (!defaultIsWidescreen)
  59. defaultDimensions = { height: datasourceOptions.height, width: datasourceOptions.height * datasourceOptions.aspectRatio };
  60. }
  61. else{
  62. widescreenDimensions = { width: datasourceOptions.width, height: datasourceOptions.width / widescreenAspectRatio };
  63. if (!defaultIsWidescreen)
  64. defaultDimensions = { width: datasourceOptions.width, height: datasourceOptions.width / datasourceOptions.aspectRatio };
  65. }
  66. }
  67. var getDimensions = function(isWidescreen){
  68. return isWidescreen ? widescreenDimensions : defaultDimensions;
  69. }
  70. return getDimensions;
  71. }
  72. var feedType;
  73. if (options.dataUrl)
  74. {
  75. var urlMatch;
  76. for (regexType in ytRegex){
  77. urlMatch = options.dataUrl.match(ytRegex[regexType]);
  78. if (urlMatch)
  79. {
  80. feedType = regexType;
  81. break;
  82. }
  83. }
  84. if (urlMatch){
  85. switch(feedType){
  86. case "singleVideo":
  87. isSingleVideo = true;
  88. datasourceOptions.url += "/" + urlMatch[1];
  89. break;
  90. case "playlist":
  91. datasourceOptions.url = "http://gdata.youtube.com/feeds/api/playlists/" + urlMatch[1];
  92. break;
  93. case "user":
  94. datasourceOptions.url = "http://gdata.youtube.com/feeds/api/users/" + urlMatch[1] + "/uploads";
  95. break;
  96. default:
  97. break;
  98. }
  99. var queryData = Yox.queryToJson(urlMatch.length == 2 ? urlMatch[1] : urlMatch[2]);
  100. if (queryData){
  101. if (queryData.search_query){
  102. queryData.q = queryData.search_query;
  103. delete queryData.search_query;
  104. }
  105. $.extend(datasourceOptions, queryData);
  106. }
  107. }
  108. }
  109. var getDimensions = getDimensionsCalc();
  110. function getEmbedObject(embedUrl){
  111. var videoPanel = $("<div>", {
  112. className: "yoxview_element",
  113. html: "<object width='100%' height='100%'><param name='movie' value='" + embedUrl + "'</param><param name='allowFullScreen' value='true'></param><param name='wmode' value='transparent'></param><param name='allowScriptAccess' value='always'></param><embed src='" + embedUrl + "' type='application/x-shockwave-flash' allowfullscreen='true' allowscriptaccess='always' wmode='transparent' width='100%' height='100%'></embed></object>"
  114. });
  115. return videoPanel;
  116. }
  117. function getVideosDataFromJson(items)
  118. {
  119. var videosData = [];
  120. jQuery.each(items, function(i, video){
  121. if (feedType === "playlist")
  122. video = video.video;
  123. var videoTitle = video.title;
  124. var videoData = {
  125. thumbnailSrc: video.thumbnail[datasourceOptions.hqThumbnails ? "hqDefault" : "sqDefault"],
  126. link: video.player["default"],
  127. media: {
  128. "element": getEmbedObject(video.content["5"] + "&fs=1&hd=1"),
  129. title: videoTitle,
  130. contentType: "flash",
  131. elementId: video.id,
  132. description: video.description
  133. }
  134. };
  135. $.extend(videoData.media, getDimensions(!!video.aspectRatio && video.aspectRatio === "widescreen"));
  136. videosData.push(videoData);
  137. });
  138. return videosData;
  139. }
  140. var returnData = {};
  141. if (options.onLoadBegin)
  142. options.onLoadBegin();
  143. $.jsonp({
  144. url: datasourceOptions.url,
  145. data: datasourceOptions,
  146. async: false,
  147. callbackParameter: "callback",
  148. success: function(jsonData)
  149. {
  150. if ((isSingleVideo && !jsonData.data) || (!isSingleVideo && (!jsonData.data.items || jsonData.data.items.length === 0)))
  151. {
  152. if (options.onNoData)
  153. options.onNoData();
  154. return;
  155. }
  156. returnData.images = getVideosDataFromJson(isSingleVideo ? [ jsonData.data ] : jsonData.data.items);
  157. if (!isSingleVideo){
  158. var dataTitle = jsonData.data.title;
  159. if (dataTitle)
  160. returnData.title = dataTitle;
  161. }
  162. if (callback)
  163. callback(returnData);
  164. if (options.onLoadComplete)
  165. options.onLoadComplete();
  166. },
  167. error : function(xOptions, textStatus){
  168. if (options.onLoadError)
  169. options.onLoadError("YouTube plugin encountered an error while retrieving data");
  170. }
  171. });
  172. }
  173. }