PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/video/video.js

https://github.com/dakshil/zeroclickinfo-spice
JavaScript | 224 lines | 171 code | 36 blank | 17 comment | 15 complexity | 22bb68b3f0b6085dc01259fdb1164812 MD5 | raw file
  1. function ddg_spice_video(api_result) {
  2. "use strict";
  3. // Do not show the single detail view for now.
  4. if(!api_result || api_result.length == 0) {
  5. return;
  6. }
  7. $.ajaxSetup({
  8. cache: true
  9. });
  10. var encodedQuery = DDG.get_query_encoded().replace(/(?:youtube(\.com)?|videos?)/i, '');
  11. // Change the "More at ..." link.
  12. var change_more = function(obj) {
  13. var more_at_link = $(".zero_click_more_at_link");
  14. more_at_link.attr("href", obj.search_link);
  15. more_at_link.find("img").attr("src", obj.image);
  16. more_at_link.find("span").html(obj.text);
  17. };
  18. // This are things that a provider needs when building the spice.
  19. var provider_data = {
  20. "YouTube": {
  21. "search_link": "https://www.youtube.com/results?search_query=",
  22. "image": "https://icons.duckduckgo.com/i/www.youtube.com.ico",
  23. "text": "More at YouTube",
  24. "embed": "https://www.youtube-nocookie.com/embed/",
  25. "play_url": "https://www.youtube.com/watch?v=",
  26. "user_url": "https://www.youtube.com/user/",
  27. "embed_options": {
  28. "iv_load_policy": 3,
  29. "autoplay": 1,
  30. "wmode": "opaque"
  31. }
  32. },
  33. "Vimeo": {
  34. "search_link": "https://www.vimeo.com/search?q=",
  35. "image": "https://icons.duckduckgo.com/i/www.vimeo.com.ico",
  36. "text": "More at Vimeo",
  37. "embed": "https://player.vimeo.com/video/",
  38. "play_url": "https://vimeo.com/",
  39. "user_url": "https://vimeo.com/",
  40. "embed_options": {
  41. "api": 0,
  42. "autoplay": 1
  43. }
  44. }
  45. };
  46. // Copy provider-specific data to the item.
  47. $.map(api_result, function(item) {
  48. item.provider_data = provider_data[item.provider];
  49. });
  50. Spice.render({
  51. data: api_result,
  52. source_name : 'YouTube',
  53. source_url : 'https://www.youtube.com/results?search_query=' + encodedQuery,
  54. header1 : decodeURIComponent(encodedQuery) + " (Video)",
  55. spice_name: "video",
  56. template_frame: "carousel",
  57. template_options: {
  58. items: api_result,
  59. template_item: "video",
  60. template_detail: "video_detail",
  61. li_width: 120,
  62. single_item_handler: function(obj) {
  63. // Prevent the video from playing if we only have one item.
  64. obj.data[0].provider_data.embed_options.autoplay = 0;
  65. },
  66. use_alternate_template: false
  67. },
  68. force_no_fold : 1,
  69. item_callback: function(i, item) {
  70. var more_at_link = $(".zero_click_more_at_link").get(0);
  71. change_more({
  72. "search_link": item.provider_data.search_link + encodedQuery,
  73. "image": item.provider_data.image,
  74. "text": item.provider_data.text
  75. });
  76. resizeDetail();
  77. }
  78. });
  79. function resizeDetail() {
  80. var $video = $("#spice_video");
  81. var width = $video.width() - 18;
  82. var height = Math.floor(width * 0.5625) + 30;
  83. $("#spice_video #video-embed").width(width);
  84. $("#spice_video #video-embed").height(height);
  85. var video_title = $("#spice_video #video-title").width();
  86. var video_links = $("#spice_video .links").width();
  87. if(width - (video_title + video_links) < 20) {
  88. $("#spice_video .links").css("position", "static");
  89. } else {
  90. $("#spice_video .links").css("position", "absolute");
  91. }
  92. }
  93. $(document).ready(function() {
  94. resizeDetail();
  95. $(window).resize(resizeDetail);
  96. });
  97. // Expose the function so that ddg_spice_video.itunes can use it.
  98. ddg_spice_video.resizeDetail = resizeDetail;
  99. }
  100. // This is the callback function of /itt.
  101. ddg_spice_video.itunes = function(api_result) {
  102. "use strict";
  103. if(!api_result || !api_result.results || api_result.results.length === 0) {
  104. return;
  105. }
  106. var itunes = $("#itunes");
  107. var artist = itunes.data("artist").toLowerCase();
  108. var song = itunes.data("song").toLowerCase();
  109. // Find the song that matches.
  110. for(var i = 0; i < api_result.results.length; i++) {
  111. if(artist === api_result.results[i].artistName.toLowerCase() ||
  112. artist === api_result.results[i].trackName.toLowerCase() ||
  113. song === api_result.results[i].artistName.toLowerCase() ||
  114. song === api_result.results[i].trackName.toLowerCase()) {
  115. itunes.attr("href", api_result.results[i].trackViewUrl);
  116. itunes.toggle();
  117. // We need to resize when we add in the iTunes button.
  118. ddg_spice_video.resizeDetail();
  119. break;
  120. }
  121. }
  122. };
  123. Handlebars.registerHelper("toHTTP", function (image) {
  124. "use strict";
  125. return image.replace(/^https/, "http");
  126. });
  127. Handlebars.registerHelper("userURL", function(provider_data, uploader) {
  128. "use strict";
  129. return provider_data.user_url + uploader;
  130. });
  131. Handlebars.registerHelper("checkMusic", function(category, title, options) {
  132. "use strict";
  133. // Remove things from the title that we don't really need.
  134. var stripTitle = function(s) {
  135. // Remove things like "(Explicit)".
  136. s = s.replace(/\(.*\)|\[.*\]/g, "");
  137. // Remove things like "feat. Alicia Keys".
  138. s = s.replace(/\s+f(?:ea|)t\..*$/g, "");
  139. // Trim the ends of the string.
  140. return s.replace(/^\s+|\s+$/g, "");
  141. };
  142. title = stripTitle(title);
  143. var songData = title.split(" - ");
  144. var artist = songData[0];
  145. var song = songData[1] || artist;
  146. // Call iTunes.
  147. $.getScript("/iit/" + encodeURIComponent(title));
  148. // Only add links to the music if, well, we have links to the music section.
  149. if(category === "Music") {
  150. // There's no need to escape the values--Handlebars.js does this for us.
  151. return options.fn({
  152. title: title,
  153. artist: artist,
  154. song: song
  155. });
  156. }
  157. });
  158. // We'll use this for showing the view counts.
  159. Handlebars.registerHelper("formatViews", function(views) {
  160. "use strict";
  161. if(views) {
  162. return String(views).replace(/(\d)(?=(\d{3})+(\.\d+|)\b)/g, "$1,");
  163. }
  164. });
  165. Handlebars.registerHelper("embedURL", function(provider_data, id) {
  166. "use strict";
  167. function parameters(embed_options) {
  168. var result = [];
  169. for(var o in embed_options) {
  170. result.push(o + "=" + embed_options[o]);
  171. }
  172. return result.join("&");
  173. }
  174. return provider_data.embed + id + "?" +
  175. parameters(provider_data.embed_options);
  176. return "";
  177. });
  178. Handlebars.registerHelper("playURL", function(provider_data, id) {
  179. "use strict";
  180. return provider_data.play_url + id;
  181. });
  182. Handlebars.registerHelper("checkStatistics", function(viewCount, options) {
  183. "use strict";
  184. if(viewCount === null) {
  185. return "";
  186. }
  187. return options.fn({viewCount: viewCount});
  188. });