client.js 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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. "real_name": function(){
  152. return Session.get("real_name");
  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("real_name", 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. return Meteor.users.find().count();
  1060. },
  1061. playlists: function() {
  1062. var playlists = Playlists.find({}).fetch();
  1063. playlists.map(function(playlist) {
  1064. if (Rooms.find({type: playlist.type}).count() !== 1) {
  1065. return;
  1066. } else {
  1067. playlist.display = Rooms.findOne({type: playlist.type}).display;
  1068. return playlist;
  1069. }
  1070. });
  1071. return playlists;
  1072. },
  1073. reportsCount: function(room) {
  1074. room = room.toLowerCase();
  1075. var reports = Reports.findOne({room:room});
  1076. return reports && "report" in reports ? reports.report.length : 0;
  1077. }
  1078. });
  1079. Template.admin.events({
  1080. "click #croom_create": function() {
  1081. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  1082. if (err) {
  1083. alert("Error " + err.error + ": " + err.reason);
  1084. } else {
  1085. window.location = "/" + $("#croom_tag").val();
  1086. }
  1087. });
  1088. },
  1089. "click a": function(e){
  1090. var id = e.currentTarget.id;
  1091. console.log(id.toLowerCase());
  1092. Session.set("playlistToEdit", id);
  1093. }
  1094. });
  1095. Template.stations.helpers({
  1096. playlist: function() {
  1097. var query = {type: Session.get("playlistToEdit").toLowerCase()};
  1098. var playlists = Playlists.find(query).fetch();
  1099. console.log(Session.get("playlistToEdit"), query, playlists);
  1100. return playlists;
  1101. },
  1102. whichStation: function(){
  1103. return Session.get("playlistToEdit");
  1104. }
  1105. });
  1106. Template.queues.helpers({
  1107. queues: function() {
  1108. var queues = Queues.find({}).fetch();
  1109. queues.map(function(queue) {
  1110. if (Rooms.find({type: queue.type}).count() !== 1) {
  1111. return;
  1112. } else {
  1113. queue.display = Rooms.findOne({type: queue.type}).display;
  1114. return queue;
  1115. }
  1116. });
  1117. return queues;
  1118. }
  1119. })
  1120. var yt_player = undefined;
  1121. var _sound = undefined;
  1122. var previewEndSongTimeout = undefined;
  1123. Template.stations.events({
  1124. "click .preview-button": function(e){
  1125. Session.set("song", this);
  1126. },
  1127. "click #previewImageButton": function() {
  1128. $("#preview-image").attr("src", Session.get("song").img);
  1129. },
  1130. "click .edit-queue-button": function(e){
  1131. Session.set("song", this);
  1132. Session.set("genre", $(e.target).data("genre"));
  1133. Session.set("type", "queue");
  1134. $("#type").val(this.type);
  1135. $("#mid").val(this.mid);
  1136. $("#artist").val(this.artist);
  1137. $("#title").val(this.title);
  1138. $("#img").val(this.img);
  1139. $("#id").val(this.id);
  1140. $("#likes").val(this.likes);
  1141. $("#dislikes").val(this.dislikes);
  1142. $("#duration").val(this.duration);
  1143. $("#skip-duration").val(this.skipDuration);
  1144. },
  1145. "click .edit-playlist-button": function(e){
  1146. Session.set("song", this);
  1147. Session.set("genre", $(e.target).data("genre"));
  1148. Session.set("type", "playlist");
  1149. $("#type").val(this.type);
  1150. $("#mid").val(this.mid);
  1151. $("#artist").val(this.artist);
  1152. $("#title").val(this.title);
  1153. $("#img").val(this.img);
  1154. $("#id").val(this.id);
  1155. $("#likes").val(this.likes);
  1156. $("#dislikes").val(this.dislikes);
  1157. $("#duration").val(this.duration);
  1158. $("#skip-duration").val(this.skipDuration);
  1159. },
  1160. "click #rreset_confirm": function(e){
  1161. Meteor.call("resetRating");
  1162. },
  1163. "click .add-song-button": function(e){
  1164. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1165. Meteor.call("addSongToPlaylist", genre, this);
  1166. },
  1167. "click .deny-song-button": function(e){
  1168. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1169. Meteor.call("removeSongFromQueue", genre, this.mid);
  1170. },
  1171. "click .remove-song-button": function(e){
  1172. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1173. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  1174. },
  1175. "click #moveSong": function(e){
  1176. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1177. if (genre !== Session.get(genre)) {
  1178. 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});
  1179. Meteor.call("removeSongFromPlaylist", Session.get("genre"), Session.get("song").mid);
  1180. }else {
  1181. console.log("Something Went Wrong?!");
  1182. return false;
  1183. }
  1184. },
  1185. "click #copySong": function(e){
  1186. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1187. 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});
  1188. },
  1189. "click .copyMove-button": function(e){
  1190. Session.set("song", this);
  1191. Session.set("genre", $(e.target).data("genre"));
  1192. },
  1193. "click #play": function() {
  1194. $("#play").attr("disabled", true);
  1195. $("#stop").attr("disabled", false);
  1196. var song = Session.get("song");
  1197. var id = song.id;
  1198. var type = song.type;
  1199. var volume = localStorage.getItem("volume") || 20;
  1200. if (type === "YouTube") {
  1201. if (yt_player === undefined) {
  1202. yt_player = new YT.Player("previewPlayer", {
  1203. height: 540,
  1204. width: 568,
  1205. videoId: id,
  1206. playerVars: {controls: 0, iv_load_policy: 3, showinfo: 0},
  1207. events: {
  1208. 'onReady': function(event) {
  1209. event.target.seekTo(Number(song.skipDuration));
  1210. event.target.playVideo();
  1211. event.target.setVolume(volume);
  1212. },
  1213. 'onStateChange': function(event){
  1214. if (event.data == YT.PlayerState.PAUSED) {
  1215. event.target.playVideo();
  1216. }
  1217. if (event.data == YT.PlayerState.PLAYING) {
  1218. $("#play").attr("disabled", true);
  1219. $("#stop").attr("disabled", false);
  1220. } else {
  1221. $("#play").attr("disabled", false);
  1222. $("#stop").attr("disabled", true);
  1223. }
  1224. }
  1225. }
  1226. });
  1227. } else {
  1228. yt_player.loadVideoById(id);
  1229. yt_player.seekTo(Number(song.skipDuration));
  1230. }
  1231. $("#previewPlayer").show();
  1232. } else if (type === "SoundCloud") {
  1233. SC.stream("/tracks/" + song.id, function(sound) {
  1234. _sound = sound;
  1235. sound.setVolume(volume / 100);
  1236. sound.play();
  1237. });
  1238. }
  1239. if (previewEndSongTimeout !== undefined) {
  1240. Meteor.clearTimeout(previewEndSongTimeout);
  1241. }
  1242. previewEndSongTimeout = Meteor.setTimeout(function() {
  1243. if (yt_player !== undefined) {
  1244. yt_player.stopVideo();
  1245. }
  1246. if (_sound !== undefined) {
  1247. _sound.stop();
  1248. }
  1249. $("#play").attr("disabled", false);
  1250. $("#stop").attr("disabled", true);
  1251. $("#previewPlayer").hide();
  1252. }, song.duration * 1000);
  1253. },
  1254. "click #stop": function() {
  1255. $("#play").attr("disabled", false);
  1256. $("#stop").attr("disabled", true);
  1257. if (previewEndSongTimeout !== undefined) {
  1258. Meteor.clearTimeout(previewEndSongTimeout);
  1259. }
  1260. if (yt_player !== undefined) {
  1261. yt_player.stopVideo();
  1262. }
  1263. if (_sound !== undefined) {
  1264. _sound.stop();
  1265. }
  1266. },
  1267. "click #forward": function() {
  1268. var error = false;
  1269. if (yt_player !== undefined) {
  1270. var duration = Number(Session.get("song").duration) | 0;
  1271. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1272. if (yt_player.getDuration() < duration + skipDuration) {
  1273. alert("The duration of the YouTube video is smaller than the duration.");
  1274. error = true;
  1275. } else {
  1276. yt_player.seekTo(skipDuration + duration - 10);
  1277. }
  1278. }
  1279. if (_sound !== undefined) {
  1280. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1281. }
  1282. if (!error) {
  1283. if (previewEndSongTimeout !== undefined) {
  1284. Meteor.clearTimeout(previewEndSongTimeout);
  1285. }
  1286. previewEndSongTimeout = Meteor.setTimeout(function() {
  1287. if (yt_player !== undefined) {
  1288. yt_player.stopVideo();
  1289. }
  1290. if (_sound !== undefined) {
  1291. _sound.stop();
  1292. }
  1293. $("#play").attr("disabled", false);
  1294. $("#stop").attr("disabled", true);
  1295. $("#previewPlayer").hide();
  1296. }, 10000);
  1297. }
  1298. },
  1299. "click #croom_create": function() {
  1300. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#two").prop("checked"), function (err, res) {
  1301. if (err) {
  1302. alert("Error " + err.error + ": " + err.reason);
  1303. } else {
  1304. window.location = "/" + $("#croom_tag").val();
  1305. }
  1306. });
  1307. },
  1308. "click #get-spotify-info": function() {
  1309. var search = $("#title").val();
  1310. var artistName = $("#artist").val();
  1311. getSpotifyInfo(search, function(data) {
  1312. for(var i in data){
  1313. for(var j in data[i].items){
  1314. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1315. $("#img").val(data[i].items[j].album.images[1].url);
  1316. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1317. return;
  1318. }
  1319. }
  1320. }
  1321. }, artistName);
  1322. },
  1323. "click #save-song-button": function() {
  1324. var newSong = {};
  1325. newSong.id = $("#id").val();
  1326. newSong.likes = Number($("#likes").val());
  1327. newSong.dislikes = Number($("#dislikes").val());
  1328. newSong.title = $("#title").val();
  1329. newSong.artist = $("#artist").val();
  1330. newSong.img = $("#img").val();
  1331. newSong.type = $("#type").val();
  1332. newSong.duration = Number($("#duration").val());
  1333. newSong.skipDuration = $("#skip-duration").val();
  1334. if(newSong.skipDuration === undefined){
  1335. newSong.skipDuration = 0;
  1336. };
  1337. if (Session.get("type") === "playlist") {
  1338. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1339. $('#editModal').modal('hide');
  1340. });
  1341. } else {
  1342. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1343. $('#editModal').modal('hide');
  1344. });
  1345. }
  1346. },
  1347. "click .delete-room": function(){
  1348. var typeDel = $(this)[0].type;
  1349. Meteor.call("deleteRoom", typeDel);
  1350. }
  1351. });
  1352. Template.queues.events({
  1353. "click .preview-button": function(e){
  1354. Session.set("song", this);
  1355. },
  1356. "click #previewImageButton": function() {
  1357. $("#preview-image").attr("src", Session.get("song").img);
  1358. },
  1359. "click .edit-queue-button": function(e){
  1360. Session.set("song", this);
  1361. Session.set("genre", $(e.target).data("genre"));
  1362. Session.set("type", "queue");
  1363. $("#type").val(this.type);
  1364. $("#mid").val(this.mid);
  1365. $("#artist").val(this.artist);
  1366. $("#title").val(this.title);
  1367. $("#img").val(this.img);
  1368. $("#id").val(this.id);
  1369. $("#likes").val(this.likes);
  1370. $("#dislikes").val(this.dislikes);
  1371. $("#duration").val(this.duration);
  1372. $("#skip-duration").val(this.skipDuration);
  1373. },
  1374. "click .add-song-button": function(e){
  1375. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1376. Meteor.call("addSongToPlaylist", genre, this);
  1377. },
  1378. "click .deny-song-button": function(e){
  1379. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1380. Meteor.call("removeSongFromQueue", genre, this.mid);
  1381. },
  1382. "click #play": function() {
  1383. $("#play").attr("disabled", true);
  1384. $("#stop").attr("disabled", false);
  1385. var song = Session.get("song");
  1386. var id = song.id;
  1387. var type = song.type;
  1388. var volume = localStorage.getItem("volume") || 20;
  1389. if (type === "YouTube") {
  1390. if (yt_player === undefined) {
  1391. yt_player = new YT.Player("previewPlayer", {
  1392. height: 540,
  1393. width: 568,
  1394. videoId: id,
  1395. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  1396. events: {
  1397. 'onReady': function(event) {
  1398. event.target.seekTo(Number(song.skipDuration));
  1399. event.target.playVideo();
  1400. event.target.setVolume(volume);
  1401. },
  1402. 'onStateChange': function(event){
  1403. if (event.data == YT.PlayerState.PAUSED) {
  1404. event.target.playVideo();
  1405. }
  1406. if (event.data == YT.PlayerState.PLAYING) {
  1407. $("#play").attr("disabled", true);
  1408. $("#stop").attr("disabled", false);
  1409. } else {
  1410. $("#play").attr("disabled", false);
  1411. $("#stop").attr("disabled", true);
  1412. }
  1413. }
  1414. }
  1415. });
  1416. } else {
  1417. yt_player.loadVideoById(id);
  1418. yt_player.seekTo(Number(song.skipDuration));
  1419. }
  1420. $("#previewPlayer").show();
  1421. } else if (type === "SoundCloud") {
  1422. SC.stream("/tracks/" + song.id, function(sound) {
  1423. _sound = sound;
  1424. sound.setVolume(volume / 100);
  1425. sound.play();
  1426. });
  1427. }
  1428. if (previewEndSongTimeout !== undefined) {
  1429. Meteor.clearTimeout(previewEndSongTimeout);
  1430. }
  1431. previewEndSongTimeout = Meteor.setTimeout(function() {
  1432. if (yt_player !== undefined) {
  1433. yt_player.stopVideo();
  1434. }
  1435. if (_sound !== undefined) {
  1436. _sound.stop();
  1437. }
  1438. $("#play").attr("disabled", false);
  1439. $("#stop").attr("disabled", true);
  1440. $("#previewPlayer").hide();
  1441. }, song.duration * 1000);
  1442. },
  1443. "click #stop": function() {
  1444. $("#play").attr("disabled", false);
  1445. $("#stop").attr("disabled", true);
  1446. if (previewEndSongTimeout !== undefined) {
  1447. Meteor.clearTimeout(previewEndSongTimeout);
  1448. }
  1449. if (yt_player !== undefined) {
  1450. yt_player.stopVideo();
  1451. }
  1452. if (_sound !== undefined) {
  1453. _sound.stop();
  1454. }
  1455. },
  1456. "click #forward": function() {
  1457. var error = false;
  1458. if (yt_player !== undefined) {
  1459. var duration = Number(Session.get("song").duration) | 0;
  1460. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1461. if (yt_player.getDuration() < duration + skipDuration) {
  1462. alert("The duration of the YouTube video is smaller than the duration.");
  1463. error = true;
  1464. } else {
  1465. yt_player.seekTo(skipDuration + duration - 10);
  1466. }
  1467. }
  1468. if (_sound !== undefined) {
  1469. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1470. }
  1471. if (!error) {
  1472. if (previewEndSongTimeout !== undefined) {
  1473. Meteor.clearTimeout(previewEndSongTimeout);
  1474. }
  1475. previewEndSongTimeout = Meteor.setTimeout(function() {
  1476. if (yt_player !== undefined) {
  1477. yt_player.stopVideo();
  1478. }
  1479. if (_sound !== undefined) {
  1480. _sound.stop();
  1481. }
  1482. $("#play").attr("disabled", false);
  1483. $("#stop").attr("disabled", true);
  1484. $("#previewPlayer").hide();
  1485. }, 10000);
  1486. }
  1487. },
  1488. "click #get-spotify-info": function() {
  1489. var search = $("#title").val();
  1490. var artistName = $("#artist").val();
  1491. getSpotifyInfo(search, function(data) {
  1492. for(var i in data){
  1493. for(var j in data[i].items){
  1494. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1495. $("#img").val(data[i].items[j].album.images[1].url);
  1496. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1497. return;
  1498. }
  1499. }
  1500. }
  1501. }, artistName);
  1502. },
  1503. "click #save-song-button": function() {
  1504. var newSong = {};
  1505. newSong.id = $("#id").val();
  1506. newSong.likes = Number($("#likes").val());
  1507. newSong.dislikes = Number($("#dislikes").val());
  1508. newSong.title = $("#title").val();
  1509. newSong.artist = $("#artist").val();
  1510. newSong.img = $("#img").val();
  1511. newSong.type = $("#type").val();
  1512. newSong.duration = Number($("#duration").val());
  1513. newSong.skipDuration = $("#skip-duration").val();
  1514. if(newSong.skipDuration === undefined){
  1515. newSong.skipDuration = 0;
  1516. };
  1517. if (Session.get("type") === "playlist") {
  1518. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1519. $('#editModal').modal('hide');
  1520. });
  1521. } else {
  1522. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1523. $('#editModal').modal('hide');
  1524. });
  1525. }
  1526. }
  1527. });
  1528. Template.stations.onCreated(function() {
  1529. var tag = document.createElement("script");
  1530. tag.src = "https://www.youtube.com/iframe_api";
  1531. var firstScriptTag = document.getElementsByTagName('script')[0];
  1532. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1533. yt_player = undefined;
  1534. _sound = undefined;
  1535. });
  1536. Template.queues.onCreated(function() {
  1537. var tag = document.createElement("script");
  1538. tag.src = "https://www.youtube.com/iframe_api";
  1539. var firstScriptTag = document.getElementsByTagName('script')[0];
  1540. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1541. yt_player = undefined;
  1542. _sound = undefined;
  1543. });
  1544. Template.stations.onRendered(function() {
  1545. $("#previewModal").on("hidden.bs.modal", function() {
  1546. if (previewEndSongTimeout !== undefined) {
  1547. Meteor.clearTimeout(previewEndSongTimeout);
  1548. }
  1549. $("#play").attr("disabled", false);
  1550. $("#stop").attr("disabled", true);
  1551. if (yt_player !== undefined) {
  1552. $("#previewPlayer").hide();
  1553. yt_player.seekTo(0);
  1554. yt_player.stopVideo();
  1555. }
  1556. if (_sound !== undefined) {
  1557. _sound.stop();
  1558. }
  1559. });
  1560. $(document).ready(function() {
  1561. function makeSlider(){
  1562. var slider = $("#volume-slider").slider();
  1563. var volume = localStorage.getItem("volume") || 20;
  1564. $("#volume-slider").slider("setValue", volume);
  1565. if (slider.length === 0) {
  1566. Meteor.setTimeout(function() {
  1567. makeSlider();
  1568. }, 500);
  1569. } else {
  1570. slider.on("slide", function(val) {
  1571. localStorage.setItem("volume", val.value);
  1572. if (yt_player !== undefined) {
  1573. yt_player.setVolume(val.value);
  1574. } else if (_sound !== undefined) {
  1575. var volume = val.value / 100;
  1576. _sound.setVolume(volume);
  1577. }
  1578. });
  1579. }
  1580. }
  1581. makeSlider();
  1582. });
  1583. });
  1584. Template.queues.onRendered(function() {
  1585. $("#previewModal").on("hidden.bs.modal", function() {
  1586. if (previewEndSongTimeout !== undefined) {
  1587. Meteor.clearTimeout(previewEndSongTimeout);
  1588. }
  1589. $("#play").attr("disabled", false);
  1590. $("#stop").attr("disabled", true);
  1591. if (yt_player !== undefined) {
  1592. $("#previewPlayer").hide();
  1593. yt_player.seekTo(0);
  1594. yt_player.stopVideo();
  1595. }
  1596. if (_sound !== undefined) {
  1597. _sound.stop();
  1598. }
  1599. });
  1600. $(document).ready(function() {
  1601. function makeSlider(){
  1602. var slider = $("#volume-slider").slider();
  1603. var volume = localStorage.getItem("volume") || 20;
  1604. $("#volume-slider").slider("setValue", volume);
  1605. if (slider.length === 0) {
  1606. Meteor.setTimeout(function() {
  1607. makeSlider();
  1608. }, 500);
  1609. } else {
  1610. slider.on("slide", function(val) {
  1611. localStorage.setItem("volume", val.value);
  1612. if (yt_player !== undefined) {
  1613. yt_player.setVolume(val.value);
  1614. } else if (_sound !== undefined) {
  1615. var volume = val.value / 100;
  1616. _sound.setVolume(volume);
  1617. }
  1618. });
  1619. }
  1620. }
  1621. makeSlider();
  1622. });
  1623. });
  1624. Template.playlist.helpers({
  1625. playlist_songs: function() {
  1626. parts = location.href.split('/');
  1627. id = parts.pop();
  1628. type = id.toLowerCase();
  1629. var data = Playlists.findOne({type: type});
  1630. if (data !== undefined) {
  1631. data.songs.map(function(song) {
  1632. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  1633. song.current = true;
  1634. } else {
  1635. song.current = false;
  1636. }
  1637. return song;
  1638. });
  1639. return data.songs;
  1640. } else {
  1641. return [];
  1642. }
  1643. }
  1644. });
  1645. Template.playlist.events({
  1646. "keyup #search-playlist": function(){
  1647. if($("#search-playlist").val().length === 0){
  1648. $(".pl-item").show();
  1649. } else {
  1650. $(".pl-item").hide();
  1651. var input = $("#search-playlist").val().toLowerCase();
  1652. $(".pl-item strong").each(function(i, el){
  1653. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1654. $(el).parent(".pl-item").show();
  1655. }
  1656. })
  1657. $(".pl-item #pl-artist").each(function(i, el){
  1658. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1659. $(el).parent(".pl-item").show();
  1660. }
  1661. })
  1662. }
  1663. },
  1664. "click #pl-item": function(){
  1665. console.log($(this).text());
  1666. }
  1667. })
  1668. Meteor.subscribe("rooms");
  1669. Template.room.onCreated(function () {
  1670. Chat.after.find(function() {
  1671. if (!$("#chat-tab").hasClass("active")) {
  1672. $("#chat-tab").addClass("unread-messages");
  1673. }
  1674. });
  1675. Session.set("reportSong", false);
  1676. Session.set("reportTitle", false);
  1677. Session.set("reportAuthor", false);
  1678. Session.set("reportDuration", false);
  1679. Session.set("reportAudio", false);
  1680. Session.set("reportAlbumart", false);
  1681. Session.set("reportOther", false);
  1682. if (resizeSeekerbarInterval !== undefined) {
  1683. Meteor.clearInterval(resizeSeekerbarInterval);
  1684. resizeSeekerbarInterval = undefined;
  1685. }
  1686. yt_player = undefined;
  1687. _sound = undefined;
  1688. Session.set("videoHidden", false);
  1689. var tag = document.createElement("script");
  1690. tag.src = "https://www.youtube.com/iframe_api";
  1691. var firstScriptTag = document.getElementsByTagName('script')[0];
  1692. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1693. var currentSong = undefined;
  1694. var currentSongR = undefined;
  1695. function getTimeElapsed() {
  1696. if (currentSong !== undefined) {
  1697. var room = Rooms.findOne({type: type});
  1698. if (room !== undefined) {
  1699. return Date.now() - currentSong.started - room.timePaused;
  1700. }
  1701. }
  1702. return 0;
  1703. }
  1704. function getSongInfo(songData){
  1705. Session.set("title", songData.title);
  1706. Session.set("artist", songData.artist);
  1707. Session.set("id", songData.id);
  1708. $("#song-img").attr("src", songData.img);
  1709. Session.set("duration", parseInt(songData.duration));
  1710. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1711. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1712. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1713. }
  1714. function resizeSeekerbar() {
  1715. if (Session.get("state") === "playing") {
  1716. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1717. }
  1718. }
  1719. function startSong() {
  1720. $("#time-elapsed").text("0:00");
  1721. $("#vote-skip").attr("disabled", false);
  1722. if (currentSong !== undefined) {
  1723. if (_sound !== undefined) _sound.stop();
  1724. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  1725. var volume = localStorage.getItem("volume") || 20;
  1726. if (currentSong.type === "SoundCloud") {
  1727. if ($("#soundcloud-image").length !== 1) {
  1728. //$("#media-container").append('<img alt="Not loading" src="/soundcloud-image.png" class="embed-responsive-item" id="soundcloud-image" />');
  1729. $("#media-container").append('<h1 id="soundcloud-image">We have temporarily disabled the playing of SoundCloud songs. We are sorry for this inconvenience.</h1>');
  1730. }
  1731. if ($("#player").length === 1) {
  1732. $("#player").hide();
  1733. }
  1734. $("#soundcloud-image").show();
  1735. //getSongInfo(currentSong);
  1736. /*SC.stream("/tracks/" + currentSong.id, function(sound){
  1737. _sound = sound;
  1738. sound.setVolume(volume / 100);
  1739. sound.play();
  1740. var interval = setInterval(function() {
  1741. if (sound.getState() === "playing") {
  1742. sound.seek(getTimeElapsed());
  1743. window.clearInterval(interval);
  1744. }
  1745. }, 200);
  1746. Session.set("duration", parseInt(currentSong.duration));
  1747. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  1748. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1749. resizeSeekerbar();
  1750. });*/
  1751. } else {
  1752. if ($("#player").length !== 1) {
  1753. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1754. }
  1755. if ($("#soundcloud-image").length === 1) {
  1756. $("#soundcloud-image").hide();
  1757. }
  1758. $("#player").show();
  1759. function loadVideo() {
  1760. if (YT.loaded === 0 && YT.loading === 1) {
  1761. Session.set("loadVideoTimeout", Meteor.setTimeout(function() {
  1762. loadVideo();
  1763. }, 500));
  1764. } else {
  1765. if (yt_player === undefined) {
  1766. yt_player = new YT.Player("player", {
  1767. height: 540,
  1768. width: 960,
  1769. videoId: currentSong.id,
  1770. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1771. events: {
  1772. 'onReady': function(event) {
  1773. if(currentSong.skipDuration === undefined){
  1774. currentSong.skipDuration = 0;
  1775. }
  1776. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1777. event.target.playVideo();
  1778. event.target.setVolume(volume);
  1779. resizeSeekerbar();
  1780. },
  1781. 'onStateChange': function(event){
  1782. if (YT !== undefined) {
  1783. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1784. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1785. event.target.playVideo();
  1786. }
  1787. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1788. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1789. event.target.pauseVideo();
  1790. }
  1791. }
  1792. }
  1793. }
  1794. });
  1795. } else {
  1796. yt_player.loadVideoById(currentSong.id);
  1797. if(currentSong.skipDuration === undefined){
  1798. currentSong.skipDuration = 0;
  1799. }
  1800. yt_player.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1801. }
  1802. Session.set("pauseVideo", false);
  1803. getSongInfo(currentSong);
  1804. }
  1805. };
  1806. loadVideo();
  1807. }
  1808. }
  1809. }
  1810. Session.set("loaded", false);
  1811. Meteor.subscribe("rooms", function() {
  1812. var parts = location.href.split('/');
  1813. var id = parts.pop();
  1814. var type = id.toLowerCase();
  1815. Session.set("type", type);
  1816. if (Rooms.find({type: type}).count() !== 1) {
  1817. window.location = "/";
  1818. } else {
  1819. station_c = Meteor.subscribe(type);
  1820. Session.set("loaded", true);
  1821. minterval = Meteor.setInterval(function () {
  1822. var room = Rooms.findOne({type: type});
  1823. if (room !== undefined) {
  1824. if (room.state === "paused" || Session.get("pauseVideo")) {
  1825. Session.set("state", "paused");
  1826. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  1827. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() === 1) {
  1828. yt_player.pauseVideo();
  1829. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  1830. _sound.pause();
  1831. }
  1832. } else {
  1833. Session.set("state", "playing");
  1834. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() !== 1) {
  1835. yt_player.playVideo();
  1836. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  1837. _sound.play();
  1838. }
  1839. }
  1840. }
  1841. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  1842. Session.set("previousSong", currentSong);
  1843. currentSongR = room.currentSong;
  1844. currentSong = room.currentSong.song;
  1845. currentSong.started = room.currentSong.started;
  1846. Session.set("currentSong", currentSong);
  1847. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  1848. startSong();
  1849. }
  1850. if (currentSong !== undefined) {
  1851. if (room !== undefined) {
  1852. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1853. var song_duration = currentSong.duration;
  1854. if (song_duration <= duration) {
  1855. Session.set("pauseVideo", true);
  1856. }
  1857. var d = moment.duration(duration, 'seconds');
  1858. if (Session.get("state") === "playing") {
  1859. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1860. }
  1861. }
  1862. }
  1863. }, 100);
  1864. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1865. resizeSeekerbar();
  1866. }, 500);
  1867. }
  1868. });
  1869. });