onCreated.js 17 KB

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