onCreated.js 14 KB

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