client.js 67 KB

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