client.js 72 KB

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