onCreated.js 14 KB

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