events.js 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. var feedbackData;
  2. function getSpotifyInfo(title, cb, artist) {
  3. var q = "";
  4. q = title;
  5. if (artist !== undefined) {
  6. q += " artist:" + artist;
  7. }
  8. $.ajax({
  9. type: "GET",
  10. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  11. applicationType: "application/json",
  12. contentType: "json",
  13. success: function (data) {
  14. cb(data);
  15. }
  16. });
  17. }
  18. function executeCommand(command, params){
  19. if (command === "help" || command === "commands") {
  20. $('#helpModal').modal('show');
  21. return true;
  22. } else if (command === "volume") {
  23. if (params.length === 1) {
  24. var volume = Number(params[0]);
  25. if (volume >= 0 || volume <= 100) {
  26. if (volume === 0) {
  27. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  28. } else {
  29. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  30. }
  31. $("#volume-slider").slider("setValue", volume);
  32. if (YTPlayer !== undefined) {
  33. YTPlayer.setVolume(volume);
  34. localStorage.setItem("volume", volume);
  35. }
  36. return true;
  37. }
  38. }
  39. } else if(command === "mute"){
  40. $("#volume-slider").slider("setValue", 0);
  41. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off");
  42. if (YTPlayer !== undefined) {
  43. YTPlayer.setVolume(0);
  44. localStorage.setItem("volume", 0);
  45. }
  46. } else if(command === "ban"){
  47. var user = params[0];
  48. var time = params[1];
  49. var reason = params[2];
  50. Meteor.call("banUser", user, time, reason, function(err, res){
  51. if(err){
  52. console.log(err);
  53. }
  54. });
  55. } else if(command === "silence"){
  56. var user = params[0];
  57. var time = params[1];
  58. Meteor.call("muteUser", user, time, function(err, res){
  59. if(err){
  60. console.log(err);
  61. }
  62. });
  63. } else if(command === "unban"){
  64. var user = params[0];
  65. Meteor.call("unbanUser", user, function(err, res){
  66. if(err){
  67. console.log(err);
  68. }
  69. });
  70. } else if(command === "unsilence"){
  71. var user = params[0];
  72. Meteor.call("unsilenceUser", user, function(err, res){
  73. if(err){
  74. console.log(err);
  75. }
  76. });
  77. } else if(command === "pause"){
  78. Meteor.call("pauseRoom", Session.get("type"), function(err, res){
  79. if(err){
  80. console.log(err);
  81. }
  82. });
  83. } else if(command === "resume"){
  84. Meteor.call("resumeRoom", Session.get("type"), function(err, res){
  85. if(err){
  86. console.log(err);
  87. }
  88. });
  89. } else if(command === "shuffle"){
  90. Meteor.call("shufflePlaylist", Session.get("type"), function(err, res){
  91. if(err){
  92. console.log(err);
  93. }
  94. });
  95. } else if(command === "skip"){
  96. Meteor.call("skipSong", Session.get("type"), function(err, res){
  97. if(err){
  98. console.log(err);
  99. }
  100. });
  101. }
  102. }
  103. function sendMessage() {
  104. var message = $("#chat-input").val();
  105. if (!$("#chat-input").hasClass("disabled")) {
  106. if (message.length > 0 && message[0] !== " ") {
  107. if (message[0] === "/") {
  108. message = message.split("");
  109. message.shift();
  110. message = message.join("");
  111. var params = message.split(" ");
  112. params = params.map(function(param) {
  113. return param.replace(/\r?\n|\r/g, "");
  114. });
  115. var command = params.shift();
  116. command = command.replace(/\r?\n|\r/g, "");
  117. if (executeCommand(command, params)) {
  118. $("#chat-input").val("");
  119. } else {
  120. $("#chat-input").val("");
  121. }
  122. } else {
  123. $("#chat-input").addClass("disabled");
  124. $("#chat-input").attr("disabled", "");
  125. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  126. if(err){
  127. $("#chat-input").val("");
  128. $("#chat-input").removeAttr("disabled");
  129. $("#chat-input").removeClass("disabled");
  130. }
  131. if (res) {
  132. $("#chat-input").val("");
  133. $("#chat-input").removeAttr("disabled");
  134. $("#chat-input").removeClass("disabled");
  135. }
  136. });
  137. }
  138. }
  139. }
  140. }
  141. function sendMessageGlobal() {
  142. var message = $("#global-chat-input").val();
  143. if (!$("#global-chat-input").hasClass("disabled")) {
  144. if (message.length > 0 && message[0] !== " ") {
  145. if (message[0] === "/") {
  146. message = message.split("");
  147. message.shift();
  148. message = message.join("");
  149. var params = message.split(" ");
  150. var command = params.shift();
  151. command = command.replace(/\r?\n|\r/g, "");
  152. if (executeCommand(command, params)) {
  153. $("#global-chat-input").val("");
  154. } else {
  155. $("#global-chat-input").val("");
  156. }
  157. } else {
  158. $("#global-chat-input").addClass("disabled");
  159. $("#global-chat-input").attr("disabled", "");
  160. Meteor.call("sendMessage", "global", message, function (err, res) {
  161. if (res) {
  162. $("#global-chat-input").val("");
  163. }
  164. $("#global-chat-input").removeClass("disabled");
  165. $("#global-chat-input").removeAttr("disabled");
  166. });
  167. }
  168. }
  169. }
  170. }
  171. Template.admin.events({
  172. "click a": function(e){
  173. var id = e.currentTarget.id;
  174. console.log(id.toLowerCase());
  175. Session.set("playlistToEdit", id);
  176. },
  177. "click #croom_create": function() {
  178. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#croom_private").prop("checked"), $("#croom_desc").val(), function (err, res) {
  179. if (err) {
  180. alert("Error " + err.error + ": " + err.reason);
  181. } else {
  182. window.location = "/" + $("#croom_tag").val();
  183. }
  184. });
  185. },
  186. "click #rreset_confirm": function(){
  187. $('#confirmModal').modal('hide');
  188. Meteor.call("resetRating");
  189. },
  190. "click #edit_desc": function(){
  191. console.log($(this));
  192. console.log($(this)[0].type);
  193. Session.set("roomDesc", $(this)[0].type);
  194. $("#desc_text").val(Rooms.findOne({type: Session.get("roomDesc")}).roomDesc);
  195. },
  196. "click #submit_desc": function(){
  197. var description = $("#desc_text").val();
  198. Meteor.call("editRoomDesc", Session.get("roomDesc"), description);
  199. $("#desc-modal").closeModal();
  200. }
  201. });
  202. Template.alertsDashboard.events({
  203. "click #calart-create": function() {
  204. Meteor.call("addAlert", $("#calert-description").val(), $("#calert-priority").val().toLowerCase(), function (err, res) {
  205. if (err) {
  206. alert("Error " + err.error + ": " + err.reason);
  207. } else {
  208. $("#calert-description").val("");
  209. }
  210. });
  211. },
  212. "click #ralert-button": function() {
  213. Meteor.call("removeAlerts");
  214. }
  215. });
  216. Template.feedback.events({
  217. "click #feedback_submit": function(){
  218. if($("#feedback_message").val().length !== 0 && $("#feedback_message").hasClass("invalid") === false){
  219. Meteor.call("sendFeedback", $("#feedback_message").val());
  220. $("#feedback_message").val("");
  221. $("#modal1").closeModal()
  222. } else{
  223. var $toastContent = $('<span><strong>Feedback not sent.</strong> Possible reasons include:<ul><li>- Empty Feedback Message</li><li>- Feedback is more than 500 words</li></ul></span>');
  224. Materialize.toast($toastContent, 8000);
  225. }
  226. },
  227. "click .upvote": function(){
  228. var message = $(this).parent("card").prevObject[0].message;
  229. Meteor.call("upvoteFeedback", message);
  230. },
  231. "click #delete": function(){
  232. var message = $(this).parent("card").prevObject[0].message;
  233. Meteor.call("deleteFeedback", message);
  234. },
  235. "click #edit": function(){
  236. $("#editModal").click()
  237. var data = Feedback.findOne({"message": $(this).parent("card").prevObject[0].message});
  238. feedbackData = data.message;
  239. $("#edit_feedback_message").val(data.message);
  240. },
  241. "click #edit_feedback_submit": function(){
  242. var oldMessage = feedbackData;
  243. var newMessage = $("#edit_feedback_message").val()
  244. $("#edit_feedback_message").val("")
  245. Meteor.call("updateFeedback", oldMessage, newMessage);
  246. $("#editFeedback").closeModal();
  247. }
  248. });
  249. Template.header.events({
  250. "click .logout": function(e){
  251. e.preventDefault();
  252. Meteor.logout();
  253. if (hpSound !== undefined) {
  254. hpSound.stop();
  255. }
  256. },
  257. "click #profile": function(){
  258. window.location = "/u/" + Meteor.user().profile.username;
  259. }
  260. });
  261. Template.login.events({
  262. "submit form": function(e){
  263. e.preventDefault();
  264. Session.set("github", false);
  265. var username = $("#username").val()
  266. var password = $("#password").val();
  267. Meteor.loginWithPassword(username, password, function(err) {
  268. if (err) {
  269. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  270. $(".landing").before(errAlert);
  271. Meteor.setTimeout(function() {
  272. errAlert.fadeOut(5000, function() {
  273. errAlert.remove();
  274. });
  275. }, 5000);
  276. } else {
  277. window.location.href = "/";
  278. }
  279. });
  280. },
  281. "click #github-login": function(){
  282. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  283. console.log(err, res);
  284. });
  285. }
  286. });
  287. Template.playlist.events({
  288. "keyup #search-playlist": function(){
  289. if($("#search-playlist").val().length === 0){
  290. $(".pl-item").show();
  291. } else {
  292. $(".pl-item").hide();
  293. var input = $("#search-playlist").val().toLowerCase();
  294. $(".pl-item strong").each(function(i, el){
  295. if($(el).text().toLowerCase().indexOf(input) !== -1){
  296. $(el).parent(".pl-item").show();
  297. }
  298. })
  299. $(".pl-item #pl-artist").each(function(i, el){
  300. if($(el).text().toLowerCase().indexOf(input) !== -1){
  301. $(el).parent(".pl-item").show();
  302. }
  303. })
  304. }
  305. },
  306. "click #pl-item": function(){
  307. console.log($(this).text());
  308. }
  309. });
  310. Template.profile.events({
  311. //Edit real name
  312. "click #edit-name": function(){
  313. $("#name").hide();
  314. $("#name-div").show();
  315. $("#edit-name").hide();
  316. $("#cancel-edit").show();
  317. },
  318. "click #submit-name": function(){
  319. var user = Meteor.user();
  320. $("#name").show();
  321. $("#name-div").hide();
  322. $("#edit-name").show();
  323. $("#cancel-edit").hide();
  324. var realname = $("#input-name").val();
  325. var username = user.profile.username;
  326. $("#name").text("Name: " + realname);
  327. $("#input-name").val("")
  328. Meteor.call("updateRealName", realname);
  329. },
  330. "click #cancel-edit": function(){
  331. $("#name").show();
  332. $("#name-div").hide();
  333. $("#edit-name").show();
  334. $("#cancel-edit").hide();
  335. $("#input-name").val("");
  336. },
  337. //Edit username
  338. "click #edit-username": function(){
  339. $("#username").hide();
  340. $("#username-div").show();
  341. $("#edit-username").hide();
  342. $("#cancel-username").show();
  343. },
  344. "click #submit-username": function(){
  345. var user = Meteor.user()
  346. $("#username").show();
  347. $("#username-div").hide();
  348. $("#edit-username").show();
  349. $("#cancel-username").hide();
  350. var username = user.username;
  351. var newUserName = $("#input-username").val();
  352. $("#profile-name").text(newUserName)
  353. $("#username").text("Username: " + newUserName);
  354. $("#input-username").val("")
  355. Meteor.call("updateUserName", newUserName);
  356. window.location = "/u/" + newUserName;
  357. },
  358. "click #cancel-username": function(){
  359. $("#username").show();
  360. $("#username-div").hide();
  361. $("#edit-username").show();
  362. $("#cancel-username").hide();
  363. $("#input-username").val("");
  364. },
  365. // Admins only Edit Rank
  366. "click #edit-rank": function() {
  367. $("#rank").hide();
  368. $("#rank-div").show();
  369. $("#edit-rank").hide();
  370. $("#cancel-rank").show();
  371. },
  372. "click #submit-rank": function() {
  373. $("#rank").show();
  374. $("#rank-div").hide();
  375. $("#edit-rank").show();
  376. $("#cancel-rank").hide();
  377. var newRank = $("#select-rank option:selected").val();
  378. var username = Session.get("username");
  379. console.log(username, newRank);
  380. },
  381. "click #cancel-rank": function() {
  382. $("#rank").show();
  383. $("#rank-div").hide();
  384. $("#edit-rank").show();
  385. $("#cancel-rank").hide();
  386. }
  387. });
  388. var seekerBarInterval = undefined;
  389. Template.queues.events({
  390. /* TODO Add undo delete button */
  391. "input #id": function() {
  392. console.log("Change!");
  393. $("#previewPlayerContainer").addClass("hide-preview");
  394. },
  395. "input #img": function() {
  396. var url = $("#img").val();
  397. console.log(url);
  398. Session.set("image_url", url);
  399. },
  400. "click .preview-button": function(e){
  401. Session.set("song", this);
  402. $("#previewModal").openModal();
  403. },
  404. "click #previewImageButton": function() {
  405. $("#preview-image").attr("src", Session.get("song").img);
  406. },
  407. "click .edit-queue-button": function(e){
  408. Session.set("song", this);
  409. Session.set("genre", $(e.target).data("genre"));
  410. Session.set("type", "queue");
  411. $("#mid").val(this.mid).change();
  412. $("#artist").val(this.artist).change();
  413. $("#title").val(this.title).change();
  414. $("#img").val(this.img).change();
  415. $("#id").val(this.id).change();
  416. $("#likes").val(this.likes).change();
  417. $("#dislikes").val(this.dislikes).change();
  418. $("#duration").val(this.duration).change();
  419. $("#skip-duration").val(this.skipDuration).change();
  420. $("#previewPlayerContainer").addClass("hide-preview");
  421. Session.set("image_url", this.img);
  422. Session.set("editing", true);
  423. $("#editModal").openModal({
  424. complete : function() {
  425. Session.set("editing", false);
  426. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  427. YTPlayer.stopVideo();
  428. }
  429. }
  430. });
  431. },
  432. "click .add-song-button": function(e){
  433. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  434. Meteor.call("addSongToPlaylist", this, function(err) {
  435. console.log(err);
  436. if (err) {
  437. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  438. Materialize.toast($toastContent, 8000);
  439. }
  440. });
  441. },
  442. "click .deny-song-button": function(e){
  443. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  444. Meteor.call("removeSongFromQueue", this.mid);
  445. },
  446. "click #play": function() {
  447. var duration = Session.get("song").duration;
  448. var d = moment.duration(parseInt(duration), 'seconds');
  449. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  450. $("#previewPlayerContainer").removeClass("hide-preview");
  451. var song = Session.get("song");
  452. var id = song.id;
  453. var volume = localStorage.getItem("volume") || 20;
  454. if (song.duration !== 0) {
  455. $("#play").attr("disabled", true);
  456. $("#stop").attr("disabled", false);
  457. $("#pause").attr("disabled", false);
  458. $("#forward").attr("disabled", false);
  459. if (YTPlayer === undefined) {
  460. YTPlayer = new YT.Player("previewPlayer", {
  461. height: 540,
  462. width: 568,
  463. videoId: id,
  464. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  465. events: {
  466. 'onReady': function(event) {
  467. event.target.seekTo(Number(song.skipDuration));
  468. event.target.playVideo();
  469. event.target.setVolume(volume);
  470. },
  471. 'onStateChange': function(event){
  472. if (event.data == YT.PlayerState.PAUSED) {
  473. if (seekerBarInterval !== undefined) {
  474. Meteor.clearInterval(seekerBarInterval);
  475. seekerBarInterval = undefined;
  476. }
  477. }
  478. if (event.data == YT.PlayerState.UNSTARTED) {
  479. if (seekerBarInterval !== undefined) {
  480. Meteor.clearInterval(seekerBarInterval);
  481. seekerBarInterval = undefined;
  482. }
  483. $(".seeker-bar").css({width: "0"});
  484. $("#time-elapsed").text("0:00");
  485. $("#previewPlayerContainer").addClass("hide-preview");
  486. }
  487. if (event.data == YT.PlayerState.PLAYING) {
  488. seekerBarInterval = Meteor.setInterval(function() {
  489. var duration = Session.get("song").duration;
  490. var timeElapsed = YTPlayer.getCurrentTime();
  491. var skipDuration = Session.get("song").skipDuration;
  492. if (duration <= (timeElapsed - skipDuration)) {
  493. YTPlayer.stopVideo();
  494. $("#play").attr("disabled", false);
  495. $("#stop").attr("disabled", true);
  496. $("#pause").attr("disabled", true);
  497. $("#forward").attr("disabled", true);
  498. $("#previewPlayerContainer").addClass("hide-preview");
  499. $(".seeker-bar").css({width: "0"});
  500. $("#time-elapsed").text("0:00");
  501. Meteor.clearInterval(seekerBarInterval);
  502. } else {
  503. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  504. $(".seeker-bar").css({width: percentComplete + "%"});
  505. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  506. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  507. }
  508. }, 100);
  509. $("#play").attr("disabled", true);
  510. $("#stop").attr("disabled", false);
  511. $("#pause").attr("disabled", false);
  512. $("#forward").attr("disabled", false);
  513. } else {
  514. $("#play").attr("disabled", false);
  515. $("#stop").attr("disabled", true);
  516. $("#pause").attr("disabled", true);
  517. $("#forward").attr("disabled", true);
  518. }
  519. }
  520. }
  521. });
  522. } else {
  523. if (YTPlayer.getPlayerState() === 2) {
  524. YTPlayer.playVideo();
  525. } else {
  526. console.log(id, song.skipDuration, song.duration);
  527. YTPlayer.loadVideoById(id);
  528. YTPlayer.seekTo(Number(song.skipDuration));
  529. }
  530. }
  531. $("#previewPlayerContainer").removeClass("hide-preview");
  532. }
  533. },
  534. "click #stop": function() {
  535. $("#play").attr("disabled", false);
  536. $("#stop").attr("disabled", true);
  537. $("#pause").attr("disabled", true);
  538. $("#forward").attr("disabled", true);
  539. if (previewEndSongTimeout !== undefined) {
  540. Meteor.clearTimeout(previewEndSongTimeout);
  541. }
  542. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  543. YTPlayer.stopVideo();
  544. }
  545. },
  546. "click #pause": function() {
  547. $("#play").attr("disabled", false);
  548. $("#stop").attr("disabled", false);
  549. $("#pause").attr("disabled", true);
  550. $("#forward").attr("disabled", true);
  551. if (previewEndSongTimeout !== undefined) {
  552. Meteor.clearTimeout(previewEndSongTimeout);
  553. }
  554. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  555. YTPlayer.pauseVideo();
  556. }
  557. },
  558. "click #forward": function() {
  559. var error = false;
  560. if (YTPlayer !== undefined) {
  561. var duration = Number(Session.get("song").duration) | 0;
  562. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  563. if (YTPlayer.getDuration() < duration + skipDuration) {
  564. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  565. Materialize.toast($toastContent, 8000);
  566. error = true;
  567. } else {
  568. YTPlayer.seekTo(skipDuration + duration - 10);
  569. }
  570. }
  571. if (!error) {
  572. if (previewEndSongTimeout !== undefined) {
  573. Meteor.clearTimeout(previewEndSongTimeout);
  574. }
  575. previewEndSongTimeout = Meteor.setTimeout(function() {
  576. if (YTPlayer !== undefined) {
  577. YTPlayer.stopVideo();
  578. }
  579. $("#play").attr("disabled", false);
  580. $("#stop").attr("disabled", true);
  581. $("#pause").attr("disabled", true);
  582. $("#forward").attr("disabled", true);
  583. $("#previewPlayerContainer").addClass("hide-preview");
  584. }, 10000);
  585. }
  586. },
  587. "click #get-spotify-info": function() {
  588. var search = $("#title").val();
  589. var artistName = $("#artist").val();
  590. getSpotifyInfo(search, function(data) {
  591. for(var i in data){
  592. for(var j in data[i].items){
  593. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  594. $("#img").val(data[i].items[j].album.images[2].url).change();
  595. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  596. return;
  597. }
  598. }
  599. }
  600. }, artistName);
  601. },
  602. "click #save-song-button": function() {
  603. var newSong = {};
  604. newSong.mid = $("#mid").val();
  605. newSong.id = $("#id").val();
  606. newSong.likes = Number($("#likes").val());
  607. newSong.dislikes = Number($("#dislikes").val());
  608. newSong.title = $("#title").val();
  609. newSong.artist = $("#artist").val();
  610. newSong.img = $("#img").val();
  611. newSong.duration = Number($("#duration").val());
  612. newSong.skipDuration = $("#skip-duration").val();
  613. newSong.requestedBy = Session.get("song").requestedBy;
  614. newSong.genres = Session.get("song").genres;
  615. if(newSong.skipDuration === undefined){
  616. newSong.skipDuration = 0;
  617. }
  618. Meteor.call("updateQueueSong", newSong.mid, newSong, function(err, res) {
  619. if (err) {
  620. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  621. Materialize.toast($toastContent, 8000);
  622. } else {
  623. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  624. Materialize.toast($toastContent, 4000);
  625. Session.set("song", newSong);
  626. }
  627. });
  628. }
  629. });
  630. Template.manageStation.events({
  631. /* TODO Add undo delete button */
  632. "input #id": function() {
  633. $("#previewPlayerContainer").addClass("hide-preview");
  634. },
  635. "input #img": function() {
  636. var url = $("#img").val();
  637. Session.set("image_url", url);
  638. },
  639. "click .preview-button": function(e){
  640. Session.set("song", this);
  641. $("#previewModal").openModal();
  642. },
  643. "click #previewImageButton": function() {
  644. $("#preview-image").attr("src", Session.get("song").img);
  645. },
  646. "click .edit-song-button": function(e){
  647. Session.set("song", this);
  648. Session.set("genre", $(e.target).data("genre"));
  649. $("#mid").val(this.mid).change();
  650. $("#artist").val(this.artist).change();
  651. $("#title").val(this.title).change();
  652. $("#img").val(this.img).change();
  653. $("#id").val(this.id).change();
  654. $("#likes").val(this.likes).change();
  655. $("#dislikes").val(this.dislikes).change();
  656. $("#duration").val(this.duration).change();
  657. $("#skip-duration").val(this.skipDuration).change();
  658. $("#previewPlayerContainer").addClass("hide-preview");
  659. Session.set("image_url", this.img);
  660. Session.set("editing", true);
  661. $("#editModal").openModal({
  662. complete : function() {
  663. Session.set("editing", false);
  664. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  665. YTPlayer.stopVideo();
  666. }
  667. }
  668. });
  669. },
  670. "click .remove-song-button": function(e){
  671. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  672. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  673. },
  674. "click #play": function() {
  675. var duration = Session.get("song").duration;
  676. var d = moment.duration(parseInt(duration), 'seconds');
  677. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  678. $("#previewPlayerContainer").removeClass("hide-preview");
  679. var song = Session.get("song");
  680. var id = song.id;
  681. var volume = localStorage.getItem("volume") || 20;
  682. if (song.duration !== 0) {
  683. $("#play").attr("disabled", true);
  684. $("#stop").attr("disabled", false);
  685. $("#pause").attr("disabled", false);
  686. $("#forward").attr("disabled", false);
  687. if (YTPlayer === undefined) {
  688. YTPlayer = new YT.Player("previewPlayer", {
  689. height: 540,
  690. width: 568,
  691. videoId: id,
  692. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  693. events: {
  694. 'onReady': function(event) {
  695. event.target.seekTo(Number(song.skipDuration));
  696. event.target.playVideo();
  697. event.target.setVolume(volume);
  698. },
  699. 'onStateChange': function(event){
  700. if (event.data == YT.PlayerState.PAUSED) {
  701. if (seekerBarInterval !== undefined) {
  702. Meteor.clearInterval(seekerBarInterval);
  703. seekerBarInterval = undefined;
  704. }
  705. }
  706. if (event.data == YT.PlayerState.UNSTARTED) {
  707. if (seekerBarInterval !== undefined) {
  708. Meteor.clearInterval(seekerBarInterval);
  709. seekerBarInterval = undefined;
  710. }
  711. $(".seeker-bar").css({width: "0"});
  712. $("#time-elapsed").text("0:00");
  713. $("#previewPlayerContainer").addClass("hide-preview");
  714. }
  715. if (event.data == YT.PlayerState.PLAYING) {
  716. seekerBarInterval = Meteor.setInterval(function() {
  717. var duration = Session.get("song").duration;
  718. var timeElapsed = YTPlayer.getCurrentTime();
  719. var skipDuration = Session.get("song").skipDuration;
  720. if (duration <= (timeElapsed - skipDuration)) {
  721. YTPlayer.stopVideo();
  722. $("#play").attr("disabled", false);
  723. $("#stop").attr("disabled", true);
  724. $("#pause").attr("disabled", true);
  725. $("#forward").attr("disabled", true);
  726. $("#previewPlayerContainer").addClass("hide-preview");
  727. $(".seeker-bar").css({width: "0"});
  728. $("#time-elapsed").text("0:00");
  729. Meteor.clearInterval(seekerBarInterval);
  730. } else {
  731. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  732. $(".seeker-bar").css({width: percentComplete + "%"});
  733. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  734. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  735. }
  736. }, 100);
  737. $("#play").attr("disabled", true);
  738. $("#stop").attr("disabled", false);
  739. $("#pause").attr("disabled", false);
  740. $("#forward").attr("disabled", false);
  741. } else {
  742. $("#play").attr("disabled", false);
  743. $("#stop").attr("disabled", true);
  744. $("#pause").attr("disabled", true);
  745. $("#forward").attr("disabled", true);
  746. }
  747. }
  748. }
  749. });
  750. } else {
  751. if (YTPlayer.getPlayerState() === 2) {
  752. YTPlayer.playVideo();
  753. } else {
  754. console.log(id, song.skipDuration, song.duration);
  755. YTPlayer.loadVideoById(id);
  756. YTPlayer.seekTo(Number(song.skipDuration));
  757. }
  758. }
  759. $("#previewPlayerContainer").removeClass("hide-preview");
  760. }
  761. },
  762. "click #stop": function() {
  763. $("#play").attr("disabled", false);
  764. $("#stop").attr("disabled", true);
  765. $("#pause").attr("disabled", true);
  766. $("#forward").attr("disabled", true);
  767. if (previewEndSongTimeout !== undefined) {
  768. Meteor.clearTimeout(previewEndSongTimeout);
  769. }
  770. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  771. YTPlayer.stopVideo();
  772. }
  773. },
  774. "click #pause": function() {
  775. $("#play").attr("disabled", false);
  776. $("#stop").attr("disabled", false);
  777. $("#pause").attr("disabled", true);
  778. $("#forward").attr("disabled", true);
  779. if (previewEndSongTimeout !== undefined) {
  780. Meteor.clearTimeout(previewEndSongTimeout);
  781. }
  782. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  783. YTPlayer.pauseVideo();
  784. }
  785. },
  786. "click #forward": function() {
  787. var error = false;
  788. if (YTPlayer !== undefined) {
  789. var duration = Number(Session.get("song").duration) | 0;
  790. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  791. if (YTPlayer.getDuration() < duration + skipDuration) {
  792. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  793. Materialize.toast($toastContent, 8000);
  794. error = true;
  795. } else {
  796. YTPlayer.seekTo(skipDuration + duration - 10);
  797. }
  798. }
  799. if (!error) {
  800. if (previewEndSongTimeout !== undefined) {
  801. Meteor.clearTimeout(previewEndSongTimeout);
  802. }
  803. previewEndSongTimeout = Meteor.setTimeout(function() {
  804. if (YTPlayer !== undefined) {
  805. YTPlayer.stopVideo();
  806. }
  807. $("#play").attr("disabled", false);
  808. $("#stop").attr("disabled", true);
  809. $("#pause").attr("disabled", true);
  810. $("#forward").attr("disabled", true);
  811. $("#previewPlayerContainer").addClass("hide-preview");
  812. }, 10000);
  813. }
  814. },
  815. "click #get-spotify-info": function() {
  816. var search = $("#title").val();
  817. var artistName = $("#artist").val();
  818. getSpotifyInfo(search, function(data) {
  819. for(var i in data){
  820. for(var j in data[i].items){
  821. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  822. $("#img").val(data[i].items[j].album.images[2].url).change();
  823. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  824. return;
  825. }
  826. }
  827. }
  828. }, artistName);
  829. },
  830. "click #save-song-button": function() {
  831. var newSong = {};
  832. newSong.mid = $("#mid").val();
  833. newSong.id = $("#id").val();
  834. newSong.likes = Number($("#likes").val());
  835. newSong.dislikes = Number($("#dislikes").val());
  836. newSong.title = $("#title").val();
  837. newSong.artist = $("#artist").val();
  838. newSong.img = $("#img").val();
  839. newSong.duration = Number($("#duration").val());
  840. newSong.skipDuration = $("#skip-duration").val();
  841. newSong.requestedBy = Session.get("song").requestedBy;
  842. newSong.genres = Session.get("song").genres;
  843. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  844. console.log(err, res);
  845. if (err) {
  846. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  847. Materialize.toast($toastContent, 8000);
  848. } else {
  849. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  850. Materialize.toast($toastContent, 4000);
  851. Session.set("song", newSong);
  852. }
  853. });
  854. }
  855. });
  856. Template.register.events({
  857. "submit form": function(e){
  858. e.preventDefault();
  859. var username = $("#username").val()
  860. var email = $("#email").val()
  861. var password = $("#password").val();
  862. var captchaData = grecaptcha.getResponse();
  863. console.log(captchaData)
  864. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  865. grecaptcha.reset();
  866. if (err) {
  867. console.log(err);
  868. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  869. $(".landing").before(errAlert);
  870. Meteor.setTimeout(function() {
  871. errAlert.fadeOut(5000, function() {
  872. errAlert.remove();
  873. });
  874. }, 5000);
  875. } else {
  876. Meteor.loginWithPassword(username, password);
  877. Accounts.onLogin(function(){
  878. window.location.href = "/";
  879. })
  880. }
  881. });
  882. },
  883. "click #github-login": function(){
  884. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  885. console.log(err, res);
  886. });
  887. }
  888. });
  889. Template.news.events({
  890. "click #createArticleButton": function() {
  891. var title = $("#title").val();
  892. var content = $("#content").val();
  893. var anonymous = $("#anonymous").is(":checked");
  894. Meteor.call("createArticle", {title: title, content: content, anonymous: anonymous}, function(err, res) {
  895. if (err) {
  896. var $toastContent = $('<span><strong>Article not created.</strong> ' + err.reason + '</span>');
  897. Materialize.toast($toastContent, 8000);
  898. } else {
  899. $('#createArticle').closeModal()
  900. $("#title").val("").change();
  901. $("#content").val("").change();
  902. $("#anonymous").prop("checked", false).change();
  903. }
  904. });
  905. }
  906. });
  907. Template.room.events({
  908. "click #youtube-playlist-button": function () {
  909. if (!Session.get("importingPlaylist")) {
  910. var playlist_link = $("#youtube-playlist-input").val();
  911. var playlist_id = gup("list", playlist_link);
  912. var ytImportQueue = [];
  913. var totalVideos = 0;
  914. var videosInvalid = 0;
  915. var videosInQueue = 0;
  916. var videosInPlaylist = 0;
  917. var ranOnce = false;
  918. Session.set("importingPlaylist", true);
  919. $("#youtube-playlist-button").attr("disabled", "");
  920. $("#youtube-playlist-button").addClass("disabled");
  921. $("#youtube-playlist-input").attr("disabled", "");
  922. $("#youtube-playlist-input").addClass("disabled");
  923. $("#playlist-import-queue").empty();
  924. $("#playlist-import-queue").hide();
  925. $("#add-youtube-playlist").addClass("hidden-2");
  926. $("#import-progress").attr("aria-valuenow", 0);
  927. $("#import-progress").css({width: "0%"});
  928. $("#import-progress").text("0%");
  929. function makeAPICall(playlist_id, nextPageToken) {
  930. if (nextPageToken !== undefined) {
  931. nextPageToken = "&pageToken=" + nextPageToken;
  932. } else {
  933. nextPageToken = "";
  934. }
  935. $.ajax({
  936. type: "GET",
  937. url: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlist_id + nextPageToken + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  938. applicationType: "application/json",
  939. contentType: "json",
  940. success: function (data) {
  941. if (!ranOnce) {
  942. ranOnce = true;
  943. totalVideos = data.pageInfo.totalResults;
  944. }
  945. var nextToken = data.nextPageToken;
  946. for (var i in data.items) {
  947. var item = data.items[i];
  948. if (item.snippet.thumbnails !== undefined) {
  949. var genre = Session.get("type");
  950. if (Playlists.find({
  951. type: genre,
  952. "songs.id": item.snippet.resourceId.videoId
  953. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  954. videosInPlaylist++;
  955. } else if (Queues.find({
  956. type: genre,
  957. "songs.id": item.snippet.resourceId.videoId
  958. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  959. videosInQueue++;
  960. } else {
  961. $("#playlist-import-queue").append(
  962. "<div class='youtube-import-queue-item'>" +
  963. "<img src='" + item.snippet.thumbnails.medium.url + "' class='song-result-thumbnail'/>" +
  964. "<div>" +
  965. "<span class='song-result-title'>" + item.snippet.title + "</span>" +
  966. "<span class='song-result-channel'>" + item.snippet.channelTitle + "</span>" +
  967. "</div>" +
  968. "<i class='fa fa-times remove-import-song'></i>" +
  969. "</div>"
  970. );
  971. var percentage = ytImportQueue.length / (totalVideos - videosInvalid) * 100;
  972. $("#import-progress").attr("aria-valuenow", percentage.toFixed(2));
  973. $("#import-progress").css({width: percentage + "%"});
  974. $("#import-progress").text(percentage.toFixed(1) + "%");
  975. ytImportQueue.push({
  976. title: item.snippet.title,
  977. id: item.snippet.resourceId.videoId
  978. });
  979. }
  980. } else {
  981. videosInvalid++;
  982. }
  983. }
  984. if (nextToken !== undefined) {
  985. makeAPICall(playlist_id, nextToken);
  986. } else {
  987. $("#playlist-import-queue > div > i").click(function () {
  988. var title = $(this).parent().find("div > .song-result-title").text();
  989. for (var i in ytImportQueue) {
  990. if (ytImportQueue[i].title === title) {
  991. ytImportQueue.splice(i, 1);
  992. }
  993. }
  994. $(this).parent().remove();
  995. Session.set("YTImportQueue", ytImportQueue);
  996. });
  997. Session.set("importingPlaylist", false);
  998. $("#import-progress").attr("aria-valuenow", 100);
  999. $("#import-progress").css({width: "100%"});
  1000. $("#import-progress").text("100%");
  1001. $("#youtube-playlist-button").removeAttr("disabled");
  1002. $("#youtube-playlist-button").removeClass("disabled");
  1003. $("#youtube-playlist-input").removeAttr("disabled");
  1004. $("#youtube-playlist-input").removeClass("disabled");
  1005. $("#playlist-import-queue").show();
  1006. $("#add-youtube-playlist").removeClass("hidden-2");
  1007. Session.set("YTImportQueue", ytImportQueue);
  1008. }
  1009. }
  1010. })
  1011. }
  1012. makeAPICall(playlist_id);
  1013. }
  1014. },
  1015. "click #add-youtube-playlist": function () {
  1016. var YTImportQueue = Session.get("YTImportQueue");
  1017. $("#youtube-playlist-button").attr("disabled", "");
  1018. $("#youtube-playlist-button").addClass("disabled");
  1019. $("#youtube-playlist-input").attr("disabled", "");
  1020. $("#youtube-playlist-input").addClass("disabled");
  1021. $("#import-progress").attr("aria-valuenow", 0);
  1022. $("#import-progress").css({width: "0%"});
  1023. $("#import-progress").text("0%");
  1024. var failed = 0;
  1025. var success = 0;
  1026. var processed = 0;
  1027. var total = YTImportQueue.length;
  1028. YTImportQueue.forEach(function (song) {
  1029. var songData = {type: "YouTube", id: song.id, title: song.title, artist: "", img: ""};
  1030. Meteor.call("addSongToQueue", songData, function (err, res) {
  1031. if (err) {
  1032. console.log(err);
  1033. failed++;
  1034. } else {
  1035. success++;
  1036. }
  1037. processed++;
  1038. var percentage = processed / total * 100;
  1039. $("#import-progress").attr("aria-valuenow", percentage.toFixed(2));
  1040. $("#import-progress").css({width: percentage + "%"});
  1041. $("#import-progress").text(percentage.toFixed(1) + "%");
  1042. });
  1043. });
  1044. },
  1045. "click #chat-tab": function () {
  1046. $("#chat-tab").removeClass("unread-messages");
  1047. },
  1048. "click #global-chat-tab": function () {
  1049. $("#global-chat-tab").removeClass("unread-messages");
  1050. },
  1051. "click #sync": function () {
  1052. if (Session.get("currentSong") !== undefined) {
  1053. var room = Rooms.findOne({type: Session.get("type")});
  1054. if (room !== undefined) {
  1055. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  1056. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  1057. if (YTPlayer !== undefined) {
  1058. YTPlayer.seekTo(skipDuration + timeIn / 1000);
  1059. }
  1060. }
  1061. }
  1062. },
  1063. "click #lock": function () {
  1064. Meteor.call("lockRoom", Session.get("type"));
  1065. },
  1066. "click #unlock": function () {
  1067. Meteor.call("unlockRoom", Session.get("type"));
  1068. },
  1069. "click #chat-tab": function (e) {
  1070. Meteor.setTimeout(function () {
  1071. $("#chat-ul").scrollTop(100000);
  1072. }, 1);
  1073. },
  1074. "click #global-chat-tab": function (e) {
  1075. Meteor.setTimeout(function () {
  1076. $("#global-chat-ul").scrollTop(100000);
  1077. }, 1);
  1078. },
  1079. "click #submit": function () {
  1080. sendMessage();
  1081. Meteor.setTimeout(function () {
  1082. $("#chat-ul").scrollTop(100000);
  1083. }, 1000)
  1084. },
  1085. "click #global-submit": function () {
  1086. sendMessageGlobal();
  1087. Meteor.setTimeout(function () {
  1088. $("#global-chat-ul").scrollTop(100000);
  1089. }, 1000)
  1090. },
  1091. "keyup #chat-input": function (e) {
  1092. if (e.type === "keyup" && e.which === 13) {
  1093. e.preventDefault();
  1094. if (!$('#chat-input').data('dropdownshown')) {
  1095. sendMessage();
  1096. Meteor.setTimeout(function () {
  1097. $("#chat-ul").scrollTop(100000);
  1098. }, 1000)
  1099. }
  1100. }
  1101. },
  1102. "keyup #global-chat-input": function (e) {
  1103. if (e.type === "keyup" && e.which === 13) {
  1104. e.preventDefault();
  1105. if (!$('#global-chat-input').data('dropdownshown')) {
  1106. sendMessageGlobal();
  1107. Meteor.setTimeout(function () {
  1108. $("#global-chat-ul").scrollTop(100000);
  1109. }, 1000)
  1110. }
  1111. }
  1112. },
  1113. "click #like": function (e) {
  1114. $("#like").blur();
  1115. Meteor.call("likeSong", Session.get("currentSong").mid);
  1116. },
  1117. "click #dislike": function (e) {
  1118. $("#dislike").blur();
  1119. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  1120. },
  1121. "click #vote-skip": function () {
  1122. Meteor.call("voteSkip", type, function (err, res) {
  1123. $("#vote-skip").attr("disabled", true);
  1124. });
  1125. },
  1126. "click #report-prev": function (e) {
  1127. if (Session.get("previousSong") !== undefined) {
  1128. Session.set("reportPrevious", true);
  1129. $("#report-prev").prop("disabled", true);
  1130. $("#report-curr").prop("disabled", false);
  1131. }
  1132. },
  1133. "click #report-curr": function (e) {
  1134. Session.set("reportPrevious", false);
  1135. $("#report-prev").prop("disabled", false);
  1136. $("#report-curr").prop("disabled", true);
  1137. },
  1138. "click #report-modal": function () {
  1139. Session.set("currentSongR", Session.get("currentSong"));
  1140. Session.set("previousSongR", Session.get("previousSong"));
  1141. },
  1142. "click #add-song-button": function (e) {
  1143. e.preventDefault();
  1144. parts = location.href.split('/');
  1145. var roomType = parts.pop();
  1146. var genre = roomType.toLowerCase();
  1147. var type = $("#type").val();
  1148. id = $("#id").val();
  1149. var title = $("#title").val();
  1150. var artist = $("#artist").val();
  1151. var img = $("#img").val();
  1152. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  1153. if (Playlists.find({
  1154. type: genre,
  1155. "songs.id": songData.id
  1156. }, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  1157. $("<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 () {
  1158. $(this).remove();
  1159. });
  1160. } else if (Queues.find({
  1161. type: genre,
  1162. "songs.id": songData.id
  1163. }, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  1164. $("<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 () {
  1165. $(this).remove();
  1166. });
  1167. } else {
  1168. Meteor.call("addSongToQueue", songData, function (err, res) {
  1169. console.log(err, res);
  1170. if (err) {
  1171. $("<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 () {
  1172. $(this).remove();
  1173. });
  1174. } else {
  1175. $("<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 () {
  1176. $(this).remove();
  1177. });
  1178. }
  1179. });
  1180. }
  1181. $("#close-modal-a").click();
  1182. },
  1183. "click #toggle-video": function (e) {
  1184. e.preventDefault();
  1185. if (Session.get("mediaHidden")) {
  1186. $("#media-container").removeClass("hidden");
  1187. $("#toggle-video").text("Hide video");
  1188. Session.set("mediaHidden", false);
  1189. } else {
  1190. $("#media-container").addClass("hidden");
  1191. $("#toggle-video").text("Show video");
  1192. Session.set("mediaHidden", true);
  1193. }
  1194. },
  1195. "click #return": function (e) {
  1196. $("#add-info").hide();
  1197. $("#search-info").show();
  1198. },
  1199. "click #search-song": function () {
  1200. var songs = [];
  1201. $("#song-results").empty();
  1202. $.ajax({
  1203. type: "GET",
  1204. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  1205. applicationType: "application/json",
  1206. contentType: "json",
  1207. success: function (data) {
  1208. for (var i in data.items) {
  1209. var item = data.items[i];
  1210. $("#song-results").append(
  1211. "<div>" +
  1212. "<img src='" + item.snippet.thumbnails.medium.url + "' class='song-result-thumbnail'/>" +
  1213. "<div>" +
  1214. "<span class='song-result-title'>" + item.snippet.title + "</span>" +
  1215. "<span class='song-result-channel'>" + item.snippet.channelTitle + "</span>" +
  1216. "</div>" +
  1217. "</div>"
  1218. );
  1219. songs.push({title: item.snippet.title, id: item.id.videoId});
  1220. }
  1221. $("#song-results > div").click(function () {
  1222. $("#search-info").hide();
  1223. $("#add-info").show();
  1224. var title = $(this).find("div > .song-result-title").text();
  1225. for (var i in songs) {
  1226. if (songs[i].title === title) {
  1227. var songObj = {
  1228. id: songs[i].id,
  1229. title: songs[i].title,
  1230. type: "youtube"
  1231. };
  1232. $("#title").val(songObj.title);
  1233. $("#artist").val("");
  1234. $("#id").val(songObj.id);
  1235. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function (data) {
  1236. if (data.tracks.items.length > 0) {
  1237. $("#title").val(data.tracks.items[0].name);
  1238. var artists = [];
  1239. $("#img").val(data.tracks.items[0].album.images[2].url);
  1240. data.tracks.items[0].artists.forEach(function (artist) {
  1241. artists.push(artist.name);
  1242. });
  1243. $("#artist").val(artists.join(", "));
  1244. }
  1245. });
  1246. }
  1247. }
  1248. })
  1249. }
  1250. })
  1251. },
  1252. "click #volume-icon": function () {
  1253. var volume = 0;
  1254. var slider = $("#volume-slider").slider();
  1255. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1256. if (YTPlayer !== undefined) {
  1257. YTPlayer.setVolume(volume);
  1258. localStorage.setItem("volume", volume);
  1259. $("#volume-slider").slider("setValue", volume);
  1260. }
  1261. },
  1262. "click #play": function () {
  1263. Meteor.call("resumeRoom", type);
  1264. },
  1265. "click #pause": function () {
  1266. Meteor.call("pauseRoom", type);
  1267. },
  1268. "click #skip": function () {
  1269. Meteor.call("skipSong", type);
  1270. },
  1271. "click #shuffle": function () {
  1272. Meteor.call("shufflePlaylist", type);
  1273. },
  1274. "change input": function (e) {
  1275. if (e.target && e.target.id) {
  1276. var partsOfId = e.target.id.split("-");
  1277. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  1278. var camelCase = partsOfId.join("");
  1279. Session.set(camelCase, e.target.checked);
  1280. }
  1281. },
  1282. "click #report-song-button": function () {
  1283. var room = Session.get("type");
  1284. var reportData = {};
  1285. reportData.song = Session.get("currentSong").mid;
  1286. reportData.type = [];
  1287. reportData.reason = [];
  1288. $(".report-layer-1 > .checkbox input:checked").each(function () {
  1289. reportData.type.push(this.id);
  1290. if (this.id == "report-other") {
  1291. var otherText = $(".other-textarea").val();
  1292. }
  1293. });
  1294. $(".report-layer-2 input:checked").each(function () {
  1295. reportData.reason.push(this.id);
  1296. });
  1297. console.log(reportData);
  1298. Meteor.call("submitReport", room, reportData, Session.get("id"), function () {
  1299. $("#close-modal-r").click();
  1300. });
  1301. },
  1302. "change #si_or_pl": function () {
  1303. if ($("#select_playlist").is(':selected')) {
  1304. $("#search-info").hide();
  1305. $("#playlist-import").show();
  1306. }
  1307. if ($("#select_single").is(':selected')) {
  1308. $("#search-info").show();
  1309. $("#playlist-import").hide();
  1310. }
  1311. },
  1312. "click #close-modal-a": function () {
  1313. $("#select_single").attr("selected", true);
  1314. $("#search-info").show();
  1315. $("#playlist-import").hide();
  1316. }
  1317. });
  1318. Template.room.helpers({
  1319. singleVideo: function() {
  1320. return true;
  1321. },
  1322. chat: function() {
  1323. Meteor.setTimeout(function() {
  1324. var elem = document.getElementById('chat');
  1325. if (elem !== undefined && elem !== null) {
  1326. elem.scrollTop = elem.scrollHeight;
  1327. }
  1328. }, 100);
  1329. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1330. },
  1331. globalChat: function() {
  1332. Meteor.setTimeout(function() {
  1333. var elem = document.getElementById('global-chat');
  1334. if (elem !== undefined && elem !== null) {
  1335. elem.scrollTop = elem.scrollHeight;
  1336. }
  1337. }, 100);
  1338. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1339. },
  1340. likes: function() {
  1341. var playlist = Playlists.findOne({type: Session.get("type")});
  1342. var likes = 0;
  1343. playlist.songs.forEach(function(song) {
  1344. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1345. likes = song.likes;
  1346. return;
  1347. }
  1348. });
  1349. return likes;
  1350. },
  1351. dislikes: function() {
  1352. var playlist = Playlists.findOne({type: Session.get("type")});
  1353. var dislikes = 0;
  1354. playlist.songs.forEach(function(song) {
  1355. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1356. dislikes = song.dislikes;
  1357. return;
  1358. }
  1359. });
  1360. return dislikes;
  1361. },
  1362. liked: function() {
  1363. if (Meteor.userId()) {
  1364. var currentSong = Session.get("currentSong");
  1365. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  1366. return "active";
  1367. } else {
  1368. return "";
  1369. }
  1370. } else {
  1371. "";
  1372. }
  1373. },
  1374. disliked: function() {
  1375. if (Meteor.userId()) {
  1376. var currentSong = Session.get("currentSong");
  1377. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  1378. return "active";
  1379. } else {
  1380. return "";
  1381. }
  1382. } else {
  1383. "";
  1384. }
  1385. },
  1386. type: function() {
  1387. var parts = location.href.split('/');
  1388. var id = parts.pop().toLowerCase();
  1389. return Rooms.findOne({type: id}).display;
  1390. },
  1391. users: function() {
  1392. var parts = location.href.split('/');
  1393. var id = parts.pop().toLowerCase();
  1394. return Rooms.findOne({type: id}).users;
  1395. },
  1396. title: function(){
  1397. return Session.get("title");
  1398. },
  1399. artist: function(){
  1400. return Session.get("artist");
  1401. },
  1402. loaded: function() {
  1403. return Session.get("loaded");
  1404. },
  1405. paused: function() {
  1406. return Session.get("state") === "paused";
  1407. },
  1408. private: function() {
  1409. return Rooms.findOne({type: Session.get("type")}).private === true;
  1410. },
  1411. report: function() {
  1412. return Session.get("reportObj");
  1413. },
  1414. reportSong: function() {
  1415. return Session.get("reportSong");
  1416. },
  1417. reportTitle: function() {
  1418. return Session.get("reportTitle");
  1419. },
  1420. reportAuthor: function() {
  1421. return Session.get("reportAuthor");
  1422. },
  1423. reportDuration: function() {
  1424. return Session.get("reportDuration");
  1425. },
  1426. reportAudio: function() {
  1427. return Session.get("reportAudio");
  1428. },
  1429. reportAlbumart: function() {
  1430. return Session.get("reportAlbumart");
  1431. },
  1432. reportOther: function() {
  1433. return Session.get("reportOther");
  1434. },
  1435. currentSong: function() {
  1436. return Session.get("currentSong");
  1437. },
  1438. previousSong: function() {
  1439. return Session.get("previousSong");
  1440. },
  1441. currentSongR: function() {
  1442. return Session.get("currentSongR");
  1443. },
  1444. previousSongR: function() {
  1445. return Session.get("previousSongR");
  1446. },
  1447. reportingSong: function() {
  1448. if (Session.get("reportPrevious")) {
  1449. return Session.get("previousSongR");
  1450. } else {
  1451. return Session.get("currentSongR");
  1452. }
  1453. },
  1454. votes: function(){
  1455. return Rooms.findOne({type: Session.get("type")}).votes;
  1456. }
  1457. });
  1458. Template.room.onCreated(function () {
  1459. Chat.after.find(function(userId, selector) {
  1460. if (selector.type === "global") {
  1461. if (!$("#global-chat-tab").hasClass("active")) {
  1462. $("#global-chat-tab").addClass("unread-messages");
  1463. }
  1464. } else if(selector.type === Session.get("type")) {
  1465. if (!$("#chat-tab").hasClass("active")) {
  1466. $("#chat-tab").addClass("unread-messages");
  1467. }
  1468. }
  1469. });
  1470. Session.set("reportSong", false);
  1471. Session.set("reportTitle", false);
  1472. Session.set("reportAuthor", false);
  1473. Session.set("reportDuration", false);
  1474. Session.set("reportAudio", false);
  1475. Session.set("reportAlbumart", false);
  1476. Session.set("reportOther", false);
  1477. /*if (resizeSeekerbarInterval !== undefined) {
  1478. Meteor.clearInterval(resizeSeekerbarInterval);
  1479. resizeSeekerbarInterval = undefined;
  1480. }*/
  1481. YTPlayer = undefined;
  1482. Session.set("videoHidden", false);
  1483. var tag = document.createElement("script");
  1484. tag.src = "https://www.youtube.com/iframe_api";
  1485. var firstScriptTag = document.getElementsByTagName('script')[0];
  1486. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1487. Session.set("singleVideo", true);
  1488. var currentSong = undefined;
  1489. var currentSongR = undefined;
  1490. function getTimeElapsed() {
  1491. if (currentSong !== undefined) {
  1492. var room = Rooms.findOne({type: type});
  1493. if (room !== undefined) {
  1494. return Date.now() - currentSong.started - room.timePaused;
  1495. }
  1496. }
  1497. return 0;
  1498. }
  1499. function getSongInfo(songData){
  1500. Session.set("title", songData.title);
  1501. Session.set("artist", songData.artist);
  1502. Session.set("id", songData.id);
  1503. $("#song-img").attr("src", songData.img);
  1504. Session.set("duration", parseInt(songData.duration));
  1505. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1506. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1507. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1508. }
  1509. function resizeSeekerbar() {
  1510. if (Session.get("state") === "playing") {
  1511. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1512. }
  1513. }
  1514. function startSong() {
  1515. $("#time-elapsed").text("0:00");
  1516. $("#vote-skip").attr("disabled", false);
  1517. if (currentSong !== undefined) {
  1518. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  1519. var volume = localStorage.getItem("volume") || 20;
  1520. if ($("#player").length !== 1) {
  1521. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1522. }
  1523. $("#player").show();
  1524. function loadVideo() {
  1525. if (!Session.get("YTLoaded")) {
  1526. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  1527. loadVideo();
  1528. }, 500));
  1529. } else {
  1530. if (YTPlayer === undefined) {
  1531. YTPlayer = new YT.Player("player", {
  1532. height: 540,
  1533. width: 960,
  1534. videoId: currentSong.id,
  1535. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1536. events: {
  1537. 'onReady': function (event) {
  1538. if (currentSong.skipDuration === undefined) {
  1539. currentSong.skipDuration = 0;
  1540. }
  1541. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1542. event.target.playVideo();
  1543. event.target.setVolume(volume);
  1544. resizeSeekerbar();
  1545. },
  1546. 'onStateChange': function (event) {
  1547. if (Session.get("YTLoaded")) {
  1548. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1549. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1550. event.target.playVideo();
  1551. }
  1552. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1553. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1554. event.target.pauseVideo();
  1555. }
  1556. }
  1557. }
  1558. }
  1559. });
  1560. } else {
  1561. YTPlayer.loadVideoById(currentSong.id);
  1562. if (currentSong.skipDuration === undefined) {
  1563. currentSong.skipDuration = 0;
  1564. }
  1565. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1566. }
  1567. Session.set("pauseVideo", false);
  1568. getSongInfo(currentSong);
  1569. }
  1570. }
  1571. loadVideo();
  1572. }
  1573. }
  1574. Session.set("loaded", false);
  1575. Meteor.subscribe("rooms", function() {
  1576. var parts = location.href.split('/');
  1577. var id = parts.pop();
  1578. var type = id.toLowerCase();
  1579. Session.set("type", type);
  1580. if (Rooms.find({type: type}).count() !== 1) {
  1581. window.location = "/";
  1582. } else {
  1583. StationSubscription = Meteor.subscribe(type);
  1584. Session.set("loaded", true);
  1585. minterval = Meteor.setInterval(function () {
  1586. var room = Rooms.findOne({type: type});
  1587. if (room !== undefined) {
  1588. if (room.state === "paused" || Session.get("pauseVideo")) {
  1589. Session.set("state", "paused");
  1590. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  1591. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  1592. YTPlayer.pauseVideo();
  1593. }
  1594. } else {
  1595. Session.set("state", "playing");
  1596. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  1597. YTPlayer.playVideo();
  1598. }
  1599. }
  1600. }
  1601. if (room.currentSong.song !== undefined && (currentSongR === undefined || room.currentSong.started !== currentSongR.started)) {
  1602. Session.set("previousSong", currentSong);
  1603. currentSongR = room.currentSong;
  1604. currentSong = room.currentSong.song;
  1605. currentSong.started = room.currentSong.started;
  1606. Session.set("currentSong", currentSong);
  1607. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  1608. startSong();
  1609. }
  1610. if (currentSong !== undefined) {
  1611. if (room !== undefined) {
  1612. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1613. var song_duration = currentSong.duration;
  1614. if (song_duration <= duration) {
  1615. Session.set("pauseVideo", true);
  1616. }
  1617. var d = moment.duration(duration, 'seconds');
  1618. if (Session.get("state") === "playing") {
  1619. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1620. }
  1621. }
  1622. }
  1623. }, 100);
  1624. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1625. resizeSeekerbar();
  1626. }, 500);
  1627. }
  1628. });
  1629. });
  1630. // Settings Template
  1631. Template.settings.events({
  1632. "click #save-settings": function() {
  1633. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  1634. },
  1635. "click #delete-account": function(){
  1636. $("#delete-account").text("Click to confirm");
  1637. $("#delete-account").click(function(){
  1638. var bool = confirm("Are you sure you want to delete your account?");
  1639. if(bool) {
  1640. Meteor.call("deleteAccount");
  1641. } else{
  1642. $("#delete-account").text("Delete");
  1643. }
  1644. })
  1645. },
  1646. "click #change-password": function(){
  1647. var oldPassword = $("#old-password").val();
  1648. var newPassword= $("#new-password").val();
  1649. var confirmPassword = $("#confirm-password").val();
  1650. if(newPassword === confirmPassword){
  1651. Accounts.changePassword(oldPassword, newPassword, function(err){
  1652. if(err){
  1653. $("#old-password").val("");
  1654. $("#new-password").val("");
  1655. $("#confirm-password").val("");
  1656. $("<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(); });
  1657. } else {
  1658. $("#old-password").val("");
  1659. $("#new-password").val("");
  1660. $("#confirm-password").val("");
  1661. $("<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(); });
  1662. }
  1663. });
  1664. }
  1665. }
  1666. });
  1667. var previewEndSongTimeout = undefined;