client.js 72 KB

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