onCreated.js 14 KB

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