client.js 66 KB

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