events.js 61 KB

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