client.js 72 KB

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