client.js 77 KB

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