2
0

client.js 78 KB

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