client.js 73 KB

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