client.js 73 KB

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