2
0

client.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. Meteor.startup(function() {
  2. reCAPTCHA.config({
  3. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  4. });
  5. });
  6. Meteor.subscribe("queues");
  7. Meteor.subscribe("chat");
  8. Meteor.subscribe("playlists");
  9. var minterval;
  10. var hpSound = undefined;
  11. var songsArr = [];
  12. var ytArr = [];
  13. var _sound = undefined;
  14. var parts = location.href.split('/');
  15. var id = parts.pop();
  16. var type = id.toLowerCase();
  17. var resizeSeekerbarInterval;
  18. function getSpotifyInfo(title, cb, artist) {
  19. var q = "";
  20. q = title;
  21. if (artist !== undefined) {
  22. q += " artist:" + artist;
  23. }
  24. $.ajax({
  25. type: "GET",
  26. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  27. applicationType: "application/json",
  28. contentType: "json",
  29. success: function (data) {
  30. cb(data);
  31. }
  32. });
  33. }
  34. Template.profile.helpers({
  35. "username": function() {
  36. return Session.get("username");
  37. },
  38. "first_joined": function() {
  39. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  40. },
  41. "rank": function() {
  42. return Session.get("rank");
  43. },
  44. loaded: function() {
  45. return Session.get("loaded");
  46. }
  47. });
  48. Template.profile.onCreated(function() {
  49. var parts = location.href.split('/');
  50. var username = parts.pop();
  51. Session.set("loaded", false);
  52. Meteor.subscribe("userProfiles", 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("username", data.profile.username);
  58. Session.set("first_joined", data.createdAt);
  59. Session.set("rank", data.profile.rank);
  60. Session.set("loaded", true);
  61. }
  62. });
  63. });
  64. curPath=function(){var c=window.location.pathname;var b=c.slice(0,-1);var a=c.slice(-1);if(b==""){return"/"}else{if(a=="/"){return b}else{return c}}};
  65. Handlebars.registerHelper('active', function(path) {
  66. return curPath() == path ? 'active' : '';
  67. });
  68. Template.header.helpers({
  69. currentUser: function() {
  70. return Meteor.user();
  71. },
  72. isAdmin: function() {
  73. if (Meteor.user() && Meteor.user().profile) {
  74. return Meteor.user().profile.rank === "admin";
  75. } else {
  76. return false;
  77. }
  78. }
  79. });
  80. Template.header.events({
  81. "click .logout": function(e){
  82. e.preventDefault();
  83. Meteor.logout();
  84. if (hpSound !== undefined) {
  85. hpSound.stop();
  86. }
  87. }
  88. });
  89. Template.register.events({
  90. "submit form": function(e){
  91. e.preventDefault();
  92. var username = e.target.registerUsername.value;
  93. var email = e.target.registerEmail.value;
  94. var password = e.target.registerPassword.value;
  95. var captchaData = grecaptcha.getResponse();
  96. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  97. grecaptcha.reset();
  98. if (err) {
  99. console.log(err);
  100. var errAlert = $('<div class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  101. $("#login").after(errAlert);
  102. errAlert.fadeOut(20000, function() {
  103. errAlert.remove();
  104. });
  105. } else {
  106. Meteor.loginWithPassword(username, password);
  107. Accounts.onLogin(function(){
  108. window.location.href = "/";
  109. })
  110. }
  111. });
  112. },
  113. "click #github-login": function(){
  114. Meteor.loginWithGithub()
  115. Accounts.onLogin(function(){
  116. window.location.href = "/"
  117. });
  118. }
  119. });
  120. Template.login.events({
  121. "submit form": function(e){
  122. e.preventDefault();
  123. var username = e.target.loginUsername.value;
  124. var password = e.target.loginPassword.value;
  125. Meteor.loginWithPassword(username, password);
  126. Accounts.onLogin(function(){
  127. window.location.href = "/";
  128. })
  129. Accounts.onLoginFailure(function(){
  130. $("#login-form input").css("background-color","indianred");
  131. $("#login-form input").on("click",function(){
  132. $("#login-form input").css({
  133. "-webkit-appearance": "none",
  134. " -moz-appearance": "none",
  135. " appearance": "none",
  136. "outline": "0",
  137. "border": "1px solid rgba(255, 255, 255, 0.4)",
  138. "background-color": "rgba(255, 255, 255, 0.2)",
  139. "width": "304px",
  140. "border-radius": "3px",
  141. "padding": "10px 15px",
  142. "margin": "0 auto 10px auto",
  143. "display": "block",
  144. "text-align": "center",
  145. "font-size": "18px",
  146. "color": "white",
  147. "-webkit-transition-duration": "0.25s",
  148. " transition-duration": "0.25s",
  149. "font-weight": "300"
  150. });
  151. $("#login-form input:focus").css({
  152. "width": "354px",
  153. "color": "white"
  154. })
  155. $("#login-form input").on("blur", function(){
  156. $(this).css("width", "304px");
  157. })
  158. })
  159. });
  160. },
  161. "click #github-login": function(){
  162. Meteor.loginWithGithub()
  163. Accounts.onLogin(function(){
  164. window.location.href = "/"
  165. });
  166. }
  167. });
  168. Template.dashboard.helpers({
  169. rooms: function() {
  170. return Rooms.find({});
  171. },
  172. currentSong: function() {
  173. var type = this.type;
  174. var room = Rooms.findOne({type: type});
  175. if (room !== undefined) {
  176. return room.currentSong;
  177. } else {
  178. return {};
  179. }
  180. }
  181. });
  182. Template.dashboard.onCreated(function() {
  183. if (_sound !== undefined) _sound.stop();
  184. if (minterval !== undefined) {
  185. Meteor.clearInterval(minterval);
  186. }
  187. if (resizeSeekerbarInterval !== undefined) {
  188. Meteor.clearInterval(resizeSeekerbarInterval);
  189. resizeSeekerbarInterval = undefined;
  190. }
  191. Session.set("type", undefined);
  192. });
  193. Template.room.events({
  194. "click #submit": function() {
  195. Meteor.call("sendMessage", Session.get("type"), $("#chat-input").val(), function(err, res) {
  196. console.log(err, res);
  197. if (res) {
  198. $("#chat-input").val("");
  199. }
  200. });
  201. },
  202. "keyup #chat-input": function(e) {
  203. if (e.type == "keyup" && e.which == 13) {
  204. e.preventDefault();
  205. Meteor.call("sendMessage", Session.get("type"), $("#chat-input").val(), function(err, res) {
  206. console.log(err, res);
  207. if (res) {
  208. $("#chat-input").val("");
  209. }
  210. });
  211. }
  212. },
  213. "click #like": function(e) {
  214. Meteor.call("likeSong", Session.get("currentSong").mid);
  215. },
  216. "click #dislike": function(e) {
  217. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  218. },
  219. "click #report-prev": function(e) {
  220. if (Session.get("previousSong") !== undefined) {
  221. Session.set("reportPrevious", true);
  222. $("#report-prev").prop("disabled", true);
  223. $("#report-curr").prop("disabled", false);
  224. }
  225. },
  226. "click #report-curr": function(e) {
  227. Session.set("reportPrevious", false);
  228. $("#report-prev").prop("disabled", false);
  229. $("#report-curr").prop("disabled", true);
  230. },
  231. "click #report-modal": function() {
  232. Session.set("currentSongR", Session.get("currentSong"));
  233. Session.set("previousSongR", Session.get("previousSong"));
  234. },
  235. "click #add-song-button": function(e){
  236. e.preventDefault();
  237. parts = location.href.split('/');
  238. var roomType = parts.pop();
  239. var genre = id.toLowerCase();
  240. var type = $("#type").val();
  241. id = $("#id").val();
  242. var title = $("#title").val();
  243. var artist = $("#artist").val();
  244. var img = $("#img").val();
  245. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  246. if(Playlists.find({type: roomType, "songs.title": songData.title}, {songs: {$elemMatch: {title: songData.title}}}).count() !== 0) {
  247. $(".landing").prepend("<div class='alert alert-danger alert-dismissible' role='alert' style='margin-bottom: 0'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'><i class='fa fa-times'></i></span></button><strong>Song not added.</strong> This song is already is in the playlist.</div>");
  248. } else{
  249. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  250. console.log(err, res);
  251. $("#close-modal-a").click();
  252. });
  253. }
  254. },
  255. "click #toggle-video": function(e){
  256. e.preventDefault();
  257. if (Session.get("mediaHidden")) {
  258. $("#media-container").removeClass("hidden");
  259. $("#toggle-video").text("Hide video");
  260. Session.set("mediaHidden", false);
  261. } else {
  262. $("#media-container").addClass("hidden");
  263. $("#toggle-video").text("Show video");
  264. Session.set("mediaHidden", true);
  265. }
  266. },
  267. "click #return": function(e){
  268. $("#add-info").hide();
  269. $("#search-info").show();
  270. },
  271. "click #search-song": function(){
  272. $("#song-results").empty();
  273. var search_type = $("#search_type").val();
  274. if (search_type === "YouTube") {
  275. $.ajax({
  276. type: "GET",
  277. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  278. applicationType: "application/json",
  279. contentType: "json",
  280. success: function(data){
  281. for(var i in data.items){
  282. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>");
  283. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  284. }
  285. $("#song-results p").click(function(){
  286. $("#search-info").hide();
  287. $("#add-info").show();
  288. var title = $(this).text();
  289. for(var i in ytArr){
  290. if(ytArr[i].title === title){
  291. var songObj = {
  292. id: ytArr[i].id,
  293. title: ytArr[i].title,
  294. type: "youtube"
  295. };
  296. $("#title").val(songObj.title);
  297. $("#artist").val("");
  298. $("#id").val(songObj.id);
  299. $("#type").val("YouTube");
  300. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  301. if (data.tracks.items.length > 0) {
  302. $("#title").val(data.tracks.items[0].name);
  303. var artists = [];
  304. $("#img").val(data.tracks.items[0].album.images[1].url);
  305. data.tracks.items[0].artists.forEach(function(artist) {
  306. artists.push(artist.name);
  307. });
  308. $("#artist").val(artists.join(", "));
  309. }
  310. });
  311. }
  312. }
  313. })
  314. }
  315. })
  316. } else if (search_type === "SoundCloud") {
  317. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  318. for(var i in tracks){
  319. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  320. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  321. }
  322. $("#song-results p").click(function(){
  323. $("#search-info").hide();
  324. $("#add-info").show();
  325. var title = $(this).text();
  326. for(var i in songsArr){
  327. if(songsArr[i].title === title){
  328. var id = songsArr[i].id;
  329. var duration = songsArr[i].duration;
  330. var songObj = {
  331. title: songsArr[i].title,
  332. id: id,
  333. duration: duration,
  334. type: "soundcloud"
  335. }
  336. $("#title").val(songObj.title);
  337. // Set ID field
  338. $("#id").val(songObj.id);
  339. $("#type").val("SoundCloud");
  340. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  341. if (data.tracks.items.length > 0) {
  342. $("#title").val(data.tracks.items[0].name);
  343. var artists = [];
  344. data.tracks.items[0].artists.forEach(function(artist) {
  345. artists.push(artist.name);
  346. });
  347. $("#artist").val(artists.join(", "));
  348. }
  349. // Set title field again if possible
  350. // Set artist if possible
  351. });
  352. }
  353. }
  354. })
  355. });
  356. }
  357. },
  358. "click #add-songs": function(){
  359. $("#add-songs-modal").show();
  360. },
  361. "click #close-modal-a": function(){
  362. $("#search-info").show();
  363. $("#add-info").hide();
  364. },
  365. "click #volume-icon": function(){
  366. var volume = 0;
  367. var slider = $("#volume-slider").slider();
  368. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  369. if (yt_player !== undefined) {
  370. yt_player.setVolume(volume);
  371. localStorage.setItem("volume", volume);
  372. $("#volume-slider").slider("setValue", volume);
  373. } else if (_sound !== undefined) {
  374. _sound.setVolume(volume);
  375. localStorage.setItem("volume", volume);
  376. $("#volume-slider").slider("setValue", volume);
  377. }
  378. },
  379. "click #play": function() {
  380. Meteor.call("resumeRoom", type);
  381. },
  382. "click #pause": function() {
  383. Meteor.call("pauseRoom", type);
  384. },
  385. "click #skip": function() {
  386. Meteor.call("skipSong", type);
  387. },
  388. "click #shuffle": function() {
  389. Meteor.call("shufflePlaylist", type);
  390. },
  391. "change input": function(e) {
  392. if (e.target && e.target.id) {
  393. var partsOfId = e.target.id.split("-");
  394. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  395. var camelCase = partsOfId.join("");
  396. Session.set(camelCase, e.target.checked);
  397. }
  398. },
  399. "click #report-song-button": function() {
  400. var report = {};
  401. report.reportSongB = $("#report-song").is(":checked");
  402. report.reportTitleB = $("#report-title").is(":checked");
  403. report.reportAuthorB = $("#report-author").is(":checked");
  404. report.reportDurationB = $("#report-duration").is(":checked");
  405. report.reportAudioB = $("#report-audio").is(":checked");
  406. report.reportAlbumartB = $("#report-albumart").is(":checked");
  407. report.reportOtherB = $("#report-other").is(":checked");
  408. if (report.reportSongB) {
  409. report.reportSong = {};
  410. report.reportSong.notPlayingB = $("#report-song-not-playing").is(":checked");
  411. report.reportSong.doesNotExistB = $("#report-song-does-not-exist").is(":checked");
  412. report.reportSong.otherB = $("#report-song-other").is(":checked");
  413. if (report.reportSong.otherB) {
  414. report.reportSong.other = $("#report-song-other-ta").val();
  415. }
  416. }
  417. if (report.reportTitleB) {
  418. report.reportTitle = {};
  419. report.reportTitle.incorrectB = $("#report-title-incorrect").is(":checked");
  420. report.reportTitle.inappropriateB = $("#report-title-inappropriate").is(":checked");
  421. report.reportTitle.otherB = $("#report-title-other").is(":checked");
  422. if (report.reportTitle.otherB) {
  423. report.reportTitle.other = $("#report-title-other-ta").val();
  424. }
  425. }
  426. if (report.reportAuthorB) {
  427. report.reportAuthor = {};
  428. report.reportAuthor.incorrectB = $("#report-author-incorrect").is(":checked");
  429. report.reportAuthor.inappropriateB = $("#report-author-inappropriate").is(":checked");
  430. report.reportAuthor.otherB = $("#report-author-other").is(":checked");
  431. if (report.reportAuthor.otherB) {
  432. report.reportAuthor.other = $("#report-author-other-ta").val();
  433. }
  434. }
  435. if (report.reportDurationB) {
  436. report.reportDuration = {};
  437. report.reportDuration.longB = $("#report-duration-incorrect").is(":checked");
  438. report.reportDuration.shortB = $("#report-duration-inappropriate").is(":checked");
  439. report.reportDuration.otherB = $("#report-duration-other").is(":checked");
  440. if (report.reportDuration.otherB) {
  441. report.reportDuration.other = $("#report-duration-other-ta").val();
  442. }
  443. }
  444. if (report.reportAudioB) {
  445. report.reportAudio = {};
  446. report.reportAudio.inappropriate = $("#report-audio-inappropriate").is(":checked");
  447. report.reportAudio.notPlayingB = $("#report-audio-incorrect").is(":checked");
  448. report.reportAudio.otherB = $("#report-audio-other").is(":checked");
  449. if (report.reportAudio.otherB) {
  450. report.reportAudio.other = $("#report-audio-other-ta").val();
  451. }
  452. }
  453. if (report.reportAlbumartB) {
  454. report.reportAlbumart = {};
  455. report.reportAlbumart.incorrectB = $("#report-albumart-incorrect").is(":checked");
  456. report.reportAlbumart.inappropriateB = $("#report-albumart-inappropriate").is(":checked");
  457. report.reportAlbumart.notShowingB = $("#report-albumart-inappropriate").is(":checked");
  458. report.reportAlbumart.otherB = $("#report-albumart-other").is(":checked");
  459. if (report.reportAlbumart.otherB) {
  460. report.reportAlbumart.other = $("#report-albumart-other-ta").val();
  461. }
  462. }
  463. if (report.reportOtherB) {
  464. report.other = $("#report-other-ta").val();
  465. }
  466. Meteor.call("submitReport", report, Session.get("id"), function() {
  467. $("#close-modal-r").click();
  468. });
  469. }
  470. });
  471. Template.room.onRendered(function() {
  472. $(document).ready(function() {
  473. function makeSlider(){
  474. var slider = $("#volume-slider").slider();
  475. var volume = localStorage.getItem("volume") || 20;
  476. $("#volume-slider").slider("setValue", volume);
  477. if (slider.length === 0) {
  478. Meteor.setTimeout(function() {
  479. makeSlider();
  480. }, 500);
  481. } else {
  482. slider.on("slide", function(val) {
  483. if (val.value === 0) {
  484. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  485. } else {
  486. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  487. }
  488. if (yt_player !== undefined) {
  489. yt_player.setVolume(val.value);
  490. localStorage.setItem("volume", val.value);
  491. } else if (_sound !== undefined) {
  492. //_sound
  493. var volume = val.value / 100;
  494. _sound.setVolume(volume);
  495. localStorage.setItem("volume", val.value);
  496. }
  497. });
  498. }
  499. }
  500. makeSlider();
  501. });
  502. });
  503. Template.room.helpers({
  504. chat: function() {
  505. Meteor.setTimeout(function() {
  506. var elem = document.getElementById('chat');
  507. if (elem !== undefined && elem !== null) {
  508. elem.scrollTop = elem.scrollHeight;
  509. }
  510. }, 100);
  511. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  512. },
  513. likes: function() {
  514. var playlist = Playlists.findOne({type: Session.get("type")});
  515. var likes = 0;
  516. playlist.songs.forEach(function(song) {
  517. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  518. likes = song.likes;
  519. return;
  520. }
  521. });
  522. return likes;
  523. },
  524. dislikes: function() {
  525. var playlist = Playlists.findOne({type: Session.get("type")});
  526. var dislikes = 0;
  527. playlist.songs.forEach(function(song) {
  528. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  529. dislikes = song.dislikes;
  530. return;
  531. }
  532. });
  533. return dislikes;
  534. },
  535. liked: function() {
  536. if (Meteor.userId()) {
  537. var currentSong = Session.get("currentSong");
  538. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  539. return "active";
  540. } else {
  541. return "";
  542. }
  543. } else {
  544. "";
  545. }
  546. },
  547. disliked: function() {
  548. if (Meteor.userId()) {
  549. var currentSong = Session.get("currentSong");
  550. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  551. return "active";
  552. } else {
  553. return "";
  554. }
  555. } else {
  556. "";
  557. }
  558. },
  559. type: function() {
  560. var parts = location.href.split('/');
  561. var id = parts.pop().toLowerCase();
  562. return Rooms.findOne({type: id}).display;
  563. },
  564. title: function(){
  565. return Session.get("title");
  566. },
  567. artist: function(){
  568. return Session.get("artist");
  569. },
  570. loaded: function() {
  571. return Session.get("loaded");
  572. },
  573. isAdmin: function() {
  574. if (Meteor.user() && Meteor.user().profile) {
  575. return Meteor.user().profile.rank === "admin";
  576. } else {
  577. return false;
  578. }
  579. },
  580. paused: function() {
  581. return Session.get("state") === "paused";
  582. },
  583. report: function() {
  584. return Session.get("reportObj");
  585. },
  586. reportSong: function() {
  587. return Session.get("reportSong");
  588. },
  589. reportTitle: function() {
  590. return Session.get("reportTitle");
  591. },
  592. reportAuthor: function() {
  593. return Session.get("reportAuthor");
  594. },
  595. reportDuration: function() {
  596. return Session.get("reportDuration");
  597. },
  598. reportAudio: function() {
  599. return Session.get("reportAudio");
  600. },
  601. reportAlbumart: function() {
  602. return Session.get("reportAlbumart");
  603. },
  604. reportOther: function() {
  605. return Session.get("reportOther");
  606. },
  607. currentSong: function() {
  608. return Session.get("currentSong");
  609. },
  610. previousSong: function() {
  611. return Session.get("previousSong");
  612. },
  613. currentSongR: function() {
  614. return Session.get("currentSongR");
  615. },
  616. previousSongR: function() {
  617. return Session.get("previousSongR");
  618. },
  619. reportingSong: function() {
  620. if (Session.get("reportPrevious")) {
  621. return Session.get("previousSongR");
  622. } else {
  623. return Session.get("currentSongR");
  624. }
  625. }
  626. });
  627. Template.admin.helpers({
  628. queues: function() {
  629. var queues = Queues.find({}).fetch();
  630. queues.map(function(queue) {
  631. if (Rooms.find({type: queue.type}).count() !== 1) {
  632. return;
  633. } else {
  634. queue.display = Rooms.findOne({type: queue.type}).display;
  635. return queue;
  636. }
  637. });
  638. return queues;
  639. },
  640. users: function(){
  641. Meteor.call("getUserNum", function(err, num){
  642. if(err){
  643. console.log(err);
  644. }
  645. Session.set("userNum", num);
  646. });
  647. return Session.get("userNum");
  648. },
  649. playlists: function() {
  650. var playlists = Playlists.find({}).fetch();
  651. playlists.map(function(playlist) {
  652. if (Rooms.find({type: playlist.type}).count() !== 1) {
  653. return;
  654. } else {
  655. playlist.display = Rooms.findOne({type: playlist.type}).display;
  656. return playlist;
  657. }
  658. });
  659. return playlists;
  660. },
  661. playlistCount: function() {
  662. var playlists = Playlists.find({}).fetch();
  663. playlists.map(function(playlist){
  664. playlist.songs = Rooms.findOne({type: playlist.type}).songs.length;
  665. return playlist;
  666. });
  667. }
  668. });
  669. Template.stations.helpers({
  670. playlists: function() {
  671. var playlists = Playlists.find({}).fetch();
  672. playlists.map(function(playlist) {
  673. if (Rooms.find({type: playlist.type}).count() !== 1) {
  674. return;
  675. } else {
  676. playlist.display = Rooms.findOne({type: playlist.type}).display;
  677. return playlist;
  678. }
  679. });
  680. return playlists;
  681. }
  682. });
  683. var yt_player = undefined;
  684. var _sound = undefined;
  685. Template.stations.events({
  686. "click .preview-button": function(e){
  687. Session.set("song", this);
  688. },
  689. "click .edit-queue-button": function(e){
  690. Session.set("song", this);
  691. Session.set("genre", $(e.toElement).data("genre"));
  692. Session.set("type", "queue");
  693. $("#type").val(this.type);
  694. $("#artist").val(this.artist);
  695. $("#title").val(this.title);
  696. $("#img").val(this.img);
  697. $("#id").val(this.id);
  698. $("#duration").val(this.duration);
  699. },
  700. "click .edit-playlist-button": function(e){
  701. Session.set("song", this);
  702. Session.set("genre", $(e.toElement).data("genre"));
  703. Session.set("type", "playlist");
  704. $("#type").val(this.type);
  705. $("#artist").val(this.artist);
  706. $("#title").val(this.title);
  707. $("#img").val(this.img);
  708. $("#id").val(this.id);
  709. $("#duration").val(this.duration);
  710. },
  711. "click .add-song-button": function(e){
  712. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  713. Meteor.call("addSongToPlaylist", genre, this);
  714. },
  715. "click .deny-song-button": function(e){
  716. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  717. Meteor.call("removeSongFromQueue", genre, this.mid);
  718. },
  719. "click .remove-song-button": function(e){
  720. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  721. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  722. },
  723. "click #play": function() {
  724. $("#play").attr("disabled", true);
  725. $("#stop").attr("disabled", false);
  726. var song = Session.get("song");
  727. var id = song.id;
  728. var type = song.type;
  729. var volume = localStorage.getItem("volume") || 20;
  730. if (type === "YouTube") {
  731. if (yt_player === undefined) {
  732. yt_player = new YT.Player("previewPlayer", {
  733. height: 540,
  734. width: 568,
  735. videoId: id,
  736. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  737. events: {
  738. 'onReady': function(event) {
  739. event.target.playVideo();
  740. event.target.setVolume(volume);
  741. },
  742. 'onStateChange': function(event){
  743. if (event.data == YT.PlayerState.PAUSED) {
  744. event.target.playVideo();
  745. }
  746. if (event.data == YT.PlayerState.PLAYING) {
  747. $("#play").attr("disabled", true);
  748. $("#stop").attr("disabled", false);
  749. } else {
  750. $("#play").attr("disabled", false);
  751. $("#stop").attr("disabled", true);
  752. }
  753. }
  754. }
  755. });
  756. } else {
  757. yt_player.loadVideoById(id);
  758. }
  759. $("#previewPlayer").show();
  760. } else if (type === "SoundCloud") {
  761. SC.stream("/tracks/" + song.id, function(sound) {
  762. _sound = sound;
  763. sound.setVolume(volume / 100);
  764. sound.play();
  765. });
  766. }
  767. },
  768. "click #stop": function() {
  769. $("#play").attr("disabled", false);
  770. $("#stop").attr("disabled", true);
  771. if (yt_player !== undefined) {
  772. yt_player.stopVideo();
  773. }
  774. if (_sound !== undefined) {
  775. _sound.stop();
  776. }
  777. },
  778. "click #croom_create": function() {
  779. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  780. if (err) {
  781. alert("Error " + err.error + ": " + err.reason);
  782. } else {
  783. window.location = "/" + $("#croom_tag").val();
  784. }
  785. });
  786. },
  787. "click #get-spotify-info": function() {
  788. var search = $("#title").val();
  789. var artistName = $("#artist").val();
  790. getSpotifyInfo(search, function(data) {
  791. for(var i in data){
  792. for(var j in data[i].items){
  793. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  794. $("#img").val(data[i].items[j].album.images[1].url);
  795. $("#duration").val(data[i].items[j].duration_ms / 1000);
  796. return;
  797. }
  798. }
  799. }
  800. }, artistName);
  801. },
  802. "click #save-song-button": function() {
  803. var newSong = {};
  804. newSong.id = $("#id").val();
  805. newSong.title = $("#title").val();
  806. newSong.artist = $("#artist").val();
  807. newSong.img = $("#img").val();
  808. newSong.type = $("#type").val();
  809. newSong.duration = $("#duration").val();
  810. if (Session.get("type") === "playlist") {
  811. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  812. $('#editModal').modal('hide');
  813. });
  814. } else {
  815. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  816. $('#editModal').modal('hide');
  817. });
  818. }
  819. },
  820. "click .delete-room": function(){
  821. var typeDel = $(this)[0].type;
  822. Meteor.call("deleteRoom", typeDel);
  823. }
  824. });
  825. Template.stations.onCreated(function() {
  826. var tag = document.createElement("script");
  827. tag.src = "https://www.youtube.com/iframe_api";
  828. var firstScriptTag = document.getElementsByTagName('script')[0];
  829. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  830. });
  831. Template.stations.onRendered(function() {
  832. $("#previewModal").on("hidden.bs.modal", function() {
  833. if (yt_player !== undefined) {
  834. $("#play").attr("disabled", false);
  835. $("#stop").attr("disabled", true);
  836. $("#previewPlayer").hide();
  837. yt_player.loadVideoById("", 0);
  838. yt_player.seekTo(0);
  839. yt_player.stopVideo();
  840. }
  841. if (_sound !== undefined) {
  842. _sound.stop();
  843. $("#play").attr("disabled", false);
  844. $("#stop").attr("disabled", true);
  845. }
  846. });
  847. $(document).ready(function() {
  848. function makeSlider(){
  849. var slider = $("#volume-slider").slider();
  850. var volume = localStorage.getItem("volume") || 20;
  851. $("#volume-slider").slider("setValue", volume);
  852. if (slider.length === 0) {
  853. Meteor.setTimeout(function() {
  854. makeSlider();
  855. }, 500);
  856. } else {
  857. slider.on("slide", function(val) {
  858. localStorage.setItem("volume", val.value);
  859. if (yt_player !== undefined) {
  860. yt_player.setVolume(val.value);
  861. } else if (_sound !== undefined) {
  862. var volume = val.value / 100;
  863. _sound.setVolume(volume);
  864. }
  865. });
  866. }
  867. }
  868. makeSlider();
  869. });
  870. });
  871. Template.playlist.helpers({
  872. playlist_songs: function() {
  873. parts = location.href.split('/');
  874. id = parts.pop();
  875. type = id.toLowerCase();
  876. var data = Playlists.findOne({type: type});
  877. if (data !== undefined) {
  878. data.songs.map(function(song) {
  879. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  880. song.current = true;
  881. } else {
  882. song.current = false;
  883. }
  884. return song;
  885. });
  886. return data.songs;
  887. } else {
  888. return [];
  889. }
  890. }
  891. });
  892. Meteor.subscribe("rooms");
  893. Template.room.onCreated(function () {
  894. Session.set("reportSong", false);
  895. Session.set("reportTitle", false);
  896. Session.set("reportAuthor", false);
  897. Session.set("reportDuration", false);
  898. Session.set("reportAudio", false);
  899. Session.set("reportAlbumart", false);
  900. Session.set("reportOther", false);
  901. if (resizeSeekerbarInterval !== undefined) {
  902. Meteor.clearInterval(resizeSeekerbarInterval);
  903. resizeSeekerbarInterval = undefined;
  904. }
  905. yt_player = undefined;
  906. _sound = undefined;
  907. Session.set("videoHidden", false);
  908. var tag = document.createElement("script");
  909. tag.src = "https://www.youtube.com/iframe_api";
  910. var firstScriptTag = document.getElementsByTagName('script')[0];
  911. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  912. var currentSong = undefined;
  913. var currentSongR = undefined;
  914. function getTimeElapsed() {
  915. if (currentSong !== undefined) {
  916. var room = Rooms.findOne({type: type});
  917. if (room !== undefined) {
  918. return Date.now() - currentSong.started - room.timePaused;
  919. }
  920. }
  921. return 0;
  922. }
  923. function getSongInfo(songData){
  924. Session.set("title", songData.title);
  925. Session.set("artist", songData.artist);
  926. Session.set("id", songData.id);
  927. $("#song-img").attr("src", songData.img);
  928. Session.set("duration", parseInt(songData.duration));
  929. var d = moment.duration(parseInt(songData.duration), 'seconds');
  930. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  931. }
  932. function resizeSeekerbar() {
  933. if (Session.get("state") === "playing") {
  934. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  935. }
  936. }
  937. function startSong() {
  938. $("#time-elapsed").text("0:00");
  939. if (currentSong !== undefined) {
  940. if (_sound !== undefined) _sound.stop();
  941. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  942. var volume = localStorage.getItem("volume") || 20;
  943. $("#media-container").empty();
  944. yt_player = undefined;
  945. if (currentSong.type === "SoundCloud") {
  946. $("#media-container").append('<img src="/soundcloud-image.png" class="embed-responsive-item" />');
  947. getSongInfo(currentSong);
  948. SC.stream("/tracks/" + currentSong.id, function(sound){
  949. _sound = sound;
  950. sound.setVolume(volume / 100);
  951. sound.play();
  952. var interval = setInterval(function() {
  953. if (sound.getState() === "playing") {
  954. sound.seek(getTimeElapsed());
  955. window.clearInterval(interval);
  956. }
  957. }, 200);
  958. Session.set("duration", parseInt(currentSong.duration));
  959. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  960. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  961. resizeSeekerbar();
  962. });
  963. } else {
  964. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  965. if (yt_player === undefined) {
  966. yt_player = new YT.Player("player", {
  967. height: 540,
  968. width: 960,
  969. videoId: currentSong.id,
  970. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  971. events: {
  972. 'onReady': function(event) {
  973. event.target.seekTo(getTimeElapsed() / 1000);
  974. event.target.playVideo();
  975. event.target.setVolume(volume);
  976. resizeSeekerbar();
  977. },
  978. 'onStateChange': function(event){
  979. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  980. event.target.seekTo(getTimeElapsed() / 1000);
  981. event.target.playVideo();
  982. }
  983. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  984. event.target.seekTo(getTimeElapsed() / 1000);
  985. event.target.pauseVideo();
  986. }
  987. }
  988. }
  989. });
  990. } else {
  991. yt_player.loadVideoById(currentSong.id);
  992. }
  993. getSongInfo(currentSong);
  994. }
  995. }
  996. }
  997. Session.set("loaded", false);
  998. Meteor.subscribe("rooms", function() {
  999. var parts = location.href.split('/');
  1000. var id = parts.pop();
  1001. var type = id.toLowerCase();
  1002. Session.set("type", type);
  1003. if (Rooms.find({type: type}).count() !== 1) {
  1004. window.location = "/";
  1005. } else {
  1006. Session.set("loaded", true);
  1007. minterval = Meteor.setInterval(function () {
  1008. var room = Rooms.findOne({type: type});
  1009. if (room !== undefined) {
  1010. if (room.state === "paused") {
  1011. Session.set("state", "paused");
  1012. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() === 1) {
  1013. yt_player.pauseVideo();
  1014. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  1015. _sound.pause();
  1016. }
  1017. } else {
  1018. Session.set("state", "playing");
  1019. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() !== 1) {
  1020. yt_player.playVideo();
  1021. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  1022. _sound.play();
  1023. }
  1024. }
  1025. }
  1026. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  1027. Session.set("previousSong", currentSong);
  1028. currentSongR = room.currentSong;
  1029. currentSong = room.currentSong.song;
  1030. currentSong.started = room.currentSong.started;
  1031. Session.set("currentSong", currentSong);
  1032. startSong();
  1033. }
  1034. if (currentSong !== undefined) {
  1035. if (room !== undefined) {
  1036. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1037. var d = moment.duration(duration, 'seconds');
  1038. if (Session.get("state") === "playing") {
  1039. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1040. }
  1041. }
  1042. }
  1043. }, 1000);
  1044. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1045. resizeSeekerbar();
  1046. }, 500);
  1047. }
  1048. });
  1049. });