onCreated.js 16 KB

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