2
0

events.js 71 KB

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