events.js 70 KB

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