client.js 66 KB

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