onCreated.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var StationSubscription = undefined;
  2. var resizeSeekerbarInterval;
  3. Template.landing.onCreated(function(){
  4. $("body").css("overflow", "hidden");
  5. function pageScroll() {
  6. window.scrollBy(0,1);
  7. if($(window).scrollTop() + $(window).height() == $(document).height()) {
  8. $(window).scrollTop(0);
  9. }
  10. scrolldelay = setTimeout(pageScroll,50);
  11. }
  12. pageScroll();
  13. });
  14. Template.banned.onCreated(function() {
  15. if (Session.get("rTimeInterval") !== undefined) {
  16. Meteor.clearInterval(Session.get("rTimeInterval"))
  17. }
  18. Session.set("rTimeInterval", Meteor.setInterval(function() {
  19. Session.set("time", new Date().getTime());
  20. }, 10000));
  21. Session.set("ban", Meteor.user().punishments.ban);
  22. });
  23. Template.home.onCreated(function() {
  24. if (Session.get("minterval") !== undefined) {
  25. Meteor.clearInterval(Session.get("minterval"));
  26. }
  27. if (Session.get("titleScroller") !== undefined) {
  28. Meteor.clearInterval(Session.get("titleScroller"));
  29. }
  30. if (resizeSeekerbarInterval !== undefined) {
  31. Meteor.clearInterval(resizeSeekerbarInterval);
  32. resizeSeekerbarInterval = undefined;
  33. }
  34. if (StationSubscription !== undefined) {
  35. StationSubscription.stop();
  36. }
  37. Session.set("type", undefined);
  38. });
  39. Template.login.onCreated(function() {
  40. Session.set("github", true);
  41. Accounts.onLoginFailure(function() {
  42. if (Session.get("github") === true) {
  43. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> Something went wrong when trying to log in with GitHub.</div>');
  44. $(".landing").before(errAlert);
  45. Meteor.setTimeout(function() {
  46. errAlert.fadeOut(5000, function() {
  47. errAlert.remove();
  48. });
  49. }, 10000);
  50. }
  51. });
  52. });
  53. Template.feedback.onCreated(function(){
  54. Meteor.subscribe("feedback");
  55. })
  56. Template.profile.onCreated(function() {
  57. var parts = Router.current().url.split('/');
  58. var username = parts.pop();
  59. Session.set("loaded", false);
  60. Meteor.subscribe("userProfiles", username.toLowerCase(), function() {
  61. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  62. window.location = "/";
  63. } else {
  64. var data = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  65. Session.set("real_name", data.profile.realname);
  66. Session.set("username", data.profile.username);
  67. Session.set("first_joined", data.createdAt);
  68. Session.set("rank", data.profile.rank);
  69. Session.set("liked", data.profile.liked);
  70. Session.set("disliked", data.profile.disliked);
  71. Session.set("loaded", true);
  72. }
  73. });
  74. });
  75. Template.queues.onCreated(function() {
  76. var tag = document.createElement("script");
  77. tag.src = "https://www.youtube.com/iframe_api";
  78. var firstScriptTag = document.getElementsByTagName('script')[0];
  79. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  80. YTPlayer = undefined;
  81. $(document).keydown(function(evt){
  82. if (evt.keyCode==83 && (evt.ctrlKey)){
  83. evt.preventDefault();
  84. if (Session.get("editing") === true) {
  85. $("#save-song-button").click();
  86. }
  87. }
  88. });
  89. });
  90. Template.manageStation.onCreated(function() {
  91. var tag = document.createElement("script");
  92. tag.src = "https://www.youtube.com/iframe_api";
  93. var firstScriptTag = document.getElementsByTagName('script')[0];
  94. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  95. YTPlayer = undefined;
  96. $(document).keydown(function(evt){
  97. if (evt.keyCode==83 && (evt.ctrlKey)){
  98. evt.preventDefault();
  99. if (Session.get("editing") === true) {
  100. $("#save-song-button").click();
  101. }
  102. }
  103. });
  104. });
  105. Template.manageSongs.onCreated(function() {
  106. Session.set("showNoGenres", false);
  107. Session.set("showGenres", true);
  108. var tag = document.createElement("script");
  109. tag.src = "https://www.youtube.com/iframe_api";
  110. var firstScriptTag = document.getElementsByTagName('script')[0];
  111. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  112. YTPlayer = undefined;
  113. $(document).keydown(function(evt){
  114. if (evt.keyCode==83 && (evt.ctrlKey)){
  115. evt.preventDefault();
  116. if (Session.get("editing") === true) {
  117. $("#save-song-button").click();
  118. }
  119. }
  120. });
  121. });
  122. Template.register.onCreated(function() {
  123. Accounts.onLoginFailure(function() {
  124. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> Something went wrong when trying to register with GitHub. Maybe an account with that username already exists?</div>');
  125. $(".landing").before(errAlert);
  126. Meteor.setTimeout(function() {
  127. errAlert.fadeOut(5000, function() {
  128. errAlert.remove();
  129. });
  130. }, 10000);
  131. });
  132. });
  133. Template.room.onCreated(function () {
  134. Chat.after.find(function(userId, selector) {
  135. if (selector.type === "global") {
  136. if (!$("#global-chat-tab").hasClass("active")) {
  137. $("#global-chat-tab").addClass("unread-messages");
  138. }
  139. } else if(selector.type === Session.get("type")) {
  140. if (!$("#chat-tab").hasClass("active")) {
  141. $("#chat-tab").addClass("unread-messages");
  142. }
  143. }
  144. });
  145. Session.set("reportSong", false);
  146. Session.set("reportTitle", false);
  147. Session.set("reportAuthor", false);
  148. Session.set("reportDuration", false);
  149. Session.set("reportAudio", false);
  150. Session.set("reportAlbumart", false);
  151. Session.set("reportOther", false);
  152. Session.set("si_or_pl", "singleVideo");
  153. Session.set("editingSong", false);
  154. var parts = location.href.split('/');
  155. var id = parts.pop();
  156. var type = id.toLowerCase();
  157. if (resizeSeekerbarInterval !== undefined) {
  158. Meteor.clearInterval(resizeSeekerbarInterval);
  159. resizeSeekerbarInterval = undefined;
  160. }
  161. YTPlayer = undefined;
  162. Session.set("videoHidden", false);
  163. var tag = document.createElement("script");
  164. tag.src = "https://www.youtube.com/iframe_api";
  165. var firstScriptTag = document.getElementsByTagName('script')[0];
  166. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  167. Session.set("singleVideo", true);
  168. var currentSong = undefined;
  169. var currentSongR = undefined;
  170. function getTimeElapsed() {
  171. var type = Session.get("type");
  172. if (currentSong !== undefined) {
  173. var room = Rooms.findOne({type: type});
  174. if (room !== undefined) {
  175. return Date.now() - currentSong.started - room.timePaused;
  176. }
  177. }
  178. return 0;
  179. }
  180. function getSongInfo(songData){
  181. Session.set("title", songData.title);
  182. Session.set("artist", songData.artist);
  183. Session.set("id", songData.id);
  184. $("#song-img").attr("src", songData.img);
  185. Session.set("duration", parseInt(songData.duration));
  186. var d = moment.duration(parseInt(songData.duration), 'seconds');
  187. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  188. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  189. document.title = Session.get("title") + " - " + Session.get("artist") + " - ";
  190. var title = Session.get("title") + " - " + Session.get("artist") + " - ";
  191. if (Session.get("titleScroller") !== undefined) {
  192. Meteor.clearInterval(Session.get("titleScroller"));
  193. }
  194. Session.set("titleScroller", Meteor.setInterval(function() {
  195. title = title.split("");
  196. var last = title.shift();
  197. title.push(last);
  198. title = title.join("");
  199. document.title = title;
  200. }, 500));
  201. }
  202. function resizeSeekerbar() {
  203. if (Session.get("state") === "playing") {
  204. $(".seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  205. }
  206. }
  207. function startSong() {
  208. $("#time-elapsed").text("0:00");
  209. $("#vote-skip").attr("disabled", false);
  210. if (currentSong !== undefined) {
  211. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  212. var volume = localStorage.getItem("volume") || 20;
  213. $("#player").show();
  214. function loadVideo() {
  215. if (!Session.get("YTLoaded")) {
  216. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  217. loadVideo();
  218. }, 500));
  219. } else {
  220. if (YTPlayer === undefined) {
  221. YTPlayer = new YT.Player("player", {
  222. height: 270,
  223. width: 480,
  224. videoId: currentSong.id,
  225. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  226. events: {
  227. 'onReady': function (event) {
  228. if (currentSong.skipDuration === undefined) {
  229. currentSong.skipDuration = 0;
  230. }
  231. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  232. event.target.playVideo();
  233. event.target.setVolume(volume);
  234. resizeSeekerbar();
  235. },
  236. 'onStateChange': function (event) {
  237. if (Session.get("YTLoaded")) {
  238. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  239. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  240. event.target.playVideo();
  241. }
  242. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  243. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  244. event.target.pauseVideo();
  245. }
  246. }
  247. }
  248. }
  249. });
  250. } else {
  251. YTPlayer.loadVideoById(currentSong.id);
  252. if (currentSong.skipDuration === undefined) {
  253. currentSong.skipDuration = 0;
  254. }
  255. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  256. }
  257. Session.set("pauseVideo", false);
  258. getSongInfo(currentSong);
  259. }
  260. }
  261. loadVideo();
  262. }
  263. }
  264. Session.set("loaded", false);
  265. Meteor.subscribe("rooms", function() {
  266. var parts = location.href.split('/');
  267. var id = parts.pop();
  268. var type = id.toLowerCase();
  269. Session.set("type", type);
  270. if (Rooms.find({type: type}).count() !== 1) {
  271. window.location = "/";
  272. } else {
  273. StationSubscription = Meteor.subscribe(type);
  274. Session.set("loaded", true);
  275. Session.set("minterval", Meteor.setInterval(function () {
  276. var room = Rooms.findOne({type: type});
  277. if (room !== undefined) {
  278. if (room.state === "paused" || Session.get("pauseVideo")) {
  279. Session.set("state", "paused");
  280. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  281. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  282. YTPlayer.pauseVideo();
  283. }
  284. } else {
  285. Session.set("state", "playing");
  286. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  287. YTPlayer.playVideo();
  288. }
  289. }
  290. }
  291. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  292. Session.set("previousSong", currentSong);
  293. currentSongR = room.currentSong;
  294. currentSong = room.currentSong.song;
  295. currentSong.started = room.currentSong.started;
  296. Session.set("currentSong", currentSong);
  297. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  298. startSong();
  299. }
  300. if (currentSong !== undefined) {
  301. if (room !== undefined) {
  302. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  303. var song_duration = currentSong.duration;
  304. if (song_duration <= duration) {
  305. Session.set("pauseVideo", true);
  306. }
  307. var d = moment.duration(duration, 'seconds');
  308. if (Session.get("state") === "playing") {
  309. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  310. }
  311. }
  312. }
  313. }, 100));
  314. resizeSeekerbarInterval = Meteor.setInterval(function () {
  315. resizeSeekerbar();
  316. }, 500)
  317. }
  318. });
  319. });
  320. Template.settings.onCreated(function() {
  321. $(document).ready(function() {
  322. var user = Meteor.user();
  323. function initSettings() {
  324. if (user !== undefined) {
  325. if (user.profile.settings && user.profile.settings.showRating === true) {
  326. function setChecked() {
  327. $("#showRating").prop("checked", true);
  328. if (!$("#showRating").prop("checked")) {
  329. Meteor.setTimeout(function() {
  330. setChecked();
  331. }, 100);
  332. }
  333. }
  334. setChecked();
  335. }
  336. } else {
  337. Meteor.setTimeout(function() {
  338. initSettings();
  339. }, 500);
  340. }
  341. }
  342. initSettings();
  343. });
  344. });