events.js 61 KB

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