client.js 67 KB

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