onCreated.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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.loginRegister.onCreated(function() {
  26. Session.set("github", true);
  27. Accounts.onLoginFailure(function() {
  28. if (Session.get("github") === true) {
  29. var $toastContent = $('<span><strong>Oh Snap!</strong> Something went wrong when trying to log in/register with GitHub. Maybe an account with that username is already registered?</span>');
  30. Materialize.toast($toastContent, 8000);
  31. }
  32. });
  33. });
  34. Template.admin.onCreated(function() {
  35. Meteor.subscribe("allAlerts");
  36. });
  37. Template.feedback.onCreated(function(){
  38. Meteor.subscribe("feedback");
  39. })
  40. Template.profile.onCreated(function() {
  41. var parts = Router.current().url.split('/');
  42. var username = parts.pop();
  43. Session.set("loaded", false);
  44. Meteor.subscribe("userProfiles", username.toLowerCase(), function() {
  45. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  46. window.location = "/";
  47. } else {
  48. var data = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  49. Session.set("real_name", data.profile.realname);
  50. Session.set("username", data.profile.username);
  51. Session.set("first_joined", data.createdAt);
  52. Session.set("rank", data.profile.rank);
  53. Session.set("songs_requested", data.profile.statistics.songsRequested);
  54. Session.set("liked", data.profile.liked);
  55. Session.set("disliked", data.profile.disliked);
  56. Session.set("loaded", true);
  57. }
  58. });
  59. });
  60. Template.queues.onCreated(function() {
  61. var tag = document.createElement("script");
  62. tag.src = "https://www.youtube.com/iframe_api";
  63. var firstScriptTag = document.getElementsByTagName('script')[0];
  64. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  65. YTPlayer = undefined;
  66. $(document).keydown(function(evt){
  67. if (evt.keyCode==83 && (evt.ctrlKey)){
  68. evt.preventDefault();
  69. if (Session.get("editing") === true) {
  70. $("#save-song-button").click();
  71. }
  72. }
  73. });
  74. });
  75. Template.manageStation.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.manageSongs.onCreated(function() {
  91. Session.set("showNoGenres", false);
  92. Session.set("showGenres", true);
  93. var tag = document.createElement("script");
  94. tag.src = "https://www.youtube.com/iframe_api";
  95. var firstScriptTag = document.getElementsByTagName('script')[0];
  96. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  97. YTPlayer = undefined;
  98. $(document).keydown(function(evt){
  99. if (evt.keyCode==83 && (evt.ctrlKey)){
  100. evt.preventDefault();
  101. if (Session.get("editing") === true) {
  102. $("#save-song-button").click();
  103. }
  104. }
  105. });
  106. });
  107. Template.room.onCreated(function () {
  108. Chat.after.find(function(userId, selector) {
  109. if (selector.type === "global") {
  110. if (!$("#global-chat-tab").hasClass("active")) {
  111. $("#global-chat-tab").addClass("unread-messages");
  112. }
  113. } else if(selector.type === Session.get("type")) {
  114. if (!$("#chat-tab").hasClass("active")) {
  115. $("#chat-tab").addClass("unread-messages");
  116. }
  117. }
  118. });
  119. Session.set("reportSong", false);
  120. Session.set("reportTitle", false);
  121. Session.set("reportAuthor", false);
  122. Session.set("reportDuration", false);
  123. Session.set("reportAudio", false);
  124. Session.set("reportAlbumart", false);
  125. Session.set("reportOther", false);
  126. Session.set("si_or_pl", "singleVideo");
  127. Session.set("editingSong", false);
  128. var parts = location.href.split('/');
  129. var id = parts.pop();
  130. var type = id.toLowerCase();
  131. if (resizeSeekerbarInterval !== undefined) {
  132. Meteor.clearInterval(resizeSeekerbarInterval);
  133. resizeSeekerbarInterval = undefined;
  134. }
  135. YTPlayer = undefined;
  136. Session.set("videoHidden", false);
  137. var tag = document.createElement("script");
  138. tag.src = "https://www.youtube.com/iframe_api";
  139. var firstScriptTag = document.getElementsByTagName('script')[0];
  140. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  141. Session.set("singleVideo", true);
  142. var currentSong = undefined;
  143. var currentSongR = undefined;
  144. function getTimeElapsed() {
  145. var type = Session.get("type");
  146. if (currentSong !== undefined) {
  147. var room = Rooms.findOne({type: type});
  148. if (room !== undefined) {
  149. return Date.now() - currentSong.started - room.timePaused;
  150. }
  151. }
  152. return 0;
  153. }
  154. function getSongInfo(songData){
  155. Session.set("title", songData.title);
  156. Session.set("artist", songData.artist);
  157. Session.set("id", songData.id);
  158. $("#song-img").attr("src", songData.img);
  159. Session.set("duration", parseInt(songData.duration));
  160. var d = moment.duration(parseInt(songData.duration), 'seconds');
  161. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  162. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  163. document.title = Session.get("title") + " - " + Session.get("artist") + " - Musare";
  164. }
  165. function resizeSeekerbar() {
  166. if (Session.get("state") === "playing") {
  167. $(".seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  168. }
  169. }
  170. function startSong() {
  171. $("#time-elapsed").text("0:00");
  172. $("#vote-skip").attr("disabled", false);
  173. if (currentSong !== undefined) {
  174. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  175. var volume = localStorage.getItem("volume") || 20;
  176. $("#volume_slider").val(volume);
  177. $("#player").show();
  178. function loadVideo() {
  179. if (!Session.get("YTLoaded")) {
  180. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  181. loadVideo();
  182. }, 500));
  183. } else {
  184. if (YTPlayer === undefined) {
  185. if (YT !== undefined && YT !== null && YT.Player !== undefined) {
  186. YTPlayer = new YT.Player("player", {
  187. height: 270,
  188. width: 480,
  189. videoId: currentSong.id,
  190. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  191. events: {
  192. 'onReady': function (event) {
  193. if (currentSong.skipDuration === undefined) {
  194. currentSong.skipDuration = 0;
  195. }
  196. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  197. event.target.playVideo();
  198. event.target.setVolume(volume);
  199. function recursion() {
  200. Meteor.setTimeout(function() {
  201. if (event.target.getPlayerState() === 1 && Session.get("state") === "playing") {
  202. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  203. } else {
  204. recursion();
  205. }
  206. }, 200);
  207. }
  208. recursion();
  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. setTimeout(function() {
  227. startSong();
  228. }, 500);
  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. } else if (Session.get("pauseVideo") === true) {
  288. Session.set("pauseVideo", false);
  289. }
  290. var d = moment.duration(duration, 'seconds');
  291. if (Session.get("state") === "playing") {
  292. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  293. }
  294. }
  295. }
  296. }, 100));
  297. resizeSeekerbarInterval = Meteor.setInterval(function () {
  298. resizeSeekerbar();
  299. }, 500)
  300. }
  301. });
  302. Meteor.setTimeout(function(){
  303. $("#playlist-slideout").on("click", function(){
  304. if($("#chat-slide-out").css("right") === "0px"){
  305. $("#chat-slideout").sideNav("hide");
  306. }
  307. else if($("#users-slide-out").css("right") === "0px"){
  308. $("#users-slideout").sideNav("hide");
  309. }
  310. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  311. $(".room-container").css("margin-right", "370px")
  312. if($("#playlist-slide-out").css("right") === "0px"){
  313. $(".room-container").css("margin-right", marginRightWidth);
  314. }
  315. });
  316. $("#chat-slideout").on("click", function(){
  317. if($("#playlist-slide-out").css("right") === "0px"){
  318. $("#playlist-slideout").sideNav("hide");
  319. }
  320. else if($("#users-slide-out").css("right") === "0px"){
  321. $("#users-slideout").sideNav("hide");
  322. }
  323. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  324. $(".chat-ul").scrollTop(1000000);
  325. $(".room-container").css("margin-right", "370px")
  326. if($("#chat-slide-out").css("right") === "0px"){
  327. $(".room-container").css("margin-right", marginRightWidth);
  328. }
  329. });
  330. $("#users-slideout").on("click", function(){
  331. if($("#playlist-slide-out").css("right") === "0px"){
  332. $("#playlist-slideout").sideNav("hide");
  333. }
  334. else if($("#chat-slide-out").css("right") === "0px"){
  335. $("#chat-slideout").sideNav("hide");
  336. }
  337. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  338. $(".room-container").css("margin-right", "370px")
  339. if($("#users-slide-out").css("right") === "0px"){
  340. $(".room-container").css("margin-right", marginRightWidth);
  341. }
  342. });
  343. $("body").on("click", function(e){
  344. });
  345. $(window).on("resize", function(){
  346. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  347. $(".container").css("margin-right", marginRightWidth);
  348. })
  349. }, 1000);
  350. });
  351. Template.communityStation.onCreated(function () {
  352. Chat.after.find(function(userId, selector) {
  353. if (selector.type === "global") {
  354. if (!$("#global-chat-tab").hasClass("active")) {
  355. $("#global-chat-tab").addClass("unread-messages");
  356. }
  357. }
  358. });
  359. var parts = location.href.split('/');
  360. var id = parts.pop();
  361. var name = id.toLowerCase();
  362. if (resizeSeekerbarInterval !== undefined) {
  363. Meteor.clearInterval(resizeSeekerbarInterval);
  364. resizeSeekerbarInterval = undefined;
  365. }
  366. YTPlayer = undefined;
  367. Session.set("videoHidden", false);
  368. var tag = document.createElement("script");
  369. tag.src = "https://www.youtube.com/iframe_api";
  370. var firstScriptTag = document.getElementsByTagName('script')[0];
  371. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  372. Session.set("singleVideo", true);
  373. var currentSong = undefined;
  374. var currentSongR = undefined;
  375. function getTimeElapsed() {
  376. var name = Session.get("CommunityStationName");
  377. if (currentSong !== undefined) {
  378. var room = CommunityStations.findOne({name: name});
  379. if (room !== undefined) {
  380. return Date.now() - currentSong.started - room.timePaused;
  381. }
  382. }
  383. return 0;
  384. }
  385. function getSongInfo(songData){
  386. Session.set("title", songData.title);
  387. Session.set("id", songData.id);
  388. Session.set("duration", parseInt(songData.duration));
  389. var d = moment.duration(parseInt(songData.duration), 'seconds');
  390. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  391. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  392. document.title = Session.get("title") + " - Musare";
  393. }
  394. function resizeSeekerbar() {
  395. if (Session.get("state") === "playing") {
  396. $(".seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  397. }
  398. }
  399. function startSong() {
  400. $("#time-elapsed").text("0:00");
  401. $("#vote-skip").attr("disabled", false);
  402. if (currentSong !== undefined) {
  403. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined && YTPlayer.getPlayerState() === YT.PlayerState.PLAYING) YTPlayer.stopVideo();
  404. var volume = localStorage.getItem("volume") || 20;
  405. $("#volume_slider").val(volume);
  406. $("#player").show();
  407. function loadVideo() {
  408. if (!Session.get("YTLoaded")) {
  409. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  410. loadVideo();
  411. }, 500));
  412. } else {
  413. if (YTPlayer === undefined) {
  414. if (YT !== undefined && YT !== null && YT.Player !== undefined) {
  415. YTPlayer = new YT.Player("player", {
  416. height: 270,
  417. width: 480,
  418. videoId: currentSong.id,
  419. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  420. events: {
  421. 'onReady': function (event) {
  422. event.target.seekTo(getTimeElapsed() / 1000);
  423. event.target.playVideo();
  424. event.target.setVolume(volume);
  425. function recursion() {
  426. Meteor.setTimeout(function() {
  427. if (event.target.getPlayerState() === 1 && Session.get("state") === "playing") {
  428. event.target.seekTo(getTimeElapsed() / 1000);
  429. } else {
  430. recursion();
  431. }
  432. }, 200);
  433. }
  434. recursion();
  435. resizeSeekerbar();
  436. },
  437. 'onStateChange': function (event) {
  438. if (Session.get("YTLoaded")) {
  439. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing" && !Session.get("noCurrentSong")) {
  440. event.target.seekTo(getTimeElapsed() / 1000);
  441. event.target.playVideo();
  442. }
  443. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  444. event.target.seekTo(getTimeElapsed() / 1000);
  445. event.target.pauseVideo();
  446. }
  447. }
  448. }
  449. }
  450. });
  451. } else {
  452. setTimeout(function() {
  453. startSong();
  454. }, 500);
  455. }
  456. } else {
  457. YTPlayer.loadVideoById(currentSong.id);
  458. YTPlayer.seekTo(getTimeElapsed() / 1000);
  459. $("#vote-skip").removeClass("disabled");
  460. }
  461. Session.set("pauseVideo", false);
  462. getSongInfo(currentSong);
  463. }
  464. }
  465. loadVideo();
  466. }
  467. }
  468. Session.set("loaded", false);
  469. Meteor.subscribe("community_stations", function() {
  470. var parts = location.href.split('/');
  471. var id = parts.pop();
  472. var name = id.toLowerCase();
  473. Session.set("CommunityStationName", name);
  474. if (CommunityStations.find({name: name}).count() !== 1) {
  475. window.location = "/";
  476. } else {
  477. StationSubscription = Meteor.subscribe("pr_" + name);
  478. Session.set("loaded", true);
  479. Session.set("minterval", Meteor.setInterval(function () {
  480. var room = CommunityStations.findOne({name: name});
  481. if (room !== undefined) {
  482. if (room.state === "paused" || Session.get("pauseVideo")) {
  483. Session.set("state", "paused");
  484. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  485. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  486. YTPlayer.pauseVideo();
  487. }
  488. } else {
  489. Session.set("state", "playing");
  490. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1 && !Session.get("noCurrentSong")) {
  491. YTPlayer.playVideo();
  492. }
  493. }
  494. }
  495. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  496. Session.set("previousSong", currentSong);
  497. if (currentSong !== undefined) {
  498. var coStation = CommunityStations.findOne({name: name});
  499. if (coStation.partyModeEnabled) {
  500. var hasSongInQueue = false;
  501. coStation.queue.forEach(function (queueSong) {
  502. if (Meteor.userId() === queueSong.requestedBy) {
  503. hasSongInQueue = true;
  504. }
  505. });
  506. if (!hasSongInQueue) {
  507. var playlistQueueName = Session.get("playlistQueueName");
  508. var playlistQueueCurrentSong = Session.get("playlistQueueCurrentSong");
  509. if (playlistQueueCurrentSong !== undefined) {
  510. if (playlistQueueCurrentSong.id === currentSong.id) {
  511. if (playlistQueueName !== undefined) {
  512. // If someone selects a different playlist this won't work. This is fine.
  513. Meteor.call("moveVideoToBottomOfPrivatePlaylist", playlistQueueName, playlistQueueCurrentSong.id, function () {
  514. var pl = PrivatePlaylists.findOne({
  515. owner: Meteor.userId(),
  516. name: playlistQueueName
  517. });
  518. if (pl !== undefined) {
  519. var plSongs = pl.songs;
  520. var plSong;
  521. if (plSongs.length === 1) {
  522. plSong = plSongs[0];
  523. } else if (plSongs.length > 1) {
  524. if (plSongs[0].id === playlistQueueCurrentSong.id) {
  525. plSong = plSongs[1];
  526. } else {
  527. plSong = plSongs[0];
  528. }
  529. }
  530. // Add song to queue
  531. if (plSong !== undefined) {
  532. var isAlreadyInQueue = false;
  533. coStation.queue.forEach(function (queueSong) {
  534. if (plSong.id === queueSong.song.id) {
  535. isAlreadyInQueue = true;
  536. }
  537. });
  538. if (isAlreadyInQueue) {
  539. // If the song we want to add is already in the queue, we moved the song to the bottom a new song will get added next song.
  540. Meteor.call("moveVideoToBottomOfPrivatePlaylist", playlistQueueName, plSong.id, function () {});
  541. Session.set("playlistQueueCurrentSong", undefined);
  542. var $toastContent = $('<span>The top song in your selected playlist is already in the playlist. We will attempt to add a different song when the current song ends.</span>');
  543. Materialize.toast($toastContent, 8000);
  544. } else {
  545. Meteor.call("addSongToCommunityStationQueue", Session.get("CommunityStationName"), plSong.id, function (err) {
  546. if (!err) {
  547. Session.set("playlistQueueCurrentSong", plSong);
  548. }
  549. });
  550. }
  551. }
  552. }
  553. });
  554. }
  555. } else {
  556. var contains = false;
  557. coStation.queue.forEach(function (queueSong) {
  558. if (playlistQueueCurrentSong.id === queueSong.song.id) {
  559. contains = true;
  560. }
  561. });
  562. if (!contains) {
  563. var pl = PrivatePlaylists.findOne({
  564. owner: Meteor.userId(),
  565. name: playlistQueueName
  566. });
  567. if (pl !== undefined) {
  568. var plSongs = pl.songs;
  569. var plSong = plSongs[0];
  570. // Add song to queue
  571. if (plSong !== undefined) {
  572. Meteor.call("addSongToCommunityStationQueue", Session.get("CommunityStationName"), plSong.id, function (err) {
  573. if (!err) {
  574. Session.set("playlistQueueCurrentSong", plSong);
  575. }
  576. });
  577. }
  578. }
  579. }
  580. }
  581. } else {
  582. var pl = PrivatePlaylists.findOne({
  583. owner: Meteor.userId(),
  584. name: playlistQueueName
  585. });
  586. if (pl !== undefined) {
  587. var plSongs = pl.songs;
  588. var plSong = plSongs[0];
  589. // Add song to queue
  590. if (plSong !== undefined) {
  591. Meteor.call("addSongToCommunityStationQueue", Session.get("CommunityStationName"), plSong.id, function (err) {
  592. if (!err) {
  593. Session.set("playlistQueueCurrentSong", plSong);
  594. }
  595. });
  596. }
  597. }
  598. }
  599. }
  600. }
  601. }
  602. currentSongR = room.currentSong;
  603. if (!_.isEqual(currentSongR, {})) {
  604. Session.set("noCurrentSong", false);
  605. currentSong = room.currentSong.song;
  606. currentSong.started = room.currentSong.started;
  607. Session.set("currentSong", currentSong);
  608. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  609. startSong();
  610. } else {
  611. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined && YTPlayer.getPlayerState() === YT.PlayerState.PLAYING) YTPlayer.stopVideo();
  612. document.title = "Musare";
  613. Session.set("noCurrentSong", true);
  614. }
  615. }
  616. if (currentSong !== undefined && !Session.get("noCurrentSong")) {
  617. if (room !== undefined) {
  618. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  619. var song_duration = currentSong.duration;
  620. if (song_duration <= duration) {
  621. Session.set("pauseVideo", true);
  622. } else if (Session.get("pauseVideo") === true) {
  623. Session.set("pauseVideo", false);
  624. }
  625. var d = moment.duration(duration, 'seconds');
  626. if (Session.get("state") === "playing") {
  627. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  628. }
  629. }
  630. }
  631. }, 100));
  632. resizeSeekerbarInterval = Meteor.setInterval(function () {
  633. resizeSeekerbar();
  634. }, 500)
  635. }
  636. });
  637. Meteor.setTimeout(function(){
  638. $("#playlist-slideout").on("click", function(){
  639. if($("#chat-slide-out").css("right") === "0px"){
  640. $("#chat-slideout").sideNav("hide");
  641. }
  642. else if($("#users-slide-out").css("right") === "0px"){
  643. $("#users-slideout").sideNav("hide");
  644. }
  645. else if($("#allowed-slide-out").css("right") === "0px"){
  646. $("#allowed-slideout").sideNav("hide");
  647. }
  648. else if($("#playlists-slide-out").css("right") === "0px"){
  649. $("#playlists-slideout").sideNav("hide");
  650. }
  651. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  652. $(".room-container").css("margin-right", "370px")
  653. if($("#playlist-slide-out").css("right") === "0px"){
  654. $(".room-container").css("margin-right", marginRightWidth);
  655. }
  656. });
  657. $("#chat-slideout").on("click", function(){
  658. if($("#playlist-slide-out").css("right") === "0px"){
  659. $("#playlist-slideout").sideNav("hide");
  660. }
  661. else if($("#users-slide-out").css("right") === "0px"){
  662. $("#users-slideout").sideNav("hide");
  663. }
  664. else if($("#allowed-slide-out").css("right") === "0px"){
  665. $("#allowed-slideout").sideNav("hide");
  666. }
  667. else if($("#playlists-slide-out").css("right") === "0px"){
  668. $("#playlists-slideout").sideNav("hide");
  669. }
  670. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  671. $(".chat-ul").scrollTop(1000000);
  672. $(".room-container").css("margin-right", "370px")
  673. if($("#chat-slide-out").css("right") === "0px"){
  674. $(".room-container").css("margin-right", marginRightWidth);
  675. }
  676. });
  677. $("#users-slideout").on("click", function(){
  678. if($("#playlist-slide-out").css("right") === "0px"){
  679. $("#playlist-slideout").sideNav("hide");
  680. }
  681. else if($("#chat-slide-out").css("right") === "0px"){
  682. $("#chat-slideout").sideNav("hide");
  683. }
  684. else if($("#allowed-slide-out").css("right") === "0px"){
  685. $("#allowed-slideout").sideNav("hide");
  686. }
  687. else if($("#playlists-slide-out").css("right") === "0px"){
  688. $("#playlists-slideout").sideNav("hide");
  689. }
  690. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  691. $(".room-container").css("margin-right", "370px")
  692. if($("#users-slide-out").css("right") === "0px"){
  693. $(".room-container").css("margin-right", marginRightWidth);
  694. }
  695. });
  696. $("#allowed-slideout").on("click", function(){
  697. if($("#playlist-slide-out").css("right") === "0px"){
  698. $("#playlist-slideout").sideNav("hide");
  699. }
  700. else if($("#chat-slide-out").css("right") === "0px"){
  701. $("#chat-slideout").sideNav("hide");
  702. }
  703. else if($("#users-slide-out").css("right") === "0px"){
  704. $("#users-slideout").sideNav("hide");
  705. }
  706. else if($("#playlists-slide-out").css("right") === "0px"){
  707. $("#playlists-slideout").sideNav("hide");
  708. }
  709. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  710. $(".room-container").css("margin-right", "370px")
  711. if($("#allowed-slide-out").css("right") === "0px"){
  712. $(".room-container").css("margin-right", marginRightWidth);
  713. }
  714. });
  715. $("#playlists-slideout").on("click", function(){
  716. if($("#playlist-slide-out").css("right") === "0px"){
  717. $("#playlist-slideout").sideNav("hide");
  718. }
  719. else if($("#chat-slide-out").css("right") === "0px"){
  720. $("#chat-slideout").sideNav("hide");
  721. }
  722. else if($("#users-slide-out").css("right") === "0px"){
  723. $("#users-slideout").sideNav("hide");
  724. }
  725. else if($("#allowed-slide-out").css("right") === "0px"){
  726. $("#allowed-slideout").sideNav("hide");
  727. }
  728. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  729. $(".room-container").css("margin-right", "370px")
  730. if($("#playlists-slide-out").css("right") === "0px"){
  731. $(".room-container").css("margin-right", marginRightWidth);
  732. }
  733. });
  734. $("body").on("click", function(e){
  735. });
  736. $(window).on("resize", function(){
  737. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  738. $(".container").css("margin-right", marginRightWidth);
  739. })
  740. }, 1000);
  741. });
  742. Template.settings.onCreated(function() {
  743. $(document).ready(function() {
  744. var user = Meteor.user();
  745. function initSettings() {
  746. if (user !== undefined) {
  747. if (user.profile.settings && user.profile.settings.showRating === true) {
  748. function setChecked() {
  749. $("#showRating").prop("checked", true);
  750. if (!$("#showRating").prop("checked")) {
  751. Meteor.setTimeout(function() {
  752. setChecked();
  753. }, 100);
  754. }
  755. }
  756. setChecked();
  757. }
  758. } else {
  759. Meteor.setTimeout(function() {
  760. initSettings();
  761. }, 500);
  762. }
  763. }
  764. initSettings();
  765. });
  766. });