client.js 73 KB

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