client.js 77 KB

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