events.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. var feedbackData;
  2. function getSpotifyInfo(title, cb, artist) {
  3. var q = "";
  4. q = title;
  5. if (artist !== undefined) {
  6. q += " artist:" + artist;
  7. }
  8. $.ajax({
  9. type: "GET",
  10. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  11. applicationType: "application/json",
  12. contentType: "json",
  13. success: function (data) {
  14. cb(data);
  15. }
  16. });
  17. }
  18. function executeCommand(command, params){
  19. if (command === "help" || command === "commands") {
  20. $('#helpModal').modal('show');
  21. return true;
  22. } else if (command === "volume") {
  23. if (params.length === 1) {
  24. var volume = Number(params[0]);
  25. if (volume >= 0 || volume <= 100) {
  26. if (volume === 0) {
  27. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  28. } else {
  29. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  30. }
  31. $("#volume-slider").slider("setValue", volume);
  32. if (YTPlayer !== undefined) {
  33. YTPlayer.setVolume(volume);
  34. localStorage.setItem("volume", volume);
  35. }
  36. return true;
  37. }
  38. }
  39. } else if(command === "mute"){
  40. $("#volume-slider").slider("setValue", 0);
  41. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off");
  42. if (YTPlayer !== undefined) {
  43. YTPlayer.setVolume(0);
  44. localStorage.setItem("volume", 0);
  45. }
  46. } else if(command === "ban"){
  47. var user = params[0];
  48. var time = params[1];
  49. var reason = params[2];
  50. Meteor.call("banUser", user, time, reason, function(err, res){
  51. if(err){
  52. console.log(err);
  53. }
  54. });
  55. } else if(command === "silence"){
  56. var user = params[0];
  57. var time = params[1];
  58. Meteor.call("muteUser", user, time, function(err, res){
  59. if(err){
  60. console.log(err);
  61. }
  62. });
  63. } else if(command === "unban"){
  64. var user = params[0];
  65. Meteor.call("unbanUser", user, function(err, res){
  66. if(err){
  67. console.log(err);
  68. }
  69. });
  70. } else if(command === "unsilence"){
  71. var user = params[0];
  72. Meteor.call("unsilenceUser", user, function(err, res){
  73. if(err){
  74. console.log(err);
  75. }
  76. });
  77. } else if(command === "pause"){
  78. Meteor.call("pauseRoom", Session.get("type"), function(err, res){
  79. if(err){
  80. console.log(err);
  81. }
  82. });
  83. } else if(command === "resume"){
  84. Meteor.call("resumeRoom", Session.get("type"), function(err, res){
  85. if(err){
  86. console.log(err);
  87. }
  88. });
  89. } else if(command === "shuffle"){
  90. Meteor.call("shufflePlaylist", Session.get("type"), function(err, res){
  91. if(err){
  92. console.log(err);
  93. }
  94. });
  95. } else if(command === "skip"){
  96. Meteor.call("skipSong", Session.get("type"), function(err, res){
  97. if(err){
  98. console.log(err);
  99. }
  100. });
  101. }
  102. }
  103. function sendMessage() {
  104. var message = $("#chat-input").val();
  105. if (!$("#chat-input").hasClass("disabled")) {
  106. if (message.length > 0 && message[0] !== " ") {
  107. if (message[0] === "/") {
  108. message = message.split("");
  109. message.shift();
  110. message = message.join("");
  111. var params = message.split(" ");
  112. params = params.map(function(param) {
  113. return param.replace(/\r?\n|\r/g, "");
  114. });
  115. var command = params.shift();
  116. command = command.replace(/\r?\n|\r/g, "");
  117. if (executeCommand(command, params)) {
  118. $("#chat-input").val("");
  119. } else {
  120. $("#chat-input").val("");
  121. }
  122. } else {
  123. $("#chat-input").addClass("disabled");
  124. $("#chat-input").attr("disabled", "");
  125. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  126. if(err){
  127. $("#chat-input").val("");
  128. $("#chat-input").removeAttr("disabled");
  129. $("#chat-input").removeClass("disabled");
  130. }
  131. if (res) {
  132. $("#chat-input").val("");
  133. $("#chat-input").removeAttr("disabled");
  134. $("#chat-input").removeClass("disabled");
  135. }
  136. });
  137. }
  138. }
  139. }
  140. }
  141. function sendMessageGlobal() {
  142. var message = $("#global-chat-input").val();
  143. if (!$("#global-chat-input").hasClass("disabled")) {
  144. if (message.length > 0 && message[0] !== " ") {
  145. if (message[0] === "/") {
  146. message = message.split("");
  147. message.shift();
  148. message = message.join("");
  149. var params = message.split(" ");
  150. var command = params.shift();
  151. command = command.replace(/\r?\n|\r/g, "");
  152. if (executeCommand(command, params)) {
  153. $("#global-chat-input").val("");
  154. } else {
  155. $("#global-chat-input").val("");
  156. }
  157. } else {
  158. $("#global-chat-input").addClass("disabled");
  159. $("#global-chat-input").attr("disabled", "");
  160. Meteor.call("sendMessage", "global", message, function (err, res) {
  161. if (res) {
  162. $("#global-chat-input").val("");
  163. }
  164. $("#global-chat-input").removeClass("disabled");
  165. $("#global-chat-input").removeAttr("disabled");
  166. });
  167. }
  168. }
  169. }
  170. }
  171. Template.admin.events({
  172. "click a": function(e){
  173. var id = e.currentTarget.id;
  174. console.log(id.toLowerCase());
  175. Session.set("playlistToEdit", id);
  176. },
  177. "click #croom_create": function() {
  178. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#croom_private").prop("checked"), function (err, res) {
  179. if (err) {
  180. alert("Error " + err.error + ": " + err.reason);
  181. } else {
  182. window.location = "/" + $("#croom_tag").val();
  183. }
  184. });
  185. },
  186. "click #rreset_confirm": function(){
  187. $('#confirmModal').modal('hide');
  188. Meteor.call("resetRating");
  189. },
  190. "click #edit_desc": function(){
  191. console.log($(this));
  192. console.log($(this)[0].type);
  193. Session.set("roomDesc", $(this)[0].type);
  194. $("#desc_text").val(Rooms.findOne({type: Session.get("roomDesc")}).roomDesc);
  195. },
  196. "click #submit_desc": function(){
  197. var description = $("#desc_text").val();
  198. Meteor.call("editRoomDesc", Session.get("roomDesc"), description);
  199. $("#desc-modal").closeModal();
  200. }
  201. });
  202. Template.alertsDashboard.events({
  203. "click #calart-create": function() {
  204. Meteor.call("addAlert", $("#calert-description").val(), $("#calert-priority").val().toLowerCase(), function (err, res) {
  205. if (err) {
  206. alert("Error " + err.error + ": " + err.reason);
  207. } else {
  208. $("#calert-description").val("");
  209. }
  210. });
  211. },
  212. "click #ralert-button": function() {
  213. Meteor.call("removeAlerts");
  214. }
  215. });
  216. Template.feedback.events({
  217. "click #feedback_submit": function(){
  218. if($("#feedback_message").val().length !== 0){
  219. Meteor.call("sendFeedback", $("#feedback_message").val());
  220. $("#feedback_message").val("");
  221. $("#modal1").closeModal()
  222. }
  223. },
  224. "click .upvote": function(){
  225. var message = $(this).parent("card").prevObject[0].message;
  226. Meteor.call("upvoteFeedback", message);
  227. },
  228. "click #delete": function(){
  229. var message = $(this).parent("card").prevObject[0].message;
  230. Meteor.call("deleteFeedback", message);
  231. },
  232. "click #edit": function(){
  233. $("#editModal").click()
  234. var data = Feedback.findOne({"message": $(this).parent("card").prevObject[0].message});
  235. feedbackData = data.message;
  236. $("#edit_feedback_message").val(data.message);
  237. },
  238. "click #edit_feedback_submit": function(){
  239. var oldMessage = feedbackData;
  240. var newMessage = $("#edit_feedback_message").val()
  241. $("#edit_feedback_message").val("")
  242. Meteor.call("updateFeedback", oldMessage, newMessage);
  243. $("#editFeedback").closeModal();
  244. }
  245. });
  246. Template.header.events({
  247. "click .logout": function(e){
  248. e.preventDefault();
  249. Meteor.logout();
  250. if (hpSound !== undefined) {
  251. hpSound.stop();
  252. }
  253. },
  254. "click #profile": function(){
  255. window.location = "/u/" + Meteor.user().profile.username;
  256. }
  257. });
  258. Template.login.events({
  259. "submit form": function(e){
  260. e.preventDefault();
  261. Session.set("github", false);
  262. var username = $("#username").val()
  263. var password = $("#password").val();
  264. Meteor.loginWithPassword(username, password, function(err) {
  265. if (err) {
  266. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  267. $(".landing").before(errAlert);
  268. Meteor.setTimeout(function() {
  269. errAlert.fadeOut(5000, function() {
  270. errAlert.remove();
  271. });
  272. }, 5000);
  273. } else {
  274. window.location.href = "/";
  275. }
  276. });
  277. },
  278. "click #github-login": function(){
  279. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  280. console.log(err, res);
  281. });
  282. }
  283. });
  284. Template.playlist.events({
  285. "keyup #search-playlist": function(){
  286. if($("#search-playlist").val().length === 0){
  287. $(".pl-item").show();
  288. } else {
  289. $(".pl-item").hide();
  290. var input = $("#search-playlist").val().toLowerCase();
  291. $(".pl-item strong").each(function(i, el){
  292. if($(el).text().toLowerCase().indexOf(input) !== -1){
  293. $(el).parent(".pl-item").show();
  294. }
  295. })
  296. $(".pl-item #pl-artist").each(function(i, el){
  297. if($(el).text().toLowerCase().indexOf(input) !== -1){
  298. $(el).parent(".pl-item").show();
  299. }
  300. })
  301. }
  302. },
  303. "click #pl-item": function(){
  304. console.log($(this).text());
  305. }
  306. });
  307. Template.profile.events({
  308. //Edit real name
  309. "click #edit-name": function(){
  310. $("#name").hide();
  311. $("#name-div").show();
  312. $("#edit-name").hide();
  313. $("#cancel-edit").show();
  314. },
  315. "click #submit-name": function(){
  316. var user = Meteor.user();
  317. $("#name").show();
  318. $("#name-div").hide();
  319. $("#edit-name").show();
  320. $("#cancel-edit").hide();
  321. var realname = $("#input-name").val();
  322. var username = user.profile.username;
  323. $("#name").text("Name: " + realname);
  324. $("#input-name").val("")
  325. Meteor.call("updateRealName", realname);
  326. },
  327. "click #cancel-edit": function(){
  328. $("#name").show();
  329. $("#name-div").hide();
  330. $("#edit-name").show();
  331. $("#cancel-edit").hide();
  332. $("#input-name").val("");
  333. },
  334. //Edit username
  335. "click #edit-username": function(){
  336. $("#username").hide();
  337. $("#username-div").show();
  338. $("#edit-username").hide();
  339. $("#cancel-username").show();
  340. },
  341. "click #submit-username": function(){
  342. var user = Meteor.user()
  343. $("#username").show();
  344. $("#username-div").hide();
  345. $("#edit-username").show();
  346. $("#cancel-username").hide();
  347. var username = user.username;
  348. var newUserName = $("#input-username").val();
  349. $("#profile-name").text(newUserName)
  350. $("#username").text("Username: " + newUserName);
  351. $("#input-username").val("")
  352. Meteor.call("updateUserName", newUserName);
  353. window.location = "/u/" + newUserName;
  354. },
  355. "click #cancel-username": function(){
  356. $("#username").show();
  357. $("#username-div").hide();
  358. $("#edit-username").show();
  359. $("#cancel-username").hide();
  360. $("#input-username").val("");
  361. },
  362. // Admins only Edit Rank
  363. "click #edit-rank": function() {
  364. $("#rank").hide();
  365. $("#rank-div").show();
  366. $("#edit-rank").hide();
  367. $("#cancel-rank").show();
  368. },
  369. "click #submit-rank": function() {
  370. $("#rank").show();
  371. $("#rank-div").hide();
  372. $("#edit-rank").show();
  373. $("#cancel-rank").hide();
  374. var newRank = $("#select-rank option:selected").val();
  375. var username = Session.get("username");
  376. console.log(username, newRank);
  377. },
  378. "click #cancel-rank": function() {
  379. $("#rank").show();
  380. $("#rank-div").hide();
  381. $("#edit-rank").show();
  382. $("#cancel-rank").hide();
  383. }
  384. });
  385. var seekerBarInterval = undefined;
  386. Template.queues.events({
  387. /* TODO Add undo delete button */
  388. "input #id": function() {
  389. console.log("Change!");
  390. $("#previewPlayerContainer").addClass("hide-preview");
  391. },
  392. "input #img": function() {
  393. var url = $("#img").val();
  394. console.log(url);
  395. Session.set("image_url", url);
  396. },
  397. "click .preview-button": function(e){
  398. Session.set("song", this);
  399. $("#previewModal").openModal();
  400. },
  401. "click #previewImageButton": function() {
  402. $("#preview-image").attr("src", Session.get("song").img);
  403. },
  404. "click .edit-queue-button": function(e){
  405. Session.set("song", this);
  406. Session.set("genre", $(e.target).data("genre"));
  407. Session.set("type", "queue");
  408. $("#mid").val(this.mid).change();
  409. $("#artist").val(this.artist).change();
  410. $("#title").val(this.title).change();
  411. $("#img").val(this.img).change();
  412. $("#id").val(this.id).change();
  413. $("#likes").val(this.likes).change();
  414. $("#dislikes").val(this.dislikes).change();
  415. $("#duration").val(this.duration).change();
  416. $("#skip-duration").val(this.skipDuration).change();
  417. $("#previewPlayerContainer").addClass("hide-preview");
  418. Session.set("image_url", this.img);
  419. Session.set("editing", true);
  420. $("#editModal").openModal({
  421. complete : function() {
  422. Session.set("editing", false);
  423. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  424. YTPlayer.stopVideo();
  425. }
  426. }
  427. });
  428. },
  429. "click .add-song-button": function(e){
  430. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  431. Meteor.call("addSongToPlaylist", this, function(err) {
  432. console.log(err);
  433. if (err) {
  434. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  435. Materialize.toast($toastContent, 8000);
  436. }
  437. });
  438. },
  439. "click .deny-song-button": function(e){
  440. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  441. Meteor.call("removeSongFromQueue", this.mid);
  442. },
  443. "click #play": function() {
  444. var duration = Session.get("song").duration;
  445. var d = moment.duration(parseInt(duration), 'seconds');
  446. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  447. $("#previewPlayerContainer").removeClass("hide-preview");
  448. var song = Session.get("song");
  449. var id = song.id;
  450. var volume = localStorage.getItem("volume") || 20;
  451. if (song.duration !== 0) {
  452. $("#play").attr("disabled", true);
  453. $("#stop").attr("disabled", false);
  454. $("#pause").attr("disabled", false);
  455. $("#forward").attr("disabled", false);
  456. if (YTPlayer === undefined) {
  457. YTPlayer = new YT.Player("previewPlayer", {
  458. height: 540,
  459. width: 568,
  460. videoId: id,
  461. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  462. events: {
  463. 'onReady': function(event) {
  464. event.target.seekTo(Number(song.skipDuration));
  465. event.target.playVideo();
  466. event.target.setVolume(volume);
  467. },
  468. 'onStateChange': function(event){
  469. if (event.data == YT.PlayerState.PAUSED) {
  470. if (seekerBarInterval !== undefined) {
  471. Meteor.clearInterval(seekerBarInterval);
  472. seekerBarInterval = undefined;
  473. }
  474. }
  475. if (event.data == YT.PlayerState.UNSTARTED) {
  476. if (seekerBarInterval !== undefined) {
  477. Meteor.clearInterval(seekerBarInterval);
  478. seekerBarInterval = undefined;
  479. }
  480. $(".seeker-bar").css({width: "0"});
  481. $("#time-elapsed").text("0:00");
  482. $("#previewPlayerContainer").addClass("hide-preview");
  483. }
  484. if (event.data == YT.PlayerState.PLAYING) {
  485. seekerBarInterval = Meteor.setInterval(function() {
  486. var duration = Session.get("song").duration;
  487. var timeElapsed = YTPlayer.getCurrentTime();
  488. var skipDuration = Session.get("song").skipDuration;
  489. if (duration <= (timeElapsed - skipDuration)) {
  490. YTPlayer.stopVideo();
  491. $("#play").attr("disabled", false);
  492. $("#stop").attr("disabled", true);
  493. $("#pause").attr("disabled", true);
  494. $("#forward").attr("disabled", true);
  495. $("#previewPlayerContainer").addClass("hide-preview");
  496. $(".seeker-bar").css({width: "0"});
  497. $("#time-elapsed").text("0:00");
  498. Meteor.clearInterval(seekerBarInterval);
  499. } else {
  500. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  501. $(".seeker-bar").css({width: percentComplete + "%"});
  502. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  503. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  504. }
  505. }, 100);
  506. $("#play").attr("disabled", true);
  507. $("#stop").attr("disabled", false);
  508. $("#pause").attr("disabled", false);
  509. $("#forward").attr("disabled", false);
  510. } else {
  511. $("#play").attr("disabled", false);
  512. $("#stop").attr("disabled", true);
  513. $("#pause").attr("disabled", true);
  514. $("#forward").attr("disabled", true);
  515. }
  516. }
  517. }
  518. });
  519. } else {
  520. if (YTPlayer.getPlayerState() === 2) {
  521. YTPlayer.playVideo();
  522. } else {
  523. console.log(id, song.skipDuration, song.duration);
  524. YTPlayer.loadVideoById(id);
  525. YTPlayer.seekTo(Number(song.skipDuration));
  526. }
  527. }
  528. $("#previewPlayerContainer").removeClass("hide-preview");
  529. }
  530. },
  531. "click #stop": function() {
  532. $("#play").attr("disabled", false);
  533. $("#stop").attr("disabled", true);
  534. $("#pause").attr("disabled", true);
  535. $("#forward").attr("disabled", true);
  536. if (previewEndSongTimeout !== undefined) {
  537. Meteor.clearTimeout(previewEndSongTimeout);
  538. }
  539. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  540. YTPlayer.stopVideo();
  541. }
  542. },
  543. "click #pause": function() {
  544. $("#play").attr("disabled", false);
  545. $("#stop").attr("disabled", false);
  546. $("#pause").attr("disabled", true);
  547. $("#forward").attr("disabled", true);
  548. if (previewEndSongTimeout !== undefined) {
  549. Meteor.clearTimeout(previewEndSongTimeout);
  550. }
  551. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  552. YTPlayer.pauseVideo();
  553. }
  554. },
  555. "click #forward": function() {
  556. var error = false;
  557. if (YTPlayer !== undefined) {
  558. var duration = Number(Session.get("song").duration) | 0;
  559. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  560. if (YTPlayer.getDuration() < duration + skipDuration) {
  561. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  562. Materialize.toast($toastContent, 8000);
  563. error = true;
  564. } else {
  565. YTPlayer.seekTo(skipDuration + duration - 10);
  566. }
  567. }
  568. if (!error) {
  569. if (previewEndSongTimeout !== undefined) {
  570. Meteor.clearTimeout(previewEndSongTimeout);
  571. }
  572. previewEndSongTimeout = Meteor.setTimeout(function() {
  573. if (YTPlayer !== undefined) {
  574. YTPlayer.stopVideo();
  575. }
  576. $("#play").attr("disabled", false);
  577. $("#stop").attr("disabled", true);
  578. $("#pause").attr("disabled", true);
  579. $("#forward").attr("disabled", true);
  580. $("#previewPlayerContainer").addClass("hide-preview");
  581. }, 10000);
  582. }
  583. },
  584. "click #get-spotify-info": function() {
  585. var search = $("#title").val();
  586. var artistName = $("#artist").val();
  587. getSpotifyInfo(search, function(data) {
  588. for(var i in data){
  589. for(var j in data[i].items){
  590. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  591. $("#img").val(data[i].items[j].album.images[2].url).change();
  592. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  593. return;
  594. }
  595. }
  596. }
  597. }, artistName);
  598. },
  599. "click #save-song-button": function() {
  600. var newSong = {};
  601. newSong.mid = $("#mid").val();
  602. newSong.id = $("#id").val();
  603. newSong.likes = Number($("#likes").val());
  604. newSong.dislikes = Number($("#dislikes").val());
  605. newSong.title = $("#title").val();
  606. newSong.artist = $("#artist").val();
  607. newSong.img = $("#img").val();
  608. newSong.duration = Number($("#duration").val());
  609. newSong.skipDuration = $("#skip-duration").val();
  610. newSong.requestedBy = Session.get("song").requestedBy;
  611. newSong.genres = Session.get("song").genres;
  612. if(newSong.skipDuration === undefined){
  613. newSong.skipDuration = 0;
  614. }
  615. Meteor.call("updateQueueSong", newSong.mid, newSong, function(err, res) {
  616. if (err) {
  617. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  618. Materialize.toast($toastContent, 8000);
  619. } else {
  620. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  621. Materialize.toast($toastContent, 4000);
  622. Session.set("song", newSong);
  623. }
  624. });
  625. }
  626. });
  627. Template.manageStation.events({
  628. /* TODO Add undo delete button */
  629. "input #id": function() {
  630. $("#previewPlayerContainer").addClass("hide-preview");
  631. },
  632. "input #img": function() {
  633. var url = $("#img").val();
  634. Session.set("image_url", url);
  635. },
  636. "click .preview-button": function(e){
  637. Session.set("song", this);
  638. $("#previewModal").openModal();
  639. },
  640. "click #previewImageButton": function() {
  641. $("#preview-image").attr("src", Session.get("song").img);
  642. },
  643. "click .edit-song-button": function(e){
  644. Session.set("song", this);
  645. Session.set("genre", $(e.target).data("genre"));
  646. $("#mid").val(this.mid).change();
  647. $("#artist").val(this.artist).change();
  648. $("#title").val(this.title).change();
  649. $("#img").val(this.img).change();
  650. $("#id").val(this.id).change();
  651. $("#likes").val(this.likes).change();
  652. $("#dislikes").val(this.dislikes).change();
  653. $("#duration").val(this.duration).change();
  654. $("#skip-duration").val(this.skipDuration).change();
  655. $("#previewPlayerContainer").addClass("hide-preview");
  656. Session.set("image_url", this.img);
  657. Session.set("editing", true);
  658. $("#editModal").openModal({
  659. complete : function() {
  660. Session.set("editing", false);
  661. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  662. YTPlayer.stopVideo();
  663. }
  664. }
  665. });
  666. },
  667. "click .remove-song-button": function(e){
  668. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  669. Meteor.call("removeSongFromPlaylist", this.mid);
  670. },
  671. "click #play": function() {
  672. var duration = Session.get("song").duration;
  673. var d = moment.duration(parseInt(duration), 'seconds');
  674. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  675. $("#previewPlayerContainer").removeClass("hide-preview");
  676. var song = Session.get("song");
  677. var id = song.id;
  678. var volume = localStorage.getItem("volume") || 20;
  679. if (song.duration !== 0) {
  680. $("#play").attr("disabled", true);
  681. $("#stop").attr("disabled", false);
  682. $("#pause").attr("disabled", false);
  683. $("#forward").attr("disabled", false);
  684. if (YTPlayer === undefined) {
  685. YTPlayer = new YT.Player("previewPlayer", {
  686. height: 540,
  687. width: 568,
  688. videoId: id,
  689. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  690. events: {
  691. 'onReady': function(event) {
  692. event.target.seekTo(Number(song.skipDuration));
  693. event.target.playVideo();
  694. event.target.setVolume(volume);
  695. },
  696. 'onStateChange': function(event){
  697. if (event.data == YT.PlayerState.PAUSED) {
  698. if (seekerBarInterval !== undefined) {
  699. Meteor.clearInterval(seekerBarInterval);
  700. seekerBarInterval = undefined;
  701. }
  702. }
  703. if (event.data == YT.PlayerState.UNSTARTED) {
  704. if (seekerBarInterval !== undefined) {
  705. Meteor.clearInterval(seekerBarInterval);
  706. seekerBarInterval = undefined;
  707. }
  708. $(".seeker-bar").css({width: "0"});
  709. $("#time-elapsed").text("0:00");
  710. $("#previewPlayerContainer").addClass("hide-preview");
  711. }
  712. if (event.data == YT.PlayerState.PLAYING) {
  713. seekerBarInterval = Meteor.setInterval(function() {
  714. var duration = Session.get("song").duration;
  715. var timeElapsed = YTPlayer.getCurrentTime();
  716. var skipDuration = Session.get("song").skipDuration;
  717. if (duration <= (timeElapsed - skipDuration)) {
  718. YTPlayer.stopVideo();
  719. $("#play").attr("disabled", false);
  720. $("#stop").attr("disabled", true);
  721. $("#pause").attr("disabled", true);
  722. $("#forward").attr("disabled", true);
  723. $("#previewPlayerContainer").addClass("hide-preview");
  724. $(".seeker-bar").css({width: "0"});
  725. $("#time-elapsed").text("0:00");
  726. Meteor.clearInterval(seekerBarInterval);
  727. } else {
  728. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  729. $(".seeker-bar").css({width: percentComplete + "%"});
  730. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  731. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  732. }
  733. }, 100);
  734. $("#play").attr("disabled", true);
  735. $("#stop").attr("disabled", false);
  736. $("#pause").attr("disabled", false);
  737. $("#forward").attr("disabled", false);
  738. } else {
  739. $("#play").attr("disabled", false);
  740. $("#stop").attr("disabled", true);
  741. $("#pause").attr("disabled", true);
  742. $("#forward").attr("disabled", true);
  743. }
  744. }
  745. }
  746. });
  747. } else {
  748. if (YTPlayer.getPlayerState() === 2) {
  749. YTPlayer.playVideo();
  750. } else {
  751. console.log(id, song.skipDuration, song.duration);
  752. YTPlayer.loadVideoById(id);
  753. YTPlayer.seekTo(Number(song.skipDuration));
  754. }
  755. }
  756. $("#previewPlayerContainer").removeClass("hide-preview");
  757. }
  758. },
  759. "click #stop": function() {
  760. $("#play").attr("disabled", false);
  761. $("#stop").attr("disabled", true);
  762. $("#pause").attr("disabled", true);
  763. $("#forward").attr("disabled", true);
  764. if (previewEndSongTimeout !== undefined) {
  765. Meteor.clearTimeout(previewEndSongTimeout);
  766. }
  767. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  768. YTPlayer.stopVideo();
  769. }
  770. },
  771. "click #pause": function() {
  772. $("#play").attr("disabled", false);
  773. $("#stop").attr("disabled", false);
  774. $("#pause").attr("disabled", true);
  775. $("#forward").attr("disabled", true);
  776. if (previewEndSongTimeout !== undefined) {
  777. Meteor.clearTimeout(previewEndSongTimeout);
  778. }
  779. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  780. YTPlayer.pauseVideo();
  781. }
  782. },
  783. "click #forward": function() {
  784. var error = false;
  785. if (YTPlayer !== undefined) {
  786. var duration = Number(Session.get("song").duration) | 0;
  787. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  788. if (YTPlayer.getDuration() < duration + skipDuration) {
  789. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  790. Materialize.toast($toastContent, 8000);
  791. error = true;
  792. } else {
  793. YTPlayer.seekTo(skipDuration + duration - 10);
  794. }
  795. }
  796. if (!error) {
  797. if (previewEndSongTimeout !== undefined) {
  798. Meteor.clearTimeout(previewEndSongTimeout);
  799. }
  800. previewEndSongTimeout = Meteor.setTimeout(function() {
  801. if (YTPlayer !== undefined) {
  802. YTPlayer.stopVideo();
  803. }
  804. $("#play").attr("disabled", false);
  805. $("#stop").attr("disabled", true);
  806. $("#pause").attr("disabled", true);
  807. $("#forward").attr("disabled", true);
  808. $("#previewPlayerContainer").addClass("hide-preview");
  809. }, 10000);
  810. }
  811. },
  812. "click #get-spotify-info": function() {
  813. var search = $("#title").val();
  814. var artistName = $("#artist").val();
  815. getSpotifyInfo(search, function(data) {
  816. for(var i in data){
  817. for(var j in data[i].items){
  818. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  819. $("#img").val(data[i].items[j].album.images[2].url).change();
  820. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  821. return;
  822. }
  823. }
  824. }
  825. }, artistName);
  826. },
  827. "click #save-song-button": function() {
  828. var newSong = {};
  829. newSong.mid = $("#mid").val();
  830. newSong.id = $("#id").val();
  831. newSong.likes = Number($("#likes").val());
  832. newSong.dislikes = Number($("#dislikes").val());
  833. newSong.title = $("#title").val();
  834. newSong.artist = $("#artist").val();
  835. newSong.img = $("#img").val();
  836. newSong.duration = Number($("#duration").val());
  837. newSong.skipDuration = $("#skip-duration").val();
  838. newSong.requestedBy = Session.get("song").requestedBy;
  839. newSong.genres = Session.get("song").genres;
  840. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  841. console.log(err, res);
  842. if (err) {
  843. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  844. Materialize.toast($toastContent, 8000);
  845. } else {
  846. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  847. Materialize.toast($toastContent, 4000);
  848. Session.set("song", newSong);
  849. }
  850. });
  851. }
  852. });
  853. Template.register.events({
  854. "submit form": function(e){
  855. e.preventDefault();
  856. var username = $("#username").val()
  857. var email = $("#email").val()
  858. var password = $("#password").val();
  859. var captchaData = grecaptcha.getResponse();
  860. console.log(captchaData)
  861. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  862. grecaptcha.reset();
  863. if (err) {
  864. console.log(err);
  865. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  866. $(".landing").before(errAlert);
  867. Meteor.setTimeout(function() {
  868. errAlert.fadeOut(5000, function() {
  869. errAlert.remove();
  870. });
  871. }, 5000);
  872. } else {
  873. Meteor.loginWithPassword(username, password);
  874. Accounts.onLogin(function(){
  875. window.location.href = "/";
  876. })
  877. }
  878. });
  879. },
  880. "click #github-login": function(){
  881. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  882. console.log(err, res);
  883. });
  884. }
  885. });
  886. /*Template.room.events({
  887. });*/
  888. // Settings Template
  889. Template.settings.events({
  890. "click #save-settings": function() {
  891. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  892. },
  893. "click #delete-account": function(){
  894. $("#delete-account").text("Click to confirm");
  895. $("#delete-account").click(function(){
  896. var bool = confirm("Are you sure you want to delete your account?");
  897. if(bool) {
  898. Meteor.call("deleteAccount");
  899. } else{
  900. $("#delete-account").text("Delete");
  901. }
  902. })
  903. },
  904. "click #change-password": function(){
  905. var oldPassword = $("#old-password").val();
  906. var newPassword= $("#new-password").val();
  907. var confirmPassword = $("#confirm-password").val();
  908. if(newPassword === confirmPassword){
  909. Accounts.changePassword(oldPassword, newPassword, function(err){
  910. if(err){
  911. $("#old-password").val("");
  912. $("#new-password").val("");
  913. $("#confirm-password").val("");
  914. $("<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(); });
  915. } else {
  916. $("#old-password").val("");
  917. $("#new-password").val("");
  918. $("#confirm-password").val("");
  919. $("<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(); });
  920. }
  921. });
  922. }
  923. }
  924. });
  925. var previewEndSongTimeout = undefined;