2
0

events.js 69 KB

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