client.js 74 KB

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