events.js 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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 = $("#chat-message").val();
  151. if (!$("#chat-message").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. $("#chat-message").val("");
  162. } else {
  163. $("#chat-message").val("");
  164. }
  165. } else {
  166. Meteor.call("sendMessage", "global", message, function (err, res) {
  167. if (res) {
  168. $("#chat-message").val("");
  169. }
  170. });
  171. }
  172. }
  173. }
  174. }
  175. Template.admin.events({
  176. "click a": function(e) {
  177. var target = $(e.target);
  178. if (target.hasClass("activate-alert-button") || target.parent().hasClass("activate-alert-button")) {
  179. var id = target.data("id") || target.parent().data("id");
  180. Meteor.call("activateAlert", id);
  181. } else if (target.hasClass("deactivate-alert-button") || target.parent().hasClass("deactivate-alert-button")) {
  182. var id = target.data("id") || target.parent().data("id");
  183. Meteor.call("deactivateAlert", id);
  184. }
  185. },
  186. "click #croom_create": function() {
  187. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#croom_private").prop("checked"), $("#croom_desc").val(), function (err, res) {
  188. if (err) {
  189. var $toastContent = $('<span><strong>Room not added.</strong> ' + err.reason + '</span>');
  190. Materialize.toast($toastContent, 8000);
  191. } else {
  192. window.location = "/" + $("#croom_tag").val();
  193. }
  194. });
  195. },
  196. "click #submit-alert": function(){
  197. var alertDesc = $("#alert-desc").val()
  198. if(alertDesc !== ""){
  199. Meteor.call("addAlert", alertDesc);
  200. }
  201. },
  202. "click .delete-alert-button": function(e) {
  203. var target = $(e.target);
  204. var id = $(e.target).data("id") || target.parent().data("id");
  205. Meteor.call("deleteAlert", id);
  206. }
  207. });
  208. Template.feedback.events({
  209. "click #feedback_submit": function(){
  210. if(Meteor.userId()){
  211. if($("#feedback_message").val().length !== 0 && $("#feedback_message").hasClass("invalid") === false){
  212. Meteor.call("sendFeedback", $("#feedback_message").val());
  213. $("#feedback_message").val("");
  214. $("#createFeedback").closeModal()
  215. } else{
  216. 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>');
  217. Materialize.toast($toastContent, 8000);
  218. }
  219. } else {
  220. var $toastContent = $('<span><strong>Feedback not sent.</strong> You must be logged in.</span>');
  221. Materialize.toast($toastContent, 4000);
  222. }
  223. },
  224. "click .upvote": function(){
  225. var message = $(this).parent("card").prevObject[0].message;
  226. Meteor.call("upvoteFeedback", message);
  227. },
  228. "click #delete": function(){
  229. var message = $(this).parent("card").prevObject[0].message;
  230. Meteor.call("deleteFeedback", message);
  231. },
  232. "click #edit": function(){
  233. $("#editModal").click()
  234. var data = Feedback.findOne({"message": $(this).parent("card").prevObject[0].message});
  235. feedbackData = data.message;
  236. $("#edit_feedback_message").val(data.message);
  237. },
  238. "click #edit_feedback_submit": function(){
  239. var oldMessage = feedbackData;
  240. var newMessage = $("#edit_feedback_message").val()
  241. $("#edit_feedback_message").val("")
  242. Meteor.call("updateFeedback", oldMessage, newMessage);
  243. $("#editFeedback").closeModal();
  244. }
  245. });
  246. Template.header.events({
  247. "click .logout": function(e){
  248. e.preventDefault();
  249. Meteor.logout();
  250. if (hpSound !== undefined) {
  251. hpSound.stop();
  252. }
  253. },
  254. "click #profile": function(){
  255. window.location = "/u/" + Meteor.user().profile.username;
  256. }
  257. });
  258. Template.login.events({
  259. "submit form": function(e){
  260. e.preventDefault();
  261. Session.set("github", false);
  262. var username = $("#username").val()
  263. var password = $("#password").val();
  264. Meteor.loginWithPassword(username, password, function(err) {
  265. if (err) {
  266. var $toastContent = $('<span><strong>Oh snap!</strong> ' + err.reason + '</span>');
  267. Materialize.toast($toastContent, 8000);
  268. } else {
  269. window.location.href = "/";
  270. }
  271. });
  272. },
  273. "click #github-login": function(){
  274. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  275. console.log(err, res);
  276. });
  277. }
  278. });
  279. Template.playlist.events({
  280. "keyup #search-playlist": function(){
  281. if($("#search-playlist").val().length === 0){
  282. $(".pl-item").show();
  283. } else {
  284. $(".pl-item").hide();
  285. var input = $("#search-playlist").val().toLowerCase();
  286. $(".pl-item strong").each(function(i, el){
  287. if($(el).text().toLowerCase().indexOf(input) !== -1){
  288. $(el).parent(".pl-item").show();
  289. }
  290. })
  291. $(".pl-item #pl-artist").each(function(i, el){
  292. if($(el).text().toLowerCase().indexOf(input) !== -1){
  293. $(el).parent(".pl-item").show();
  294. }
  295. })
  296. }
  297. },
  298. "click #pl-item": function(){
  299. console.log($(this).text());
  300. }
  301. });
  302. Template.profile.events({
  303. //Edit real name
  304. "click #edit-name": function(){
  305. $("#name").hide();
  306. $("#name-div").show();
  307. $("#edit-name").hide();
  308. $("#cancel-edit").show();
  309. },
  310. "click #submit-name": function(){
  311. var user = Meteor.user();
  312. $("#name").show();
  313. $("#name-div").hide();
  314. $("#edit-name").show();
  315. $("#cancel-edit").hide();
  316. var realname = $("#input-name").val();
  317. var username = user.profile.username;
  318. $("#name").text("Name: " + realname);
  319. $("#input-name").val("")
  320. Meteor.call("updateRealName", realname);
  321. },
  322. "click #cancel-edit": function(){
  323. $("#name").show();
  324. $("#name-div").hide();
  325. $("#edit-name").show();
  326. $("#cancel-edit").hide();
  327. $("#input-name").val("");
  328. },
  329. //Edit username
  330. "click #edit-username": function(){
  331. $("#username").hide();
  332. $("#username-div").show();
  333. $("#edit-username").hide();
  334. $("#cancel-username").show();
  335. },
  336. "click #submit-username": function(){
  337. var user = Meteor.user()
  338. $("#username").show();
  339. $("#username-div").hide();
  340. $("#edit-username").show();
  341. $("#cancel-username").hide();
  342. var username = user.username;
  343. var newUserName = $("#input-username").val();
  344. $("#profile-name").text(newUserName)
  345. $("#username").text("Username: " + newUserName);
  346. $("#input-username").val("")
  347. Meteor.call("updateUserName", newUserName);
  348. window.location = "/u/" + newUserName;
  349. },
  350. "click #cancel-username": function(){
  351. $("#username").show();
  352. $("#username-div").hide();
  353. $("#edit-username").show();
  354. $("#cancel-username").hide();
  355. $("#input-username").val("");
  356. },
  357. // Admins only Edit Rank
  358. "click #edit-rank": function() {
  359. $("#rank").hide();
  360. $("#rank-div").show();
  361. $("#edit-rank").hide();
  362. $("#cancel-rank").show();
  363. },
  364. "click #submit-rank": function() {
  365. $("#rank").show();
  366. $("#rank-div").hide();
  367. $("#edit-rank").show();
  368. $("#cancel-rank").hide();
  369. var newRank = $("#select-rank option:selected").val();
  370. var username = Session.get("username");
  371. console.log(username, newRank);
  372. },
  373. "click #cancel-rank": function() {
  374. $("#rank").show();
  375. $("#rank-div").hide();
  376. $("#edit-rank").show();
  377. $("#cancel-rank").hide();
  378. }
  379. });
  380. var seekerBarInterval = undefined;
  381. Template.queues.events({
  382. /* TODO Add undo delete button */
  383. "input #id": function() {
  384. console.log("Change!");
  385. $("#previewPlayerContainer").addClass("hide-preview");
  386. },
  387. "input #img": function() {
  388. var url = $("#img").val();
  389. console.log(url);
  390. Session.set("image_url", url);
  391. },
  392. "click .preview-button": function(e){
  393. Session.set("song", this);
  394. $("#previewModal").openModal();
  395. },
  396. "click #previewImageButton": function() {
  397. $("#preview-image").attr("src", Session.get("song").img);
  398. },
  399. "click .edit-queue-button": function(e){
  400. Session.set("song", this);
  401. Session.set("genre", $(e.target).data("genre"));
  402. Session.set("type", "queue");
  403. $("#mid").val(this.mid).change();
  404. $("#artist").val(this.artist).change();
  405. $("#title").val(this.title).change();
  406. $("#img").val(this.img).change();
  407. $("#id").val(this.id).change();
  408. $("#likes").val(this.likes).change();
  409. $("#dislikes").val(this.dislikes).change();
  410. $("#duration").val(this.duration).change();
  411. $("#skip-duration").val(this.skipDuration).change();
  412. $("#genres").val(this.genres).change();
  413. $("#genres").material_select();
  414. $("#previewPlayerContainer").addClass("hide-preview");
  415. Session.set("image_url", this.img);
  416. Session.set("editing", true);
  417. $("#editModal").openModal({
  418. complete : function() {
  419. Session.set("editing", false);
  420. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  421. YTPlayer.stopVideo();
  422. }
  423. }
  424. });
  425. },
  426. "click .add-song-button": function(e){
  427. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  428. Meteor.call("addSongToPlaylist", this, function(err) {
  429. console.log(err);
  430. if (err) {
  431. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  432. Materialize.toast($toastContent, 8000);
  433. }
  434. });
  435. },
  436. "click .deny-song-button": function(e){
  437. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  438. Meteor.call("removeSongFromQueue", this.mid);
  439. },
  440. "click #play": function() {
  441. var duration = Session.get("song").duration;
  442. var d = moment.duration(parseInt(duration), 'seconds');
  443. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  444. $("#previewPlayerContainer").removeClass("hide-preview");
  445. var song = Session.get("song");
  446. var id = song.id;
  447. var volume = localStorage.getItem("volume") || 20;
  448. if (song.duration !== 0) {
  449. $("#play").attr("disabled", true);
  450. $("#stop").attr("disabled", false);
  451. $("#pause").attr("disabled", false);
  452. $("#forward").attr("disabled", false);
  453. if (YTPlayer === undefined) {
  454. YTPlayer = new YT.Player("previewPlayer", {
  455. height: 540,
  456. width: 568,
  457. videoId: id,
  458. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  459. events: {
  460. 'onReady': function(event) {
  461. event.target.seekTo(Number(song.skipDuration));
  462. event.target.playVideo();
  463. event.target.setVolume(volume);
  464. },
  465. 'onStateChange': function(event){
  466. if (event.data == YT.PlayerState.PAUSED) {
  467. if (seekerBarInterval !== undefined) {
  468. Meteor.clearInterval(seekerBarInterval);
  469. seekerBarInterval = undefined;
  470. }
  471. }
  472. // if (event.data == YT.PlayerState.UNSTARTED) {
  473. // if (seekerBarInterval !== undefined) {
  474. // Meteor.clearInterval(seekerBarInterval);
  475. // seekerBarInterval = undefined;
  476. // }
  477. // $(".seeker-bar").css({width: "0"});
  478. // $("#time-elapsed").text("0:00");
  479. // $("#previewPlayerContainer").addClass("hide-preview");
  480. // console.log("HIDE MEY STACY!!!!")
  481. // }
  482. if (event.data == YT.PlayerState.PLAYING) {
  483. seekerBarInterval = Meteor.setInterval(function() {
  484. var duration = Session.get("song").duration;
  485. var timeElapsed = YTPlayer.getCurrentTime();
  486. var skipDuration = Session.get("song").skipDuration;
  487. if (duration <= (timeElapsed - skipDuration)) {
  488. YTPlayer.stopVideo();
  489. $("#play").attr("disabled", false);
  490. $("#stop").attr("disabled", true);
  491. $("#pause").attr("disabled", true);
  492. $("#forward").attr("disabled", true);
  493. $("#previewPlayerContainer").addClass("hide-preview");
  494. $(".seeker-bar").css({width: "0"});
  495. $("#time-elapsed").text("0:00");
  496. Meteor.clearInterval(seekerBarInterval);
  497. } else {
  498. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  499. $(".seeker-bar").css({width: percentComplete + "%"});
  500. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  501. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  502. }
  503. }, 100);
  504. $("#play").attr("disabled", true);
  505. $("#stop").attr("disabled", false);
  506. $("#pause").attr("disabled", false);
  507. $("#forward").attr("disabled", false);
  508. } else {
  509. $("#play").attr("disabled", false);
  510. $("#stop").attr("disabled", true);
  511. $("#pause").attr("disabled", true);
  512. $("#forward").attr("disabled", true);
  513. }
  514. }
  515. }
  516. });
  517. } else {
  518. if (YTPlayer.getPlayerState() === 2) {
  519. YTPlayer.playVideo();
  520. } else {
  521. console.log(id, song.skipDuration, song.duration);
  522. YTPlayer.loadVideoById(id);
  523. YTPlayer.seekTo(Number(song.skipDuration));
  524. }
  525. }
  526. $("#previewPlayerContainer").removeClass("hide-preview");
  527. }
  528. },
  529. "click #stop": function() {
  530. $("#play").attr("disabled", false);
  531. $("#stop").attr("disabled", true);
  532. $("#pause").attr("disabled", true);
  533. $("#forward").attr("disabled", true);
  534. if (previewEndSongTimeout !== undefined) {
  535. Meteor.clearTimeout(previewEndSongTimeout);
  536. }
  537. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  538. YTPlayer.stopVideo();
  539. }
  540. },
  541. "click #pause": function() {
  542. $("#play").attr("disabled", false);
  543. $("#stop").attr("disabled", false);
  544. $("#pause").attr("disabled", true);
  545. $("#forward").attr("disabled", true);
  546. if (previewEndSongTimeout !== undefined) {
  547. Meteor.clearTimeout(previewEndSongTimeout);
  548. }
  549. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  550. YTPlayer.pauseVideo();
  551. }
  552. },
  553. "click #forward": function() {
  554. var error = false;
  555. if (YTPlayer !== undefined) {
  556. var duration = Number(Session.get("song").duration) | 0;
  557. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  558. if (YTPlayer.getDuration() < duration + skipDuration) {
  559. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  560. Materialize.toast($toastContent, 8000);
  561. error = true;
  562. } else {
  563. YTPlayer.seekTo(skipDuration + duration - 10);
  564. }
  565. }
  566. if (!error) {
  567. if (previewEndSongTimeout !== undefined) {
  568. Meteor.clearTimeout(previewEndSongTimeout);
  569. }
  570. previewEndSongTimeout = Meteor.setTimeout(function() {
  571. if (YTPlayer !== undefined) {
  572. YTPlayer.stopVideo();
  573. }
  574. $("#play").attr("disabled", false);
  575. $("#stop").attr("disabled", true);
  576. $("#pause").attr("disabled", true);
  577. $("#forward").attr("disabled", true);
  578. $("#previewPlayerContainer").addClass("hide-preview");
  579. }, 10000);
  580. }
  581. },
  582. "click #get-spotify-info": function() {
  583. var search = $("#title").val();
  584. var artistName = $("#artist").val();
  585. getSpotifyInfo(search, function(data) {
  586. for(var i in data){
  587. for(var j in data[i].items){
  588. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  589. $("#img").val(data[i].items[j].album.images[2].url).change();
  590. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  591. return;
  592. }
  593. }
  594. }
  595. }, artistName);
  596. },
  597. "click #save-song-button": function() {
  598. var newSong = {};
  599. newSong.mid = $("#mid").val();
  600. newSong.id = $("#id").val();
  601. newSong.likes = Number($("#likes").val());
  602. newSong.dislikes = Number($("#dislikes").val());
  603. newSong.title = $("#title").val();
  604. newSong.artist = $("#artist").val();
  605. newSong.img = $("#img").val();
  606. newSong.duration = Number($("#duration").val());
  607. newSong.skipDuration = $("#skip-duration").val();
  608. newSong.requestedBy = Session.get("song").requestedBy;
  609. newSong.genres = $("#genres").val() || [];
  610. if(newSong.skipDuration === undefined){
  611. newSong.skipDuration = 0;
  612. }
  613. Meteor.call("updateQueueSong", newSong.mid, newSong, function(err, res) {
  614. if (err) {
  615. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  616. Materialize.toast($toastContent, 8000);
  617. } else {
  618. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  619. Materialize.toast($toastContent, 4000);
  620. Session.set("song", newSong);
  621. }
  622. });
  623. }
  624. });
  625. Template.manageStation.events({
  626. /* TODO Add undo delete button */
  627. "click #editDescButton": function() {
  628. var parts = location.href.split('/');
  629. parts.pop();
  630. var id = parts.pop();
  631. var type = id.toLowerCase();
  632. var editingDesc = Session.get("editingDesc");
  633. if (!editingDesc) {
  634. Session.set("editingDesc", !editingDesc);
  635. } else {
  636. var newDesc = $("#editDesc").val();
  637. Meteor.call("editRoomDesc", type, newDesc);
  638. Session.set("editingDesc", !editingDesc);
  639. }
  640. },
  641. "input #id": function() {
  642. $("#previewPlayerContainer").addClass("hide-preview");
  643. },
  644. "input #img": function() {
  645. var url = $("#img").val();
  646. Session.set("image_url", url);
  647. },
  648. "click .preview-button": function(e){
  649. Session.set("song", this);
  650. $("#previewModal").openModal();
  651. },
  652. "click #previewImageButton": function() {
  653. $("#preview-image").attr("src", Session.get("song").img);
  654. },
  655. "click .edit-song-button": function(e){
  656. Session.set("song", this);
  657. Session.set("genre", $(e.target).data("genre"));
  658. $("#mid").val(this.mid).change();
  659. $("#artist").val(this.artist).change();
  660. $("#title").val(this.title).change();
  661. $("#img").val(this.img).change();
  662. $("#id").val(this.id).change();
  663. $("#likes").val(this.likes).change();
  664. $("#dislikes").val(this.dislikes).change();
  665. $("#duration").val(this.duration).change();
  666. $("#skip-duration").val(this.skipDuration).change();
  667. $("#previewPlayerContainer").addClass("hide-preview");
  668. Session.set("image_url", this.img);
  669. Session.set("editing", true);
  670. $("#editModal").openModal({
  671. complete : function() {
  672. Session.set("editing", false);
  673. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  674. YTPlayer.stopVideo();
  675. }
  676. }
  677. });
  678. },
  679. "click .remove-song-button": function(e){
  680. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  681. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  682. },
  683. "click #play": function() {
  684. var duration = Session.get("song").duration;
  685. var d = moment.duration(parseInt(duration), 'seconds');
  686. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  687. $("#previewPlayerContainer").removeClass("hide-preview");
  688. var song = Session.get("song");
  689. var id = song.id;
  690. var volume = localStorage.getItem("volume") || 20;
  691. if (song.duration !== 0) {
  692. $("#play").attr("disabled", true);
  693. $("#stop").attr("disabled", false);
  694. $("#pause").attr("disabled", false);
  695. $("#forward").attr("disabled", false);
  696. if (YTPlayer === undefined) {
  697. YTPlayer = new YT.Player("previewPlayer", {
  698. height: 540,
  699. width: 568,
  700. videoId: id,
  701. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  702. events: {
  703. 'onReady': function(event) {
  704. event.target.seekTo(Number(song.skipDuration));
  705. event.target.playVideo();
  706. event.target.setVolume(volume);
  707. },
  708. 'onStateChange': function(event){
  709. if (event.data == YT.PlayerState.PAUSED) {
  710. if (seekerBarInterval !== undefined) {
  711. Meteor.clearInterval(seekerBarInterval);
  712. seekerBarInterval = undefined;
  713. }
  714. }
  715. // if (event.data == YT.PlayerState.UNSTARTED) {
  716. // if (seekerBarInterval !== undefined) {
  717. // Meteor.clearInterval(seekerBarInterval);
  718. // seekerBarInterval = undefined;
  719. // }
  720. // $(".seeker-bar").css({width: "0"});
  721. // $("#time-elapsed").text("0:00");
  722. // $("#previewPlayerContainer").addClass("hide-preview");
  723. // }
  724. if (event.data == YT.PlayerState.PLAYING) {
  725. seekerBarInterval = Meteor.setInterval(function() {
  726. var duration = Session.get("song").duration;
  727. var timeElapsed = YTPlayer.getCurrentTime();
  728. var skipDuration = Session.get("song").skipDuration;
  729. if (duration <= (timeElapsed - skipDuration)) {
  730. YTPlayer.stopVideo();
  731. $("#play").attr("disabled", false);
  732. $("#stop").attr("disabled", true);
  733. $("#pause").attr("disabled", true);
  734. $("#forward").attr("disabled", true);
  735. $("#previewPlayerContainer").addClass("hide-preview");
  736. $(".seeker-bar").css({width: "0"});
  737. $("#time-elapsed").text("0:00");
  738. Meteor.clearInterval(seekerBarInterval);
  739. } else {
  740. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  741. $(".seeker-bar").css({width: percentComplete + "%"});
  742. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  743. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  744. }
  745. }, 100);
  746. $("#play").attr("disabled", true);
  747. $("#stop").attr("disabled", false);
  748. $("#pause").attr("disabled", false);
  749. $("#forward").attr("disabled", false);
  750. } else {
  751. $("#play").attr("disabled", false);
  752. $("#stop").attr("disabled", true);
  753. $("#pause").attr("disabled", true);
  754. $("#forward").attr("disabled", true);
  755. }
  756. }
  757. }
  758. });
  759. } else {
  760. if (YTPlayer.getPlayerState() === 2) {
  761. YTPlayer.playVideo();
  762. } else {
  763. console.log(id, song.skipDuration, song.duration);
  764. YTPlayer.loadVideoById(id);
  765. YTPlayer.seekTo(Number(song.skipDuration));
  766. }
  767. }
  768. $("#previewPlayerContainer").removeClass("hide-preview");
  769. }
  770. },
  771. "click #stop": function() {
  772. $("#play").attr("disabled", false);
  773. $("#stop").attr("disabled", true);
  774. $("#pause").attr("disabled", true);
  775. $("#forward").attr("disabled", true);
  776. if (previewEndSongTimeout !== undefined) {
  777. Meteor.clearTimeout(previewEndSongTimeout);
  778. }
  779. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  780. YTPlayer.stopVideo();
  781. }
  782. },
  783. "click #pause": function() {
  784. $("#play").attr("disabled", false);
  785. $("#stop").attr("disabled", false);
  786. $("#pause").attr("disabled", true);
  787. $("#forward").attr("disabled", true);
  788. if (previewEndSongTimeout !== undefined) {
  789. Meteor.clearTimeout(previewEndSongTimeout);
  790. }
  791. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  792. YTPlayer.pauseVideo();
  793. }
  794. },
  795. "click #forward": function() {
  796. var error = false;
  797. if (YTPlayer !== undefined) {
  798. var duration = Number(Session.get("song").duration) | 0;
  799. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  800. if (YTPlayer.getDuration() < duration + skipDuration) {
  801. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  802. Materialize.toast($toastContent, 8000);
  803. error = true;
  804. } else {
  805. YTPlayer.seekTo(skipDuration + duration - 10);
  806. }
  807. }
  808. if (!error) {
  809. if (previewEndSongTimeout !== undefined) {
  810. Meteor.clearTimeout(previewEndSongTimeout);
  811. }
  812. previewEndSongTimeout = Meteor.setTimeout(function() {
  813. if (YTPlayer !== undefined) {
  814. YTPlayer.stopVideo();
  815. }
  816. $("#play").attr("disabled", false);
  817. $("#stop").attr("disabled", true);
  818. $("#pause").attr("disabled", true);
  819. $("#forward").attr("disabled", true);
  820. $("#previewPlayerContainer").addClass("hide-preview");
  821. }, 10000);
  822. }
  823. },
  824. "click #get-spotify-info": function() {
  825. var search = $("#title").val();
  826. var artistName = $("#artist").val();
  827. getSpotifyInfo(search, function(data) {
  828. for(var i in data){
  829. for(var j in data[i].items){
  830. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  831. $("#img").val(data[i].items[j].album.images[2].url).change();
  832. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  833. return;
  834. }
  835. }
  836. }
  837. }, artistName);
  838. },
  839. "click #save-song-button": function() {
  840. var newSong = {};
  841. newSong.mid = $("#mid").val();
  842. newSong.id = $("#id").val();
  843. newSong.likes = Number($("#likes").val());
  844. newSong.dislikes = Number($("#dislikes").val());
  845. newSong.title = $("#title").val();
  846. newSong.artist = $("#artist").val();
  847. newSong.img = $("#img").val();
  848. newSong.duration = Number($("#duration").val());
  849. newSong.skipDuration = $("#skip-duration").val();
  850. newSong.requestedBy = Session.get("song").requestedBy;
  851. newSong.genres = $("#genres").val();
  852. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  853. console.log(err, res);
  854. if (err) {
  855. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  856. Materialize.toast($toastContent, 8000);
  857. } else {
  858. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  859. Materialize.toast($toastContent, 4000);
  860. Session.set("song", newSong);
  861. }
  862. });
  863. }
  864. });
  865. Template.manageSongs.events({
  866. /* TODO Add undo delete button */
  867. "change #show_genres_cb": function() {
  868. var selected = $("#show_genres_cb").is(":checked");
  869. Session.set("showGenres", selected);
  870. },
  871. "change #show_no_genres_cb": function() {
  872. var selected = $("#show_no_genres_cb").is(":checked");
  873. Session.set("showNoGenres", selected);
  874. },
  875. "input #id": function() {
  876. $("#previewPlayerContainer").addClass("hide-preview");
  877. },
  878. "input #img": function() {
  879. var url = $("#img").val();
  880. Session.set("image_url", url);
  881. },
  882. "click .preview-button": function(e){
  883. Session.set("song", this);
  884. $("#previewModal").openModal();
  885. },
  886. "click #previewImageButton": function() {
  887. $("#preview-image").attr("src", Session.get("song").img);
  888. },
  889. "click .edit-song-button": function(e){
  890. Session.set("song", this);
  891. Session.set("genre", $(e.target).data("genre"));
  892. $("#mid").val(this.mid).change();
  893. $("#artist").val(this.artist).change();
  894. $("#title").val(this.title).change();
  895. $("#img").val(this.img).change();
  896. $("#id").val(this.id).change();
  897. $("#likes").val(this.likes).change();
  898. $("#dislikes").val(this.dislikes).change();
  899. $("#duration").val(this.duration).change();
  900. $("#skip-duration").val(this.skipDuration).change();
  901. $("#genres").val(this.genres).change();
  902. $("#genres").material_select();
  903. $("#previewPlayerContainer").addClass("hide-preview");
  904. Session.set("image_url", this.img);
  905. Session.set("editing", true);
  906. $("#editModal").openModal({
  907. complete : function() {
  908. Session.set("editing", false);
  909. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  910. YTPlayer.stopVideo();
  911. }
  912. }
  913. });
  914. },
  915. "click .remove-song-button": function(){
  916. console.log(this.mid);
  917. console.log("TEST!");
  918. Meteor.call("deleteSong", 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 acceptedTermsAndPrivacy = $("#termsPrivacyBTN").is(":checked");
  1109. var captchaData = grecaptcha.getResponse();
  1110. if (acceptedTermsAndPrivacy) {
  1111. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  1112. grecaptcha.reset();
  1113. if (err) {
  1114. console.log(err);
  1115. var $toastContent = $('<span><strong>Oh snap!</strong> ' + err.reason + '</span>');
  1116. Materialize.toast($toastContent, 8000);
  1117. } else {
  1118. Meteor.loginWithPassword(username, password);
  1119. Accounts.onLogin(function(){
  1120. window.location.href = "/";
  1121. })
  1122. }
  1123. });
  1124. } else {
  1125. var $toastContent = $('<span><strong>Oh snap!</strong> Please read and accept the Terms and Conditions and the Privacy Policy.</span>');
  1126. Materialize.toast($toastContent, 8000);
  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 #global-chat-tab": function () {
  1341. $("#global-chat-tab").removeClass("unread-messages");
  1342. },
  1343. "click #sync": function () {
  1344. if (Session.get("currentSong") !== undefined) {
  1345. var room = Rooms.findOne({type: Session.get("type")});
  1346. if (room !== undefined) {
  1347. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  1348. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  1349. if (YTPlayer !== undefined) {
  1350. YTPlayer.seekTo(skipDuration + timeIn / 1000);
  1351. }
  1352. }
  1353. }
  1354. },
  1355. "click #lock": function () {
  1356. Meteor.call("lockRoom", Session.get("type"));
  1357. },
  1358. "click #unlock": function () {
  1359. Meteor.call("unlockRoom", Session.get("type"));
  1360. },
  1361. "click #submit": function () {
  1362. sendMessageGlobal();
  1363. Meteor.setTimeout(function () {
  1364. $(".chat-ul").scrollTop(100000);
  1365. }, 1000)
  1366. },
  1367. "keyup #chat-message": function (e) {
  1368. if (e.type === "keyup" && e.which === 13) {
  1369. e.preventDefault();
  1370. if (!$('#chat-message').data('dropdownshown')) {
  1371. sendMessageGlobal();
  1372. Meteor.setTimeout(function () {
  1373. $(".chat-ul").scrollTop(100000);
  1374. }, 1000)
  1375. }
  1376. }
  1377. },
  1378. "click #like": function (e) {
  1379. $("#like").blur();
  1380. Meteor.call("likeSong", Session.get("currentSong").mid);
  1381. },
  1382. "click #dislike": function (e) {
  1383. $("#dislike").blur();
  1384. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  1385. },
  1386. "click #vote-skip": function () {
  1387. Meteor.call("voteSkip", type, function (err, res) {
  1388. $("#vote-skip").attr("disabled", true);
  1389. });
  1390. },
  1391. "click #report-prev": function (e) {
  1392. if (Session.get("previousSong") !== undefined) {
  1393. Session.set("reportPrevious", true);
  1394. $("#report-prev").prop("disabled", true);
  1395. $("#report-curr").prop("disabled", false);
  1396. }
  1397. },
  1398. "click #report-curr": function (e) {
  1399. Session.set("reportPrevious", false);
  1400. $("#report-prev").prop("disabled", false);
  1401. $("#report-curr").prop("disabled", true);
  1402. },
  1403. "click #report-modal": function () {
  1404. Session.set("currentSongR", Session.get("currentSong"));
  1405. Session.set("previousSongR", Session.get("previousSong"));
  1406. },
  1407. "click #add-song-button": function (e) {
  1408. e.preventDefault();
  1409. parts = location.href.split('/');
  1410. var roomType = parts.pop();
  1411. var genre = roomType.toLowerCase();
  1412. var type = $("#type").val();
  1413. id = $("#id").val();
  1414. var title = $("#title").val();
  1415. var artist = $("#artist").val();
  1416. var genres = $("#genres").val() || [];
  1417. var songData = {type: type, id: id, title: title, artist: artist, genres: genres};
  1418. if (Songs.find({"id": songData.id}).count() > 0) {
  1419. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been added.</span>');
  1420. Materialize.toast($toastContent, 8000);
  1421. } else if (Queues.find({"id": songData.id}).count() > 0) {
  1422. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been requested.</span>');
  1423. Materialize.toast($toastContent, 8000);
  1424. } else {
  1425. Meteor.call("addSongToQueue", songData, function (err, res) {
  1426. console.log(err, res);
  1427. if (err) {
  1428. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  1429. Materialize.toast($toastContent, 8000);
  1430. } else {
  1431. var $toastContent = $('<span><strong>Song added.</strong> Your song has succesfully been added to the queue.</span>');
  1432. Materialize.toast($toastContent, 8000);
  1433. $('#add_song_modal').closeModal();
  1434. Session.set("editingSong", false);
  1435. }
  1436. });
  1437. }
  1438. },
  1439. "click #toggle-video": function (e) {
  1440. e.preventDefault();
  1441. if (Session.get("mediaHidden")) {
  1442. $("#media-container").removeClass("hidden");
  1443. $("#toggle-video").text("Hide video");
  1444. Session.set("mediaHidden", false);
  1445. } else {
  1446. $("#media-container").addClass("hidden");
  1447. $("#toggle-video").text("Show video");
  1448. Session.set("mediaHidden", true);
  1449. }
  1450. },
  1451. "click #return": function (e) {
  1452. $("#add-info").hide();
  1453. $("#search-info").show();
  1454. },
  1455. "click #search-song": function () {
  1456. var songs = [];
  1457. Session.set("songResults", songs);
  1458. $.ajax({
  1459. type: "GET",
  1460. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY&type=video&maxResults=25",
  1461. applicationType: "application/json",
  1462. contentType: "json",
  1463. success: function (data) {
  1464. for (var i in data.items) {
  1465. var item = data.items[i];
  1466. console.log(item);
  1467. songs.push({title: item.snippet.title, artist: item.snippet.channelTitle, id: item.id.videoId, image: item.snippet.thumbnails.medium.url});
  1468. }
  1469. Session.set("songResults", songs);
  1470. /*$("#song-results > div").click(function () {
  1471. $("#search-info").hide();
  1472. $("#add-info").show();
  1473. var title = $(this).find("div > .song-result-title").text();
  1474. for (var i in songs) {
  1475. if (songs[i].title === title) {
  1476. var songObj = {
  1477. id: songs[i].id,
  1478. title: songs[i].title,
  1479. type: "youtube"
  1480. };
  1481. $("#title").val(songObj.title);
  1482. $("#artist").val("");
  1483. $("#id").val(songObj.id);
  1484. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function (data) {
  1485. if (data.tracks.items.length > 0) {
  1486. $("#title").val(data.tracks.items[0].name);
  1487. var artists = [];
  1488. $("#img").val(data.tracks.items[0].album.images[2].url);
  1489. data.tracks.items[0].artists.forEach(function (artist) {
  1490. artists.push(artist.name);
  1491. });
  1492. $("#artist").val(artists.join(", "));
  1493. }
  1494. });
  1495. }
  1496. }
  1497. })*/
  1498. }
  1499. })
  1500. },
  1501. "click #volume-icon": function () {
  1502. var volume = 0;
  1503. var slider = $("#volume-slider").slider();
  1504. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1505. if (YTPlayer !== undefined) {
  1506. YTPlayer.setVolume(volume);
  1507. localStorage.setItem("volume", volume);
  1508. $("#volume-slider").slider("setValue", volume);
  1509. }
  1510. },
  1511. "click #play": function () {
  1512. Meteor.call("resumeRoom", type);
  1513. },
  1514. "click #pause": function () {
  1515. Meteor.call("pauseRoom", type);
  1516. },
  1517. "click #skip": function () {
  1518. Meteor.call("skipSong", type);
  1519. },
  1520. "click #shuffle": function () {
  1521. Meteor.call("shufflePlaylist", type);
  1522. },
  1523. "change input": function (e) {
  1524. /*if (e.target && e.target.id) {
  1525. var partsOfId = e.target.id.split("-");
  1526. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  1527. var camelCase = partsOfId.join("");
  1528. Session.set(camelCase, e.target.checked);
  1529. }*/
  1530. },
  1531. "click #report-song-button": function () {
  1532. var room = Session.get("type");
  1533. var reportData = {};
  1534. reportData.song = Session.get("currentSong").mid;
  1535. reportData.type = [];
  1536. reportData.reason = [];
  1537. $(".report-layer-1 > .checkbox input:checked").each(function () {
  1538. reportData.type.push(this.id);
  1539. if (this.id == "report-other") {
  1540. var otherText = $(".other-textarea").val();
  1541. }
  1542. });
  1543. $(".report-layer-2 input:checked").each(function () {
  1544. reportData.reason.push(this.id);
  1545. });
  1546. console.log(reportData);
  1547. Meteor.call("submitReport", room, reportData, Session.get("id"), function () {
  1548. $("#close-modal-r").click();
  1549. });
  1550. },
  1551. "change #si_or_pl": function () {
  1552. Session.set("songResults", []);
  1553. Session.set("si_or_pl", $("#si_or_pl").val());
  1554. },
  1555. "click #close-modal-a": function () {
  1556. $("#select_single").attr("selected", true);
  1557. $("#search-info").show();
  1558. $("#playlist-import").hide();
  1559. },
  1560. "click #admin-dropdown a": function(){
  1561. Meteor.setTimeout(function(){
  1562. $(".dropdown-button").click();
  1563. }, 1);
  1564. }
  1565. });
  1566. // Settings Template
  1567. Template.settings.events({
  1568. "click #save-settings": function() {
  1569. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  1570. },
  1571. "click #delete-account": function(){
  1572. $("#delete-account").text("Click to confirm");
  1573. $("#delete-account").click(function(){
  1574. var bool = confirm("Are you sure you want to delete your account?");
  1575. if(bool) {
  1576. Meteor.call("deleteAccount");
  1577. } else{
  1578. $("#delete-account").text("Delete");
  1579. }
  1580. })
  1581. },
  1582. "click #change-password": function(){
  1583. var oldPassword = $("#old-password").val();
  1584. var newPassword= $("#new-password").val();
  1585. var confirmPassword = $("#confirm-password").val();
  1586. if(newPassword === confirmPassword){
  1587. Accounts.changePassword(oldPassword, newPassword, function(err){
  1588. if(err){
  1589. $("#old-password").val("");
  1590. $("#new-password").val("");
  1591. $("#confirm-password").val("");
  1592. $("<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(); });
  1593. } else {
  1594. $("#old-password").val("");
  1595. $("#new-password").val("");
  1596. $("#confirm-password").val("");
  1597. $("<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(); });
  1598. }
  1599. });
  1600. }
  1601. }
  1602. });
  1603. var previewEndSongTimeout = undefined;