events.js 54 KB

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