client.js 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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", 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", 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. $("#add-youtube-playlist").addClass("hidden-2");
  849. $("#import-progress").attr("aria-valuenow", 0);
  850. $("#import-progress").css({width: "0%"});
  851. $("#import-progress").text("0%");
  852. function makeAPICall(playlist_id, nextPageToken){
  853. if (nextPageToken !== undefined) {
  854. nextPageToken = "&pageToken=" + nextPageToken;
  855. } else {
  856. nextPageToken = "";
  857. }
  858. $.ajax({
  859. type: "GET",
  860. url: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlist_id + nextPageToken + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  861. applicationType: "application/json",
  862. contentType: "json",
  863. success: function(data){
  864. if (!ranOnce) {
  865. ranOnce = true;
  866. totalVideos = data.pageInfo.totalResults;
  867. }
  868. var nextToken = data.nextPageToken;
  869. for(var i in data.items){
  870. var item = data.items[i];
  871. if (item.snippet.thumbnails !== undefined) {
  872. var genre = Session.get("type");
  873. if (Playlists.find({type: genre, "songs.id": item.snippet.resourceId.videoId}, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  874. videosInPlaylist++;
  875. } else if (Queues.find({type: genre, "songs.id": item.snippet.resourceId.videoId}, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  876. videosInQueue++;
  877. } else {
  878. $("#playlist-import-queue").append(
  879. "<div class='youtube-import-queue-item'>" +
  880. "<img src='" + item.snippet.thumbnails.medium.url + "' class='song-result-thumbnail'/>" +
  881. "<div>" +
  882. "<span class='song-result-title'>" + item.snippet.title + "</span>" +
  883. "<span class='song-result-channel'>" + item.snippet.channelTitle + "</span>" +
  884. "</div>" +
  885. "<i class='fa fa-times remove-import-song'></i>" +
  886. "</div>"
  887. );
  888. var percentage = ytImportQueue.length / (totalVideos - videosInvalid) * 100;
  889. $("#import-progress").attr("aria-valuenow", percentage.toFixed(2));
  890. $("#import-progress").css({width: percentage + "%"});
  891. $("#import-progress").text(percentage.toFixed(1) + "%");
  892. ytImportQueue.push({title: item.snippet.title, id: item.snippet.resourceId.videoId});
  893. }
  894. } else {
  895. videosInvalid++;
  896. }
  897. }
  898. if (nextToken !== undefined) {
  899. makeAPICall(playlist_id, nextToken);
  900. } else {
  901. $("#playlist-import-queue > div > i").click(function(){
  902. var title = $(this).parent().find("div > .song-result-title").text();
  903. for(var i in ytImportQueue){
  904. if(ytImportQueue[i].title === title){
  905. ytImportQueue.splice(i, 1);
  906. }
  907. }
  908. $(this).parent().remove();
  909. });
  910. Session.set("importingPlaylist", false);
  911. $("#import-progress").attr("aria-valuenow", 100);
  912. $("#import-progress").css({width: "100%"});
  913. $("#import-progress").text("100%");
  914. $("#youtube-playlist-button").removeAttr("disabled");
  915. $("#youtube-playlist-button").removeClass("disabled");
  916. $("#youtube-playlist-input").removeAttr("disabled");
  917. $("#youtube-playlist-input").removeClass("disabled");
  918. $("#playlist-import-queue").show();
  919. $("#add-youtube-playlist").removeClass("hidden-2");
  920. Session.set("YTImportQueue", ytImportQueue);
  921. }
  922. }
  923. })
  924. }
  925. makeAPICall(playlist_id);
  926. }
  927. },
  928. "click #add-youtube-playlist": function() {
  929. var YTImportQueue = Session.get("YTImportQueue");
  930. $("#youtube-playlist-button").attr("disabled", "");
  931. $("#youtube-playlist-button").addClass("disabled");
  932. $("#youtube-playlist-input").attr("disabled", "");
  933. $("#youtube-playlist-input").addClass("disabled");
  934. $("#import-progress").attr("aria-valuenow", 0);
  935. $("#import-progress").css({width: "0%"});
  936. $("#import-progress").text("0%");
  937. var failed = 0;
  938. var success = 0;
  939. var processed = 0;
  940. var total = YTImportQueue.length;
  941. YTImportQueue.forEach(function(song) {
  942. var songData = {type: "YouTube", id: song.id, title: song.title, artist: "", img: ""};
  943. Meteor.call("addSongToQueue", Session.get("type"), songData, function(err, res) {
  944. if (err) {
  945. failed++;
  946. } else {
  947. success++;
  948. }
  949. processed++;
  950. var percentage = processed / total * 100;
  951. $("#import-progress").attr("aria-valuenow", percentage.toFixed(2));
  952. $("#import-progress").css({width: percentage + "%"});
  953. $("#import-progress").text(percentage.toFixed(1) + "%");
  954. });
  955. });
  956. },
  957. "click #chat-tab": function() {
  958. $("#chat-tab").removeClass("unread-messages");
  959. },
  960. "click #global-chat-tab": function() {
  961. $("#global-chat-tab").removeClass("unread-messages");
  962. },
  963. "click #sync": function() {
  964. if (Session.get("currentSong") !== undefined) {
  965. var room = Rooms.findOne({type: Session.get("type")});
  966. if (room !== undefined) {
  967. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  968. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  969. if (YTPlayer !== undefined) {
  970. YTPlayer.seekTo(skipDuration + timeIn / 1000);
  971. }
  972. else if (SCPlayer !== undefined) {
  973. SCPlayer.seekTo(skipDuration * 1000 + timeIn);
  974. }
  975. }
  976. }
  977. },
  978. "click #lock": function() {
  979. Meteor.call("lockRoom", Session.get("type"));
  980. },
  981. "click #unlock": function() {
  982. Meteor.call("unlockRoom", Session.get("type"));
  983. },
  984. "click #chat-tab": function(e) {
  985. Meteor.setTimeout(function() {
  986. $("#chat-ul").scrollTop(100000);
  987. }, 1);
  988. },
  989. "click #global-chat-tab": function(e) {
  990. Meteor.setTimeout(function() {
  991. $("#global-chat-ul").scrollTop(100000);
  992. }, 1);
  993. },
  994. "click #submit": function() {
  995. sendMessage();
  996. Meteor.setTimeout(function(){
  997. $("#chat-ul").scrollTop(100000);
  998. }, 1000)
  999. },
  1000. "click #global-submit": function() {
  1001. sendMessageGlobal();
  1002. Meteor.setTimeout(function(){
  1003. $("#global-chat-ul").scrollTop(100000);
  1004. }, 1000)
  1005. },
  1006. "keyup #chat-input": function(e) {
  1007. if (e.type === "keyup" && e.which === 13) {
  1008. e.preventDefault();
  1009. if (!$('#chat-input').data('dropdownshown')) {
  1010. sendMessage();
  1011. Meteor.setTimeout(function(){
  1012. $("#chat-ul").scrollTop(100000);
  1013. }, 1000)
  1014. }
  1015. }
  1016. },
  1017. "keyup #global-chat-input": function(e) {
  1018. if (e.type === "keyup" && e.which === 13) {
  1019. e.preventDefault();
  1020. if (!$('#global-chat-input').data('dropdownshown')) {
  1021. sendMessageGlobal();
  1022. Meteor.setTimeout(function(){
  1023. $("#global-chat-ul").scrollTop(100000);
  1024. }, 1000)
  1025. }
  1026. }
  1027. },
  1028. "click #like": function(e) {
  1029. $("#like").blur();
  1030. Meteor.call("likeSong", Session.get("currentSong").mid);
  1031. },
  1032. "click #dislike": function(e) {
  1033. $("#dislike").blur();
  1034. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  1035. },
  1036. "click #vote-skip": function(){
  1037. Meteor.call("voteSkip", type, function(err, res) {
  1038. $("#vote-skip").attr("disabled", true);
  1039. });
  1040. },
  1041. "click #report-prev": function(e) {
  1042. if (Session.get("previousSong") !== undefined) {
  1043. Session.set("reportPrevious", true);
  1044. $("#report-prev").prop("disabled", true);
  1045. $("#report-curr").prop("disabled", false);
  1046. }
  1047. },
  1048. "click #report-curr": function(e) {
  1049. Session.set("reportPrevious", false);
  1050. $("#report-prev").prop("disabled", false);
  1051. $("#report-curr").prop("disabled", true);
  1052. },
  1053. "click #report-modal": function() {
  1054. Session.set("currentSongR", Session.get("currentSong"));
  1055. Session.set("previousSongR", Session.get("previousSong"));
  1056. },
  1057. "click #add-song-button": function(e){
  1058. e.preventDefault();
  1059. parts = location.href.split('/');
  1060. var roomType = parts.pop();
  1061. var genre = roomType.toLowerCase();
  1062. var type = $("#type").val();
  1063. id = $("#id").val();
  1064. var title = $("#title").val();
  1065. var artist = $("#artist").val();
  1066. var img = $("#img").val();
  1067. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  1068. if(Playlists.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  1069. $("<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(); });
  1070. } else if(Queues.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  1071. $("<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(); });
  1072. } else{
  1073. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  1074. console.log(err, res);
  1075. if (err) {
  1076. $("<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(); });
  1077. } else {
  1078. $("<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(); });
  1079. }
  1080. });
  1081. }
  1082. $("#close-modal-a").click();
  1083. },
  1084. "click #toggle-video": function(e){
  1085. e.preventDefault();
  1086. if (Session.get("mediaHidden")) {
  1087. $("#media-container").removeClass("hidden");
  1088. $("#toggle-video").text("Hide video");
  1089. Session.set("mediaHidden", false);
  1090. } else {
  1091. $("#media-container").addClass("hidden");
  1092. $("#toggle-video").text("Show video");
  1093. Session.set("mediaHidden", true);
  1094. }
  1095. },
  1096. "click #return": function(e){
  1097. $("#add-info").hide();
  1098. $("#search-info").show();
  1099. },
  1100. "click #search-song": function(){
  1101. var songs = [];
  1102. $("#song-results").empty();
  1103. var search_type = $("#search_type").val();
  1104. if (search_type === "YouTube") {
  1105. $.ajax({
  1106. type: "GET",
  1107. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  1108. applicationType: "application/json",
  1109. contentType: "json",
  1110. success: function(data){
  1111. for(var i in data.items){
  1112. var item = data.items[i];
  1113. $("#song-results").append(
  1114. "<div>" +
  1115. "<img src='" + item.snippet.thumbnails.medium.url + "' class='song-result-thumbnail'/>" +
  1116. "<div>" +
  1117. "<span class='song-result-title'>" + item.snippet.title + "</span>" +
  1118. "<span class='song-result-channel'>" + item.snippet.channelTitle + "</span>" +
  1119. "</div>" +
  1120. "</div>"
  1121. );
  1122. songs.push({title: item.snippet.title, id: item.id.videoId});
  1123. }
  1124. $("#song-results > div").click(function(){
  1125. $("#search-info").hide();
  1126. $("#add-info").show();
  1127. var title = $(this).find("div > .song-result-title").text();
  1128. for(var i in songs){
  1129. if(songs[i].title === title){
  1130. var songObj = {
  1131. id: songs[i].id,
  1132. title: songs[i].title,
  1133. type: "youtube"
  1134. };
  1135. $("#title").val(songObj.title);
  1136. $("#artist").val("");
  1137. $("#id").val(songObj.id);
  1138. $("#type").val("YouTube");
  1139. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  1140. if (data.tracks.items.length > 0) {
  1141. $("#title").val(data.tracks.items[0].name);
  1142. var artists = [];
  1143. $("#img").val(data.tracks.items[0].album.images[1].url);
  1144. data.tracks.items[0].artists.forEach(function(artist) {
  1145. artists.push(artist.name);
  1146. });
  1147. $("#artist").val(artists.join(", "));
  1148. }
  1149. });
  1150. }
  1151. }
  1152. })
  1153. }
  1154. })
  1155. } else if (search_type === "SoundCloud") {
  1156. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  1157. for(var i in tracks){
  1158. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  1159. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  1160. }
  1161. $("#song-results p").click(function(){
  1162. $("#search-info").hide();
  1163. $("#add-info").show();
  1164. var title = $(this).text();
  1165. for(var i in songsArr){
  1166. if(songsArr[i].title === title){
  1167. var id = songsArr[i].id;
  1168. var duration = songsArr[i].duration;
  1169. var songObj = {
  1170. title: songsArr[i].title,
  1171. id: id,
  1172. duration: duration,
  1173. type: "soundcloud"
  1174. }
  1175. $("#title").val(songObj.title);
  1176. // Set ID field
  1177. $("#id").val(songObj.id);
  1178. $("#type").val("SoundCloud");
  1179. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  1180. if (data.tracks.items.length > 0) {
  1181. $("#title").val(data.tracks.items[0].name);
  1182. var artists = [];
  1183. data.tracks.items[0].artists.forEach(function(artist) {
  1184. artists.push(artist.name);
  1185. });
  1186. $("#artist").val(artists.join(", "));
  1187. }
  1188. // Set title field again if possible
  1189. // Set artist if possible
  1190. });
  1191. }
  1192. }
  1193. })
  1194. });
  1195. }
  1196. },
  1197. "click #volume-icon": function(){
  1198. var volume = 0;
  1199. var slider = $("#volume-slider").slider();
  1200. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1201. if (YTPlayer !== undefined) {
  1202. YTPlayer.setVolume(volume);
  1203. localStorage.setItem("volume", volume);
  1204. $("#volume-slider").slider("setValue", volume);
  1205. } else if (SCPlayer !== undefined) {
  1206. SCPlayer.setVolume(volume);
  1207. localStorage.setItem("volume", volume);
  1208. $("#volume-slider").slider("setValue", volume);
  1209. }
  1210. },
  1211. "click #play": function() {
  1212. Meteor.call("resumeRoom", type);
  1213. },
  1214. "click #pause": function() {
  1215. Meteor.call("pauseRoom", type);
  1216. },
  1217. "click #skip": function() {
  1218. Meteor.call("skipSong", type);
  1219. },
  1220. "click #shuffle": function() {
  1221. Meteor.call("shufflePlaylist", type);
  1222. },
  1223. "change input": function(e) {
  1224. if (e.target && e.target.id) {
  1225. var partsOfId = e.target.id.split("-");
  1226. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  1227. var camelCase = partsOfId.join("");
  1228. Session.set(camelCase, e.target.checked);
  1229. }
  1230. },
  1231. "click #report-song-button": function() {
  1232. var room = Session.get("type");
  1233. var reportData = {};
  1234. reportData.song = Session.get("currentSong").mid;
  1235. reportData.type = [];
  1236. reportData.reason = [];
  1237. $(".report-layer-1 > .checkbox input:checked").each(function(){
  1238. reportData.type.push(this.id);
  1239. if (this.id == "report-other") {
  1240. var otherText = $(".other-textarea").val();
  1241. }
  1242. });
  1243. $(".report-layer-2 input:checked").each(function(){
  1244. reportData.reason.push(this.id);
  1245. });
  1246. console.log(reportData);
  1247. Meteor.call("submitReport", room, reportData, Session.get("id"), function() {
  1248. $("#close-modal-r").click();
  1249. });
  1250. },
  1251. "change #si_or_pl": function(){
  1252. if($("#select_playlist").is(':selected')){
  1253. $("#search-info").hide();
  1254. $("#playlist-import").show();
  1255. }
  1256. if($("#select_single").is(':selected')){
  1257. $("#search-info").show();
  1258. $("#playlist-import").hide();
  1259. }
  1260. },
  1261. "click #close-modal-a": function(){
  1262. $("#select_single").attr("selected", true);
  1263. $("#search-info").show();
  1264. $("#playlist-import").hide();
  1265. }
  1266. });
  1267. Template.room.onRendered(function() {
  1268. if (rTimeInterval !== undefined) {
  1269. Meteor.clearInterval(rTimeInterval)
  1270. }
  1271. rTimeInterval = Meteor.setInterval(function() {
  1272. Session.set("time", new Date().getTime());
  1273. }, 10000);
  1274. $(document).ready(function() {
  1275. function makeSlider(){
  1276. var slider = $("#volume-slider").slider();
  1277. var volume = Number(localStorage.getItem("volume"));
  1278. $("#volume-slider").slider("setValue", volume);
  1279. if (slider.length === 0) {
  1280. Meteor.setTimeout(function() {
  1281. makeSlider();
  1282. }, 500);
  1283. } else {
  1284. if (volume === 0) {
  1285. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1286. } else {
  1287. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  1288. }
  1289. slider.on("slide", function(val) {
  1290. if (val.value === 0) {
  1291. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1292. } else {
  1293. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  1294. }
  1295. if (YTPlayer !== undefined) {
  1296. YTPlayer.setVolume(val.value);
  1297. localStorage.setItem("volume", val.value);
  1298. } else if (SCPlayer !== undefined) {
  1299. //SCPlayer
  1300. var volume = val.value / 100;
  1301. SCPlayer.setVolume(volume);
  1302. localStorage.setItem("volume", val.value);
  1303. }
  1304. });
  1305. }
  1306. }
  1307. makeSlider();
  1308. });
  1309. });
  1310. Template.room.helpers({
  1311. singleVideo: function() {
  1312. return true;
  1313. },
  1314. chat: function() {
  1315. Meteor.setTimeout(function() {
  1316. var elem = document.getElementById('chat');
  1317. if (elem !== undefined && elem !== null) {
  1318. elem.scrollTop = elem.scrollHeight;
  1319. }
  1320. }, 100);
  1321. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1322. },
  1323. globalChat: function() {
  1324. Meteor.setTimeout(function() {
  1325. var elem = document.getElementById('global-chat');
  1326. if (elem !== undefined && elem !== null) {
  1327. elem.scrollTop = elem.scrollHeight;
  1328. }
  1329. }, 100);
  1330. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1331. },
  1332. likes: function() {
  1333. var playlist = Playlists.findOne({type: Session.get("type")});
  1334. var likes = 0;
  1335. playlist.songs.forEach(function(song) {
  1336. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1337. likes = song.likes;
  1338. return;
  1339. }
  1340. });
  1341. return likes;
  1342. },
  1343. dislikes: function() {
  1344. var playlist = Playlists.findOne({type: Session.get("type")});
  1345. var dislikes = 0;
  1346. playlist.songs.forEach(function(song) {
  1347. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1348. dislikes = song.dislikes;
  1349. return;
  1350. }
  1351. });
  1352. return dislikes;
  1353. },
  1354. liked: function() {
  1355. if (Meteor.userId()) {
  1356. var currentSong = Session.get("currentSong");
  1357. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  1358. return "active";
  1359. } else {
  1360. return "";
  1361. }
  1362. } else {
  1363. "";
  1364. }
  1365. },
  1366. disliked: function() {
  1367. if (Meteor.userId()) {
  1368. var currentSong = Session.get("currentSong");
  1369. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  1370. return "active";
  1371. } else {
  1372. return "";
  1373. }
  1374. } else {
  1375. "";
  1376. }
  1377. },
  1378. type: function() {
  1379. var parts = location.href.split('/');
  1380. var id = parts.pop().toLowerCase();
  1381. return Rooms.findOne({type: id}).display;
  1382. },
  1383. users: function() {
  1384. var parts = location.href.split('/');
  1385. var id = parts.pop().toLowerCase();
  1386. return Rooms.findOne({type: id}).users;
  1387. },
  1388. title: function(){
  1389. return Session.get("title");
  1390. },
  1391. artist: function(){
  1392. return Session.get("artist");
  1393. },
  1394. loaded: function() {
  1395. return Session.get("loaded");
  1396. },
  1397. paused: function() {
  1398. return Session.get("state") === "paused";
  1399. },
  1400. private: function() {
  1401. return Rooms.findOne({type: Session.get("type")}).private === true;
  1402. },
  1403. report: function() {
  1404. return Session.get("reportObj");
  1405. },
  1406. reportSong: function() {
  1407. return Session.get("reportSong");
  1408. },
  1409. reportTitle: function() {
  1410. return Session.get("reportTitle");
  1411. },
  1412. reportAuthor: function() {
  1413. return Session.get("reportAuthor");
  1414. },
  1415. reportDuration: function() {
  1416. return Session.get("reportDuration");
  1417. },
  1418. reportAudio: function() {
  1419. return Session.get("reportAudio");
  1420. },
  1421. reportAlbumart: function() {
  1422. return Session.get("reportAlbumart");
  1423. },
  1424. reportOther: function() {
  1425. return Session.get("reportOther");
  1426. },
  1427. currentSong: function() {
  1428. return Session.get("currentSong");
  1429. },
  1430. previousSong: function() {
  1431. return Session.get("previousSong");
  1432. },
  1433. currentSongR: function() {
  1434. return Session.get("currentSongR");
  1435. },
  1436. previousSongR: function() {
  1437. return Session.get("previousSongR");
  1438. },
  1439. reportingSong: function() {
  1440. if (Session.get("reportPrevious")) {
  1441. return Session.get("previousSongR");
  1442. } else {
  1443. return Session.get("currentSongR");
  1444. }
  1445. },
  1446. votes: function(){
  1447. return Rooms.findOne({type: Session.get("type")}).votes;
  1448. }
  1449. });
  1450. Template.room.onCreated(function () {
  1451. Chat.after.find(function(userId, selector) {
  1452. if (selector.type === "global") {
  1453. if (!$("#global-chat-tab").hasClass("active")) {
  1454. $("#global-chat-tab").addClass("unread-messages");
  1455. }
  1456. } else if(selector.type === Session.get("type")) {
  1457. if (!$("#chat-tab").hasClass("active")) {
  1458. $("#chat-tab").addClass("unread-messages");
  1459. }
  1460. }
  1461. });
  1462. Session.set("reportSong", false);
  1463. Session.set("reportTitle", false);
  1464. Session.set("reportAuthor", false);
  1465. Session.set("reportDuration", false);
  1466. Session.set("reportAudio", false);
  1467. Session.set("reportAlbumart", false);
  1468. Session.set("reportOther", false);
  1469. if (resizeSeekerbarInterval !== undefined) {
  1470. Meteor.clearInterval(resizeSeekerbarInterval);
  1471. resizeSeekerbarInterval = undefined;
  1472. }
  1473. YTPlayer = undefined;
  1474. SCPlayer = undefined;
  1475. Session.set("videoHidden", false);
  1476. var tag = document.createElement("script");
  1477. tag.src = "https://www.youtube.com/iframe_api";
  1478. var firstScriptTag = document.getElementsByTagName('script')[0];
  1479. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1480. Session.set("singleVideo", true);
  1481. var currentSong = undefined;
  1482. var currentSongR = undefined;
  1483. function getTimeElapsed() {
  1484. if (currentSong !== undefined) {
  1485. var room = Rooms.findOne({type: type});
  1486. if (room !== undefined) {
  1487. return Date.now() - currentSong.started - room.timePaused;
  1488. }
  1489. }
  1490. return 0;
  1491. }
  1492. function getSongInfo(songData){
  1493. Session.set("title", songData.title);
  1494. Session.set("artist", songData.artist);
  1495. Session.set("id", songData.id);
  1496. $("#song-img").attr("src", songData.img);
  1497. Session.set("duration", parseInt(songData.duration));
  1498. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1499. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1500. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1501. }
  1502. function resizeSeekerbar() {
  1503. if (Session.get("state") === "playing") {
  1504. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1505. }
  1506. }
  1507. function startSong() {
  1508. $("#time-elapsed").text("0:00");
  1509. $("#vote-skip").attr("disabled", false);
  1510. if (currentSong !== undefined) {
  1511. if (SCPlayer !== undefined) SCPlayer.stop();
  1512. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  1513. var volume = localStorage.getItem("volume") || 20;
  1514. if (currentSong.type === "SoundCloud") {
  1515. if ($("#soundcloud-image").length !== 1) {
  1516. //$("#media-container").append('<img alt="Not loading" src="/soundcloud-image.png" class="embed-responsive-item" id="soundcloud-image" />');
  1517. $("#media-container").append('<h1 id="soundcloud-image">We have temporarily disabled the playing of SoundCloud songs. We are sorry for this inconvenience.</h1>');
  1518. }
  1519. if ($("#player").length === 1) {
  1520. $("#player").hide();
  1521. }
  1522. $("#soundcloud-image").show();
  1523. //getSongInfo(currentSong);
  1524. /*SC.stream("/tracks/" + currentSong.id, function(sound){
  1525. SCPlayer = sound;
  1526. sound.setVolume(volume / 100);
  1527. sound.play();
  1528. var interval = setInterval(function() {
  1529. if (sound.getState() === "playing") {
  1530. sound.seek(getTimeElapsed());
  1531. window.clearInterval(interval);
  1532. }
  1533. }, 200);
  1534. Session.set("duration", parseInt(currentSong.duration));
  1535. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  1536. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1537. resizeSeekerbar();
  1538. });*/
  1539. } else {
  1540. if ($("#player").length !== 1) {
  1541. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1542. }
  1543. if ($("#soundcloud-image").length === 1) {
  1544. $("#soundcloud-image").hide();
  1545. }
  1546. $("#player").show();
  1547. function loadVideo() {
  1548. if (!Session.get("YTLoaded")) {
  1549. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  1550. loadVideo();
  1551. }, 500));
  1552. } else {
  1553. if (YTPlayer === undefined) {
  1554. YTPlayer = new YT.Player("player", {
  1555. height: 540,
  1556. width: 960,
  1557. videoId: currentSong.id,
  1558. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1559. events: {
  1560. 'onReady': function (event) {
  1561. if (currentSong.skipDuration === undefined) {
  1562. currentSong.skipDuration = 0;
  1563. }
  1564. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1565. event.target.playVideo();
  1566. event.target.setVolume(volume);
  1567. resizeSeekerbar();
  1568. },
  1569. 'onStateChange': function (event) {
  1570. if (Session.get("YTLoaded")) {
  1571. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1572. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1573. event.target.playVideo();
  1574. }
  1575. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1576. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1577. event.target.pauseVideo();
  1578. }
  1579. }
  1580. }
  1581. }
  1582. });
  1583. } else {
  1584. YTPlayer.loadVideoById(currentSong.id);
  1585. if (currentSong.skipDuration === undefined) {
  1586. currentSong.skipDuration = 0;
  1587. }
  1588. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1589. }
  1590. Session.set("pauseVideo", false);
  1591. getSongInfo(currentSong);
  1592. }
  1593. }
  1594. loadVideo();
  1595. }
  1596. }
  1597. }
  1598. Session.set("loaded", false);
  1599. Meteor.subscribe("rooms", function() {
  1600. var parts = location.href.split('/');
  1601. var id = parts.pop();
  1602. var type = id.toLowerCase();
  1603. Session.set("type", type);
  1604. if (Rooms.find({type: type}).count() !== 1) {
  1605. window.location = "/";
  1606. } else {
  1607. StationSubscription = Meteor.subscribe(type);
  1608. Session.set("loaded", true);
  1609. minterval = Meteor.setInterval(function () {
  1610. var room = Rooms.findOne({type: type});
  1611. if (room !== undefined) {
  1612. if (room.state === "paused" || Session.get("pauseVideo")) {
  1613. Session.set("state", "paused");
  1614. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  1615. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  1616. YTPlayer.pauseVideo();
  1617. } else if (SCPlayer !== undefined && SCPlayer.getState().indexOf("playing") !== -1) {
  1618. SCPlayer.pause();
  1619. }
  1620. } else {
  1621. Session.set("state", "playing");
  1622. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  1623. YTPlayer.playVideo();
  1624. } else if (SCPlayer !== undefined && SCPlayer.getState().indexOf("paused") !== -1) {
  1625. SCPlayer.play();
  1626. }
  1627. }
  1628. }
  1629. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  1630. Session.set("previousSong", currentSong);
  1631. currentSongR = room.currentSong;
  1632. currentSong = room.currentSong.song;
  1633. currentSong.started = room.currentSong.started;
  1634. Session.set("currentSong", currentSong);
  1635. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  1636. startSong();
  1637. }
  1638. if (currentSong !== undefined) {
  1639. if (room !== undefined) {
  1640. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1641. var song_duration = currentSong.duration;
  1642. if (song_duration <= duration) {
  1643. Session.set("pauseVideo", true);
  1644. }
  1645. var d = moment.duration(duration, 'seconds');
  1646. if (Session.get("state") === "playing") {
  1647. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1648. }
  1649. }
  1650. }
  1651. }, 100);
  1652. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1653. resizeSeekerbar();
  1654. }, 500);
  1655. }
  1656. });
  1657. });
  1658. // Settings Template
  1659. Template.settings.events({
  1660. "click #save-settings": function() {
  1661. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  1662. },
  1663. "click #delete-account": function(){
  1664. $("#delete-account").text("Click to confirm");
  1665. $("#delete-account").click(function(){
  1666. var bool = confirm("Are you sure you want to delete your account?");
  1667. if(bool) {
  1668. Meteor.call("deleteAccount");
  1669. }
  1670. })
  1671. },
  1672. "click #change-password": function(){
  1673. var oldPassword = $("#old-password").val();
  1674. var newPassword= $("#new-password").val();
  1675. var confirmPassword = $("#confirm-password").val();
  1676. if(newPassword === confirmPassword){
  1677. Accounts.changePassword(oldPassword, newPassword, function(err){
  1678. if(err){
  1679. $("#old-password").val("");
  1680. $("#new-password").val("");
  1681. $("#confirm-password").val("");
  1682. $("<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(); });
  1683. } else {
  1684. $("#old-password").val("");
  1685. $("#new-password").val("");
  1686. $("#confirm-password").val("");
  1687. $("<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(); });
  1688. }
  1689. });
  1690. }
  1691. }
  1692. });
  1693. Template.settings.helpers({
  1694. username: function() {
  1695. if (Meteor.user() !== undefined) {
  1696. return Meteor.user().profile.username;
  1697. } else {
  1698. return "";
  1699. }
  1700. }
  1701. });
  1702. Template.settings.onCreated(function() {
  1703. $(document).ready(function() {
  1704. var user = Meteor.user();
  1705. function initSettings() {
  1706. if (user !== undefined) {
  1707. if (user.profile.settings && user.profile.settings.showRating === true) {
  1708. function setChecked() {
  1709. $("#showRating").prop("checked", true);
  1710. if (!$("#showRating").prop("checked")) {
  1711. Meteor.setTimeout(function() {
  1712. setChecked();
  1713. }, 100);
  1714. }
  1715. }
  1716. setChecked();
  1717. }
  1718. } else {
  1719. Meteor.setTimeout(function() {
  1720. initSettings();
  1721. }, 500);
  1722. }
  1723. }
  1724. initSettings();
  1725. });
  1726. });
  1727. // Stations Template
  1728. Template.stations.helpers({
  1729. playlist: function() {
  1730. var query = {type: Session.get("playlistToEdit").toLowerCase()};
  1731. var playlists = Playlists.find(query).fetch();
  1732. return playlists;
  1733. },
  1734. whichStation: function(){
  1735. return Session.get("playlistToEdit");
  1736. },
  1737. reports: function() {
  1738. var query = {room: Session.get("playlistToEdit").toLowerCase()};
  1739. var reports = Reports.find(query).fetch();
  1740. console.log(reports);
  1741. return reports;
  1742. }
  1743. });
  1744. Template.stations.events({
  1745. "click .preview-button": function(e){
  1746. Session.set("song", this);
  1747. },
  1748. "click #previewImageButton": function() {
  1749. $("#preview-image").attr("src", Session.get("song").img);
  1750. },
  1751. "click .edit-queue-button": function(e){
  1752. Session.set("song", this);
  1753. Session.set("genre", $(e.target).data("genre"));
  1754. Session.set("type", "queue");
  1755. $("#type").val(this.type);
  1756. $("#mid").val(this.mid);
  1757. $("#artist").val(this.artist);
  1758. $("#title").val(this.title);
  1759. $("#img").val(this.img);
  1760. $("#id").val(this.id);
  1761. $("#likes").val(this.likes);
  1762. $("#dislikes").val(this.dislikes);
  1763. $("#duration").val(this.duration);
  1764. $("#skip-duration").val(this.skipDuration);
  1765. },
  1766. "click .edit-playlist-button": function(e){
  1767. Session.set("song", this);
  1768. Session.set("genre", $(e.target).data("genre"));
  1769. Session.set("type", "playlist");
  1770. $("#type").val(this.type);
  1771. $("#mid").val(this.mid);
  1772. $("#artist").val(this.artist);
  1773. $("#title").val(this.title);
  1774. $("#img").val(this.img);
  1775. $("#id").val(this.id);
  1776. $("#likes").val(this.likes);
  1777. $("#dislikes").val(this.dislikes);
  1778. $("#duration").val(this.duration);
  1779. $("#skip-duration").val(this.skipDuration);
  1780. },
  1781. "click .add-song-button": function(e){
  1782. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1783. Meteor.call("addSongToPlaylist", genre, this);
  1784. },
  1785. "click .deny-song-button": function(e){
  1786. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1787. Meteor.call("removeSongFromQueue", genre, this.mid);
  1788. },
  1789. "click .remove-song-button": function(e){
  1790. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1791. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  1792. },
  1793. "click #moveSong": function(e){
  1794. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1795. if (genre !== Session.get(genre)) {
  1796. 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});
  1797. Meteor.call("removeSongFromPlaylist", Session.get("genre"), Session.get("song").mid);
  1798. }else {
  1799. console.log("Something Went Wrong?!");
  1800. return false;
  1801. }
  1802. },
  1803. "click #copySong": function(e){
  1804. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1805. 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});
  1806. },
  1807. "click .copyMove-button": function(e){
  1808. Session.set("song", this);
  1809. Session.set("genre", $(e.target).data("genre"));
  1810. },
  1811. "click #play": function() {
  1812. $("#play").attr("disabled", true);
  1813. $("#stop").attr("disabled", false);
  1814. var song = Session.get("song");
  1815. var id = song.id;
  1816. var type = song.type;
  1817. var volume = localStorage.getItem("volume") || 20;
  1818. if (type === "YouTube") {
  1819. if (YTPlayer === undefined) {
  1820. YTPlayer = new YT.Player("previewPlayer", {
  1821. height: 540,
  1822. width: 568,
  1823. videoId: id,
  1824. playerVars: {controls: 0, iv_load_policy: 3, showinfo: 0},
  1825. events: {
  1826. 'onReady': function(event) {
  1827. event.target.seekTo(Number(song.skipDuration));
  1828. event.target.playVideo();
  1829. event.target.setVolume(volume);
  1830. },
  1831. 'onStateChange': function(event){
  1832. if (event.data == YT.PlayerState.PAUSED) {
  1833. event.target.playVideo();
  1834. }
  1835. if (event.data == YT.PlayerState.PLAYING) {
  1836. $("#play").attr("disabled", true);
  1837. $("#stop").attr("disabled", false);
  1838. } else {
  1839. $("#play").attr("disabled", false);
  1840. $("#stop").attr("disabled", true);
  1841. }
  1842. }
  1843. }
  1844. });
  1845. } else {
  1846. YTPlayer.loadVideoById(id);
  1847. YTPlayer.seekTo(Number(song.skipDuration));
  1848. }
  1849. $("#previewPlayer").show();
  1850. } else if (type === "SoundCloud") {
  1851. SC.stream("/tracks/" + song.id, function(sound) {
  1852. SCPlayer = sound;
  1853. sound.setVolume(volume / 100);
  1854. sound.play();
  1855. });
  1856. }
  1857. if (previewEndSongTimeout !== undefined) {
  1858. Meteor.clearTimeout(previewEndSongTimeout);
  1859. }
  1860. previewEndSongTimeout = Meteor.setTimeout(function() {
  1861. if (YTPlayer !== undefined) {
  1862. YTPlayer.stopVideo();
  1863. }
  1864. if (SCPlayer !== undefined) {
  1865. SCPlayer.stop();
  1866. }
  1867. $("#play").attr("disabled", false);
  1868. $("#stop").attr("disabled", true);
  1869. $("#previewPlayer").hide();
  1870. }, song.duration * 1000);
  1871. },
  1872. "click #stop": function() {
  1873. $("#play").attr("disabled", false);
  1874. $("#stop").attr("disabled", true);
  1875. if (previewEndSongTimeout !== undefined) {
  1876. Meteor.clearTimeout(previewEndSongTimeout);
  1877. }
  1878. if (YTPlayer !== undefined) {
  1879. YTPlayer.stopVideo();
  1880. }
  1881. if (SCPlayer !== undefined) {
  1882. SCPlayer.stop();
  1883. }
  1884. },
  1885. "click #forward": function() {
  1886. var error = false;
  1887. if (YTPlayer !== undefined) {
  1888. var duration = Number(Session.get("song").duration) | 0;
  1889. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1890. if (YTPlayer.getDuration() < duration + skipDuration) {
  1891. alert("The duration of the YouTube video is smaller than the duration.");
  1892. error = true;
  1893. } else {
  1894. YTPlayer.seekTo(skipDuration + duration - 10);
  1895. }
  1896. }
  1897. if (SCPlayer !== undefined) {
  1898. SCPlayer.seekTo((skipDuration + duration - 10) * 1000);
  1899. }
  1900. if (!error) {
  1901. if (previewEndSongTimeout !== undefined) {
  1902. Meteor.clearTimeout(previewEndSongTimeout);
  1903. }
  1904. previewEndSongTimeout = Meteor.setTimeout(function() {
  1905. if (YTPlayer !== undefined) {
  1906. YTPlayer.stopVideo();
  1907. }
  1908. if (SCPlayer !== undefined) {
  1909. SCPlayer.stop();
  1910. }
  1911. $("#play").attr("disabled", false);
  1912. $("#stop").attr("disabled", true);
  1913. $("#previewPlayer").hide();
  1914. }, 10000);
  1915. }
  1916. },
  1917. "click #get-spotify-info": function() {
  1918. var search = $("#title").val();
  1919. var artistName = $("#artist").val();
  1920. getSpotifyInfo(search, function(data) {
  1921. for(var i in data){
  1922. for(var j in data[i].items){
  1923. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1924. $("#img").val(data[i].items[j].album.images[1].url);
  1925. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1926. return;
  1927. }
  1928. }
  1929. }
  1930. }, artistName);
  1931. },
  1932. "click #save-song-button": function() {
  1933. var newSong = {};
  1934. newSong.id = $("#id").val();
  1935. newSong.likes = Number($("#likes").val());
  1936. newSong.dislikes = Number($("#dislikes").val());
  1937. newSong.title = $("#title").val();
  1938. newSong.artist = $("#artist").val();
  1939. newSong.img = $("#img").val();
  1940. newSong.type = $("#type").val();
  1941. newSong.duration = Number($("#duration").val());
  1942. newSong.skipDuration = $("#skip-duration").val();
  1943. if(newSong.skipDuration === undefined){
  1944. newSong.skipDuration = 0;
  1945. };
  1946. if (Session.get("type") === "playlist") {
  1947. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1948. $('#editModal').modal('hide');
  1949. });
  1950. } else {
  1951. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1952. $('#editModal').modal('hide');
  1953. });
  1954. }
  1955. },
  1956. "click .delete-room": function(){
  1957. var typeDel = $(this)[0].type;
  1958. Meteor.call("deleteRoom", typeDel);
  1959. }
  1960. });
  1961. Template.stations.onCreated(function() {
  1962. var tag = document.createElement("script");
  1963. tag.src = "https://www.youtube.com/iframe_api";
  1964. var firstScriptTag = document.getElementsByTagName('script')[0];
  1965. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1966. YTPlayer = undefined;
  1967. SCPlayer = undefined;
  1968. });
  1969. Template.stations.onRendered(function() {
  1970. $("#previewModal").on("hidden.bs.modal", function() {
  1971. if (previewEndSongTimeout !== undefined) {
  1972. Meteor.clearTimeout(previewEndSongTimeout);
  1973. }
  1974. $("#play").attr("disabled", false);
  1975. $("#stop").attr("disabled", true);
  1976. if (YTPlayer !== undefined) {
  1977. $("#previewPlayer").hide();
  1978. YTPlayer.seekTo(0);
  1979. YTPlayer.stopVideo();
  1980. }
  1981. if (SCPlayer !== undefined) {
  1982. SCPlayer.stop();
  1983. }
  1984. });
  1985. $(document).ready(function() {
  1986. function makeSlider(){
  1987. var slider = $("#volume-slider").slider();
  1988. var volume = localStorage.getItem("volume") || 20;
  1989. $("#volume-slider").slider("setValue", volume);
  1990. if (slider.length === 0) {
  1991. Meteor.setTimeout(function() {
  1992. makeSlider();
  1993. }, 500);
  1994. } else {
  1995. slider.on("slide", function(val) {
  1996. localStorage.setItem("volume", val.value);
  1997. if (YTPlayer !== undefined) {
  1998. YTPlayer.setVolume(val.value);
  1999. } else if (SCPlayer !== undefined) {
  2000. var volume = val.value / 100;
  2001. SCPlayer.setVolume(volume);
  2002. }
  2003. });
  2004. }
  2005. }
  2006. makeSlider();
  2007. });
  2008. });
  2009. 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}}};
  2010. Handlebars.registerHelper('active', function(path) {
  2011. return curPath() == path ? 'active' : '';
  2012. });
  2013. function executeCommand(command, params){
  2014. if (command === "help" || command === "commands") {
  2015. $('#helpModal').modal('show');
  2016. return true;
  2017. } else if (command === "volume") {
  2018. if (params.length === 1) {
  2019. var volume = Number(params[0]);
  2020. if (volume >= 0 || volume <= 100) {
  2021. if (volume === 0) {
  2022. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  2023. } else {
  2024. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  2025. }
  2026. $("#volume-slider").slider("setValue", volume);
  2027. if (YTPlayer !== undefined) {
  2028. YTPlayer.setVolume(volume);
  2029. localStorage.setItem("volume", volume);
  2030. } else if (SCPlayer !== undefined) {
  2031. //SCPlayer
  2032. var volume = volume / 100;
  2033. SCPlayer.setVolume(volume);
  2034. localStorage.setItem("volume", volume * 100);
  2035. }
  2036. return true;
  2037. }
  2038. }
  2039. } else if(command === "mute"){
  2040. $("#volume-slider").slider("setValue", 0);
  2041. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off");
  2042. if (YTPlayer !== undefined) {
  2043. YTPlayer.setVolume(0);
  2044. localStorage.setItem("volume", 0);
  2045. } else if (SCPlayer !== undefined) {
  2046. //SCPlayer
  2047. SCPlayer.setVolume(0);
  2048. localStorage.setItem("volume", 0);
  2049. }
  2050. } else if(command === "ban"){
  2051. var user = params[0];
  2052. var time = params[1];
  2053. var reason = params[2];
  2054. Meteor.call("banUser", user, time, reason, function(err, res){
  2055. if(err){
  2056. console.log(err);
  2057. }
  2058. });
  2059. } else if(command === "silence"){
  2060. var user = params[0];
  2061. var time = params[1];
  2062. Meteor.call("muteUser", user, time, function(err, res){
  2063. if(err){
  2064. console.log(err);
  2065. }
  2066. });
  2067. } else if(command === "unban"){
  2068. var user = params[0];
  2069. Meteor.call("unbanUser", user, function(err, res){
  2070. if(err){
  2071. console.log(err);
  2072. }
  2073. });
  2074. } else if(command === "unsilence"){
  2075. var user = params[0];
  2076. Meteor.call("unsilenceUser", user, function(err, res){
  2077. if(err){
  2078. console.log(err);
  2079. }
  2080. });
  2081. } else if(command === "pause"){
  2082. Meteor.call("pauseRoom", Session.get("type"), function(err, res){
  2083. if(err){
  2084. console.log(err);
  2085. }
  2086. });
  2087. } else if(command === "resume"){
  2088. Meteor.call("resumeRoom", Session.get("type"), function(err, res){
  2089. if(err){
  2090. console.log(err);
  2091. }
  2092. });
  2093. } else if(command === "shuffle"){
  2094. Meteor.call("shufflePlaylist", Session.get("type"), function(err, res){
  2095. if(err){
  2096. console.log(err);
  2097. }
  2098. });
  2099. } else if(command === "skip"){
  2100. Meteor.call("skipSong", Session.get("type"), function(err, res){
  2101. if(err){
  2102. console.log(err);
  2103. }
  2104. });
  2105. }
  2106. }
  2107. function sendMessage() {
  2108. var message = $("#chat-input").val();
  2109. if (!$("#chat-input").hasClass("disabled")) {
  2110. if (message.length > 0 && message[0] !== " ") {
  2111. if (message[0] === "/") {
  2112. message = message.split("");
  2113. message.shift();
  2114. message = message.join("");
  2115. var params = message.split(" ");
  2116. params = params.map(function(param) {
  2117. return param.replace(/\r?\n|\r/g, "");
  2118. });
  2119. var command = params.shift();
  2120. command = command.replace(/\r?\n|\r/g, "");
  2121. if (executeCommand(command, params)) {
  2122. $("#chat-input").val("");
  2123. } else {
  2124. $("#chat-input").val("");
  2125. }
  2126. } else {
  2127. $("#chat-input").addClass("disabled");
  2128. $("#chat-input").attr("disabled", "");
  2129. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  2130. if (res) {
  2131. $("#chat-input").val("");
  2132. $("#chat-input").removeAttr("disabled");
  2133. $("#chat-input").removeClass("disabled");
  2134. }
  2135. });
  2136. }
  2137. }
  2138. }
  2139. }
  2140. function sendMessageGlobal() {
  2141. var message = $("#global-chat-input").val();
  2142. if (!$("#global-chat-input").hasClass("disabled")) {
  2143. if (message.length > 0 && message[0] !== " ") {
  2144. if (message[0] === "/") {
  2145. message = message.split("");
  2146. message.shift();
  2147. message = message.join("");
  2148. var params = message.split(" ");
  2149. var command = params.shift();
  2150. command = command.replace(/\r?\n|\r/g, "");
  2151. if (executeCommand(command, params)) {
  2152. $("#global-chat-input").val("");
  2153. } else {
  2154. $("#global-chat-input").val("");
  2155. }
  2156. } else {
  2157. $("#global-chat-input").addClass("disabled");
  2158. $("#global-chat-input").attr("disabled", "");
  2159. Meteor.call("sendMessage", "global", message, function (err, res) {
  2160. if (res) {
  2161. $("#global-chat-input").val("");
  2162. }
  2163. $("#global-chat-input").removeClass("disabled");
  2164. $("#global-chat-input").removeAttr("disabled");
  2165. });
  2166. }
  2167. }
  2168. }
  2169. }
  2170. Template.registerHelper("rtime", function(date) {
  2171. Session.get("time");
  2172. if (date) {
  2173. return moment(date).fromNow();
  2174. }
  2175. });
  2176. var rTimeInterval = undefined;
  2177. var allAlertSub = undefined;
  2178. var YTPlayer = undefined;
  2179. var SCPlayer = undefined;
  2180. var previewEndSongTimeout = undefined;