events.js 70 KB

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