client.js 45 KB

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