client.js 69 KB

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