events.js 72 KB

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