onCreated.js 13 KB

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