client.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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. /*UI.registerHelper("formatTimeFromNow", function(time) {
  27. var d = moment(time);
  28. return d.fromNow();
  29. });*/
  30. function getSpotifyInfo(title, cb, artist) {
  31. var q = "";
  32. q = title;
  33. if (artist !== undefined) {
  34. q += " artist:" + artist;
  35. }
  36. $.ajax({
  37. type: "GET",
  38. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  39. applicationType: "application/json",
  40. contentType: "json",
  41. success: function (data) {
  42. cb(data);
  43. }
  44. });
  45. }
  46. Template.settings.events({
  47. "click #save-settings": function() {
  48. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  49. }
  50. });
  51. Template.profile.helpers({
  52. "username": function() {
  53. return Session.get("username");
  54. },
  55. "first_joined": function() {
  56. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  57. },
  58. "rank": function() {
  59. return Session.get("rank");
  60. },
  61. loaded: function() {
  62. return Session.get("loaded");
  63. },
  64. likedSongs: function(){
  65. var likedArr = [];
  66. Session.get("liked").forEach(function(mid){
  67. Rooms.find().forEach(function(room){
  68. Playlists.find({type: room.type}).forEach(function(pl){
  69. for(var i in pl.songs){
  70. if(pl.songs[i].mid === mid){
  71. likedArr.push({title: pl.songs[i].title, artist: pl.songs[i].artist, room: room.display});
  72. }
  73. }
  74. });
  75. })
  76. });
  77. return likedArr;
  78. },
  79. dislikedSongs: function(){
  80. var dislikedArr = [];
  81. Session.get("disliked").forEach(function(mid){
  82. Rooms.find().forEach(function(room){
  83. Playlists.find({type: room.type}).forEach(function(pl){
  84. for(var i in pl.songs){
  85. if(pl.songs[i].mid === mid){
  86. dislikedArr.push({title: pl.songs[i].title, artist: pl.songs[i].artist, room: room.display});
  87. }
  88. }
  89. });
  90. })
  91. });
  92. return dislikedArr;
  93. }
  94. });
  95. Template.profile.onCreated(function() {
  96. var parts = Router.current().url.split('/');
  97. var username = parts.pop();
  98. Session.set("loaded", false);
  99. Meteor.subscribe("userProfiles", username.toLowerCase(), function() {
  100. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  101. window.location = "/";
  102. } else {
  103. var data = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  104. Session.set("username", data.profile.username);
  105. Session.set("first_joined", data.createdAt);
  106. Session.set("rank", data.profile.rank);
  107. Session.set("liked", data.profile.liked);
  108. Session.set("disliked", data.profile.disliked);
  109. Session.set("loaded", true);
  110. }
  111. });
  112. });
  113. Template.settings.helpers({
  114. username: function() {
  115. if (Meteor.user() !== undefined) {
  116. return Meteor.user().profile.username;
  117. } else {
  118. return "";
  119. }
  120. }
  121. });
  122. Template.settings.onCreated(function() {
  123. $(document).ready(function() {
  124. var user = Meteor.user();
  125. function initSettings() {
  126. if (user !== undefined) {
  127. if (user.profile.settings && user.profile.settings.showRating === true) {
  128. function setChecked() {
  129. $("#showRating").prop("checked", true);
  130. if (!$("#showRating").prop("checked")) {
  131. Meteor.setTimeout(function() {
  132. setChecked();
  133. }, 100);
  134. }
  135. }
  136. setChecked();
  137. }
  138. } else {
  139. Meteor.setTimeout(function() {
  140. initSettings();
  141. }, 500);
  142. }
  143. }
  144. initSettings();
  145. });
  146. });
  147. 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}}};
  148. Handlebars.registerHelper('active', function(path) {
  149. return curPath() == path ? 'active' : '';
  150. });
  151. Template.header.helpers({
  152. currentUser: function() {
  153. return Meteor.user();
  154. },
  155. isAdmin: function() {
  156. if (Meteor.user() && Meteor.user().profile) {
  157. return Meteor.user().profile.rank === "admin";
  158. } else {
  159. return false;
  160. }
  161. },
  162. isModerator: function() {
  163. if (Meteor.user() && Meteor.user().profile && (Meteor.user().profile.rank === "admin" || Meteor.user().profile.rank === "moderator")) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. },
  169. });
  170. Template.header.events({
  171. "click .logout": function(e){
  172. e.preventDefault();
  173. Meteor.logout();
  174. if (hpSound !== undefined) {
  175. hpSound.stop();
  176. }
  177. }
  178. });
  179. Template.register.events({
  180. "submit form": function(e){
  181. e.preventDefault();
  182. var username = e.target.registerUsername.value;
  183. var email = e.target.registerEmail.value;
  184. var password = e.target.registerPassword.value;
  185. var captchaData = grecaptcha.getResponse();
  186. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  187. grecaptcha.reset();
  188. if (err) {
  189. console.log(err);
  190. var errAlert = $('<div class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  191. $("#login").after(errAlert);
  192. errAlert.fadeOut(20000, function() {
  193. errAlert.remove();
  194. });
  195. } else {
  196. Meteor.loginWithPassword(username, password);
  197. Accounts.onLogin(function(){
  198. window.location.href = "/";
  199. })
  200. }
  201. });
  202. },
  203. "click #github-login": function(){
  204. Meteor.loginWithGithub({loginStyle: "redirect"});
  205. }
  206. });
  207. Template.login.events({
  208. "submit form": function(e){
  209. e.preventDefault();
  210. var username = e.target.loginUsername.value;
  211. var password = e.target.loginPassword.value;
  212. Meteor.loginWithPassword(username, password);
  213. Accounts.onLogin(function(){
  214. window.location.href = "/";
  215. })
  216. Accounts.onLoginFailure(function(){
  217. $("#login-form input").css("background-color","indianred");
  218. $("#login-form input").on("click",function(){
  219. $("#login-form input").css({
  220. "-webkit-appearance": "none",
  221. " -moz-appearance": "none",
  222. " appearance": "none",
  223. "outline": "0",
  224. "border": "1px solid rgba(255, 255, 255, 0.4)",
  225. "background-color": "rgba(255, 255, 255, 0.2)",
  226. "width": "304px",
  227. "border-radius": "3px",
  228. "padding": "10px 15px",
  229. "margin": "0 auto 10px auto",
  230. "display": "block",
  231. "text-align": "center",
  232. "font-size": "18px",
  233. "color": "white",
  234. "-webkit-transition-duration": "0.25s",
  235. " transition-duration": "0.25s",
  236. "font-weight": "300"
  237. });
  238. $("#login-form input:focus").css({
  239. "width": "354px",
  240. "color": "white"
  241. })
  242. $("#login-form input").on("blur", function(){
  243. $(this).css("width", "304px");
  244. })
  245. })
  246. });
  247. },
  248. "click #github-login": function(){
  249. Meteor.loginWithGithub({loginStyle: "redirect"});
  250. }
  251. });
  252. Template.dashboard.helpers({
  253. rooms: function() {
  254. return Rooms.find({});
  255. },
  256. currentSong: function() {
  257. var type = this.type;
  258. var room = Rooms.findOne({type: type});
  259. if (room !== undefined) {
  260. return room.currentSong;
  261. } else {
  262. return {};
  263. }
  264. }
  265. });
  266. Template.dashboard.onCreated(function() {
  267. if (_sound !== undefined) _sound.stop();
  268. if (minterval !== undefined) {
  269. Meteor.clearInterval(minterval);
  270. }
  271. if (resizeSeekerbarInterval !== undefined) {
  272. Meteor.clearInterval(resizeSeekerbarInterval);
  273. resizeSeekerbarInterval = undefined;
  274. }
  275. if (station_c !== undefined) {
  276. station_c.stop();
  277. }
  278. Session.set("type", undefined);
  279. });
  280. function executeCommand(command, params){
  281. if (command === "help" || command === "commands") {
  282. $('#helpModal').modal('show');
  283. return true;
  284. } else if (command === "volume") {
  285. if (params.length === 1) {
  286. var volume = Number(params[0]);
  287. if (volume >= 0 || volume <= 100) {
  288. if (volume === 0) {
  289. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  290. } else {
  291. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  292. }
  293. $("#volume-slider").slider("setValue", volume);
  294. if (yt_player !== undefined) {
  295. yt_player.setVolume(volume);
  296. localStorage.setItem("volume", volume);
  297. } else if (_sound !== undefined) {
  298. //_sound
  299. var volume = volume / 100;
  300. _sound.setVolume(volume);
  301. localStorage.setItem("volume", volume * 100);
  302. }
  303. return true;
  304. }
  305. }
  306. } else if(command === "ban"){
  307. var user = params[0];
  308. var time = params[1];
  309. var reason = params[2];
  310. Meteor.call("banUser", user, time, reason, function(err, res){
  311. if(err){
  312. console.log(err);
  313. }
  314. });
  315. }
  316. }
  317. function sendMessage() {
  318. var message = $("#chat-input").val();
  319. if (message.length > 0 && message[0] !== " ") {
  320. if (message[0] === "/") {
  321. message = message.split("");
  322. message.shift();
  323. message = message.join("");
  324. var params = message.split(" ");
  325. var command = params.shift();
  326. if (executeCommand(command, params)) {
  327. $("#chat-input").val("");
  328. } else {
  329. $("#chat-input").val("");
  330. }
  331. } else {
  332. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  333. console.log(err, res);
  334. if (res) {
  335. $("#chat-input").val("");
  336. }
  337. });
  338. }
  339. }
  340. }
  341. Template.room.events({
  342. "click #sync": function() {
  343. if (Session.get("currentSong") !== undefined) {
  344. var room = Rooms.findOne({type: Session.get("type")});
  345. if (room !== undefined) {
  346. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  347. console.log(timeIn);
  348. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  349. if (yt_player !== undefined) {
  350. yt_player.seekTo(skipDuration + timeIn / 1000);
  351. }
  352. else if (_sound !== undefined) {
  353. _sound.seekTo(skipDuration * 1000 + timeIn);
  354. }
  355. }
  356. }
  357. },
  358. "click #side-panel": function(e) {
  359. Meteor.setTimeout(function() {
  360. var elem = document.getElementById('chat');
  361. elem.scrollTop = elem.scrollHeight;
  362. }, 1);
  363. },
  364. "click #submit": function() {
  365. sendMessage();
  366. },
  367. //"keyup #chat-input": function(e) {
  368. // if (e.type === "keyup" && e.which === 13) {
  369. // e.preventDefault();
  370. // sendMessage()
  371. // }
  372. //},
  373. "click #like": function(e) {
  374. $("#like").blur();
  375. Meteor.call("likeSong", Session.get("currentSong").mid);
  376. },
  377. "click #dislike": function(e) {
  378. $("#dislike").blur();
  379. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  380. },
  381. "click #vote-skip": function(){
  382. Meteor.call("voteSkip", type, function(err, res) {
  383. $("#vote-skip").attr("disabled", true);
  384. });
  385. songMID = Session.get("currentSong").mid;
  386. },
  387. "click #report-prev": function(e) {
  388. if (Session.get("previousSong") !== undefined) {
  389. Session.set("reportPrevious", true);
  390. $("#report-prev").prop("disabled", true);
  391. $("#report-curr").prop("disabled", false);
  392. }
  393. },
  394. "click #report-curr": function(e) {
  395. Session.set("reportPrevious", false);
  396. $("#report-prev").prop("disabled", false);
  397. $("#report-curr").prop("disabled", true);
  398. },
  399. "click #report-modal": function() {
  400. Session.set("currentSongR", Session.get("currentSong"));
  401. Session.set("previousSongR", Session.get("previousSong"));
  402. },
  403. "click #add-song-button": function(e){
  404. e.preventDefault();
  405. parts = location.href.split('/');
  406. var roomType = parts.pop();
  407. var genre = roomType.toLowerCase();
  408. var type = $("#type").val();
  409. id = $("#id").val();
  410. var title = $("#title").val();
  411. var artist = $("#artist").val();
  412. var img = $("#img").val();
  413. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  414. if(Playlists.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  415. $("<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 in the playlist.</div>").prependTo($(".landing")).delay(7000).fadeOut(1000, function() { $(this).remove(); });
  416. } else if(Queues.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  417. $("<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 has already been requested.</div>").prependTo($(".landing")).delay(7000).fadeOut(1000, function() { $(this).remove(); });
  418. } else{
  419. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  420. console.log(err, res);
  421. if (err) {
  422. $("<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> Something went wrong.</div>").prependTo($(".landing")).delay(7000).fadeOut(1000, function() { $(this).remove(); });
  423. } else {
  424. $("<div class='alert alert-success 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 added.</strong> Your song has been added to the queue.</div>").prependTo($(".landing")).delay(7000).fadeOut(1000, function() { $(this).remove(); });
  425. }
  426. });
  427. }
  428. $("#close-modal-a").click();
  429. },
  430. "click #toggle-video": function(e){
  431. e.preventDefault();
  432. if (Session.get("mediaHidden")) {
  433. $("#media-container").removeClass("hidden");
  434. $("#toggle-video").text("Hide video");
  435. Session.set("mediaHidden", false);
  436. } else {
  437. $("#media-container").addClass("hidden");
  438. $("#toggle-video").text("Show video");
  439. Session.set("mediaHidden", true);
  440. }
  441. },
  442. "click #return": function(e){
  443. $("#add-info").hide();
  444. $("#search-info").show();
  445. },
  446. "click #search-song": function(){
  447. $("#song-results").empty();
  448. var search_type = $("#search_type").val();
  449. if (search_type === "YouTube") {
  450. $.ajax({
  451. type: "GET",
  452. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  453. applicationType: "application/json",
  454. contentType: "json",
  455. success: function(data){
  456. for(var i in data.items){
  457. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>");
  458. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  459. }
  460. $("#song-results p").click(function(){
  461. $("#search-info").hide();
  462. $("#add-info").show();
  463. var title = $(this).text();
  464. for(var i in ytArr){
  465. if(ytArr[i].title === title){
  466. var songObj = {
  467. id: ytArr[i].id,
  468. title: ytArr[i].title,
  469. type: "youtube"
  470. };
  471. $("#title").val(songObj.title);
  472. $("#artist").val("");
  473. $("#id").val(songObj.id);
  474. $("#type").val("YouTube");
  475. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  476. if (data.tracks.items.length > 0) {
  477. $("#title").val(data.tracks.items[0].name);
  478. var artists = [];
  479. $("#img").val(data.tracks.items[0].album.images[1].url);
  480. data.tracks.items[0].artists.forEach(function(artist) {
  481. artists.push(artist.name);
  482. });
  483. $("#artist").val(artists.join(", "));
  484. }
  485. });
  486. }
  487. }
  488. })
  489. }
  490. })
  491. } else if (search_type === "SoundCloud") {
  492. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  493. for(var i in tracks){
  494. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  495. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  496. }
  497. $("#song-results p").click(function(){
  498. $("#search-info").hide();
  499. $("#add-info").show();
  500. var title = $(this).text();
  501. for(var i in songsArr){
  502. if(songsArr[i].title === title){
  503. var id = songsArr[i].id;
  504. var duration = songsArr[i].duration;
  505. var songObj = {
  506. title: songsArr[i].title,
  507. id: id,
  508. duration: duration,
  509. type: "soundcloud"
  510. }
  511. $("#title").val(songObj.title);
  512. // Set ID field
  513. $("#id").val(songObj.id);
  514. $("#type").val("SoundCloud");
  515. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  516. if (data.tracks.items.length > 0) {
  517. $("#title").val(data.tracks.items[0].name);
  518. var artists = [];
  519. data.tracks.items[0].artists.forEach(function(artist) {
  520. artists.push(artist.name);
  521. });
  522. $("#artist").val(artists.join(", "));
  523. }
  524. // Set title field again if possible
  525. // Set artist if possible
  526. });
  527. }
  528. }
  529. })
  530. });
  531. }
  532. },
  533. "click #close-modal-a": function(){
  534. $("#search-info").show();
  535. $("#add-info").hide();
  536. },
  537. "click #volume-icon": function(){
  538. var volume = 0;
  539. var slider = $("#volume-slider").slider();
  540. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  541. if (yt_player !== undefined) {
  542. yt_player.setVolume(volume);
  543. localStorage.setItem("volume", volume);
  544. $("#volume-slider").slider("setValue", volume);
  545. } else if (_sound !== undefined) {
  546. _sound.setVolume(volume);
  547. localStorage.setItem("volume", volume);
  548. $("#volume-slider").slider("setValue", volume);
  549. }
  550. },
  551. "click #play": function() {
  552. Meteor.call("resumeRoom", type);
  553. },
  554. "click #pause": function() {
  555. Meteor.call("pauseRoom", type);
  556. },
  557. "click #skip": function() {
  558. Meteor.call("skipSong", type);
  559. },
  560. "click #shuffle": function() {
  561. Meteor.call("shufflePlaylist", type);
  562. },
  563. "change input": function(e) {
  564. if (e.target && e.target.id) {
  565. var partsOfId = e.target.id.split("-");
  566. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  567. var camelCase = partsOfId.join("");
  568. Session.set(camelCase, e.target.checked);
  569. }
  570. },
  571. "click #report-song-button": function() {
  572. var report = {};
  573. report.reportSongB = $("#report-song").is(":checked");
  574. report.reportTitleB = $("#report-title").is(":checked");
  575. report.reportAuthorB = $("#report-author").is(":checked");
  576. report.reportDurationB = $("#report-duration").is(":checked");
  577. report.reportAudioB = $("#report-audio").is(":checked");
  578. report.reportAlbumartB = $("#report-albumart").is(":checked");
  579. report.reportOtherB = $("#report-other").is(":checked");
  580. if (report.reportSongB) {
  581. report.reportSong = {};
  582. report.reportSong.notPlayingB = $("#report-song-not-playing").is(":checked");
  583. report.reportSong.doesNotExistB = $("#report-song-does-not-exist").is(":checked");
  584. report.reportSong.otherB = $("#report-song-other").is(":checked");
  585. if (report.reportSong.otherB) {
  586. report.reportSong.other = $("#report-song-other-ta").val();
  587. }
  588. }
  589. if (report.reportTitleB) {
  590. report.reportTitle = {};
  591. report.reportTitle.incorrectB = $("#report-title-incorrect").is(":checked");
  592. report.reportTitle.inappropriateB = $("#report-title-inappropriate").is(":checked");
  593. report.reportTitle.otherB = $("#report-title-other").is(":checked");
  594. if (report.reportTitle.otherB) {
  595. report.reportTitle.other = $("#report-title-other-ta").val();
  596. }
  597. }
  598. if (report.reportAuthorB) {
  599. report.reportAuthor = {};
  600. report.reportAuthor.incorrectB = $("#report-author-incorrect").is(":checked");
  601. report.reportAuthor.inappropriateB = $("#report-author-inappropriate").is(":checked");
  602. report.reportAuthor.otherB = $("#report-author-other").is(":checked");
  603. if (report.reportAuthor.otherB) {
  604. report.reportAuthor.other = $("#report-author-other-ta").val();
  605. }
  606. }
  607. if (report.reportDurationB) {
  608. report.reportDuration = {};
  609. report.reportDuration.longB = $("#report-duration-incorrect").is(":checked");
  610. report.reportDuration.shortB = $("#report-duration-inappropriate").is(":checked");
  611. report.reportDuration.otherB = $("#report-duration-other").is(":checked");
  612. if (report.reportDuration.otherB) {
  613. report.reportDuration.other = $("#report-duration-other-ta").val();
  614. }
  615. }
  616. if (report.reportAudioB) {
  617. report.reportAudio = {};
  618. report.reportAudio.inappropriate = $("#report-audio-inappropriate").is(":checked");
  619. report.reportAudio.notPlayingB = $("#report-audio-incorrect").is(":checked");
  620. report.reportAudio.otherB = $("#report-audio-other").is(":checked");
  621. if (report.reportAudio.otherB) {
  622. report.reportAudio.other = $("#report-audio-other-ta").val();
  623. }
  624. }
  625. if (report.reportAlbumartB) {
  626. report.reportAlbumart = {};
  627. report.reportAlbumart.incorrectB = $("#report-albumart-incorrect").is(":checked");
  628. report.reportAlbumart.inappropriateB = $("#report-albumart-inappropriate").is(":checked");
  629. report.reportAlbumart.notShowingB = $("#report-albumart-inappropriate").is(":checked");
  630. report.reportAlbumart.otherB = $("#report-albumart-other").is(":checked");
  631. if (report.reportAlbumart.otherB) {
  632. report.reportAlbumart.other = $("#report-albumart-other-ta").val();
  633. }
  634. }
  635. if (report.reportOtherB) {
  636. report.other = $("#report-other-ta").val();
  637. }
  638. Meteor.call("submitReport", report, Session.get("id"), function() {
  639. $("#close-modal-r").click();
  640. });
  641. }
  642. });
  643. Template.banned.helpers({
  644. bannedAt: function() {
  645. if (Session.get("ban") !== undefined) {
  646. return Session.get("ban").bannedAt;
  647. }
  648. },
  649. bannedBy: function() {
  650. if (Session.get("ban") !== undefined) {
  651. return Session.get("ban").bannedBy;
  652. }
  653. },
  654. bannedUntil: function() {
  655. if (Session.get("ban") !== undefined) {
  656. return Session.get("ban").bannedUntil;
  657. }
  658. },
  659. bannedReason: function() {
  660. if (Session.get("ban") !== undefined) {
  661. return Session.get("ban").bannedReason;
  662. }
  663. }
  664. });
  665. Template.banned.onCreated(function() {
  666. if (rTimeInterval !== undefined) {
  667. Meteor.clearInterval(rTimeInterval)
  668. }
  669. rTimeInterval = Meteor.setInterval(function() {
  670. Session.set("time", new Date().getTime());
  671. }, 10000);
  672. Meteor.subscribe("ownBan", Meteor.userId(), function() {
  673. Session.set("ban", Meteor.user().punishments.ban);
  674. });
  675. });
  676. Template.registerHelper("rtime", function(date) {
  677. Session.get("time");
  678. if (date) {
  679. return moment(date).fromNow();
  680. }
  681. });
  682. var rTimeInterval = undefined;
  683. Template.room.onRendered(function() {
  684. if (rTimeInterval !== undefined) {
  685. Meteor.clearInterval(rTimeInterval)
  686. }
  687. rTimeInterval = Meteor.setInterval(function() {
  688. Session.set("time", new Date().getTime());
  689. }, 10000);
  690. $(document).ready(function() {
  691. function makeSlider(){
  692. var slider = $("#volume-slider").slider();
  693. var volume = Number(localStorage.getItem("volume"));
  694. $("#volume-slider").slider("setValue", volume);
  695. if (slider.length === 0) {
  696. Meteor.setTimeout(function() {
  697. makeSlider();
  698. }, 500);
  699. } else {
  700. if (volume === 0) {
  701. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  702. } else {
  703. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  704. }
  705. slider.on("slide", function(val) {
  706. if (val.value === 0) {
  707. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  708. } else {
  709. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  710. }
  711. if (yt_player !== undefined) {
  712. yt_player.setVolume(val.value);
  713. localStorage.setItem("volume", val.value);
  714. } else if (_sound !== undefined) {
  715. //_sound
  716. var volume = val.value / 100;
  717. _sound.setVolume(volume);
  718. localStorage.setItem("volume", val.value);
  719. }
  720. });
  721. }
  722. }
  723. makeSlider();
  724. });
  725. });
  726. Template.alerts.helpers({
  727. alerts: function() {
  728. return Alerts.find({active: true});
  729. }
  730. });
  731. Template.room.helpers({
  732. chat: function() {
  733. Meteor.setTimeout(function() {
  734. var elem = document.getElementById('chat');
  735. if (elem !== undefined && elem !== null) {
  736. elem.scrollTop = elem.scrollHeight;
  737. }
  738. }, 100);
  739. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  740. },
  741. likes: function() {
  742. var playlist = Playlists.findOne({type: Session.get("type")});
  743. var likes = 0;
  744. playlist.songs.forEach(function(song) {
  745. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  746. likes = song.likes;
  747. return;
  748. }
  749. });
  750. return likes;
  751. },
  752. dislikes: function() {
  753. var playlist = Playlists.findOne({type: Session.get("type")});
  754. var dislikes = 0;
  755. playlist.songs.forEach(function(song) {
  756. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  757. dislikes = song.dislikes;
  758. return;
  759. }
  760. });
  761. return dislikes;
  762. },
  763. liked: function() {
  764. if (Meteor.userId()) {
  765. var currentSong = Session.get("currentSong");
  766. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  767. return "active";
  768. } else {
  769. return "";
  770. }
  771. } else {
  772. "";
  773. }
  774. },
  775. disliked: function() {
  776. if (Meteor.userId()) {
  777. var currentSong = Session.get("currentSong");
  778. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  779. return "active";
  780. } else {
  781. return "";
  782. }
  783. } else {
  784. "";
  785. }
  786. },
  787. type: function() {
  788. var parts = location.href.split('/');
  789. var id = parts.pop().toLowerCase();
  790. return Rooms.findOne({type: id}).display;
  791. },
  792. users: function() {
  793. var parts = location.href.split('/');
  794. var id = parts.pop().toLowerCase();
  795. return Rooms.findOne({type: id}).users;
  796. },
  797. title: function(){
  798. return Session.get("title");
  799. },
  800. artist: function(){
  801. return Session.get("artist");
  802. },
  803. loaded: function() {
  804. return Session.get("loaded");
  805. },
  806. isAdmin: function() {
  807. if (Meteor.user() && Meteor.user().profile) {
  808. return Meteor.user().profile.rank === "admin";
  809. } else {
  810. return false;
  811. }
  812. },
  813. isModerator: function() {
  814. if (Meteor.user() && Meteor.user().profile && (Meteor.user().profile.rank === "admin" || Meteor.user().profile.rank === "moderator")) {
  815. return true;
  816. } else {
  817. return false;
  818. }
  819. },
  820. paused: function() {
  821. return Session.get("state") === "paused";
  822. },
  823. report: function() {
  824. return Session.get("reportObj");
  825. },
  826. reportSong: function() {
  827. return Session.get("reportSong");
  828. },
  829. reportTitle: function() {
  830. return Session.get("reportTitle");
  831. },
  832. reportAuthor: function() {
  833. return Session.get("reportAuthor");
  834. },
  835. reportDuration: function() {
  836. return Session.get("reportDuration");
  837. },
  838. reportAudio: function() {
  839. return Session.get("reportAudio");
  840. },
  841. reportAlbumart: function() {
  842. return Session.get("reportAlbumart");
  843. },
  844. reportOther: function() {
  845. return Session.get("reportOther");
  846. },
  847. currentSong: function() {
  848. return Session.get("currentSong");
  849. },
  850. previousSong: function() {
  851. return Session.get("previousSong");
  852. },
  853. currentSongR: function() {
  854. return Session.get("currentSongR");
  855. },
  856. previousSongR: function() {
  857. return Session.get("previousSongR");
  858. },
  859. reportingSong: function() {
  860. if (Session.get("reportPrevious")) {
  861. return Session.get("previousSongR");
  862. } else {
  863. return Session.get("currentSongR");
  864. }
  865. },
  866. votes: function(){
  867. return Rooms.findOne({type: Session.get("type")}).votes;
  868. }
  869. });
  870. var allAlertSub = undefined;
  871. Template.alertsDashboard.onCreated(function() {
  872. if (allAlertSub === undefined) {
  873. allAlertSub = Meteor.subscribe("allAlerts");
  874. }
  875. });
  876. Template.alertsDashboard.helpers({
  877. "activeAlerts": function() {
  878. return Alerts.find({active: true});
  879. },
  880. "inactiveAlerts": function() {
  881. return Alerts.find({active: false});
  882. }
  883. });
  884. Template.alertsDashboard.events({
  885. "click #calart-create": function() {
  886. Meteor.call("addAlert", $("#calert-description").val(), $("#calert-priority").val().toLowerCase(), function (err, res) {
  887. if (err) {
  888. alert("Error " + err.error + ": " + err.reason);
  889. } else {
  890. $("#calert-description").val("");
  891. }
  892. });
  893. },
  894. "click #ralert-button": function() {
  895. Meteor.call("removeAlerts");
  896. }
  897. });
  898. Template.admin.helpers({
  899. queueCount: function(display) {
  900. display = display.toLowerCase();
  901. var queues = Queues.findOne({type:display});
  902. return queues && "songs" in queues ? queues.songs.length : 0;
  903. },
  904. queues: function() {
  905. var queues = Queues.find({}).fetch();
  906. return queues;
  907. },
  908. users: function(){
  909. Meteor.call("getUserNum", function(err, num){
  910. if(err){
  911. console.log(err);
  912. }
  913. Session.set("userNum", num);
  914. });
  915. return Session.get("userNum");
  916. },
  917. playlists: function() {
  918. var playlists = Playlists.find({}).fetch();
  919. playlists.map(function(playlist) {
  920. if (Rooms.find({type: playlist.type}).count() !== 1) {
  921. return;
  922. } else {
  923. playlist.display = Rooms.findOne({type: playlist.type}).display;
  924. return playlist;
  925. }
  926. });
  927. return playlists;
  928. }/*,
  929. reports: function() {
  930. var reports = Reports.find({}).fetch();
  931. reports.findOne(
  932. {
  933. $eq: [
  934. ]
  935. }
  936. )
  937. }*/
  938. });
  939. Template.stations.helpers({
  940. playlists: function() {
  941. var playlists = Playlists.find({}).fetch();
  942. playlists.map(function(playlist) {
  943. if (Rooms.find({type: playlist.type}).count() !== 1) {
  944. return;
  945. } else {
  946. playlist.display = Rooms.findOne({type: playlist.type}).display;
  947. return playlist;
  948. }
  949. });
  950. return playlists;
  951. }
  952. });
  953. Template.queues.helpers({
  954. queues: function() {
  955. var queues = Queues.find({}).fetch();
  956. queues.map(function(queue) {
  957. if (Rooms.find({type: queue.type}).count() !== 1) {
  958. return;
  959. } else {
  960. queue.display = Rooms.findOne({type: queue.type}).display;
  961. return queue;
  962. }
  963. });
  964. return queues;
  965. }
  966. })
  967. var yt_player = undefined;
  968. var _sound = undefined;
  969. var previewEndSongTimeout = undefined;
  970. Template.stations.events({
  971. "click .preview-button": function(e){
  972. Session.set("song", this);
  973. },
  974. "click #previewImageButton": function() {
  975. $("#preview-image").attr("src", Session.get("song").img);
  976. },
  977. "click .edit-queue-button": function(e){
  978. Session.set("song", this);
  979. Session.set("genre", $(e.toElement).data("genre"));
  980. Session.set("type", "queue");
  981. $("#type").val(this.type);
  982. $("#mid").val(this.mid);
  983. $("#artist").val(this.artist);
  984. $("#title").val(this.title);
  985. $("#img").val(this.img);
  986. $("#id").val(this.id);
  987. $("#likes").val(this.likes);
  988. $("#dislikes").val(this.dislikes);
  989. $("#duration").val(this.duration);
  990. $("#skip-duration").val(this.skipDuration);
  991. },
  992. "click .edit-playlist-button": function(e){
  993. Session.set("song", this);
  994. Session.set("genre", $(e.toElement).data("genre"));
  995. Session.set("type", "playlist");
  996. $("#type").val(this.type);
  997. $("#mid").val(this.mid);
  998. $("#artist").val(this.artist);
  999. $("#title").val(this.title);
  1000. $("#img").val(this.img);
  1001. $("#id").val(this.id);
  1002. $("#likes").val(this.likes);
  1003. $("#dislikes").val(this.dislikes);
  1004. $("#duration").val(this.duration);
  1005. $("#skip-duration").val(this.skipDuration);
  1006. },
  1007. "click #rreset_confirm": function(e){
  1008. Meteor.call("resetRating");
  1009. },
  1010. "click .add-song-button": function(e){
  1011. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  1012. Meteor.call("addSongToPlaylist", genre, this);
  1013. },
  1014. "click .deny-song-button": function(e){
  1015. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  1016. console.log(genre);
  1017. Meteor.call("removeSongFromQueue", genre, this.mid);
  1018. },
  1019. "click .remove-song-button": function(e){
  1020. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  1021. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  1022. },
  1023. "click #play": function() {
  1024. $("#play").attr("disabled", true);
  1025. $("#stop").attr("disabled", false);
  1026. var song = Session.get("song");
  1027. var id = song.id;
  1028. var type = song.type;
  1029. var volume = localStorage.getItem("volume") || 20;
  1030. if (type === "YouTube") {
  1031. if (yt_player === undefined) {
  1032. yt_player = new YT.Player("previewPlayer", {
  1033. height: 540,
  1034. width: 568,
  1035. videoId: id,
  1036. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  1037. events: {
  1038. 'onReady': function(event) {
  1039. event.target.seekTo(Number(song.skipDuration));
  1040. event.target.playVideo();
  1041. event.target.setVolume(volume);
  1042. },
  1043. 'onStateChange': function(event){
  1044. if (event.data == YT.PlayerState.PAUSED) {
  1045. event.target.playVideo();
  1046. }
  1047. if (event.data == YT.PlayerState.PLAYING) {
  1048. $("#play").attr("disabled", true);
  1049. $("#stop").attr("disabled", false);
  1050. } else {
  1051. $("#play").attr("disabled", false);
  1052. $("#stop").attr("disabled", true);
  1053. }
  1054. }
  1055. }
  1056. });
  1057. } else {
  1058. yt_player.loadVideoById(id);
  1059. yt_player.seekTo(Number(song.skipDuration));
  1060. }
  1061. $("#previewPlayer").show();
  1062. } else if (type === "SoundCloud") {
  1063. SC.stream("/tracks/" + song.id, function(sound) {
  1064. _sound = sound;
  1065. sound.setVolume(volume / 100);
  1066. sound.play();
  1067. });
  1068. }
  1069. if (previewEndSongTimeout !== undefined) {
  1070. Meteor.clearTimeout(previewEndSongTimeout);
  1071. }
  1072. previewEndSongTimeout = Meteor.setTimeout(function() {
  1073. if (yt_player !== undefined) {
  1074. yt_player.stopVideo();
  1075. }
  1076. if (_sound !== undefined) {
  1077. _sound.stop();
  1078. }
  1079. $("#play").attr("disabled", false);
  1080. $("#stop").attr("disabled", true);
  1081. $("#previewPlayer").hide();
  1082. }, song.duration * 1000);
  1083. },
  1084. "click #stop": function() {
  1085. $("#play").attr("disabled", false);
  1086. $("#stop").attr("disabled", true);
  1087. if (previewEndSongTimeout !== undefined) {
  1088. Meteor.clearTimeout(previewEndSongTimeout);
  1089. }
  1090. if (yt_player !== undefined) {
  1091. yt_player.stopVideo();
  1092. }
  1093. if (_sound !== undefined) {
  1094. _sound.stop();
  1095. }
  1096. },
  1097. "click #forward": function() {
  1098. console.log(yt_player);
  1099. console.log(Session.get("song"));
  1100. var error = false;
  1101. if (yt_player !== undefined) {
  1102. var duration = Number(Session.get("song").duration) | 0;
  1103. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1104. if (yt_player.getDuration() < duration + skipDuration) {
  1105. alert("The duration of the YouTube video is smaller than the duration.");
  1106. error = true;
  1107. } else {
  1108. yt_player.seekTo(skipDuration + duration - 10);
  1109. }
  1110. }
  1111. if (_sound !== undefined) {
  1112. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1113. }
  1114. if (!error) {
  1115. if (previewEndSongTimeout !== undefined) {
  1116. Meteor.clearTimeout(previewEndSongTimeout);
  1117. }
  1118. previewEndSongTimeout = Meteor.setTimeout(function() {
  1119. if (yt_player !== undefined) {
  1120. yt_player.stopVideo();
  1121. }
  1122. if (_sound !== undefined) {
  1123. _sound.stop();
  1124. }
  1125. $("#play").attr("disabled", false);
  1126. $("#stop").attr("disabled", true);
  1127. $("#previewPlayer").hide();
  1128. }, 10000);
  1129. }
  1130. },
  1131. "click #croom_create": function() {
  1132. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  1133. if (err) {
  1134. alert("Error " + err.error + ": " + err.reason);
  1135. } else {
  1136. window.location = "/" + $("#croom_tag").val();
  1137. }
  1138. });
  1139. },
  1140. "click #get-spotify-info": function() {
  1141. var search = $("#title").val();
  1142. var artistName = $("#artist").val();
  1143. getSpotifyInfo(search, function(data) {
  1144. for(var i in data){
  1145. for(var j in data[i].items){
  1146. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1147. $("#img").val(data[i].items[j].album.images[1].url);
  1148. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1149. return;
  1150. }
  1151. }
  1152. }
  1153. }, artistName);
  1154. },
  1155. "click #save-song-button": function() {
  1156. var newSong = {};
  1157. newSong.id = $("#id").val();
  1158. newSong.likes = Number($("#likes").val());
  1159. newSong.dislikes = Number($("#dislikes").val());
  1160. newSong.title = $("#title").val();
  1161. newSong.artist = $("#artist").val();
  1162. newSong.img = $("#img").val();
  1163. newSong.type = $("#type").val();
  1164. newSong.duration = Number($("#duration").val());
  1165. newSong.skipDuration = $("#skip-duration").val();
  1166. if(newSong.skipDuration === undefined){
  1167. newSong.skipDuration = 0;
  1168. };
  1169. if (Session.get("type") === "playlist") {
  1170. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1171. $('#editModal').modal('hide');
  1172. });
  1173. } else {
  1174. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1175. $('#editModal').modal('hide');
  1176. });
  1177. }
  1178. },
  1179. "click .delete-room": function(){
  1180. var typeDel = $(this)[0].type;
  1181. Meteor.call("deleteRoom", typeDel);
  1182. }
  1183. });
  1184. Template.queues.events({
  1185. "click .preview-button": function(e){
  1186. Session.set("song", this);
  1187. },
  1188. "click #previewImageButton": function() {
  1189. $("#preview-image").attr("src", Session.get("song").img);
  1190. },
  1191. "click .edit-queue-button": function(e){
  1192. Session.set("song", this);
  1193. Session.set("genre", $(e.toElement).data("genre"));
  1194. Session.set("type", "queue");
  1195. $("#type").val(this.type);
  1196. $("#mid").val(this.mid);
  1197. $("#artist").val(this.artist);
  1198. $("#title").val(this.title);
  1199. $("#img").val(this.img);
  1200. $("#id").val(this.id);
  1201. $("#likes").val(this.likes);
  1202. $("#dislikes").val(this.dislikes);
  1203. $("#duration").val(this.duration);
  1204. $("#skip-duration").val(this.skipDuration);
  1205. },
  1206. "click .add-song-button": function(e){
  1207. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  1208. Meteor.call("addSongToPlaylist", genre, this);
  1209. },
  1210. "click .deny-song-button": function(e){
  1211. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  1212. console.log(genre);
  1213. Meteor.call("removeSongFromQueue", genre, this.mid);
  1214. },
  1215. "click #play": function() {
  1216. $("#play").attr("disabled", true);
  1217. $("#stop").attr("disabled", false);
  1218. var song = Session.get("song");
  1219. var id = song.id;
  1220. var type = song.type;
  1221. var volume = localStorage.getItem("volume") || 20;
  1222. if (type === "YouTube") {
  1223. if (yt_player === undefined) {
  1224. yt_player = new YT.Player("previewPlayer", {
  1225. height: 540,
  1226. width: 568,
  1227. videoId: id,
  1228. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  1229. events: {
  1230. 'onReady': function(event) {
  1231. event.target.seekTo(Number(song.skipDuration));
  1232. event.target.playVideo();
  1233. event.target.setVolume(volume);
  1234. },
  1235. 'onStateChange': function(event){
  1236. if (event.data == YT.PlayerState.PAUSED) {
  1237. event.target.playVideo();
  1238. }
  1239. if (event.data == YT.PlayerState.PLAYING) {
  1240. $("#play").attr("disabled", true);
  1241. $("#stop").attr("disabled", false);
  1242. } else {
  1243. $("#play").attr("disabled", false);
  1244. $("#stop").attr("disabled", true);
  1245. }
  1246. }
  1247. }
  1248. });
  1249. } else {
  1250. yt_player.loadVideoById(id);
  1251. yt_player.seekTo(Number(song.skipDuration));
  1252. }
  1253. $("#previewPlayer").show();
  1254. } else if (type === "SoundCloud") {
  1255. SC.stream("/tracks/" + song.id, function(sound) {
  1256. _sound = sound;
  1257. sound.setVolume(volume / 100);
  1258. sound.play();
  1259. });
  1260. }
  1261. if (previewEndSongTimeout !== undefined) {
  1262. Meteor.clearTimeout(previewEndSongTimeout);
  1263. }
  1264. previewEndSongTimeout = Meteor.setTimeout(function() {
  1265. if (yt_player !== undefined) {
  1266. yt_player.stopVideo();
  1267. }
  1268. if (_sound !== undefined) {
  1269. _sound.stop();
  1270. }
  1271. $("#play").attr("disabled", false);
  1272. $("#stop").attr("disabled", true);
  1273. $("#previewPlayer").hide();
  1274. }, song.duration * 1000);
  1275. },
  1276. "click #stop": function() {
  1277. $("#play").attr("disabled", false);
  1278. $("#stop").attr("disabled", true);
  1279. if (previewEndSongTimeout !== undefined) {
  1280. Meteor.clearTimeout(previewEndSongTimeout);
  1281. }
  1282. if (yt_player !== undefined) {
  1283. yt_player.stopVideo();
  1284. }
  1285. if (_sound !== undefined) {
  1286. _sound.stop();
  1287. }
  1288. },
  1289. "click #forward": function() {
  1290. console.log(yt_player);
  1291. console.log(Session.get("song"));
  1292. var error = false;
  1293. if (yt_player !== undefined) {
  1294. var duration = Number(Session.get("song").duration) | 0;
  1295. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1296. if (yt_player.getDuration() < duration + skipDuration) {
  1297. alert("The duration of the YouTube video is smaller than the duration.");
  1298. error = true;
  1299. } else {
  1300. yt_player.seekTo(skipDuration + duration - 10);
  1301. }
  1302. }
  1303. if (_sound !== undefined) {
  1304. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1305. }
  1306. if (!error) {
  1307. if (previewEndSongTimeout !== undefined) {
  1308. Meteor.clearTimeout(previewEndSongTimeout);
  1309. }
  1310. previewEndSongTimeout = Meteor.setTimeout(function() {
  1311. if (yt_player !== undefined) {
  1312. yt_player.stopVideo();
  1313. }
  1314. if (_sound !== undefined) {
  1315. _sound.stop();
  1316. }
  1317. $("#play").attr("disabled", false);
  1318. $("#stop").attr("disabled", true);
  1319. $("#previewPlayer").hide();
  1320. }, 10000);
  1321. }
  1322. },
  1323. "click #get-spotify-info": function() {
  1324. var search = $("#title").val();
  1325. var artistName = $("#artist").val();
  1326. getSpotifyInfo(search, function(data) {
  1327. for(var i in data){
  1328. for(var j in data[i].items){
  1329. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1330. $("#img").val(data[i].items[j].album.images[1].url);
  1331. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1332. return;
  1333. }
  1334. }
  1335. }
  1336. }, artistName);
  1337. },
  1338. "click #save-song-button": function() {
  1339. var newSong = {};
  1340. newSong.id = $("#id").val();
  1341. newSong.likes = Number($("#likes").val());
  1342. newSong.dislikes = Number($("#dislikes").val());
  1343. newSong.title = $("#title").val();
  1344. newSong.artist = $("#artist").val();
  1345. newSong.img = $("#img").val();
  1346. newSong.type = $("#type").val();
  1347. newSong.duration = Number($("#duration").val());
  1348. newSong.skipDuration = $("#skip-duration").val();
  1349. if(newSong.skipDuration === undefined){
  1350. newSong.skipDuration = 0;
  1351. };
  1352. if (Session.get("type") === "playlist") {
  1353. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1354. $('#editModal').modal('hide');
  1355. });
  1356. } else {
  1357. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1358. $('#editModal').modal('hide');
  1359. });
  1360. }
  1361. }
  1362. });
  1363. Template.stations.onCreated(function() {
  1364. var tag = document.createElement("script");
  1365. tag.src = "https://www.youtube.com/iframe_api";
  1366. var firstScriptTag = document.getElementsByTagName('script')[0];
  1367. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1368. });
  1369. Template.queues.onCreated(function() {
  1370. var tag = document.createElement("script");
  1371. tag.src = "https://www.youtube.com/iframe_api";
  1372. var firstScriptTag = document.getElementsByTagName('script')[0];
  1373. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1374. });
  1375. Template.stations.onRendered(function() {
  1376. $("#previewModal").on("hidden.bs.modal", function() {
  1377. if (previewEndSongTimeout !== undefined) {
  1378. Meteor.clearTimeout(previewEndSongTimeout);
  1379. }
  1380. $("#play").attr("disabled", false);
  1381. $("#stop").attr("disabled", true);
  1382. if (yt_player !== undefined) {
  1383. $("#previewPlayer").hide();
  1384. yt_player.seekTo(0);
  1385. yt_player.stopVideo();
  1386. }
  1387. if (_sound !== undefined) {
  1388. _sound.stop();
  1389. }
  1390. });
  1391. $(document).ready(function() {
  1392. function makeSlider(){
  1393. var slider = $("#volume-slider").slider();
  1394. var volume = localStorage.getItem("volume") || 20;
  1395. $("#volume-slider").slider("setValue", volume);
  1396. if (slider.length === 0) {
  1397. Meteor.setTimeout(function() {
  1398. makeSlider();
  1399. }, 500);
  1400. } else {
  1401. slider.on("slide", function(val) {
  1402. localStorage.setItem("volume", val.value);
  1403. if (yt_player !== undefined) {
  1404. yt_player.setVolume(val.value);
  1405. } else if (_sound !== undefined) {
  1406. var volume = val.value / 100;
  1407. _sound.setVolume(volume);
  1408. }
  1409. });
  1410. }
  1411. }
  1412. makeSlider();
  1413. });
  1414. });
  1415. Template.queues.onRendered(function() {
  1416. $("#previewModal").on("hidden.bs.modal", function() {
  1417. if (previewEndSongTimeout !== undefined) {
  1418. Meteor.clearTimeout(previewEndSongTimeout);
  1419. }
  1420. $("#play").attr("disabled", false);
  1421. $("#stop").attr("disabled", true);
  1422. if (yt_player !== undefined) {
  1423. $("#previewPlayer").hide();
  1424. yt_player.seekTo(0);
  1425. yt_player.stopVideo();
  1426. }
  1427. if (_sound !== undefined) {
  1428. _sound.stop();
  1429. }
  1430. });
  1431. $(document).ready(function() {
  1432. function makeSlider(){
  1433. var slider = $("#volume-slider").slider();
  1434. var volume = localStorage.getItem("volume") || 20;
  1435. $("#volume-slider").slider("setValue", volume);
  1436. if (slider.length === 0) {
  1437. Meteor.setTimeout(function() {
  1438. makeSlider();
  1439. }, 500);
  1440. } else {
  1441. slider.on("slide", function(val) {
  1442. localStorage.setItem("volume", val.value);
  1443. if (yt_player !== undefined) {
  1444. yt_player.setVolume(val.value);
  1445. } else if (_sound !== undefined) {
  1446. var volume = val.value / 100;
  1447. _sound.setVolume(volume);
  1448. }
  1449. });
  1450. }
  1451. }
  1452. makeSlider();
  1453. });
  1454. });
  1455. Template.playlist.helpers({
  1456. playlist_songs: function() {
  1457. parts = location.href.split('/');
  1458. id = parts.pop();
  1459. type = id.toLowerCase();
  1460. var data = Playlists.findOne({type: type});
  1461. if (data !== undefined) {
  1462. data.songs.map(function(song) {
  1463. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  1464. song.current = true;
  1465. } else {
  1466. song.current = false;
  1467. }
  1468. return song;
  1469. });
  1470. return data.songs;
  1471. } else {
  1472. return [];
  1473. }
  1474. }
  1475. });
  1476. Template.playlist.events({
  1477. "keyup #search-playlist": function(){
  1478. console.log($("#search-playlist").val());
  1479. if($("#search-playlist").val().length === 0){
  1480. $(".pl-item").show();
  1481. } else {
  1482. $(".pl-item").hide();
  1483. var input = $("#search-playlist").val().toLowerCase();
  1484. $(".pl-item strong").each(function(i, el){
  1485. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1486. $(el).parent(".pl-item").show();
  1487. }
  1488. })
  1489. $(".pl-item #pl-artist").each(function(i, el){
  1490. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1491. $(el).parent(".pl-item").show();
  1492. }
  1493. })
  1494. }
  1495. },
  1496. "click #pl-item": function(){
  1497. console.log($(this).text());
  1498. }
  1499. })
  1500. Meteor.subscribe("rooms");
  1501. Template.room.onCreated(function () {
  1502. Session.set("reportSong", false);
  1503. Session.set("reportTitle", false);
  1504. Session.set("reportAuthor", false);
  1505. Session.set("reportDuration", false);
  1506. Session.set("reportAudio", false);
  1507. Session.set("reportAlbumart", false);
  1508. Session.set("reportOther", false);
  1509. if (resizeSeekerbarInterval !== undefined) {
  1510. Meteor.clearInterval(resizeSeekerbarInterval);
  1511. resizeSeekerbarInterval = undefined;
  1512. }
  1513. yt_player = undefined;
  1514. _sound = undefined;
  1515. Session.set("videoHidden", false);
  1516. var tag = document.createElement("script");
  1517. tag.src = "https://www.youtube.com/iframe_api";
  1518. var firstScriptTag = document.getElementsByTagName('script')[0];
  1519. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1520. var currentSong = undefined;
  1521. var currentSongR = undefined;
  1522. function getTimeElapsed() {
  1523. if (currentSong !== undefined) {
  1524. var room = Rooms.findOne({type: type});
  1525. if (room !== undefined) {
  1526. return Date.now() - currentSong.started - room.timePaused;
  1527. }
  1528. }
  1529. return 0;
  1530. }
  1531. function getSongInfo(songData){
  1532. Session.set("title", songData.title);
  1533. Session.set("artist", songData.artist);
  1534. Session.set("id", songData.id);
  1535. $("#song-img").attr("src", songData.img);
  1536. Session.set("duration", parseInt(songData.duration));
  1537. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1538. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1539. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1540. }
  1541. function resizeSeekerbar() {
  1542. if (Session.get("state") === "playing") {
  1543. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1544. }
  1545. }
  1546. function startSong() {
  1547. $("#time-elapsed").text("0:00");
  1548. $("#vote-skip").attr("disabled", false);
  1549. if (currentSong !== undefined) {
  1550. if (_sound !== undefined) _sound.stop();
  1551. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  1552. var volume = localStorage.getItem("volume") || 20;
  1553. if (currentSong.type === "SoundCloud") {
  1554. if ($("#soundcloud-image").length !== 1) {
  1555. //$("#media-container").append('<img alt="Not loading" src="/soundcloud-image.png" class="embed-responsive-item" id="soundcloud-image" />');
  1556. $("#media-container").append('<h1 id="soundcloud-image">We have temporarily disabled the playing of SoundCloud songs. We are sorry for this inconvenience.</h1>');
  1557. }
  1558. if ($("#player").length === 1) {
  1559. $("#player").hide();
  1560. }
  1561. $("#soundcloud-image").show();
  1562. //getSongInfo(currentSong);
  1563. /*SC.stream("/tracks/" + currentSong.id, function(sound){
  1564. _sound = sound;
  1565. sound.setVolume(volume / 100);
  1566. sound.play();
  1567. var interval = setInterval(function() {
  1568. if (sound.getState() === "playing") {
  1569. sound.seek(getTimeElapsed());
  1570. window.clearInterval(interval);
  1571. }
  1572. }, 200);
  1573. Session.set("duration", parseInt(currentSong.duration));
  1574. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  1575. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1576. resizeSeekerbar();
  1577. });*/
  1578. } else {
  1579. if ($("#player").length !== 1) {
  1580. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1581. }
  1582. if ($("#soundcloud-image").length === 1) {
  1583. $("#soundcloud-image").hide();
  1584. }
  1585. $("#player").show();
  1586. if (yt_player === undefined) {
  1587. yt_player = new YT.Player("player", {
  1588. height: 540,
  1589. width: 960,
  1590. videoId: currentSong.id,
  1591. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1592. events: {
  1593. 'onReady': function(event) {
  1594. if(currentSong.skipDuration === undefined){
  1595. currentSong.skipDuration = 0;
  1596. }
  1597. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1598. event.target.playVideo();
  1599. event.target.setVolume(volume);
  1600. resizeSeekerbar();
  1601. },
  1602. 'onStateChange': function(event){
  1603. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1604. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1605. event.target.playVideo();
  1606. }
  1607. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1608. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1609. event.target.pauseVideo();
  1610. }
  1611. }
  1612. }
  1613. });
  1614. } else {
  1615. yt_player.loadVideoById(currentSong.id);
  1616. if(currentSong.skipDuration === undefined){
  1617. currentSong.skipDuration = 0;
  1618. }
  1619. yt_player.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1620. }
  1621. getSongInfo(currentSong);
  1622. }
  1623. }
  1624. }
  1625. Session.set("loaded", false);
  1626. Meteor.subscribe("rooms", function() {
  1627. var parts = location.href.split('/');
  1628. var id = parts.pop();
  1629. var type = id.toLowerCase();
  1630. Session.set("type", type);
  1631. if (Rooms.find({type: type}).count() !== 1) {
  1632. window.location = "/";
  1633. } else {
  1634. station_c = Meteor.subscribe(type);
  1635. Session.set("loaded", true);
  1636. minterval = Meteor.setInterval(function () {
  1637. var room = Rooms.findOne({type: type});
  1638. if (room !== undefined) {
  1639. if (room.state === "paused") {
  1640. Session.set("state", "paused");
  1641. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() === 1) {
  1642. yt_player.pauseVideo();
  1643. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  1644. _sound.pause();
  1645. }
  1646. } else {
  1647. Session.set("state", "playing");
  1648. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() !== 1) {
  1649. yt_player.playVideo();
  1650. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  1651. _sound.play();
  1652. }
  1653. }
  1654. }
  1655. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  1656. Session.set("previousSong", currentSong);
  1657. currentSongR = room.currentSong;
  1658. currentSong = room.currentSong.song;
  1659. currentSong.started = room.currentSong.started;
  1660. Session.set("currentSong", currentSong);
  1661. startSong();
  1662. }
  1663. if (currentSong !== undefined) {
  1664. if (room !== undefined) {
  1665. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1666. var d = moment.duration(duration, 'seconds');
  1667. if (Session.get("state") === "playing") {
  1668. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1669. }
  1670. }
  1671. }
  1672. }, 1000);
  1673. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1674. resizeSeekerbar();
  1675. }, 500);
  1676. }
  1677. });
  1678. });