client.js 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089
  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 #chat-tab": function(e) {
  644. Meteor.setTimeout(function() {
  645. var elem = document.getElementById('chat');
  646. elem.scrollTop = elem.scrollHeight;
  647. }, 1);
  648. },
  649. "click #global-tab": function(e) {
  650. Meteor.setTimeout(function() {
  651. var elem = document.getElementById('global-chat');
  652. elem.scrollTop = elem1.scrollHeight;
  653. }, 1);
  654. },
  655. "click #submit": function() {
  656. sendMessage();
  657. },
  658. "click #global-submit": function() {
  659. sendMessageGlobal();
  660. },
  661. "keyup #chat-input": function(e) {
  662. if (e.type === "keyup" && e.which === 13) {
  663. e.preventDefault();
  664. if (!$('#chat-input').data('dropdownshown')) {
  665. sendMessage();
  666. }
  667. }
  668. },
  669. "keyup #global-chat-input": function(e) {
  670. if (e.type === "keyup" && e.which === 13) {
  671. e.preventDefault();
  672. if (!$('#global-chat-input').data('dropdownshown')) {
  673. sendMessageGlobal();
  674. }
  675. }
  676. },
  677. "click #like": function(e) {
  678. $("#like").blur();
  679. Meteor.call("likeSong", Session.get("currentSong").mid);
  680. },
  681. "click #dislike": function(e) {
  682. $("#dislike").blur();
  683. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  684. },
  685. "click #vote-skip": function(){
  686. Meteor.call("voteSkip", type, function(err, res) {
  687. $("#vote-skip").attr("disabled", true);
  688. });
  689. songMID = Session.get("currentSong").mid;
  690. },
  691. "click #report-prev": function(e) {
  692. if (Session.get("previousSong") !== undefined) {
  693. Session.set("reportPrevious", true);
  694. $("#report-prev").prop("disabled", true);
  695. $("#report-curr").prop("disabled", false);
  696. }
  697. },
  698. "click #report-curr": function(e) {
  699. Session.set("reportPrevious", false);
  700. $("#report-prev").prop("disabled", false);
  701. $("#report-curr").prop("disabled", true);
  702. },
  703. "click #report-modal": function() {
  704. Session.set("currentSongR", Session.get("currentSong"));
  705. Session.set("previousSongR", Session.get("previousSong"));
  706. },
  707. "click #add-song-button": function(e){
  708. e.preventDefault();
  709. parts = location.href.split('/');
  710. var roomType = parts.pop();
  711. var genre = roomType.toLowerCase();
  712. var type = $("#type").val();
  713. id = $("#id").val();
  714. var title = $("#title").val();
  715. var artist = $("#artist").val();
  716. var img = $("#img").val();
  717. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  718. if(Playlists.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  719. $("<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(); });
  720. } else if(Queues.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  721. $("<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(); });
  722. } else{
  723. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  724. console.log(err, res);
  725. if (err) {
  726. $("<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(); });
  727. } else {
  728. $("<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(); });
  729. }
  730. });
  731. }
  732. $("#close-modal-a").click();
  733. },
  734. "click #toggle-video": function(e){
  735. e.preventDefault();
  736. if (Session.get("mediaHidden")) {
  737. $("#media-container").removeClass("hidden");
  738. $("#toggle-video").text("Hide video");
  739. Session.set("mediaHidden", false);
  740. } else {
  741. $("#media-container").addClass("hidden");
  742. $("#toggle-video").text("Show video");
  743. Session.set("mediaHidden", true);
  744. }
  745. },
  746. "click #return": function(e){
  747. $("#add-info").hide();
  748. $("#search-info").show();
  749. },
  750. "click #search-song": function(){
  751. $("#song-results").empty();
  752. var search_type = $("#search_type").val();
  753. if (search_type === "YouTube") {
  754. $.ajax({
  755. type: "GET",
  756. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  757. applicationType: "application/json",
  758. contentType: "json",
  759. success: function(data){
  760. for(var i in data.items){
  761. var item = data.items[i];
  762. $("#song-results").append(
  763. "<div>" +
  764. "<img src='" + item.snippet.thumbnails.medium.url + "' class='song-result-thumbnail'/>" +
  765. "<div>" +
  766. "<span class='song-result-title'>" + item.snippet.title + "</span>" +
  767. "<span class='song-result-channel'>" + item.snippet.channelTitle + "</span>" +
  768. "</div>" +
  769. "</div>"
  770. );
  771. ytArr.push({title: item.snippet.title, id: item.id.videoId});
  772. }
  773. $("#song-results > div").click(function(){
  774. $("#search-info").hide();
  775. $("#add-info").show();
  776. var title = $(this).find("div > .song-result-title").text();
  777. for(var i in ytArr){
  778. if(ytArr[i].title === title){
  779. var songObj = {
  780. id: ytArr[i].id,
  781. title: ytArr[i].title,
  782. type: "youtube"
  783. };
  784. $("#title").val(songObj.title);
  785. $("#artist").val("");
  786. $("#id").val(songObj.id);
  787. $("#type").val("YouTube");
  788. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  789. if (data.tracks.items.length > 0) {
  790. $("#title").val(data.tracks.items[0].name);
  791. var artists = [];
  792. $("#img").val(data.tracks.items[0].album.images[1].url);
  793. data.tracks.items[0].artists.forEach(function(artist) {
  794. artists.push(artist.name);
  795. });
  796. $("#artist").val(artists.join(", "));
  797. }
  798. });
  799. }
  800. }
  801. })
  802. }
  803. })
  804. } else if (search_type === "SoundCloud") {
  805. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  806. for(var i in tracks){
  807. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  808. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  809. }
  810. $("#song-results p").click(function(){
  811. $("#search-info").hide();
  812. $("#add-info").show();
  813. var title = $(this).text();
  814. for(var i in songsArr){
  815. if(songsArr[i].title === title){
  816. var id = songsArr[i].id;
  817. var duration = songsArr[i].duration;
  818. var songObj = {
  819. title: songsArr[i].title,
  820. id: id,
  821. duration: duration,
  822. type: "soundcloud"
  823. }
  824. $("#title").val(songObj.title);
  825. // Set ID field
  826. $("#id").val(songObj.id);
  827. $("#type").val("SoundCloud");
  828. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  829. if (data.tracks.items.length > 0) {
  830. $("#title").val(data.tracks.items[0].name);
  831. var artists = [];
  832. data.tracks.items[0].artists.forEach(function(artist) {
  833. artists.push(artist.name);
  834. });
  835. $("#artist").val(artists.join(", "));
  836. }
  837. // Set title field again if possible
  838. // Set artist if possible
  839. });
  840. }
  841. }
  842. })
  843. });
  844. }
  845. },
  846. "click #close-modal-a": function(){
  847. $("#search-info").show();
  848. $("#add-info").hide();
  849. },
  850. "click #volume-icon": function(){
  851. var volume = 0;
  852. var slider = $("#volume-slider").slider();
  853. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  854. if (yt_player !== undefined) {
  855. yt_player.setVolume(volume);
  856. localStorage.setItem("volume", volume);
  857. $("#volume-slider").slider("setValue", volume);
  858. } else if (_sound !== undefined) {
  859. _sound.setVolume(volume);
  860. localStorage.setItem("volume", volume);
  861. $("#volume-slider").slider("setValue", volume);
  862. }
  863. },
  864. "click #play": function() {
  865. Meteor.call("resumeRoom", type);
  866. },
  867. "click #pause": function() {
  868. Meteor.call("pauseRoom", type);
  869. },
  870. "click #skip": function() {
  871. Meteor.call("skipSong", type);
  872. },
  873. "click #shuffle": function() {
  874. Meteor.call("shufflePlaylist", type);
  875. },
  876. "change input": function(e) {
  877. if (e.target && e.target.id) {
  878. var partsOfId = e.target.id.split("-");
  879. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  880. var camelCase = partsOfId.join("");
  881. Session.set(camelCase, e.target.checked);
  882. }
  883. },
  884. "click #report-song-button": function() {
  885. var room = Session.get("type");
  886. var reportData = {};
  887. reportData.song = Session.get("currentSong").mid;
  888. reportData.type = [];
  889. reportData.reason = [];
  890. $(".report-layer-1 > .checkbox input:checked").each(function(){
  891. reportData.type.push(this.id);
  892. if (this.id == "report-other") {
  893. var otherText = $(".other-textarea").val();
  894. }
  895. });
  896. $(".report-layer-2 input:checked").each(function(){
  897. reportData.reason.push(this.id);
  898. });
  899. console.log(reportData);
  900. Meteor.call("submitReport", room, reportData, Session.get("id"), function() {
  901. $("#close-modal-r").click();
  902. });
  903. }
  904. });
  905. Template.banned.helpers({
  906. bannedAt: function() {
  907. if (Session.get("ban") !== undefined) {
  908. return Session.get("ban").bannedAt;
  909. }
  910. },
  911. bannedBy: function() {
  912. if (Session.get("ban") !== undefined) {
  913. return Session.get("ban").bannedBy;
  914. }
  915. },
  916. bannedUntil: function() {
  917. if (Session.get("ban") !== undefined) {
  918. return Session.get("ban").bannedUntil;
  919. }
  920. },
  921. bannedReason: function() {
  922. if (Session.get("ban") !== undefined) {
  923. return Session.get("ban").bannedReason;
  924. }
  925. }
  926. });
  927. Template.banned.onCreated(function() {
  928. if (rTimeInterval !== undefined) {
  929. Meteor.clearInterval(rTimeInterval)
  930. }
  931. rTimeInterval = Meteor.setInterval(function() {
  932. Session.set("time", new Date().getTime());
  933. }, 10000);
  934. Session.set("ban", Meteor.user().punishments.ban);
  935. });
  936. Template.registerHelper("rtime", function(date) {
  937. Session.get("time");
  938. if (date) {
  939. return moment(date).fromNow();
  940. }
  941. });
  942. var rTimeInterval = undefined;
  943. Template.room.onRendered(function() {
  944. if (rTimeInterval !== undefined) {
  945. Meteor.clearInterval(rTimeInterval)
  946. }
  947. rTimeInterval = Meteor.setInterval(function() {
  948. Session.set("time", new Date().getTime());
  949. }, 10000);
  950. $(document).ready(function() {
  951. function makeSlider(){
  952. var slider = $("#volume-slider").slider();
  953. var volume = Number(localStorage.getItem("volume"));
  954. $("#volume-slider").slider("setValue", volume);
  955. if (slider.length === 0) {
  956. Meteor.setTimeout(function() {
  957. makeSlider();
  958. }, 500);
  959. } else {
  960. if (volume === 0) {
  961. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  962. } else {
  963. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  964. }
  965. slider.on("slide", function(val) {
  966. if (val.value === 0) {
  967. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  968. } else {
  969. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  970. }
  971. if (yt_player !== undefined) {
  972. yt_player.setVolume(val.value);
  973. localStorage.setItem("volume", val.value);
  974. } else if (_sound !== undefined) {
  975. //_sound
  976. var volume = val.value / 100;
  977. _sound.setVolume(volume);
  978. localStorage.setItem("volume", val.value);
  979. }
  980. });
  981. }
  982. }
  983. makeSlider();
  984. });
  985. });
  986. Template.alerts.helpers({
  987. alerts: function() {
  988. return Alerts.find({active: true});
  989. }
  990. });
  991. Template.room.helpers({
  992. singleVideo: function() {
  993. return true;
  994. },
  995. chat: function() {
  996. Meteor.setTimeout(function() {
  997. var elem = document.getElementById('chat');
  998. if (elem !== undefined && elem !== null) {
  999. elem.scrollTop = elem.scrollHeight;
  1000. }
  1001. }, 100);
  1002. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1003. },
  1004. globalChat: function() {
  1005. Meteor.setTimeout(function() {
  1006. var elem = document.getElementById('global-chat');
  1007. if (elem !== undefined && elem !== null) {
  1008. elem.scrollTop = elem.scrollHeight;
  1009. }
  1010. }, 100);
  1011. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1012. },
  1013. likes: function() {
  1014. var playlist = Playlists.findOne({type: Session.get("type")});
  1015. var likes = 0;
  1016. playlist.songs.forEach(function(song) {
  1017. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1018. likes = song.likes;
  1019. return;
  1020. }
  1021. });
  1022. return likes;
  1023. },
  1024. dislikes: function() {
  1025. var playlist = Playlists.findOne({type: Session.get("type")});
  1026. var dislikes = 0;
  1027. playlist.songs.forEach(function(song) {
  1028. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1029. dislikes = song.dislikes;
  1030. return;
  1031. }
  1032. });
  1033. return dislikes;
  1034. },
  1035. liked: function() {
  1036. if (Meteor.userId()) {
  1037. var currentSong = Session.get("currentSong");
  1038. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  1039. return "active";
  1040. } else {
  1041. return "";
  1042. }
  1043. } else {
  1044. "";
  1045. }
  1046. },
  1047. disliked: function() {
  1048. if (Meteor.userId()) {
  1049. var currentSong = Session.get("currentSong");
  1050. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  1051. return "active";
  1052. } else {
  1053. return "";
  1054. }
  1055. } else {
  1056. "";
  1057. }
  1058. },
  1059. type: function() {
  1060. var parts = location.href.split('/');
  1061. var id = parts.pop().toLowerCase();
  1062. return Rooms.findOne({type: id}).display;
  1063. },
  1064. users: function() {
  1065. var parts = location.href.split('/');
  1066. var id = parts.pop().toLowerCase();
  1067. return Rooms.findOne({type: id}).users;
  1068. },
  1069. title: function(){
  1070. return Session.get("title");
  1071. },
  1072. artist: function(){
  1073. return Session.get("artist");
  1074. },
  1075. loaded: function() {
  1076. return Session.get("loaded");
  1077. },
  1078. paused: function() {
  1079. return Session.get("state") === "paused";
  1080. },
  1081. private: function() {
  1082. return Rooms.findOne({type: Session.get("type")}).private === true;
  1083. },
  1084. report: function() {
  1085. return Session.get("reportObj");
  1086. },
  1087. reportSong: function() {
  1088. return Session.get("reportSong");
  1089. },
  1090. reportTitle: function() {
  1091. return Session.get("reportTitle");
  1092. },
  1093. reportAuthor: function() {
  1094. return Session.get("reportAuthor");
  1095. },
  1096. reportDuration: function() {
  1097. return Session.get("reportDuration");
  1098. },
  1099. reportAudio: function() {
  1100. return Session.get("reportAudio");
  1101. },
  1102. reportAlbumart: function() {
  1103. return Session.get("reportAlbumart");
  1104. },
  1105. reportOther: function() {
  1106. return Session.get("reportOther");
  1107. },
  1108. currentSong: function() {
  1109. return Session.get("currentSong");
  1110. },
  1111. previousSong: function() {
  1112. return Session.get("previousSong");
  1113. },
  1114. currentSongR: function() {
  1115. return Session.get("currentSongR");
  1116. },
  1117. previousSongR: function() {
  1118. return Session.get("previousSongR");
  1119. },
  1120. reportingSong: function() {
  1121. if (Session.get("reportPrevious")) {
  1122. return Session.get("previousSongR");
  1123. } else {
  1124. return Session.get("currentSongR");
  1125. }
  1126. },
  1127. votes: function(){
  1128. return Rooms.findOne({type: Session.get("type")}).votes;
  1129. }
  1130. });
  1131. var allAlertSub = undefined;
  1132. Template.alertsDashboard.onCreated(function() {
  1133. if (allAlertSub === undefined) {
  1134. allAlertSub = Meteor.subscribe("allAlerts");
  1135. }
  1136. });
  1137. Template.alertsDashboard.helpers({
  1138. "activeAlerts": function() {
  1139. return Alerts.find({active: true});
  1140. },
  1141. "inactiveAlerts": function() {
  1142. return Alerts.find({active: false});
  1143. }
  1144. });
  1145. Template.alertsDashboard.events({
  1146. "click #calart-create": function() {
  1147. Meteor.call("addAlert", $("#calert-description").val(), $("#calert-priority").val().toLowerCase(), function (err, res) {
  1148. if (err) {
  1149. alert("Error " + err.error + ": " + err.reason);
  1150. } else {
  1151. $("#calert-description").val("");
  1152. }
  1153. });
  1154. },
  1155. "click #ralert-button": function() {
  1156. Meteor.call("removeAlerts");
  1157. }
  1158. });
  1159. Template.admin.helpers({
  1160. queueCount: function(display) {
  1161. display = display.toLowerCase();
  1162. var queues = Queues.findOne({type:display});
  1163. return queues && "songs" in queues ? queues.songs.length : 0;
  1164. },
  1165. queues: function() {
  1166. var queues = Queues.find({}).fetch();
  1167. return queues;
  1168. },
  1169. usersOnline: function(){
  1170. Meteor.call("getUserNum", function(err, num){
  1171. if(err){
  1172. console.log(err);
  1173. }
  1174. Session.set("userNum", num);
  1175. });
  1176. return Session.get("userNum");
  1177. },
  1178. allUsers: function(){
  1179. Meteor.call("getTotalUsers", function(err, num){
  1180. Session.set("allUsers", num);
  1181. })
  1182. return Session.get("allUsers");
  1183. },
  1184. playlists: function() {
  1185. var playlists = Playlists.find({}).fetch();
  1186. playlists.map(function(playlist) {
  1187. if (Rooms.find({type: playlist.type}).count() !== 1) {
  1188. return;
  1189. } else {
  1190. playlist.display = Rooms.findOne({type: playlist.type}).display;
  1191. return playlist;
  1192. }
  1193. });
  1194. return playlists;
  1195. },
  1196. reportsCount: function(room) {
  1197. room = room.toLowerCase();
  1198. var reports = Reports.findOne({room:room});
  1199. return reports && "report" in reports ? reports.report.length : 0;
  1200. }
  1201. });
  1202. Template.admin.events({
  1203. "click #croom_create": function() {
  1204. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  1205. if (err) {
  1206. alert("Error " + err.error + ": " + err.reason);
  1207. } else {
  1208. window.location = "/" + $("#croom_tag").val();
  1209. }
  1210. });
  1211. },
  1212. "click a": function(e){
  1213. var id = e.currentTarget.id;
  1214. console.log(id.toLowerCase());
  1215. Session.set("playlistToEdit", id);
  1216. },
  1217. "click #croom_create": function() {
  1218. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#two").prop("checked"), function (err, res) {
  1219. if (err) {
  1220. alert("Error " + err.error + ": " + err.reason);
  1221. } else {
  1222. window.location = "/" + $("#croom_tag").val();
  1223. }
  1224. });
  1225. },
  1226. "click #rreset_confirm": function(){
  1227. $('#confirmModal').modal('hide');
  1228. Meteor.call("resetRating");
  1229. }
  1230. });
  1231. Template.stations.helpers({
  1232. playlist: function() {
  1233. var query = {type: Session.get("playlistToEdit").toLowerCase()};
  1234. var playlists = Playlists.find(query).fetch();
  1235. return playlists;
  1236. },
  1237. whichStation: function(){
  1238. return Session.get("playlistToEdit");
  1239. },
  1240. reports: function() {
  1241. var query = {room: Session.get("playlistToEdit").toLowerCase()};
  1242. var reports = Reports.find(query).fetch();
  1243. console.log(reports);
  1244. return reports;
  1245. }
  1246. });
  1247. Template.queues.helpers({
  1248. queues: function() {
  1249. var queues = Queues.find({}).fetch();
  1250. queues.map(function(queue) {
  1251. if (Rooms.find({type: queue.type}).count() !== 1) {
  1252. return;
  1253. } else {
  1254. queue.display = Rooms.findOne({type: queue.type}).display;
  1255. return queue;
  1256. }
  1257. });
  1258. return queues;
  1259. }
  1260. })
  1261. var yt_player = undefined;
  1262. var _sound = undefined;
  1263. var previewEndSongTimeout = undefined;
  1264. Template.stations.events({
  1265. "click .preview-button": function(e){
  1266. Session.set("song", this);
  1267. },
  1268. "click #previewImageButton": function() {
  1269. $("#preview-image").attr("src", Session.get("song").img);
  1270. },
  1271. "click .edit-queue-button": function(e){
  1272. Session.set("song", this);
  1273. Session.set("genre", $(e.target).data("genre"));
  1274. Session.set("type", "queue");
  1275. $("#type").val(this.type);
  1276. $("#mid").val(this.mid);
  1277. $("#artist").val(this.artist);
  1278. $("#title").val(this.title);
  1279. $("#img").val(this.img);
  1280. $("#id").val(this.id);
  1281. $("#likes").val(this.likes);
  1282. $("#dislikes").val(this.dislikes);
  1283. $("#duration").val(this.duration);
  1284. $("#skip-duration").val(this.skipDuration);
  1285. },
  1286. "click .edit-playlist-button": function(e){
  1287. Session.set("song", this);
  1288. Session.set("genre", $(e.target).data("genre"));
  1289. Session.set("type", "playlist");
  1290. $("#type").val(this.type);
  1291. $("#mid").val(this.mid);
  1292. $("#artist").val(this.artist);
  1293. $("#title").val(this.title);
  1294. $("#img").val(this.img);
  1295. $("#id").val(this.id);
  1296. $("#likes").val(this.likes);
  1297. $("#dislikes").val(this.dislikes);
  1298. $("#duration").val(this.duration);
  1299. $("#skip-duration").val(this.skipDuration);
  1300. },
  1301. "click .add-song-button": function(e){
  1302. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1303. Meteor.call("addSongToPlaylist", genre, this);
  1304. },
  1305. "click .deny-song-button": function(e){
  1306. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1307. Meteor.call("removeSongFromQueue", genre, this.mid);
  1308. },
  1309. "click .remove-song-button": function(e){
  1310. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1311. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  1312. },
  1313. "click #moveSong": function(e){
  1314. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1315. if (genre !== Session.get(genre)) {
  1316. 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});
  1317. Meteor.call("removeSongFromPlaylist", Session.get("genre"), Session.get("song").mid);
  1318. }else {
  1319. console.log("Something Went Wrong?!");
  1320. return false;
  1321. }
  1322. },
  1323. "click #copySong": function(e){
  1324. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1325. 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});
  1326. },
  1327. "click .copyMove-button": function(e){
  1328. Session.set("song", this);
  1329. Session.set("genre", $(e.target).data("genre"));
  1330. },
  1331. "click #play": function() {
  1332. $("#play").attr("disabled", true);
  1333. $("#stop").attr("disabled", false);
  1334. var song = Session.get("song");
  1335. var id = song.id;
  1336. var type = song.type;
  1337. var volume = localStorage.getItem("volume") || 20;
  1338. if (type === "YouTube") {
  1339. if (yt_player === undefined) {
  1340. yt_player = new YT.Player("previewPlayer", {
  1341. height: 540,
  1342. width: 568,
  1343. videoId: id,
  1344. playerVars: {controls: 0, iv_load_policy: 3, showinfo: 0},
  1345. events: {
  1346. 'onReady': function(event) {
  1347. event.target.seekTo(Number(song.skipDuration));
  1348. event.target.playVideo();
  1349. event.target.setVolume(volume);
  1350. },
  1351. 'onStateChange': function(event){
  1352. if (event.data == YT.PlayerState.PAUSED) {
  1353. event.target.playVideo();
  1354. }
  1355. if (event.data == YT.PlayerState.PLAYING) {
  1356. $("#play").attr("disabled", true);
  1357. $("#stop").attr("disabled", false);
  1358. } else {
  1359. $("#play").attr("disabled", false);
  1360. $("#stop").attr("disabled", true);
  1361. }
  1362. }
  1363. }
  1364. });
  1365. } else {
  1366. yt_player.loadVideoById(id);
  1367. yt_player.seekTo(Number(song.skipDuration));
  1368. }
  1369. $("#previewPlayer").show();
  1370. } else if (type === "SoundCloud") {
  1371. SC.stream("/tracks/" + song.id, function(sound) {
  1372. _sound = sound;
  1373. sound.setVolume(volume / 100);
  1374. sound.play();
  1375. });
  1376. }
  1377. if (previewEndSongTimeout !== undefined) {
  1378. Meteor.clearTimeout(previewEndSongTimeout);
  1379. }
  1380. previewEndSongTimeout = Meteor.setTimeout(function() {
  1381. if (yt_player !== undefined) {
  1382. yt_player.stopVideo();
  1383. }
  1384. if (_sound !== undefined) {
  1385. _sound.stop();
  1386. }
  1387. $("#play").attr("disabled", false);
  1388. $("#stop").attr("disabled", true);
  1389. $("#previewPlayer").hide();
  1390. }, song.duration * 1000);
  1391. },
  1392. "click #stop": function() {
  1393. $("#play").attr("disabled", false);
  1394. $("#stop").attr("disabled", true);
  1395. if (previewEndSongTimeout !== undefined) {
  1396. Meteor.clearTimeout(previewEndSongTimeout);
  1397. }
  1398. if (yt_player !== undefined) {
  1399. yt_player.stopVideo();
  1400. }
  1401. if (_sound !== undefined) {
  1402. _sound.stop();
  1403. }
  1404. },
  1405. "click #forward": function() {
  1406. var error = false;
  1407. if (yt_player !== undefined) {
  1408. var duration = Number(Session.get("song").duration) | 0;
  1409. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1410. if (yt_player.getDuration() < duration + skipDuration) {
  1411. alert("The duration of the YouTube video is smaller than the duration.");
  1412. error = true;
  1413. } else {
  1414. yt_player.seekTo(skipDuration + duration - 10);
  1415. }
  1416. }
  1417. if (_sound !== undefined) {
  1418. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1419. }
  1420. if (!error) {
  1421. if (previewEndSongTimeout !== undefined) {
  1422. Meteor.clearTimeout(previewEndSongTimeout);
  1423. }
  1424. previewEndSongTimeout = Meteor.setTimeout(function() {
  1425. if (yt_player !== undefined) {
  1426. yt_player.stopVideo();
  1427. }
  1428. if (_sound !== undefined) {
  1429. _sound.stop();
  1430. }
  1431. $("#play").attr("disabled", false);
  1432. $("#stop").attr("disabled", true);
  1433. $("#previewPlayer").hide();
  1434. }, 10000);
  1435. }
  1436. },
  1437. "click #get-spotify-info": function() {
  1438. var search = $("#title").val();
  1439. var artistName = $("#artist").val();
  1440. getSpotifyInfo(search, function(data) {
  1441. for(var i in data){
  1442. for(var j in data[i].items){
  1443. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1444. $("#img").val(data[i].items[j].album.images[1].url);
  1445. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1446. return;
  1447. }
  1448. }
  1449. }
  1450. }, artistName);
  1451. },
  1452. "click #save-song-button": function() {
  1453. var newSong = {};
  1454. newSong.id = $("#id").val();
  1455. newSong.likes = Number($("#likes").val());
  1456. newSong.dislikes = Number($("#dislikes").val());
  1457. newSong.title = $("#title").val();
  1458. newSong.artist = $("#artist").val();
  1459. newSong.img = $("#img").val();
  1460. newSong.type = $("#type").val();
  1461. newSong.duration = Number($("#duration").val());
  1462. newSong.skipDuration = $("#skip-duration").val();
  1463. if(newSong.skipDuration === undefined){
  1464. newSong.skipDuration = 0;
  1465. };
  1466. if (Session.get("type") === "playlist") {
  1467. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1468. $('#editModal').modal('hide');
  1469. });
  1470. } else {
  1471. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1472. $('#editModal').modal('hide');
  1473. });
  1474. }
  1475. },
  1476. "click .delete-room": function(){
  1477. var typeDel = $(this)[0].type;
  1478. Meteor.call("deleteRoom", typeDel);
  1479. }
  1480. });
  1481. Template.queues.events({
  1482. "click .preview-button": function(e){
  1483. Session.set("song", this);
  1484. },
  1485. "click #previewImageButton": function() {
  1486. $("#preview-image").attr("src", Session.get("song").img);
  1487. },
  1488. "click .edit-queue-button": function(e){
  1489. Session.set("song", this);
  1490. Session.set("genre", $(e.target).data("genre"));
  1491. Session.set("type", "queue");
  1492. $("#type").val(this.type);
  1493. $("#mid").val(this.mid);
  1494. $("#artist").val(this.artist);
  1495. $("#title").val(this.title);
  1496. $("#img").val(this.img);
  1497. $("#id").val(this.id);
  1498. $("#likes").val(this.likes);
  1499. $("#dislikes").val(this.dislikes);
  1500. $("#duration").val(this.duration);
  1501. $("#skip-duration").val(this.skipDuration);
  1502. },
  1503. "click .add-song-button": function(e){
  1504. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1505. Meteor.call("addSongToPlaylist", genre, this);
  1506. },
  1507. "click .deny-song-button": function(e){
  1508. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1509. Meteor.call("removeSongFromQueue", genre, this.mid);
  1510. },
  1511. "click #play": function() {
  1512. $("#play").attr("disabled", true);
  1513. $("#stop").attr("disabled", false);
  1514. var song = Session.get("song");
  1515. var id = song.id;
  1516. var type = song.type;
  1517. var volume = localStorage.getItem("volume") || 20;
  1518. if (type === "YouTube") {
  1519. if (yt_player === undefined) {
  1520. yt_player = new YT.Player("previewPlayer", {
  1521. height: 540,
  1522. width: 568,
  1523. videoId: id,
  1524. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  1525. events: {
  1526. 'onReady': function(event) {
  1527. event.target.seekTo(Number(song.skipDuration));
  1528. event.target.playVideo();
  1529. event.target.setVolume(volume);
  1530. },
  1531. 'onStateChange': function(event){
  1532. if (event.data == YT.PlayerState.PAUSED) {
  1533. event.target.playVideo();
  1534. }
  1535. if (event.data == YT.PlayerState.PLAYING) {
  1536. $("#play").attr("disabled", true);
  1537. $("#stop").attr("disabled", false);
  1538. } else {
  1539. $("#play").attr("disabled", false);
  1540. $("#stop").attr("disabled", true);
  1541. }
  1542. }
  1543. }
  1544. });
  1545. } else {
  1546. yt_player.loadVideoById(id);
  1547. yt_player.seekTo(Number(song.skipDuration));
  1548. }
  1549. $("#previewPlayer").show();
  1550. } else if (type === "SoundCloud") {
  1551. SC.stream("/tracks/" + song.id, function(sound) {
  1552. _sound = sound;
  1553. sound.setVolume(volume / 100);
  1554. sound.play();
  1555. });
  1556. }
  1557. if (previewEndSongTimeout !== undefined) {
  1558. Meteor.clearTimeout(previewEndSongTimeout);
  1559. }
  1560. previewEndSongTimeout = Meteor.setTimeout(function() {
  1561. if (yt_player !== undefined) {
  1562. yt_player.stopVideo();
  1563. }
  1564. if (_sound !== undefined) {
  1565. _sound.stop();
  1566. }
  1567. $("#play").attr("disabled", false);
  1568. $("#stop").attr("disabled", true);
  1569. $("#previewPlayer").hide();
  1570. }, song.duration * 1000);
  1571. },
  1572. "click #stop": function() {
  1573. $("#play").attr("disabled", false);
  1574. $("#stop").attr("disabled", true);
  1575. if (previewEndSongTimeout !== undefined) {
  1576. Meteor.clearTimeout(previewEndSongTimeout);
  1577. }
  1578. if (yt_player !== undefined) {
  1579. yt_player.stopVideo();
  1580. }
  1581. if (_sound !== undefined) {
  1582. _sound.stop();
  1583. }
  1584. },
  1585. "click #forward": function() {
  1586. var error = false;
  1587. if (yt_player !== undefined) {
  1588. var duration = Number(Session.get("song").duration) | 0;
  1589. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1590. if (yt_player.getDuration() < duration + skipDuration) {
  1591. alert("The duration of the YouTube video is smaller than the duration.");
  1592. error = true;
  1593. } else {
  1594. yt_player.seekTo(skipDuration + duration - 10);
  1595. }
  1596. }
  1597. if (_sound !== undefined) {
  1598. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1599. }
  1600. if (!error) {
  1601. if (previewEndSongTimeout !== undefined) {
  1602. Meteor.clearTimeout(previewEndSongTimeout);
  1603. }
  1604. previewEndSongTimeout = Meteor.setTimeout(function() {
  1605. if (yt_player !== undefined) {
  1606. yt_player.stopVideo();
  1607. }
  1608. if (_sound !== undefined) {
  1609. _sound.stop();
  1610. }
  1611. $("#play").attr("disabled", false);
  1612. $("#stop").attr("disabled", true);
  1613. $("#previewPlayer").hide();
  1614. }, 10000);
  1615. }
  1616. },
  1617. "click #get-spotify-info": function() {
  1618. var search = $("#title").val();
  1619. var artistName = $("#artist").val();
  1620. getSpotifyInfo(search, function(data) {
  1621. for(var i in data){
  1622. for(var j in data[i].items){
  1623. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1624. $("#img").val(data[i].items[j].album.images[1].url);
  1625. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1626. return;
  1627. }
  1628. }
  1629. }
  1630. }, artistName);
  1631. },
  1632. "click #save-song-button": function() {
  1633. var newSong = {};
  1634. newSong.id = $("#id").val();
  1635. newSong.likes = Number($("#likes").val());
  1636. newSong.dislikes = Number($("#dislikes").val());
  1637. newSong.title = $("#title").val();
  1638. newSong.artist = $("#artist").val();
  1639. newSong.img = $("#img").val();
  1640. newSong.type = $("#type").val();
  1641. newSong.duration = Number($("#duration").val());
  1642. newSong.skipDuration = $("#skip-duration").val();
  1643. if(newSong.skipDuration === undefined){
  1644. newSong.skipDuration = 0;
  1645. };
  1646. if (Session.get("type") === "playlist") {
  1647. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1648. $('#editModal').modal('hide');
  1649. });
  1650. } else {
  1651. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1652. $('#editModal').modal('hide');
  1653. });
  1654. }
  1655. }
  1656. });
  1657. Template.stations.onCreated(function() {
  1658. var tag = document.createElement("script");
  1659. tag.src = "https://www.youtube.com/iframe_api";
  1660. var firstScriptTag = document.getElementsByTagName('script')[0];
  1661. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1662. yt_player = undefined;
  1663. _sound = undefined;
  1664. });
  1665. Template.queues.onCreated(function() {
  1666. var tag = document.createElement("script");
  1667. tag.src = "https://www.youtube.com/iframe_api";
  1668. var firstScriptTag = document.getElementsByTagName('script')[0];
  1669. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1670. yt_player = undefined;
  1671. _sound = undefined;
  1672. });
  1673. Template.stations.onRendered(function() {
  1674. $("#previewModal").on("hidden.bs.modal", function() {
  1675. if (previewEndSongTimeout !== undefined) {
  1676. Meteor.clearTimeout(previewEndSongTimeout);
  1677. }
  1678. $("#play").attr("disabled", false);
  1679. $("#stop").attr("disabled", true);
  1680. if (yt_player !== undefined) {
  1681. $("#previewPlayer").hide();
  1682. yt_player.seekTo(0);
  1683. yt_player.stopVideo();
  1684. }
  1685. if (_sound !== undefined) {
  1686. _sound.stop();
  1687. }
  1688. });
  1689. $(document).ready(function() {
  1690. function makeSlider(){
  1691. var slider = $("#volume-slider").slider();
  1692. var volume = localStorage.getItem("volume") || 20;
  1693. $("#volume-slider").slider("setValue", volume);
  1694. if (slider.length === 0) {
  1695. Meteor.setTimeout(function() {
  1696. makeSlider();
  1697. }, 500);
  1698. } else {
  1699. slider.on("slide", function(val) {
  1700. localStorage.setItem("volume", val.value);
  1701. if (yt_player !== undefined) {
  1702. yt_player.setVolume(val.value);
  1703. } else if (_sound !== undefined) {
  1704. var volume = val.value / 100;
  1705. _sound.setVolume(volume);
  1706. }
  1707. });
  1708. }
  1709. }
  1710. makeSlider();
  1711. });
  1712. });
  1713. Template.queues.onRendered(function() {
  1714. $("#previewModal").on("hidden.bs.modal", function() {
  1715. if (previewEndSongTimeout !== undefined) {
  1716. Meteor.clearTimeout(previewEndSongTimeout);
  1717. }
  1718. $("#play").attr("disabled", false);
  1719. $("#stop").attr("disabled", true);
  1720. if (yt_player !== undefined) {
  1721. $("#previewPlayer").hide();
  1722. yt_player.seekTo(0);
  1723. yt_player.stopVideo();
  1724. }
  1725. if (_sound !== undefined) {
  1726. _sound.stop();
  1727. }
  1728. });
  1729. $(document).ready(function() {
  1730. function makeSlider(){
  1731. var slider = $("#volume-slider").slider();
  1732. var volume = localStorage.getItem("volume") || 20;
  1733. $("#volume-slider").slider("setValue", volume);
  1734. if (slider.length === 0) {
  1735. Meteor.setTimeout(function() {
  1736. makeSlider();
  1737. }, 500);
  1738. } else {
  1739. slider.on("slide", function(val) {
  1740. localStorage.setItem("volume", val.value);
  1741. if (yt_player !== undefined) {
  1742. yt_player.setVolume(val.value);
  1743. } else if (_sound !== undefined) {
  1744. var volume = val.value / 100;
  1745. _sound.setVolume(volume);
  1746. }
  1747. });
  1748. }
  1749. }
  1750. makeSlider();
  1751. });
  1752. });
  1753. Template.playlist.helpers({
  1754. playlist_songs: function() {
  1755. parts = location.href.split('/');
  1756. id = parts.pop();
  1757. type = id.toLowerCase();
  1758. var data = Playlists.findOne({type: type});
  1759. if (data !== undefined) {
  1760. data.songs.map(function(song) {
  1761. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  1762. song.current = true;
  1763. } else {
  1764. song.current = false;
  1765. }
  1766. return song;
  1767. });
  1768. return data.songs;
  1769. } else {
  1770. return [];
  1771. }
  1772. }
  1773. });
  1774. Template.playlist.events({
  1775. "keyup #search-playlist": function(){
  1776. if($("#search-playlist").val().length === 0){
  1777. $(".pl-item").show();
  1778. } else {
  1779. $(".pl-item").hide();
  1780. var input = $("#search-playlist").val().toLowerCase();
  1781. $(".pl-item strong").each(function(i, el){
  1782. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1783. $(el).parent(".pl-item").show();
  1784. }
  1785. })
  1786. $(".pl-item #pl-artist").each(function(i, el){
  1787. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1788. $(el).parent(".pl-item").show();
  1789. }
  1790. })
  1791. }
  1792. },
  1793. "click #pl-item": function(){
  1794. console.log($(this).text());
  1795. }
  1796. });
  1797. Meteor.subscribe("rooms");
  1798. Template.room.onCreated(function () {
  1799. Chat.after.find(function(userId, selector) {
  1800. if (selector.type === "global") {
  1801. if (!$("#global-chat-tab").hasClass("active")) {
  1802. $("#global-chat-tab").addClass("unread-messages");
  1803. }
  1804. } else if(selector.type === Session.get("type")) {
  1805. if (!$("#chat-tab").hasClass("active")) {
  1806. $("#chat-tab").addClass("unread-messages");
  1807. }
  1808. }
  1809. });
  1810. Session.set("reportSong", false);
  1811. Session.set("reportTitle", false);
  1812. Session.set("reportAuthor", false);
  1813. Session.set("reportDuration", false);
  1814. Session.set("reportAudio", false);
  1815. Session.set("reportAlbumart", false);
  1816. Session.set("reportOther", false);
  1817. if (resizeSeekerbarInterval !== undefined) {
  1818. Meteor.clearInterval(resizeSeekerbarInterval);
  1819. resizeSeekerbarInterval = undefined;
  1820. }
  1821. yt_player = undefined;
  1822. _sound = undefined;
  1823. Session.set("videoHidden", false);
  1824. var tag = document.createElement("script");
  1825. tag.src = "https://www.youtube.com/iframe_api";
  1826. var firstScriptTag = document.getElementsByTagName('script')[0];
  1827. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1828. Session.set("singleVideo", true);
  1829. var currentSong = undefined;
  1830. var currentSongR = undefined;
  1831. function getTimeElapsed() {
  1832. if (currentSong !== undefined) {
  1833. var room = Rooms.findOne({type: type});
  1834. if (room !== undefined) {
  1835. return Date.now() - currentSong.started - room.timePaused;
  1836. }
  1837. }
  1838. return 0;
  1839. }
  1840. function getSongInfo(songData){
  1841. Session.set("title", songData.title);
  1842. Session.set("artist", songData.artist);
  1843. Session.set("id", songData.id);
  1844. $("#song-img").attr("src", songData.img);
  1845. Session.set("duration", parseInt(songData.duration));
  1846. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1847. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1848. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1849. }
  1850. function resizeSeekerbar() {
  1851. if (Session.get("state") === "playing") {
  1852. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1853. }
  1854. }
  1855. function startSong() {
  1856. $("#time-elapsed").text("0:00");
  1857. $("#vote-skip").attr("disabled", false);
  1858. if (currentSong !== undefined) {
  1859. if (_sound !== undefined) _sound.stop();
  1860. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  1861. var volume = localStorage.getItem("volume") || 20;
  1862. if (currentSong.type === "SoundCloud") {
  1863. if ($("#soundcloud-image").length !== 1) {
  1864. //$("#media-container").append('<img alt="Not loading" src="/soundcloud-image.png" class="embed-responsive-item" id="soundcloud-image" />');
  1865. $("#media-container").append('<h1 id="soundcloud-image">We have temporarily disabled the playing of SoundCloud songs. We are sorry for this inconvenience.</h1>');
  1866. }
  1867. if ($("#player").length === 1) {
  1868. $("#player").hide();
  1869. }
  1870. $("#soundcloud-image").show();
  1871. //getSongInfo(currentSong);
  1872. /*SC.stream("/tracks/" + currentSong.id, function(sound){
  1873. _sound = sound;
  1874. sound.setVolume(volume / 100);
  1875. sound.play();
  1876. var interval = setInterval(function() {
  1877. if (sound.getState() === "playing") {
  1878. sound.seek(getTimeElapsed());
  1879. window.clearInterval(interval);
  1880. }
  1881. }, 200);
  1882. Session.set("duration", parseInt(currentSong.duration));
  1883. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  1884. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1885. resizeSeekerbar();
  1886. });*/
  1887. } else {
  1888. if ($("#player").length !== 1) {
  1889. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1890. }
  1891. if ($("#soundcloud-image").length === 1) {
  1892. $("#soundcloud-image").hide();
  1893. }
  1894. $("#player").show();
  1895. function loadVideo() {
  1896. if (!Session.get("YTLoaded")) {
  1897. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  1898. loadVideo();
  1899. }, 500));
  1900. } else {
  1901. if (yt_player === undefined) {
  1902. yt_player = new YT.Player("player", {
  1903. height: 540,
  1904. width: 960,
  1905. videoId: currentSong.id,
  1906. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1907. events: {
  1908. 'onReady': function (event) {
  1909. if (currentSong.skipDuration === undefined) {
  1910. currentSong.skipDuration = 0;
  1911. }
  1912. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1913. event.target.playVideo();
  1914. event.target.setVolume(volume);
  1915. resizeSeekerbar();
  1916. },
  1917. 'onStateChange': function (event) {
  1918. if (Session.get("YTLoaded")) {
  1919. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1920. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1921. event.target.playVideo();
  1922. }
  1923. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1924. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1925. event.target.pauseVideo();
  1926. }
  1927. }
  1928. }
  1929. }
  1930. });
  1931. } else {
  1932. yt_player.loadVideoById(currentSong.id);
  1933. if (currentSong.skipDuration === undefined) {
  1934. currentSong.skipDuration = 0;
  1935. }
  1936. yt_player.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1937. }
  1938. Session.set("pauseVideo", false);
  1939. getSongInfo(currentSong);
  1940. }
  1941. }
  1942. loadVideo();
  1943. }
  1944. }
  1945. }
  1946. Session.set("loaded", false);
  1947. Meteor.subscribe("rooms", function() {
  1948. var parts = location.href.split('/');
  1949. var id = parts.pop();
  1950. var type = id.toLowerCase();
  1951. Session.set("type", type);
  1952. if (Rooms.find({type: type}).count() !== 1) {
  1953. window.location = "/";
  1954. } else {
  1955. station_c = Meteor.subscribe(type);
  1956. Session.set("loaded", true);
  1957. minterval = Meteor.setInterval(function () {
  1958. var room = Rooms.findOne({type: type});
  1959. if (room !== undefined) {
  1960. if (room.state === "paused" || Session.get("pauseVideo")) {
  1961. Session.set("state", "paused");
  1962. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  1963. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() === 1) {
  1964. yt_player.pauseVideo();
  1965. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  1966. _sound.pause();
  1967. }
  1968. } else {
  1969. Session.set("state", "playing");
  1970. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() !== 1) {
  1971. yt_player.playVideo();
  1972. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  1973. _sound.play();
  1974. }
  1975. }
  1976. }
  1977. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  1978. Session.set("previousSong", currentSong);
  1979. currentSongR = room.currentSong;
  1980. currentSong = room.currentSong.song;
  1981. currentSong.started = room.currentSong.started;
  1982. Session.set("currentSong", currentSong);
  1983. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  1984. startSong();
  1985. }
  1986. if (currentSong !== undefined) {
  1987. if (room !== undefined) {
  1988. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1989. var song_duration = currentSong.duration;
  1990. if (song_duration <= duration) {
  1991. Session.set("pauseVideo", true);
  1992. }
  1993. var d = moment.duration(duration, 'seconds');
  1994. if (Session.get("state") === "playing") {
  1995. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1996. }
  1997. }
  1998. }
  1999. }, 100);
  2000. resizeSeekerbarInterval = Meteor.setInterval(function () {
  2001. resizeSeekerbar();
  2002. }, 500);
  2003. }
  2004. });
  2005. });