client.js 66 KB

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