2
0

client.js 74 KB

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