onCreated.js 14 KB

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