2
0

client.js 84 KB

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