client.js 45 KB

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