client.js 41 KB

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