onCreated.js 16 KB

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