client.js 79 KB

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