client.js 68 KB

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