2
0

onCreated.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 (Session.get("rTimeInterval") !== undefined) {
  21. Meteor.clearInterval(Session.get("rTimeInterval"))
  22. }
  23. Session.set("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.manageSongs.onCreated(function() {
  111. Session.set("showNoGenres", false);
  112. Session.set("showGenres", true);
  113. var tag = document.createElement("script");
  114. tag.src = "https://www.youtube.com/iframe_api";
  115. var firstScriptTag = document.getElementsByTagName('script')[0];
  116. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  117. YTPlayer = undefined;
  118. $(document).keydown(function(evt){
  119. if (evt.keyCode==83 && (evt.ctrlKey)){
  120. evt.preventDefault();
  121. if (Session.get("editing") === true) {
  122. $("#save-song-button").click();
  123. }
  124. }
  125. });
  126. });
  127. Template.register.onCreated(function() {
  128. Accounts.onLoginFailure(function() {
  129. 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>');
  130. $(".landing").before(errAlert);
  131. Meteor.setTimeout(function() {
  132. errAlert.fadeOut(5000, function() {
  133. errAlert.remove();
  134. });
  135. }, 10000);
  136. });
  137. });
  138. Template.room.onCreated(function () {
  139. Chat.after.find(function(userId, selector) {
  140. if (selector.type === "global") {
  141. if (!$("#global-chat-tab").hasClass("active")) {
  142. $("#global-chat-tab").addClass("unread-messages");
  143. }
  144. } else if(selector.type === Session.get("type")) {
  145. if (!$("#chat-tab").hasClass("active")) {
  146. $("#chat-tab").addClass("unread-messages");
  147. }
  148. }
  149. });
  150. Session.set("reportSong", false);
  151. Session.set("reportTitle", false);
  152. Session.set("reportAuthor", false);
  153. Session.set("reportDuration", false);
  154. Session.set("reportAudio", false);
  155. Session.set("reportAlbumart", false);
  156. Session.set("reportOther", false);
  157. Session.set("si_or_pl", "singleVideo");
  158. Session.set("editingSong", false);
  159. var parts = location.href.split('/');
  160. var id = parts.pop();
  161. var type = id.toLowerCase();
  162. if (resizeSeekerbarInterval !== undefined) {
  163. Meteor.clearInterval(resizeSeekerbarInterval);
  164. resizeSeekerbarInterval = undefined;
  165. }
  166. YTPlayer = undefined;
  167. Session.set("videoHidden", false);
  168. var tag = document.createElement("script");
  169. tag.src = "https://www.youtube.com/iframe_api";
  170. var firstScriptTag = document.getElementsByTagName('script')[0];
  171. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  172. Session.set("singleVideo", true);
  173. var currentSong = undefined;
  174. var currentSongR = undefined;
  175. function getTimeElapsed() {
  176. var type = Session.get("type");
  177. if (currentSong !== undefined) {
  178. var room = Rooms.findOne({type: type});
  179. if (room !== undefined) {
  180. return Date.now() - currentSong.started - room.timePaused;
  181. }
  182. }
  183. return 0;
  184. }
  185. function getSongInfo(songData){
  186. Session.set("title", songData.title);
  187. Session.set("artist", songData.artist);
  188. Session.set("id", songData.id);
  189. $("#song-img").attr("src", songData.img);
  190. Session.set("duration", parseInt(songData.duration));
  191. var d = moment.duration(parseInt(songData.duration), 'seconds');
  192. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  193. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  194. document.title = Session.get("title") + " - " + Session.get("artist") + " - ";
  195. var title = Session.get("title") + " - " + Session.get("artist") + " - ";
  196. if (Session.get("titleScroller") !== undefined) {
  197. Meteor.clearInterval(Session.get("titleScroller"));
  198. }
  199. Session.set("titleScroller", Meteor.setInterval(function() {
  200. title = title.split("");
  201. var last = title.shift();
  202. title.push(last);
  203. title = title.join("");
  204. document.title = title;
  205. }, 500));
  206. }
  207. function resizeSeekerbar() {
  208. if (Session.get("state") === "playing") {
  209. $(".seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  210. }
  211. }
  212. function startSong() {
  213. $("#time-elapsed").text("0:00");
  214. $("#vote-skip").attr("disabled", false);
  215. if (currentSong !== undefined) {
  216. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  217. var volume = localStorage.getItem("volume") || 20;
  218. $("#player").show();
  219. function loadVideo() {
  220. if (!Session.get("YTLoaded")) {
  221. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  222. loadVideo();
  223. }, 500));
  224. } else {
  225. if (YTPlayer === undefined) {
  226. YTPlayer = new YT.Player("player", {
  227. height: 270,
  228. width: 480,
  229. videoId: currentSong.id,
  230. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  231. events: {
  232. 'onReady': function (event) {
  233. if (currentSong.skipDuration === undefined) {
  234. currentSong.skipDuration = 0;
  235. }
  236. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  237. event.target.playVideo();
  238. event.target.setVolume(volume);
  239. resizeSeekerbar();
  240. },
  241. 'onStateChange': function (event) {
  242. if (Session.get("YTLoaded")) {
  243. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  244. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  245. event.target.playVideo();
  246. }
  247. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  248. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  249. event.target.pauseVideo();
  250. }
  251. }
  252. }
  253. }
  254. });
  255. } else {
  256. YTPlayer.loadVideoById(currentSong.id);
  257. if (currentSong.skipDuration === undefined) {
  258. currentSong.skipDuration = 0;
  259. }
  260. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  261. }
  262. Session.set("pauseVideo", false);
  263. getSongInfo(currentSong);
  264. }
  265. }
  266. loadVideo();
  267. }
  268. }
  269. Session.set("loaded", false);
  270. Meteor.subscribe("rooms", function() {
  271. var parts = location.href.split('/');
  272. var id = parts.pop();
  273. var type = id.toLowerCase();
  274. Session.set("type", type);
  275. if (Rooms.find({type: type}).count() !== 1) {
  276. window.location = "/";
  277. } else {
  278. StationSubscription = Meteor.subscribe(type);
  279. Session.set("loaded", true);
  280. Session.set("minterval", Meteor.setInterval(function () {
  281. var room = Rooms.findOne({type: type});
  282. if (room !== undefined) {
  283. if (room.state === "paused" || Session.get("pauseVideo")) {
  284. Session.set("state", "paused");
  285. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  286. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  287. YTPlayer.pauseVideo();
  288. }
  289. } else {
  290. Session.set("state", "playing");
  291. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  292. YTPlayer.playVideo();
  293. }
  294. }
  295. }
  296. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  297. Session.set("previousSong", currentSong);
  298. currentSongR = room.currentSong;
  299. currentSong = room.currentSong.song;
  300. currentSong.started = room.currentSong.started;
  301. Session.set("currentSong", currentSong);
  302. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  303. startSong();
  304. }
  305. if (currentSong !== undefined) {
  306. if (room !== undefined) {
  307. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  308. var song_duration = currentSong.duration;
  309. if (song_duration <= duration) {
  310. Session.set("pauseVideo", true);
  311. }
  312. var d = moment.duration(duration, 'seconds');
  313. if (Session.get("state") === "playing") {
  314. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  315. }
  316. }
  317. }
  318. }, 100));
  319. resizeSeekerbarInterval = Meteor.setInterval(function () {
  320. resizeSeekerbar();
  321. }, 500)
  322. }
  323. });
  324. });
  325. Template.settings.onCreated(function() {
  326. $(document).ready(function() {
  327. var user = Meteor.user();
  328. function initSettings() {
  329. if (user !== undefined) {
  330. if (user.profile.settings && user.profile.settings.showRating === true) {
  331. function setChecked() {
  332. $("#showRating").prop("checked", true);
  333. if (!$("#showRating").prop("checked")) {
  334. Meteor.setTimeout(function() {
  335. setChecked();
  336. }, 100);
  337. }
  338. }
  339. setChecked();
  340. }
  341. } else {
  342. Meteor.setTimeout(function() {
  343. initSettings();
  344. }, 500);
  345. }
  346. }
  347. initSettings();
  348. });
  349. });