2
0

onCreated.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. });
  74. Template.register.onCreated(function() {
  75. Accounts.onLoginFailure(function() {
  76. 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>');
  77. $(".landing").before(errAlert);
  78. Meteor.setTimeout(function() {
  79. errAlert.fadeOut(5000, function() {
  80. errAlert.remove();
  81. });
  82. }, 10000);
  83. });
  84. });
  85. Template.room.onCreated(function () {
  86. Chat.after.find(function(userId, selector) {
  87. if (selector.type === "global") {
  88. if (!$("#global-chat-tab").hasClass("active")) {
  89. $("#global-chat-tab").addClass("unread-messages");
  90. }
  91. } else if(selector.type === Session.get("type")) {
  92. if (!$("#chat-tab").hasClass("active")) {
  93. $("#chat-tab").addClass("unread-messages");
  94. }
  95. }
  96. });
  97. Session.set("reportSong", false);
  98. Session.set("reportTitle", false);
  99. Session.set("reportAuthor", false);
  100. Session.set("reportDuration", false);
  101. Session.set("reportAudio", false);
  102. Session.set("reportAlbumart", false);
  103. Session.set("reportOther", false);
  104. if (resizeSeekerbarInterval !== undefined) {
  105. Meteor.clearInterval(resizeSeekerbarInterval);
  106. resizeSeekerbarInterval = undefined;
  107. }
  108. YTPlayer = undefined;
  109. Session.set("videoHidden", false);
  110. var tag = document.createElement("script");
  111. tag.src = "https://www.youtube.com/iframe_api";
  112. var firstScriptTag = document.getElementsByTagName('script')[0];
  113. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  114. Session.set("singleVideo", true);
  115. var currentSong = undefined;
  116. var currentSongR = undefined;
  117. function getTimeElapsed() {
  118. if (currentSong !== undefined) {
  119. var room = Rooms.findOne({type: type});
  120. if (room !== undefined) {
  121. return Date.now() - currentSong.started - room.timePaused;
  122. }
  123. }
  124. return 0;
  125. }
  126. function getSongInfo(songData){
  127. Session.set("title", songData.title);
  128. Session.set("artist", songData.artist);
  129. Session.set("id", songData.id);
  130. $("#song-img").attr("src", songData.img);
  131. Session.set("duration", parseInt(songData.duration));
  132. var d = moment.duration(parseInt(songData.duration), 'seconds');
  133. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  134. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  135. }
  136. function resizeSeekerbar() {
  137. if (Session.get("state") === "playing") {
  138. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  139. }
  140. }
  141. function startSong() {
  142. $("#time-elapsed").text("0:00");
  143. $("#vote-skip").attr("disabled", false);
  144. if (currentSong !== undefined) {
  145. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  146. var volume = localStorage.getItem("volume") || 20;
  147. if ($("#player").length !== 1) {
  148. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  149. }
  150. $("#player").show();
  151. function loadVideo() {
  152. if (!Session.get("YTLoaded")) {
  153. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  154. loadVideo();
  155. }, 500));
  156. } else {
  157. if (YTPlayer === undefined) {
  158. YTPlayer = new YT.Player("player", {
  159. height: 540,
  160. width: 960,
  161. videoId: currentSong.id,
  162. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  163. events: {
  164. 'onReady': function (event) {
  165. if (currentSong.skipDuration === undefined) {
  166. currentSong.skipDuration = 0;
  167. }
  168. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  169. event.target.playVideo();
  170. event.target.setVolume(volume);
  171. resizeSeekerbar();
  172. },
  173. 'onStateChange': function (event) {
  174. if (Session.get("YTLoaded")) {
  175. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  176. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  177. event.target.playVideo();
  178. }
  179. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  180. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  181. event.target.pauseVideo();
  182. }
  183. }
  184. }
  185. }
  186. });
  187. } else {
  188. YTPlayer.loadVideoById(currentSong.id);
  189. if (currentSong.skipDuration === undefined) {
  190. currentSong.skipDuration = 0;
  191. }
  192. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  193. }
  194. Session.set("pauseVideo", false);
  195. getSongInfo(currentSong);
  196. }
  197. }
  198. loadVideo();
  199. }
  200. }
  201. Session.set("loaded", false);
  202. Meteor.subscribe("rooms", function() {
  203. var parts = location.href.split('/');
  204. var id = parts.pop();
  205. var type = id.toLowerCase();
  206. Session.set("type", type);
  207. if (Rooms.find({type: type}).count() !== 1) {
  208. window.location = "/";
  209. } else {
  210. StationSubscription = Meteor.subscribe(type);
  211. Session.set("loaded", true);
  212. minterval = Meteor.setInterval(function () {
  213. var room = Rooms.findOne({type: type});
  214. if (room !== undefined) {
  215. if (room.state === "paused" || Session.get("pauseVideo")) {
  216. Session.set("state", "paused");
  217. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  218. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  219. YTPlayer.pauseVideo();
  220. }
  221. } else {
  222. Session.set("state", "playing");
  223. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  224. YTPlayer.playVideo();
  225. }
  226. }
  227. }
  228. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  229. Session.set("previousSong", currentSong);
  230. currentSongR = room.currentSong;
  231. currentSong = room.currentSong.song;
  232. currentSong.started = room.currentSong.started;
  233. Session.set("currentSong", currentSong);
  234. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  235. startSong();
  236. }
  237. if (currentSong !== undefined) {
  238. if (room !== undefined) {
  239. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  240. var song_duration = currentSong.duration;
  241. if (song_duration <= duration) {
  242. Session.set("pauseVideo", true);
  243. }
  244. var d = moment.duration(duration, 'seconds');
  245. if (Session.get("state") === "playing") {
  246. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  247. }
  248. }
  249. }
  250. }, 100);
  251. resizeSeekerbarInterval = Meteor.setInterval(function () {
  252. resizeSeekerbar();
  253. }, 500);
  254. }
  255. });
  256. });
  257. Template.settings.onCreated(function() {
  258. $(document).ready(function() {
  259. var user = Meteor.user();
  260. function initSettings() {
  261. if (user !== undefined) {
  262. if (user.profile.settings && user.profile.settings.showRating === true) {
  263. function setChecked() {
  264. $("#showRating").prop("checked", true);
  265. if (!$("#showRating").prop("checked")) {
  266. Meteor.setTimeout(function() {
  267. setChecked();
  268. }, 100);
  269. }
  270. }
  271. setChecked();
  272. }
  273. } else {
  274. Meteor.setTimeout(function() {
  275. initSettings();
  276. }, 500);
  277. }
  278. }
  279. initSettings();
  280. });
  281. });
  282. Template.stations.onCreated(function() {
  283. var tag = document.createElement("script");
  284. tag.src = "https://www.youtube.com/iframe_api";
  285. var firstScriptTag = document.getElementsByTagName('script')[0];
  286. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  287. YTPlayer = undefined;
  288. });