events.js 71 KB

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