events.js 54 KB

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