onCreated.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. var minterval;
  2. var StationSubscription = undefined;
  3. var resizeSeekerbarInterval;
  4. Template.alertsDashboard.onCreated(function() {
  5. if (allAlertSub === undefined) {
  6. allAlertSub = Meteor.subscribe("allAlerts");
  7. }
  8. });
  9. Template.landing.onCreated(function(){
  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.dashboard.onCreated(function() {
  29. if (minterval !== undefined) {
  30. Meteor.clearInterval(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. var parts = location.href.split('/');
  139. var id = parts.pop();
  140. var type = id.toLowerCase();
  141. if (resizeSeekerbarInterval !== undefined) {
  142. Meteor.clearInterval(resizeSeekerbarInterval);
  143. resizeSeekerbarInterval = undefined;
  144. }
  145. YTPlayer = undefined;
  146. Session.set("videoHidden", false);
  147. var tag = document.createElement("script");
  148. tag.src = "https://www.youtube.com/iframe_api";
  149. var firstScriptTag = document.getElementsByTagName('script')[0];
  150. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  151. Session.set("singleVideo", true);
  152. var currentSong = undefined;
  153. var currentSongR = undefined;
  154. var type = Session.get("type");
  155. function getTimeElapsed() {
  156. if (currentSong !== undefined) {
  157. var room = Rooms.findOne({type: type});
  158. if (room !== undefined) {
  159. return Date.now() - currentSong.started - room.timePaused;
  160. }
  161. }
  162. return 0;
  163. }
  164. function getSongInfo(songData){
  165. Session.set("title", songData.title);
  166. Session.set("artist", songData.artist);
  167. Session.set("id", songData.id);
  168. $("#song-img").attr("src", songData.img);
  169. Session.set("duration", parseInt(songData.duration));
  170. var d = moment.duration(parseInt(songData.duration), 'seconds');
  171. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  172. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  173. }
  174. function resizeSeekerbar() {
  175. if (Session.get("state") === "playing") {
  176. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  177. }
  178. }
  179. function startSong() {
  180. $("#time-elapsed").text("0:00");
  181. $("#vote-skip").attr("disabled", false);
  182. if (currentSong !== undefined) {
  183. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  184. var volume = localStorage.getItem("volume") || 20;
  185. $("#player").show();
  186. function loadVideo() {
  187. if (!Session.get("YTLoaded")) {
  188. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  189. loadVideo();
  190. }, 500));
  191. } else {
  192. if (YTPlayer === undefined) {
  193. YTPlayer = new YT.Player("player", {
  194. height: 270,
  195. width: 480,
  196. videoId: currentSong.id,
  197. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  198. events: {
  199. 'onReady': function (event) {
  200. if (currentSong.skipDuration === undefined) {
  201. currentSong.skipDuration = 0;
  202. }
  203. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  204. event.target.playVideo();
  205. event.target.setVolume(volume);
  206. resizeSeekerbar();
  207. },
  208. 'onStateChange': function (event) {
  209. if (Session.get("YTLoaded")) {
  210. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  211. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  212. event.target.playVideo();
  213. }
  214. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  215. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  216. event.target.pauseVideo();
  217. }
  218. }
  219. }
  220. }
  221. });
  222. } else {
  223. YTPlayer.loadVideoById(currentSong.id);
  224. if (currentSong.skipDuration === undefined) {
  225. currentSong.skipDuration = 0;
  226. }
  227. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  228. }
  229. Session.set("pauseVideo", false);
  230. getSongInfo(currentSong);
  231. }
  232. }
  233. loadVideo();
  234. }
  235. }
  236. function getSongAudio() {
  237. var ytURL = "www.youtube.com/watch?v=" + currentSong.id;
  238. Meteor.call('getSongAudio', ytURL);
  239. startSong();
  240. }
  241. Session.set("loaded", false);
  242. Meteor.subscribe("rooms", function() {
  243. var parts = location.href.split('/');
  244. var id = parts.pop();
  245. var type = id.toLowerCase();
  246. Session.set("type", type);
  247. if (Rooms.find({type: type}).count() !== 1) {
  248. window.location = "/";
  249. } else {
  250. StationSubscription = Meteor.subscribe(type);
  251. Session.set("loaded", true);
  252. minterval = Meteor.setInterval(function () {
  253. var room = Rooms.findOne({type: type});
  254. if (room !== undefined) {
  255. if (room.state === "paused" || Session.get("pauseVideo")) {
  256. Session.set("state", "paused");
  257. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  258. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  259. YTPlayer.pauseVideo();
  260. }
  261. } else {
  262. Session.set("state", "playing");
  263. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  264. YTPlayer.playVideo();
  265. }
  266. }
  267. }
  268. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  269. Session.set("previousSong", currentSong);
  270. currentSongR = room.currentSong;
  271. currentSong = room.currentSong.song;
  272. currentSong.started = room.currentSong.started;
  273. Session.set("currentSong", currentSong);
  274. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  275. getSongAudio();
  276. }
  277. if (currentSong !== undefined) {
  278. if (room !== undefined) {
  279. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  280. var song_duration = currentSong.duration;
  281. if (song_duration <= duration) {
  282. Session.set("pauseVideo", true);
  283. }
  284. var d = moment.duration(duration, 'seconds');
  285. if (Session.get("state") === "playing") {
  286. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  287. }
  288. }
  289. }
  290. }, 100);
  291. resizeSeekerbarInterval = Meteor.setInterval(function () {
  292. resizeSeekerbar();
  293. }, 500);
  294. }
  295. });
  296. });
  297. Template.settings.onCreated(function() {
  298. $(document).ready(function() {
  299. var user = Meteor.user();
  300. function initSettings() {
  301. if (user !== undefined) {
  302. if (user.profile.settings && user.profile.settings.showRating === true) {
  303. function setChecked() {
  304. $("#showRating").prop("checked", true);
  305. if (!$("#showRating").prop("checked")) {
  306. Meteor.setTimeout(function() {
  307. setChecked();
  308. }, 100);
  309. }
  310. }
  311. setChecked();
  312. }
  313. } else {
  314. Meteor.setTimeout(function() {
  315. initSettings();
  316. }, 500);
  317. }
  318. }
  319. initSettings();
  320. });
  321. });