client.js 66 KB

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