client.js 65 KB

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