client.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. Meteor.startup(function() {
  2. reCAPTCHA.config({
  3. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  4. });
  5. });
  6. Meteor.subscribe("queues");
  7. Meteor.subscribe("reports");
  8. Meteor.subscribe("chat");
  9. Meteor.subscribe("playlists");
  10. Meteor.subscribe("alerts");
  11. var minterval;
  12. var hpSound = undefined;
  13. var songsArr = [];
  14. var ytArr = [];
  15. var _sound = undefined;
  16. var parts = location.href.split('/');
  17. var id = parts.pop();
  18. var type = id.toLowerCase();
  19. var resizeSeekerbarInterval;
  20. var station_c = undefined;
  21. var songMID;
  22. UI.registerHelper("formatTime", function(seconds) {
  23. var d = moment.duration(parseInt(seconds), 'seconds');
  24. return d.minutes() + ":" + ("0" + d.seconds()).slice(-2);
  25. });
  26. /*UI.registerHelper("formatTimeFromNow", function(time) {
  27. var d = moment(time);
  28. return d.fromNow();
  29. });*/
  30. function getSpotifyInfo(title, cb, artist) {
  31. var q = "";
  32. q = title;
  33. if (artist !== undefined) {
  34. q += " artist:" + artist;
  35. }
  36. $.ajax({
  37. type: "GET",
  38. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  39. applicationType: "application/json",
  40. contentType: "json",
  41. success: function (data) {
  42. cb(data);
  43. }
  44. });
  45. }
  46. Template.settings.events({
  47. "click #save-settings": function() {
  48. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  49. }
  50. });
  51. Template.profile.helpers({
  52. "username": function() {
  53. return Session.get("username");
  54. },
  55. "first_joined": function() {
  56. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  57. },
  58. "rank": function() {
  59. return Session.get("rank");
  60. },
  61. loaded: function() {
  62. return Session.get("loaded");
  63. },
  64. likedSongs: function(){
  65. var likedArr = [];
  66. Session.get("liked").forEach(function(mid){
  67. Rooms.find().forEach(function(room){
  68. Playlists.find({type: room.type}).forEach(function(pl){
  69. for(var i in pl.songs){
  70. if(pl.songs[i].mid === mid){
  71. likedArr.push({title: pl.songs[i].title, artist: pl.songs[i].artist, room: room.display});
  72. }
  73. }
  74. });
  75. })
  76. });
  77. return likedArr;
  78. }
  79. });
  80. Template.profile.onCreated(function() {
  81. var parts = Router.current().url.split('/');
  82. var username = parts.pop();
  83. Session.set("loaded", false);
  84. Meteor.subscribe("userProfiles", username.toLowerCase(), function() {
  85. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  86. window.location = "/";
  87. } else {
  88. var data = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  89. Session.set("username", data.profile.username);
  90. Session.set("first_joined", data.createdAt);
  91. Session.set("rank", data.profile.rank);
  92. Session.set("liked", data.profile.liked);
  93. Session.set("loaded", true);
  94. }
  95. });
  96. });
  97. Template.settings.helpers({
  98. username: function() {
  99. if (Meteor.user() !== undefined) {
  100. return Meteor.user().profile.username;
  101. } else {
  102. return "";
  103. }
  104. }
  105. });
  106. Template.settings.onCreated(function() {
  107. $(document).ready(function() {
  108. var user = Meteor.user();
  109. function initSettings() {
  110. if (user !== undefined) {
  111. if (user.profile.settings && user.profile.settings.showRating === true) {
  112. function setChecked() {
  113. $("#showRating").prop("checked", true);
  114. if (!$("#showRating").prop("checked")) {
  115. Meteor.setTimeout(function() {
  116. setChecked();
  117. }, 100);
  118. }
  119. }
  120. setChecked();
  121. }
  122. } else {
  123. Meteor.setTimeout(function() {
  124. initSettings();
  125. }, 500);
  126. }
  127. }
  128. initSettings();
  129. });
  130. });
  131. 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}}};
  132. Handlebars.registerHelper('active', function(path) {
  133. return curPath() == path ? 'active' : '';
  134. });
  135. Template.header.helpers({
  136. currentUser: function() {
  137. return Meteor.user();
  138. },
  139. isAdmin: function() {
  140. if (Meteor.user() && Meteor.user().profile) {
  141. return Meteor.user().profile.rank === "admin";
  142. } else {
  143. return false;
  144. }
  145. }
  146. });
  147. Template.header.events({
  148. "click .logout": function(e){
  149. e.preventDefault();
  150. Meteor.logout();
  151. if (hpSound !== undefined) {
  152. hpSound.stop();
  153. }
  154. }
  155. });
  156. Template.register.events({
  157. "submit form": function(e){
  158. e.preventDefault();
  159. var username = e.target.registerUsername.value;
  160. var email = e.target.registerEmail.value;
  161. var password = e.target.registerPassword.value;
  162. var captchaData = grecaptcha.getResponse();
  163. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  164. grecaptcha.reset();
  165. if (err) {
  166. console.log(err);
  167. var errAlert = $('<div class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  168. $("#login").after(errAlert);
  169. errAlert.fadeOut(20000, function() {
  170. errAlert.remove();
  171. });
  172. } else {
  173. Meteor.loginWithPassword(username, password);
  174. Accounts.onLogin(function(){
  175. window.location.href = "/";
  176. })
  177. }
  178. });
  179. },
  180. "click #github-login": function(){
  181. Meteor.loginWithGithub({loginStyle: "redirect"});
  182. }
  183. });
  184. Template.login.events({
  185. "submit form": function(e){
  186. e.preventDefault();
  187. var username = e.target.loginUsername.value;
  188. var password = e.target.loginPassword.value;
  189. Meteor.loginWithPassword(username, password);
  190. Accounts.onLogin(function(){
  191. window.location.href = "/";
  192. })
  193. Accounts.onLoginFailure(function(){
  194. $("#login-form input").css("background-color","indianred");
  195. $("#login-form input").on("click",function(){
  196. $("#login-form input").css({
  197. "-webkit-appearance": "none",
  198. " -moz-appearance": "none",
  199. " appearance": "none",
  200. "outline": "0",
  201. "border": "1px solid rgba(255, 255, 255, 0.4)",
  202. "background-color": "rgba(255, 255, 255, 0.2)",
  203. "width": "304px",
  204. "border-radius": "3px",
  205. "padding": "10px 15px",
  206. "margin": "0 auto 10px auto",
  207. "display": "block",
  208. "text-align": "center",
  209. "font-size": "18px",
  210. "color": "white",
  211. "-webkit-transition-duration": "0.25s",
  212. " transition-duration": "0.25s",
  213. "font-weight": "300"
  214. });
  215. $("#login-form input:focus").css({
  216. "width": "354px",
  217. "color": "white"
  218. })
  219. $("#login-form input").on("blur", function(){
  220. $(this).css("width", "304px");
  221. })
  222. })
  223. });
  224. },
  225. "click #github-login": function(){
  226. Meteor.loginWithGithub({loginStyle: "redirect"});
  227. }
  228. });
  229. Template.dashboard.helpers({
  230. rooms: function() {
  231. return Rooms.find({});
  232. },
  233. currentSong: function() {
  234. var type = this.type;
  235. var room = Rooms.findOne({type: type});
  236. if (room !== undefined) {
  237. return room.currentSong;
  238. } else {
  239. return {};
  240. }
  241. }
  242. });
  243. Template.dashboard.onCreated(function() {
  244. if (_sound !== undefined) _sound.stop();
  245. if (minterval !== undefined) {
  246. Meteor.clearInterval(minterval);
  247. }
  248. if (resizeSeekerbarInterval !== undefined) {
  249. Meteor.clearInterval(resizeSeekerbarInterval);
  250. resizeSeekerbarInterval = undefined;
  251. }
  252. if (station_c !== undefined) {
  253. station_c.stop();
  254. }
  255. Session.set("type", undefined);
  256. });
  257. function executeCommand(command, params){
  258. if (command === "help" || command === "commands") {
  259. $('#helpModal').modal('show');
  260. return true;
  261. } else if (command === "volume") {
  262. if (params.length === 1) {
  263. var volume = Number(params[0]);
  264. if (volume >= 0 || volume <= 100) {
  265. if (volume === 0) {
  266. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  267. } else {
  268. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  269. }
  270. $("#volume-slider").slider("setValue", volume);
  271. if (yt_player !== undefined) {
  272. yt_player.setVolume(volume);
  273. localStorage.setItem("volume", volume);
  274. } else if (_sound !== undefined) {
  275. //_sound
  276. var volume = volume / 100;
  277. _sound.setVolume(volume);
  278. localStorage.setItem("volume", volume * 100);
  279. }
  280. return true;
  281. }
  282. }
  283. }
  284. }
  285. function sendMessage() {
  286. var message = $("#chat-input").val();
  287. if (message.length > 0 && message[0] !== " ") {
  288. if (message[0] === "/") {
  289. message = message.split("");
  290. message.shift();
  291. message = message.join("");
  292. var params = message.split(" ");
  293. var command = params.shift();
  294. if (executeCommand(command, params)) {
  295. $("#chat-input").val("");
  296. } else {
  297. $("#chat-input").val("");
  298. }
  299. } else {
  300. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  301. console.log(err, res);
  302. if (res) {
  303. $("#chat-input").val("");
  304. }
  305. });
  306. }
  307. }
  308. }
  309. Template.room.events({
  310. "click #sync": function() {
  311. if (Session.get("currentSong") !== undefined) {
  312. var room = Rooms.findOne({type: Session.get("type")});
  313. if (room !== undefined) {
  314. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  315. console.log(timeIn);
  316. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  317. if (yt_player !== undefined) {
  318. yt_player.seekTo(skipDuration + timeIn / 1000);
  319. }
  320. else if (_sound !== undefined) {
  321. _sound.seekTo(skipDuration * 1000 + timeIn);
  322. }
  323. }
  324. }
  325. },
  326. "click #side-panel": function(e) {
  327. Meteor.setTimeout(function() {
  328. var elem = document.getElementById('chat');
  329. elem.scrollTop = elem.scrollHeight;
  330. }, 1);
  331. },
  332. "click #submit": function() {
  333. sendMessage();
  334. },
  335. //"keyup #chat-input": function(e) {
  336. // if (e.type === "keyup" && e.which === 13) {
  337. // e.preventDefault();
  338. // sendMessage()
  339. // }
  340. //},
  341. "click #like": function(e) {
  342. $("#like").blur();
  343. Meteor.call("likeSong", Session.get("currentSong").mid);
  344. },
  345. "click #dislike": function(e) {
  346. $("#dislike").blur();
  347. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  348. },
  349. "click #vote-skip": function(){
  350. Meteor.call("voteSkip", type, function(err, res) {
  351. $("#vote-skip").attr("disabled", true);
  352. });
  353. songMID = Session.get("currentSong").mid;
  354. },
  355. "click #report-prev": function(e) {
  356. if (Session.get("previousSong") !== undefined) {
  357. Session.set("reportPrevious", true);
  358. $("#report-prev").prop("disabled", true);
  359. $("#report-curr").prop("disabled", false);
  360. }
  361. },
  362. "click #report-curr": function(e) {
  363. Session.set("reportPrevious", false);
  364. $("#report-prev").prop("disabled", false);
  365. $("#report-curr").prop("disabled", true);
  366. },
  367. "click #report-modal": function() {
  368. Session.set("currentSongR", Session.get("currentSong"));
  369. Session.set("previousSongR", Session.get("previousSong"));
  370. },
  371. "click #add-song-button": function(e){
  372. e.preventDefault();
  373. parts = location.href.split('/');
  374. var roomType = parts.pop();
  375. var genre = roomType.toLowerCase();
  376. var type = $("#type").val();
  377. id = $("#id").val();
  378. var title = $("#title").val();
  379. var artist = $("#artist").val();
  380. var img = $("#img").val();
  381. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  382. if(Playlists.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  383. $("<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(); });
  384. } else if(Queues.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  385. $("<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(); });
  386. } else{
  387. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  388. console.log(err, res);
  389. if (err) {
  390. $("<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(); });
  391. } else {
  392. $("<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(); });
  393. }
  394. });
  395. }
  396. $("#close-modal-a").click();
  397. },
  398. "click #toggle-video": function(e){
  399. e.preventDefault();
  400. if (Session.get("mediaHidden")) {
  401. $("#media-container").removeClass("hidden");
  402. $("#toggle-video").text("Hide video");
  403. Session.set("mediaHidden", false);
  404. } else {
  405. $("#media-container").addClass("hidden");
  406. $("#toggle-video").text("Show video");
  407. Session.set("mediaHidden", true);
  408. }
  409. },
  410. "click #return": function(e){
  411. $("#add-info").hide();
  412. $("#search-info").show();
  413. },
  414. "click #search-song": function(){
  415. $("#song-results").empty();
  416. var search_type = $("#search_type").val();
  417. if (search_type === "YouTube") {
  418. $.ajax({
  419. type: "GET",
  420. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  421. applicationType: "application/json",
  422. contentType: "json",
  423. success: function(data){
  424. for(var i in data.items){
  425. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>");
  426. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  427. }
  428. $("#song-results p").click(function(){
  429. $("#search-info").hide();
  430. $("#add-info").show();
  431. var title = $(this).text();
  432. for(var i in ytArr){
  433. if(ytArr[i].title === title){
  434. var songObj = {
  435. id: ytArr[i].id,
  436. title: ytArr[i].title,
  437. type: "youtube"
  438. };
  439. $("#title").val(songObj.title);
  440. $("#artist").val("");
  441. $("#id").val(songObj.id);
  442. $("#type").val("YouTube");
  443. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  444. if (data.tracks.items.length > 0) {
  445. $("#title").val(data.tracks.items[0].name);
  446. var artists = [];
  447. $("#img").val(data.tracks.items[0].album.images[1].url);
  448. data.tracks.items[0].artists.forEach(function(artist) {
  449. artists.push(artist.name);
  450. });
  451. $("#artist").val(artists.join(", "));
  452. }
  453. });
  454. }
  455. }
  456. })
  457. }
  458. })
  459. } else if (search_type === "SoundCloud") {
  460. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  461. for(var i in tracks){
  462. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  463. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  464. }
  465. $("#song-results p").click(function(){
  466. $("#search-info").hide();
  467. $("#add-info").show();
  468. var title = $(this).text();
  469. for(var i in songsArr){
  470. if(songsArr[i].title === title){
  471. var id = songsArr[i].id;
  472. var duration = songsArr[i].duration;
  473. var songObj = {
  474. title: songsArr[i].title,
  475. id: id,
  476. duration: duration,
  477. type: "soundcloud"
  478. }
  479. $("#title").val(songObj.title);
  480. // Set ID field
  481. $("#id").val(songObj.id);
  482. $("#type").val("SoundCloud");
  483. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  484. if (data.tracks.items.length > 0) {
  485. $("#title").val(data.tracks.items[0].name);
  486. var artists = [];
  487. data.tracks.items[0].artists.forEach(function(artist) {
  488. artists.push(artist.name);
  489. });
  490. $("#artist").val(artists.join(", "));
  491. }
  492. // Set title field again if possible
  493. // Set artist if possible
  494. });
  495. }
  496. }
  497. })
  498. });
  499. }
  500. },
  501. "click #close-modal-a": function(){
  502. $("#search-info").show();
  503. $("#add-info").hide();
  504. },
  505. "click #volume-icon": function(){
  506. var volume = 0;
  507. var slider = $("#volume-slider").slider();
  508. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  509. if (yt_player !== undefined) {
  510. yt_player.setVolume(volume);
  511. localStorage.setItem("volume", volume);
  512. $("#volume-slider").slider("setValue", volume);
  513. } else if (_sound !== undefined) {
  514. _sound.setVolume(volume);
  515. localStorage.setItem("volume", volume);
  516. $("#volume-slider").slider("setValue", volume);
  517. }
  518. },
  519. "click #play": function() {
  520. Meteor.call("resumeRoom", type);
  521. },
  522. "click #pause": function() {
  523. Meteor.call("pauseRoom", type);
  524. },
  525. "click #skip": function() {
  526. Meteor.call("skipSong", type);
  527. },
  528. "click #shuffle": function() {
  529. Meteor.call("shufflePlaylist", type);
  530. },
  531. "change input": function(e) {
  532. if (e.target && e.target.id) {
  533. var partsOfId = e.target.id.split("-");
  534. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  535. var camelCase = partsOfId.join("");
  536. Session.set(camelCase, e.target.checked);
  537. }
  538. },
  539. "click #report-song-button": function() {
  540. var report = {};
  541. report.reportSongB = $("#report-song").is(":checked");
  542. report.reportTitleB = $("#report-title").is(":checked");
  543. report.reportAuthorB = $("#report-author").is(":checked");
  544. report.reportDurationB = $("#report-duration").is(":checked");
  545. report.reportAudioB = $("#report-audio").is(":checked");
  546. report.reportAlbumartB = $("#report-albumart").is(":checked");
  547. report.reportOtherB = $("#report-other").is(":checked");
  548. if (report.reportSongB) {
  549. report.reportSong = {};
  550. report.reportSong.notPlayingB = $("#report-song-not-playing").is(":checked");
  551. report.reportSong.doesNotExistB = $("#report-song-does-not-exist").is(":checked");
  552. report.reportSong.otherB = $("#report-song-other").is(":checked");
  553. if (report.reportSong.otherB) {
  554. report.reportSong.other = $("#report-song-other-ta").val();
  555. }
  556. }
  557. if (report.reportTitleB) {
  558. report.reportTitle = {};
  559. report.reportTitle.incorrectB = $("#report-title-incorrect").is(":checked");
  560. report.reportTitle.inappropriateB = $("#report-title-inappropriate").is(":checked");
  561. report.reportTitle.otherB = $("#report-title-other").is(":checked");
  562. if (report.reportTitle.otherB) {
  563. report.reportTitle.other = $("#report-title-other-ta").val();
  564. }
  565. }
  566. if (report.reportAuthorB) {
  567. report.reportAuthor = {};
  568. report.reportAuthor.incorrectB = $("#report-author-incorrect").is(":checked");
  569. report.reportAuthor.inappropriateB = $("#report-author-inappropriate").is(":checked");
  570. report.reportAuthor.otherB = $("#report-author-other").is(":checked");
  571. if (report.reportAuthor.otherB) {
  572. report.reportAuthor.other = $("#report-author-other-ta").val();
  573. }
  574. }
  575. if (report.reportDurationB) {
  576. report.reportDuration = {};
  577. report.reportDuration.longB = $("#report-duration-incorrect").is(":checked");
  578. report.reportDuration.shortB = $("#report-duration-inappropriate").is(":checked");
  579. report.reportDuration.otherB = $("#report-duration-other").is(":checked");
  580. if (report.reportDuration.otherB) {
  581. report.reportDuration.other = $("#report-duration-other-ta").val();
  582. }
  583. }
  584. if (report.reportAudioB) {
  585. report.reportAudio = {};
  586. report.reportAudio.inappropriate = $("#report-audio-inappropriate").is(":checked");
  587. report.reportAudio.notPlayingB = $("#report-audio-incorrect").is(":checked");
  588. report.reportAudio.otherB = $("#report-audio-other").is(":checked");
  589. if (report.reportAudio.otherB) {
  590. report.reportAudio.other = $("#report-audio-other-ta").val();
  591. }
  592. }
  593. if (report.reportAlbumartB) {
  594. report.reportAlbumart = {};
  595. report.reportAlbumart.incorrectB = $("#report-albumart-incorrect").is(":checked");
  596. report.reportAlbumart.inappropriateB = $("#report-albumart-inappropriate").is(":checked");
  597. report.reportAlbumart.notShowingB = $("#report-albumart-inappropriate").is(":checked");
  598. report.reportAlbumart.otherB = $("#report-albumart-other").is(":checked");
  599. if (report.reportAlbumart.otherB) {
  600. report.reportAlbumart.other = $("#report-albumart-other-ta").val();
  601. }
  602. }
  603. if (report.reportOtherB) {
  604. report.other = $("#report-other-ta").val();
  605. }
  606. Meteor.call("submitReport", report, Session.get("id"), function() {
  607. $("#close-modal-r").click();
  608. });
  609. }
  610. });
  611. Template.registerHelper("rtime", function(date) {
  612. Session.get("time");
  613. if (date) {
  614. return moment(date).fromNow();
  615. }
  616. });
  617. var chatTimeInterval = undefined;
  618. Template.room.onRendered(function() {
  619. if (chatTimeInterval !== undefined) {
  620. Meteor.clearInterval(chatTimeInterval)
  621. }
  622. chatTimeInterval = Meteor.setInterval(function() {
  623. Session.set("time", new Date().getTime());
  624. }, 10000);
  625. $(document).ready(function() {
  626. function makeSlider(){
  627. var slider = $("#volume-slider").slider();
  628. var volume = Number(localStorage.getItem("volume"));
  629. $("#volume-slider").slider("setValue", volume);
  630. if (slider.length === 0) {
  631. Meteor.setTimeout(function() {
  632. makeSlider();
  633. }, 500);
  634. } else {
  635. if (volume === 0) {
  636. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  637. } else {
  638. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  639. }
  640. slider.on("slide", function(val) {
  641. if (val.value === 0) {
  642. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  643. } else {
  644. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  645. }
  646. if (yt_player !== undefined) {
  647. yt_player.setVolume(val.value);
  648. localStorage.setItem("volume", val.value);
  649. } else if (_sound !== undefined) {
  650. //_sound
  651. var volume = val.value / 100;
  652. _sound.setVolume(volume);
  653. localStorage.setItem("volume", val.value);
  654. }
  655. });
  656. }
  657. }
  658. makeSlider();
  659. });
  660. });
  661. Template.alerts.helpers({
  662. alerts: function() {
  663. return Alerts.find({active: true});
  664. }
  665. });
  666. Template.room.helpers({
  667. chat: function() {
  668. Meteor.setTimeout(function() {
  669. var elem = document.getElementById('chat');
  670. if (elem !== undefined && elem !== null) {
  671. elem.scrollTop = elem.scrollHeight;
  672. }
  673. }, 100);
  674. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  675. },
  676. likes: function() {
  677. var playlist = Playlists.findOne({type: Session.get("type")});
  678. var likes = 0;
  679. playlist.songs.forEach(function(song) {
  680. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  681. likes = song.likes;
  682. return;
  683. }
  684. });
  685. return likes;
  686. },
  687. dislikes: function() {
  688. var playlist = Playlists.findOne({type: Session.get("type")});
  689. var dislikes = 0;
  690. playlist.songs.forEach(function(song) {
  691. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  692. dislikes = song.dislikes;
  693. return;
  694. }
  695. });
  696. return dislikes;
  697. },
  698. liked: function() {
  699. if (Meteor.userId()) {
  700. var currentSong = Session.get("currentSong");
  701. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  702. return "active";
  703. } else {
  704. return "";
  705. }
  706. } else {
  707. "";
  708. }
  709. },
  710. disliked: function() {
  711. if (Meteor.userId()) {
  712. var currentSong = Session.get("currentSong");
  713. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  714. return "active";
  715. } else {
  716. return "";
  717. }
  718. } else {
  719. "";
  720. }
  721. },
  722. type: function() {
  723. var parts = location.href.split('/');
  724. var id = parts.pop().toLowerCase();
  725. return Rooms.findOne({type: id}).display;
  726. },
  727. users: function() {
  728. var parts = location.href.split('/');
  729. var id = parts.pop().toLowerCase();
  730. return Rooms.findOne({type: id}).users;
  731. },
  732. title: function(){
  733. return Session.get("title");
  734. },
  735. artist: function(){
  736. return Session.get("artist");
  737. },
  738. loaded: function() {
  739. return Session.get("loaded");
  740. },
  741. isAdmin: function() {
  742. if (Meteor.user() && Meteor.user().profile) {
  743. return Meteor.user().profile.rank === "admin";
  744. } else {
  745. return false;
  746. }
  747. },
  748. paused: function() {
  749. return Session.get("state") === "paused";
  750. },
  751. report: function() {
  752. return Session.get("reportObj");
  753. },
  754. reportSong: function() {
  755. return Session.get("reportSong");
  756. },
  757. reportTitle: function() {
  758. return Session.get("reportTitle");
  759. },
  760. reportAuthor: function() {
  761. return Session.get("reportAuthor");
  762. },
  763. reportDuration: function() {
  764. return Session.get("reportDuration");
  765. },
  766. reportAudio: function() {
  767. return Session.get("reportAudio");
  768. },
  769. reportAlbumart: function() {
  770. return Session.get("reportAlbumart");
  771. },
  772. reportOther: function() {
  773. return Session.get("reportOther");
  774. },
  775. currentSong: function() {
  776. return Session.get("currentSong");
  777. },
  778. previousSong: function() {
  779. return Session.get("previousSong");
  780. },
  781. currentSongR: function() {
  782. return Session.get("currentSongR");
  783. },
  784. previousSongR: function() {
  785. return Session.get("previousSongR");
  786. },
  787. reportingSong: function() {
  788. if (Session.get("reportPrevious")) {
  789. return Session.get("previousSongR");
  790. } else {
  791. return Session.get("currentSongR");
  792. }
  793. },
  794. votes: function(){
  795. return Rooms.findOne({type: Session.get("type")}).votes;
  796. }
  797. });
  798. var allAlertSub = undefined;
  799. Template.alertsDashboard.onCreated(function() {
  800. if (allAlertSub === undefined) {
  801. allAlertSub = Meteor.subscribe("allAlerts");
  802. }
  803. });
  804. Template.alertsDashboard.helpers({
  805. "activeAlerts": function() {
  806. return Alerts.find({active: true});
  807. },
  808. "inactiveAlerts": function() {
  809. return Alerts.find({active: false});
  810. }
  811. });
  812. Template.alertsDashboard.events({
  813. "click #calart-create": function() {
  814. Meteor.call("addAlert", $("#calert-description").val(), $("#calert-priority").val().toLowerCase(), function (err, res) {
  815. if (err) {
  816. alert("Error " + err.error + ": " + err.reason);
  817. } else {
  818. $("#calert-description").val("");
  819. }
  820. });
  821. },
  822. "click #ralert-button": function() {
  823. Meteor.call("removeAlerts");
  824. }
  825. });
  826. Template.admin.helpers({
  827. queueCount: function(display) {
  828. display = display.toLowerCase();
  829. var queues = Queues.findOne({type:display});
  830. return queues && "songs" in queues ? queues.songs.length : 0;
  831. },
  832. queues: function() {
  833. var queues = Queues.find({}).fetch();
  834. queues.map(function(queue) {
  835. return queue;
  836. });
  837. return queues;
  838. },
  839. users: function(){
  840. Meteor.call("getUserNum", function(err, num){
  841. if(err){
  842. console.log(err);
  843. }
  844. Session.set("userNum", num);
  845. });
  846. return Session.get("userNum");
  847. },
  848. playlists: function() {
  849. var playlists = Playlists.find({}).fetch();
  850. playlists.map(function(playlist) {
  851. if (Rooms.find({type: playlist.type}).count() !== 1) {
  852. return;
  853. } else {
  854. playlist.display = Rooms.findOne({type: playlist.type}).display;
  855. return playlist;
  856. }
  857. });
  858. return playlists;
  859. }/*,
  860. reports: function() {
  861. var reports = Reports.find({}).fetch();
  862. reports.findOne(
  863. {
  864. $eq: [
  865. ]
  866. }
  867. )
  868. }*/
  869. });
  870. Template.stations.helpers({
  871. queues: function() {
  872. var queues = Queues.find({}).fetch();
  873. queues.map(function(queue) {
  874. if (Rooms.find({type: queue.type}).count() !== 1) {
  875. return;
  876. } else {
  877. queue.display = Rooms.findOne({type: queue.type}).display;
  878. return queue;
  879. }
  880. });
  881. return queues;
  882. },
  883. playlists: function() {
  884. var playlists = Playlists.find({}).fetch();
  885. playlists.map(function(playlist) {
  886. if (Rooms.find({type: playlist.type}).count() !== 1) {
  887. return;
  888. } else {
  889. playlist.display = Rooms.findOne({type: playlist.type}).display;
  890. return playlist;
  891. }
  892. });
  893. return playlists;
  894. }
  895. });
  896. var yt_player = undefined;
  897. var _sound = undefined;
  898. var previewEndSongTimeout = undefined;
  899. Template.stations.events({
  900. "click .preview-button": function(e){
  901. Session.set("song", this);
  902. },
  903. "click #previewImageButton": function() {
  904. $("#preview-image").attr("src", Session.get("song").img);
  905. },
  906. "click .edit-queue-button": function(e){
  907. Session.set("song", this);
  908. Session.set("genre", $(e.toElement).data("genre"));
  909. Session.set("type", "queue");
  910. $("#type").val(this.type);
  911. $("#mid").val(this.mid);
  912. $("#artist").val(this.artist);
  913. $("#title").val(this.title);
  914. $("#img").val(this.img);
  915. $("#id").val(this.id);
  916. $("#likes").val(this.likes);
  917. $("#dislikes").val(this.dislikes);
  918. $("#duration").val(this.duration);
  919. $("#skip-duration").val(this.skipDuration);
  920. },
  921. "click .edit-playlist-button": function(e){
  922. Session.set("song", this);
  923. Session.set("genre", $(e.toElement).data("genre"));
  924. Session.set("type", "playlist");
  925. $("#type").val(this.type);
  926. $("#mid").val(this.mid);
  927. $("#artist").val(this.artist);
  928. $("#title").val(this.title);
  929. $("#img").val(this.img);
  930. $("#id").val(this.id);
  931. $("#likes").val(this.likes);
  932. $("#dislikes").val(this.dislikes);
  933. $("#duration").val(this.duration);
  934. $("#skip-duration").val(this.skipDuration);
  935. },
  936. "click #rreset_confirm": function(e){
  937. Meteor.call("resetRating");
  938. },
  939. "click .add-song-button": function(e){
  940. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  941. Meteor.call("addSongToPlaylist", genre, this);
  942. },
  943. "click .deny-song-button": function(e){
  944. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  945. Meteor.call("removeSongFromQueue", genre, this.mid);
  946. },
  947. "click .remove-song-button": function(e){
  948. var genre = $(e.toElement).data("genre") || $(e.toElement).parent().data("genre");
  949. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  950. },
  951. "click #play": function() {
  952. $("#play").attr("disabled", true);
  953. $("#stop").attr("disabled", false);
  954. var song = Session.get("song");
  955. var id = song.id;
  956. var type = song.type;
  957. var volume = localStorage.getItem("volume") || 20;
  958. if (type === "YouTube") {
  959. if (yt_player === undefined) {
  960. yt_player = new YT.Player("previewPlayer", {
  961. height: 540,
  962. width: 568,
  963. videoId: id,
  964. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  965. events: {
  966. 'onReady': function(event) {
  967. event.target.seekTo(Number(song.skipDuration));
  968. event.target.playVideo();
  969. event.target.setVolume(volume);
  970. },
  971. 'onStateChange': function(event){
  972. if (event.data == YT.PlayerState.PAUSED) {
  973. event.target.playVideo();
  974. }
  975. if (event.data == YT.PlayerState.PLAYING) {
  976. $("#play").attr("disabled", true);
  977. $("#stop").attr("disabled", false);
  978. } else {
  979. $("#play").attr("disabled", false);
  980. $("#stop").attr("disabled", true);
  981. }
  982. }
  983. }
  984. });
  985. } else {
  986. yt_player.loadVideoById(id);
  987. yt_player.seekTo(Number(song.skipDuration));
  988. }
  989. $("#previewPlayer").show();
  990. } else if (type === "SoundCloud") {
  991. SC.stream("/tracks/" + song.id, function(sound) {
  992. _sound = sound;
  993. sound.setVolume(volume / 100);
  994. sound.play();
  995. });
  996. }
  997. if (previewEndSongTimeout !== undefined) {
  998. Meteor.clearTimeout(previewEndSongTimeout);
  999. }
  1000. Meteor.setTimeout(function() {
  1001. if (yt_player !== undefined) {
  1002. yt_player.stopVideo();
  1003. }
  1004. if (_sound !== undefined) {
  1005. _sound.stop();
  1006. }
  1007. $("#play").attr("disabled", false);
  1008. $("#stop").attr("disabled", true);
  1009. $("#previewPlayer").hide();
  1010. }, song.duration * 1000);
  1011. },
  1012. "click #stop": function() {
  1013. $("#play").attr("disabled", false);
  1014. $("#stop").attr("disabled", true);
  1015. if (previewEndSongTimeout !== undefined) {
  1016. Meteor.clearTimeout(previewEndSongTimeout);
  1017. }
  1018. if (yt_player !== undefined) {
  1019. yt_player.stopVideo();
  1020. }
  1021. if (_sound !== undefined) {
  1022. _sound.stop();
  1023. }
  1024. },
  1025. "click #forward": function() {
  1026. console.log(yt_player);
  1027. console.log(Session.get("song"));
  1028. var error = false;
  1029. if (yt_player !== undefined) {
  1030. if (yt_player.getDuration() < Session.get("song").duration) {
  1031. alert("The duration of the YouTube video is smaller than the duration.");
  1032. error = true;
  1033. } else {
  1034. yt_player.seekTo(Session.get("song").skipDuration + Session.get("song").duration - 10);
  1035. }
  1036. }
  1037. if (_sound !== undefined) {
  1038. _sound.seekTo((Session.get("song").skipDuration + Session.get("song").duration - 10) * 1000);
  1039. }
  1040. if (!error) {
  1041. if (previewEndSongTimeout !== undefined) {
  1042. Meteor.clearTimeout(previewEndSongTimeout);
  1043. }
  1044. Meteor.setTimeout(function() {
  1045. if (yt_player !== undefined) {
  1046. yt_player.stopVideo();
  1047. }
  1048. if (_sound !== undefined) {
  1049. _sound.stop();
  1050. }
  1051. $("#play").attr("disabled", false);
  1052. $("#stop").attr("disabled", true);
  1053. $("#previewPlayer").hide();
  1054. }, 10000);
  1055. }
  1056. },
  1057. "click #croom_create": function() {
  1058. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  1059. if (err) {
  1060. alert("Error " + err.error + ": " + err.reason);
  1061. } else {
  1062. window.location = "/" + $("#croom_tag").val();
  1063. }
  1064. });
  1065. },
  1066. "click #get-spotify-info": function() {
  1067. var search = $("#title").val();
  1068. var artistName = $("#artist").val();
  1069. getSpotifyInfo(search, function(data) {
  1070. for(var i in data){
  1071. for(var j in data[i].items){
  1072. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1073. $("#img").val(data[i].items[j].album.images[1].url);
  1074. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1075. return;
  1076. }
  1077. }
  1078. }
  1079. }, artistName);
  1080. },
  1081. "click #save-song-button": function() {
  1082. var newSong = {};
  1083. newSong.id = $("#id").val();
  1084. newSong.likes = Number($("#likes").val());
  1085. newSong.dislikes = Number($("#dislikes").val());
  1086. newSong.title = $("#title").val();
  1087. newSong.artist = $("#artist").val();
  1088. newSong.img = $("#img").val();
  1089. newSong.type = $("#type").val();
  1090. newSong.duration = Number($("#duration").val());
  1091. newSong.skipDuration = $("#skip-duration").val();
  1092. if(newSong.skipDuration === undefined){
  1093. newSong.skipDuration = 0;
  1094. };
  1095. if (Session.get("type") === "playlist") {
  1096. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1097. $('#editModal').modal('hide');
  1098. });
  1099. } else {
  1100. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1101. $('#editModal').modal('hide');
  1102. });
  1103. }
  1104. },
  1105. "click .delete-room": function(){
  1106. var typeDel = $(this)[0].type;
  1107. Meteor.call("deleteRoom", typeDel);
  1108. }
  1109. });
  1110. Template.stations.onCreated(function() {
  1111. var tag = document.createElement("script");
  1112. tag.src = "https://www.youtube.com/iframe_api";
  1113. var firstScriptTag = document.getElementsByTagName('script')[0];
  1114. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1115. });
  1116. Template.stations.onRendered(function() {
  1117. $("#previewModal").on("hidden.bs.modal", function() {
  1118. if (previewEndSongTimeout !== undefined) {
  1119. Meteor.clearTimeout(previewEndSongTimeout);
  1120. }
  1121. $("#play").attr("disabled", false);
  1122. $("#stop").attr("disabled", true);
  1123. if (yt_player !== undefined) {
  1124. $("#previewPlayer").hide();
  1125. yt_player.seekTo(0);
  1126. yt_player.stopVideo();
  1127. }
  1128. if (_sound !== undefined) {
  1129. _sound.stop();
  1130. }
  1131. });
  1132. $(document).ready(function() {
  1133. function makeSlider(){
  1134. var slider = $("#volume-slider").slider();
  1135. var volume = localStorage.getItem("volume") || 20;
  1136. $("#volume-slider").slider("setValue", volume);
  1137. if (slider.length === 0) {
  1138. Meteor.setTimeout(function() {
  1139. makeSlider();
  1140. }, 500);
  1141. } else {
  1142. slider.on("slide", function(val) {
  1143. localStorage.setItem("volume", val.value);
  1144. if (yt_player !== undefined) {
  1145. yt_player.setVolume(val.value);
  1146. } else if (_sound !== undefined) {
  1147. var volume = val.value / 100;
  1148. _sound.setVolume(volume);
  1149. }
  1150. });
  1151. }
  1152. }
  1153. makeSlider();
  1154. });
  1155. });
  1156. Template.playlist.helpers({
  1157. playlist_songs: function() {
  1158. parts = location.href.split('/');
  1159. id = parts.pop();
  1160. type = id.toLowerCase();
  1161. var data = Playlists.findOne({type: type});
  1162. if (data !== undefined) {
  1163. data.songs.map(function(song) {
  1164. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  1165. song.current = true;
  1166. } else {
  1167. song.current = false;
  1168. }
  1169. return song;
  1170. });
  1171. return data.songs;
  1172. } else {
  1173. return [];
  1174. }
  1175. }
  1176. });
  1177. Template.playlist.events({
  1178. "keyup #search-playlist": function(){
  1179. console.log($("#search-playlist").val());
  1180. if($("#search-playlist").val().length === 0){
  1181. $(".pl-item").show();
  1182. } else {
  1183. $(".pl-item").hide();
  1184. var input = $("#search-playlist").val().toLowerCase();
  1185. $(".pl-item strong").each(function(i, el){
  1186. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1187. $(el).parent(".pl-item").show();
  1188. }
  1189. })
  1190. $(".pl-item #pl-artist").each(function(i, el){
  1191. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1192. $(el).parent(".pl-item").show();
  1193. }
  1194. })
  1195. }
  1196. }
  1197. })
  1198. Meteor.subscribe("rooms");
  1199. Template.room.onCreated(function () {
  1200. Session.set("reportSong", false);
  1201. Session.set("reportTitle", false);
  1202. Session.set("reportAuthor", false);
  1203. Session.set("reportDuration", false);
  1204. Session.set("reportAudio", false);
  1205. Session.set("reportAlbumart", false);
  1206. Session.set("reportOther", false);
  1207. if (resizeSeekerbarInterval !== undefined) {
  1208. Meteor.clearInterval(resizeSeekerbarInterval);
  1209. resizeSeekerbarInterval = undefined;
  1210. }
  1211. yt_player = undefined;
  1212. _sound = undefined;
  1213. Session.set("videoHidden", false);
  1214. var tag = document.createElement("script");
  1215. tag.src = "https://www.youtube.com/iframe_api";
  1216. var firstScriptTag = document.getElementsByTagName('script')[0];
  1217. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1218. var currentSong = undefined;
  1219. var currentSongR = undefined;
  1220. function getTimeElapsed() {
  1221. if (currentSong !== undefined) {
  1222. var room = Rooms.findOne({type: type});
  1223. if (room !== undefined) {
  1224. return Date.now() - currentSong.started - room.timePaused;
  1225. }
  1226. }
  1227. return 0;
  1228. }
  1229. function getSongInfo(songData){
  1230. Session.set("title", songData.title);
  1231. Session.set("artist", songData.artist);
  1232. Session.set("id", songData.id);
  1233. $("#song-img").attr("src", songData.img);
  1234. Session.set("duration", parseInt(songData.duration));
  1235. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1236. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1237. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1238. }
  1239. function resizeSeekerbar() {
  1240. if (Session.get("state") === "playing") {
  1241. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1242. }
  1243. }
  1244. function startSong() {
  1245. $("#time-elapsed").text("0:00");
  1246. $("#vote-skip").attr("disabled", false);
  1247. if (currentSong !== undefined) {
  1248. if (_sound !== undefined) _sound.stop();
  1249. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  1250. var volume = localStorage.getItem("volume") || 20;
  1251. if (currentSong.type === "SoundCloud") {
  1252. if ($("#soundcloud-image").length !== 1) {
  1253. //$("#media-container").append('<img alt="Not loading" src="/soundcloud-image.png" class="embed-responsive-item" id="soundcloud-image" />');
  1254. $("#media-container").append('<h1 id="soundcloud-image">We have temporarily disabled the playing of SoundCloud songs. We are sorry for this inconvenience.</h1>');
  1255. }
  1256. if ($("#player").length === 1) {
  1257. $("#player").hide();
  1258. }
  1259. $("#soundcloud-image").show();
  1260. //getSongInfo(currentSong);
  1261. /*SC.stream("/tracks/" + currentSong.id, function(sound){
  1262. _sound = sound;
  1263. sound.setVolume(volume / 100);
  1264. sound.play();
  1265. var interval = setInterval(function() {
  1266. if (sound.getState() === "playing") {
  1267. sound.seek(getTimeElapsed());
  1268. window.clearInterval(interval);
  1269. }
  1270. }, 200);
  1271. Session.set("duration", parseInt(currentSong.duration));
  1272. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  1273. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1274. resizeSeekerbar();
  1275. });*/
  1276. } else {
  1277. if ($("#player").length !== 1) {
  1278. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1279. }
  1280. if ($("#soundcloud-image").length === 1) {
  1281. $("#soundcloud-image").hide();
  1282. }
  1283. $("#player").show();
  1284. if (yt_player === undefined) {
  1285. yt_player = new YT.Player("player", {
  1286. height: 540,
  1287. width: 960,
  1288. videoId: currentSong.id,
  1289. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1290. events: {
  1291. 'onReady': function(event) {
  1292. if(currentSong.skipDuration === undefined){
  1293. currentSong.skipDuration = 0;
  1294. }
  1295. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1296. event.target.playVideo();
  1297. event.target.setVolume(volume);
  1298. resizeSeekerbar();
  1299. },
  1300. 'onStateChange': function(event){
  1301. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1302. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1303. event.target.playVideo();
  1304. }
  1305. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1306. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1307. event.target.pauseVideo();
  1308. }
  1309. }
  1310. }
  1311. });
  1312. } else {
  1313. yt_player.loadVideoById(currentSong.id);
  1314. if(currentSong.skipDuration === undefined){
  1315. currentSong.skipDuration = 0;
  1316. }
  1317. yt_player.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1318. }
  1319. getSongInfo(currentSong);
  1320. }
  1321. }
  1322. }
  1323. Session.set("loaded", false);
  1324. Meteor.subscribe("rooms", function() {
  1325. var parts = location.href.split('/');
  1326. var id = parts.pop();
  1327. var type = id.toLowerCase();
  1328. Session.set("type", type);
  1329. if (Rooms.find({type: type}).count() !== 1) {
  1330. window.location = "/";
  1331. } else {
  1332. station_c = Meteor.subscribe(type);
  1333. Session.set("loaded", true);
  1334. minterval = Meteor.setInterval(function () {
  1335. var room = Rooms.findOne({type: type});
  1336. if (room !== undefined) {
  1337. if (room.state === "paused") {
  1338. Session.set("state", "paused");
  1339. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() === 1) {
  1340. yt_player.pauseVideo();
  1341. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  1342. _sound.pause();
  1343. }
  1344. } else {
  1345. Session.set("state", "playing");
  1346. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() !== 1) {
  1347. yt_player.playVideo();
  1348. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  1349. _sound.play();
  1350. }
  1351. }
  1352. }
  1353. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  1354. Session.set("previousSong", currentSong);
  1355. currentSongR = room.currentSong;
  1356. currentSong = room.currentSong.song;
  1357. currentSong.started = room.currentSong.started;
  1358. Session.set("currentSong", currentSong);
  1359. startSong();
  1360. }
  1361. if (currentSong !== undefined) {
  1362. if (room !== undefined) {
  1363. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1364. var d = moment.duration(duration, 'seconds');
  1365. if (Session.get("state") === "playing") {
  1366. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1367. }
  1368. }
  1369. }
  1370. }, 1000);
  1371. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1372. resizeSeekerbar();
  1373. }, 500);
  1374. }
  1375. });
  1376. });