events.js 59 KB

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