client.js 68 KB

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