client.js 69 KB

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