events.js 70 KB

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