client.js 67 KB

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