client.js 68 KB

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