onCreated.js 13 KB

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