2
0

events.js 59 KB

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