events.js 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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 = $("#username").val()
  239. var password = $("#password").val();
  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[2].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[2].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.room.helpers({
  1032. singleVideo: function() {
  1033. return true;
  1034. },
  1035. chat: function() {
  1036. Meteor.setTimeout(function() {
  1037. var elem = document.getElementById('chat');
  1038. if (elem !== undefined && elem !== null) {
  1039. elem.scrollTop = elem.scrollHeight;
  1040. }
  1041. }, 100);
  1042. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1043. },
  1044. globalChat: function() {
  1045. Meteor.setTimeout(function() {
  1046. var elem = document.getElementById('global-chat');
  1047. if (elem !== undefined && elem !== null) {
  1048. elem.scrollTop = elem.scrollHeight;
  1049. }
  1050. }, 100);
  1051. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1052. },
  1053. likes: function() {
  1054. var playlist = Playlists.findOne({type: Session.get("type")});
  1055. var likes = 0;
  1056. playlist.songs.forEach(function(song) {
  1057. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1058. likes = song.likes;
  1059. return;
  1060. }
  1061. });
  1062. return likes;
  1063. },
  1064. dislikes: function() {
  1065. var playlist = Playlists.findOne({type: Session.get("type")});
  1066. var dislikes = 0;
  1067. playlist.songs.forEach(function(song) {
  1068. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1069. dislikes = song.dislikes;
  1070. return;
  1071. }
  1072. });
  1073. return dislikes;
  1074. },
  1075. liked: function() {
  1076. if (Meteor.userId()) {
  1077. var currentSong = Session.get("currentSong");
  1078. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  1079. return "active";
  1080. } else {
  1081. return "";
  1082. }
  1083. } else {
  1084. "";
  1085. }
  1086. },
  1087. disliked: function() {
  1088. if (Meteor.userId()) {
  1089. var currentSong = Session.get("currentSong");
  1090. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  1091. return "active";
  1092. } else {
  1093. return "";
  1094. }
  1095. } else {
  1096. "";
  1097. }
  1098. },
  1099. type: function() {
  1100. var parts = location.href.split('/');
  1101. var id = parts.pop().toLowerCase();
  1102. return Rooms.findOne({type: id}).display;
  1103. },
  1104. users: function() {
  1105. var parts = location.href.split('/');
  1106. var id = parts.pop().toLowerCase();
  1107. return Rooms.findOne({type: id}).users;
  1108. },
  1109. title: function(){
  1110. return Session.get("title");
  1111. },
  1112. artist: function(){
  1113. return Session.get("artist");
  1114. },
  1115. loaded: function() {
  1116. return Session.get("loaded");
  1117. },
  1118. paused: function() {
  1119. return Session.get("state") === "paused";
  1120. },
  1121. private: function() {
  1122. return Rooms.findOne({type: Session.get("type")}).private === true;
  1123. },
  1124. report: function() {
  1125. return Session.get("reportObj");
  1126. },
  1127. reportSong: function() {
  1128. return Session.get("reportSong");
  1129. },
  1130. reportTitle: function() {
  1131. return Session.get("reportTitle");
  1132. },
  1133. reportAuthor: function() {
  1134. return Session.get("reportAuthor");
  1135. },
  1136. reportDuration: function() {
  1137. return Session.get("reportDuration");
  1138. },
  1139. reportAudio: function() {
  1140. return Session.get("reportAudio");
  1141. },
  1142. reportAlbumart: function() {
  1143. return Session.get("reportAlbumart");
  1144. },
  1145. reportOther: function() {
  1146. return Session.get("reportOther");
  1147. },
  1148. currentSong: function() {
  1149. return Session.get("currentSong");
  1150. },
  1151. previousSong: function() {
  1152. return Session.get("previousSong");
  1153. },
  1154. currentSongR: function() {
  1155. return Session.get("currentSongR");
  1156. },
  1157. previousSongR: function() {
  1158. return Session.get("previousSongR");
  1159. },
  1160. reportingSong: function() {
  1161. if (Session.get("reportPrevious")) {
  1162. return Session.get("previousSongR");
  1163. } else {
  1164. return Session.get("currentSongR");
  1165. }
  1166. },
  1167. votes: function(){
  1168. return Rooms.findOne({type: Session.get("type")}).votes;
  1169. }
  1170. });
  1171. Template.room.onCreated(function () {
  1172. Chat.after.find(function(userId, selector) {
  1173. if (selector.type === "global") {
  1174. if (!$("#global-chat-tab").hasClass("active")) {
  1175. $("#global-chat-tab").addClass("unread-messages");
  1176. }
  1177. } else if(selector.type === Session.get("type")) {
  1178. if (!$("#chat-tab").hasClass("active")) {
  1179. $("#chat-tab").addClass("unread-messages");
  1180. }
  1181. }
  1182. });
  1183. Session.set("reportSong", false);
  1184. Session.set("reportTitle", false);
  1185. Session.set("reportAuthor", false);
  1186. Session.set("reportDuration", false);
  1187. Session.set("reportAudio", false);
  1188. Session.set("reportAlbumart", false);
  1189. Session.set("reportOther", false);
  1190. if (resizeSeekerbarInterval !== undefined) {
  1191. Meteor.clearInterval(resizeSeekerbarInterval);
  1192. resizeSeekerbarInterval = undefined;
  1193. }
  1194. YTPlayer = undefined;
  1195. SCPlayer = undefined;
  1196. Session.set("videoHidden", false);
  1197. var tag = document.createElement("script");
  1198. tag.src = "https://www.youtube.com/iframe_api";
  1199. var firstScriptTag = document.getElementsByTagName('script')[0];
  1200. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1201. Session.set("singleVideo", true);
  1202. var currentSong = undefined;
  1203. var currentSongR = undefined;
  1204. function getTimeElapsed() {
  1205. if (currentSong !== undefined) {
  1206. var room = Rooms.findOne({type: type});
  1207. if (room !== undefined) {
  1208. return Date.now() - currentSong.started - room.timePaused;
  1209. }
  1210. }
  1211. return 0;
  1212. }
  1213. function getSongInfo(songData){
  1214. Session.set("title", songData.title);
  1215. Session.set("artist", songData.artist);
  1216. Session.set("id", songData.id);
  1217. $("#song-img").attr("src", songData.img);
  1218. Session.set("duration", parseInt(songData.duration));
  1219. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1220. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1221. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1222. }
  1223. function resizeSeekerbar() {
  1224. if (Session.get("state") === "playing") {
  1225. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1226. }
  1227. }
  1228. function startSong() {
  1229. $("#time-elapsed").text("0:00");
  1230. $("#vote-skip").attr("disabled", false);
  1231. if (currentSong !== undefined) {
  1232. if (SCPlayer !== undefined) SCPlayer.stop();
  1233. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  1234. var volume = localStorage.getItem("volume") || 20;
  1235. if (currentSong.type === "SoundCloud") {
  1236. if ($("#soundcloud-image").length !== 1) {
  1237. //$("#media-container").append('<img alt="Not loading" src="/soundcloud-image.png" class="embed-responsive-item" id="soundcloud-image" />');
  1238. $("#media-container").append('<h1 id="soundcloud-image">We have temporarily disabled the playing of SoundCloud songs. We are sorry for this inconvenience.</h1>');
  1239. }
  1240. if ($("#player").length === 1) {
  1241. $("#player").hide();
  1242. }
  1243. $("#soundcloud-image").show();
  1244. //getSongInfo(currentSong);
  1245. /*SC.stream("/tracks/" + currentSong.id, function(sound){
  1246. SCPlayer = sound;
  1247. sound.setVolume(volume / 100);
  1248. sound.play();
  1249. var interval = setInterval(function() {
  1250. if (sound.getState() === "playing") {
  1251. sound.seek(getTimeElapsed());
  1252. window.clearInterval(interval);
  1253. }
  1254. }, 200);
  1255. Session.set("duration", parseInt(currentSong.duration));
  1256. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  1257. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1258. resizeSeekerbar();
  1259. });*/
  1260. } else {
  1261. if ($("#player").length !== 1) {
  1262. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1263. }
  1264. if ($("#soundcloud-image").length === 1) {
  1265. $("#soundcloud-image").hide();
  1266. }
  1267. $("#player").show();
  1268. function loadVideo() {
  1269. if (!Session.get("YTLoaded")) {
  1270. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  1271. loadVideo();
  1272. }, 500));
  1273. } else {
  1274. if (YTPlayer === undefined) {
  1275. YTPlayer = new YT.Player("player", {
  1276. height: 540,
  1277. width: 960,
  1278. videoId: currentSong.id,
  1279. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1280. events: {
  1281. 'onReady': function (event) {
  1282. if (currentSong.skipDuration === undefined) {
  1283. currentSong.skipDuration = 0;
  1284. }
  1285. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1286. event.target.playVideo();
  1287. event.target.setVolume(volume);
  1288. resizeSeekerbar();
  1289. },
  1290. 'onStateChange': function (event) {
  1291. if (Session.get("YTLoaded")) {
  1292. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1293. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1294. event.target.playVideo();
  1295. }
  1296. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1297. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1298. event.target.pauseVideo();
  1299. }
  1300. }
  1301. }
  1302. }
  1303. });
  1304. } else {
  1305. YTPlayer.loadVideoById(currentSong.id);
  1306. if (currentSong.skipDuration === undefined) {
  1307. currentSong.skipDuration = 0;
  1308. }
  1309. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1310. }
  1311. Session.set("pauseVideo", false);
  1312. getSongInfo(currentSong);
  1313. }
  1314. }
  1315. loadVideo();
  1316. }
  1317. }
  1318. }
  1319. Session.set("loaded", false);
  1320. Meteor.subscribe("rooms", function() {
  1321. var parts = location.href.split('/');
  1322. var id = parts.pop();
  1323. var type = id.toLowerCase();
  1324. Session.set("type", type);
  1325. if (Rooms.find({type: type}).count() !== 1) {
  1326. window.location = "/";
  1327. } else {
  1328. StationSubscription = Meteor.subscribe(type);
  1329. Session.set("loaded", true);
  1330. minterval = Meteor.setInterval(function () {
  1331. var room = Rooms.findOne({type: type});
  1332. if (room !== undefined) {
  1333. if (room.state === "paused" || Session.get("pauseVideo")) {
  1334. Session.set("state", "paused");
  1335. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  1336. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  1337. YTPlayer.pauseVideo();
  1338. } else if (SCPlayer !== undefined && SCPlayer.getState().indexOf("playing") !== -1) {
  1339. SCPlayer.pause();
  1340. }
  1341. } else {
  1342. Session.set("state", "playing");
  1343. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  1344. YTPlayer.playVideo();
  1345. } else if (SCPlayer !== undefined && SCPlayer.getState().indexOf("paused") !== -1) {
  1346. SCPlayer.play();
  1347. }
  1348. }
  1349. }
  1350. if (room.currentSong.song !== undefined && (currentSongR === undefined || room.currentSong.started !== currentSongR.started)) {
  1351. Session.set("previousSong", currentSong);
  1352. currentSongR = room.currentSong;
  1353. currentSong = room.currentSong.song;
  1354. currentSong.started = room.currentSong.started;
  1355. Session.set("currentSong", currentSong);
  1356. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  1357. startSong();
  1358. }
  1359. if (currentSong !== undefined) {
  1360. if (room !== undefined) {
  1361. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1362. var song_duration = currentSong.duration;
  1363. if (song_duration <= duration) {
  1364. Session.set("pauseVideo", true);
  1365. }
  1366. var d = moment.duration(duration, 'seconds');
  1367. if (Session.get("state") === "playing") {
  1368. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1369. }
  1370. }
  1371. }
  1372. }, 100);
  1373. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1374. resizeSeekerbar();
  1375. }, 500);
  1376. }
  1377. });
  1378. });
  1379. // Settings Template
  1380. Template.settings.events({
  1381. "click #save-settings": function() {
  1382. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  1383. },
  1384. "click #delete-account": function(){
  1385. $("#delete-account").text("Click to confirm");
  1386. $("#delete-account").click(function(){
  1387. var bool = confirm("Are you sure you want to delete your account?");
  1388. if(bool) {
  1389. Meteor.call("deleteAccount");
  1390. }
  1391. })
  1392. },
  1393. "click #change-password": function(){
  1394. var oldPassword = $("#old-password").val();
  1395. var newPassword= $("#new-password").val();
  1396. var confirmPassword = $("#confirm-password").val();
  1397. if(newPassword === confirmPassword){
  1398. Accounts.changePassword(oldPassword, newPassword, function(err){
  1399. if(err){
  1400. $("#old-password").val("");
  1401. $("#new-password").val("");
  1402. $("#confirm-password").val("");
  1403. $("<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(); });
  1404. } else {
  1405. $("#old-password").val("");
  1406. $("#new-password").val("");
  1407. $("#confirm-password").val("");
  1408. $("<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(); });
  1409. }
  1410. });
  1411. }
  1412. }
  1413. });
  1414. Template.stations.events({
  1415. "click .preview-button": function(e){
  1416. Session.set("song", this);
  1417. },
  1418. "click #previewImageButton": function() {
  1419. $("#preview-image").attr("src", Session.get("song").img);
  1420. },
  1421. "click .edit-queue-button": function(e){
  1422. Session.set("song", this);
  1423. Session.set("genre", $(e.target).data("genre"));
  1424. Session.set("type", "queue");
  1425. $("#type").val(this.type);
  1426. $("#mid").val(this.mid);
  1427. $("#artist").val(this.artist);
  1428. $("#title").val(this.title);
  1429. $("#img").val(this.img);
  1430. $("#id").val(this.id);
  1431. $("#likes").val(this.likes);
  1432. $("#dislikes").val(this.dislikes);
  1433. $("#duration").val(this.duration);
  1434. $("#skip-duration").val(this.skipDuration);
  1435. },
  1436. "click .edit-playlist-button": function(e){
  1437. Session.set("song", this);
  1438. Session.set("genre", $(e.target).data("genre"));
  1439. Session.set("type", "playlist");
  1440. $("#type").val(this.type);
  1441. $("#mid").val(this.mid);
  1442. $("#artist").val(this.artist);
  1443. $("#title").val(this.title);
  1444. $("#img").val(this.img);
  1445. $("#id").val(this.id);
  1446. $("#likes").val(this.likes);
  1447. $("#dislikes").val(this.dislikes);
  1448. $("#duration").val(this.duration);
  1449. $("#skip-duration").val(this.skipDuration);
  1450. },
  1451. "click .add-song-button": function(e){
  1452. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1453. Meteor.call("addSongToPlaylist", genre, this);
  1454. },
  1455. "click .deny-song-button": function(e){
  1456. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1457. Meteor.call("removeSongFromQueue", genre, this.mid);
  1458. },
  1459. "click .remove-song-button": function(e){
  1460. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1461. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  1462. },
  1463. "click #moveSong": function(e){
  1464. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1465. if (genre !== Session.get(genre)) {
  1466. 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});
  1467. Meteor.call("removeSongFromPlaylist", Session.get("genre"), Session.get("song").mid);
  1468. }else {
  1469. console.log("Something Went Wrong?!");
  1470. return false;
  1471. }
  1472. },
  1473. "click #copySong": function(e){
  1474. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1475. 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});
  1476. },
  1477. "click .copyMove-button": function(e){
  1478. Session.set("song", this);
  1479. Session.set("genre", $(e.target).data("genre"));
  1480. },
  1481. "click #play": function() {
  1482. $("#play").attr("disabled", true);
  1483. $("#stop").attr("disabled", false);
  1484. var song = Session.get("song");
  1485. var id = song.id;
  1486. var type = song.type;
  1487. var volume = localStorage.getItem("volume") || 20;
  1488. if (type === "YouTube") {
  1489. if (YTPlayer === undefined) {
  1490. YTPlayer = new YT.Player("previewPlayer", {
  1491. height: 540,
  1492. width: 568,
  1493. videoId: id,
  1494. playerVars: {controls: 0, iv_load_policy: 3, showinfo: 0},
  1495. events: {
  1496. 'onReady': function(event) {
  1497. event.target.seekTo(Number(song.skipDuration));
  1498. event.target.playVideo();
  1499. event.target.setVolume(volume);
  1500. },
  1501. 'onStateChange': function(event){
  1502. if (event.data == YT.PlayerState.PAUSED) {
  1503. event.target.playVideo();
  1504. }
  1505. if (event.data == YT.PlayerState.PLAYING) {
  1506. $("#play").attr("disabled", true);
  1507. $("#stop").attr("disabled", false);
  1508. } else {
  1509. $("#play").attr("disabled", false);
  1510. $("#stop").attr("disabled", true);
  1511. }
  1512. }
  1513. }
  1514. });
  1515. } else {
  1516. YTPlayer.loadVideoById(id);
  1517. YTPlayer.seekTo(Number(song.skipDuration));
  1518. }
  1519. $("#previewPlayer").show();
  1520. } else if (type === "SoundCloud") {
  1521. SC.stream("/tracks/" + song.id, function(sound) {
  1522. SCPlayer = sound;
  1523. sound.setVolume(volume / 100);
  1524. sound.play();
  1525. });
  1526. }
  1527. if (previewEndSongTimeout !== undefined) {
  1528. Meteor.clearTimeout(previewEndSongTimeout);
  1529. }
  1530. previewEndSongTimeout = Meteor.setTimeout(function() {
  1531. if (YTPlayer !== undefined) {
  1532. YTPlayer.stopVideo();
  1533. }
  1534. if (SCPlayer !== undefined) {
  1535. SCPlayer.stop();
  1536. }
  1537. $("#play").attr("disabled", false);
  1538. $("#stop").attr("disabled", true);
  1539. $("#previewPlayer").hide();
  1540. }, song.duration * 1000);
  1541. },
  1542. "click #stop": function() {
  1543. $("#play").attr("disabled", false);
  1544. $("#stop").attr("disabled", true);
  1545. if (previewEndSongTimeout !== undefined) {
  1546. Meteor.clearTimeout(previewEndSongTimeout);
  1547. }
  1548. if (YTPlayer !== undefined) {
  1549. YTPlayer.stopVideo();
  1550. }
  1551. if (SCPlayer !== undefined) {
  1552. SCPlayer.stop();
  1553. }
  1554. },
  1555. "click #forward": function() {
  1556. var error = false;
  1557. if (YTPlayer !== undefined) {
  1558. var duration = Number(Session.get("song").duration) | 0;
  1559. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1560. if (YTPlayer.getDuration() < duration + skipDuration) {
  1561. alert("The duration of the YouTube video is smaller than the duration.");
  1562. error = true;
  1563. } else {
  1564. YTPlayer.seekTo(skipDuration + duration - 10);
  1565. }
  1566. }
  1567. if (SCPlayer !== undefined) {
  1568. SCPlayer.seekTo((skipDuration + duration - 10) * 1000);
  1569. }
  1570. if (!error) {
  1571. if (previewEndSongTimeout !== undefined) {
  1572. Meteor.clearTimeout(previewEndSongTimeout);
  1573. }
  1574. previewEndSongTimeout = Meteor.setTimeout(function() {
  1575. if (YTPlayer !== undefined) {
  1576. YTPlayer.stopVideo();
  1577. }
  1578. if (SCPlayer !== undefined) {
  1579. SCPlayer.stop();
  1580. }
  1581. $("#play").attr("disabled", false);
  1582. $("#stop").attr("disabled", true);
  1583. $("#previewPlayer").hide();
  1584. }, 10000);
  1585. }
  1586. },
  1587. "click #get-spotify-info": function() {
  1588. var search = $("#title").val();
  1589. var artistName = $("#artist").val();
  1590. getSpotifyInfo(search, function(data) {
  1591. for(var i in data){
  1592. for(var j in data[i].items){
  1593. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1594. $("#img").val(data[i].items[j].album.images[2].url);
  1595. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1596. return;
  1597. }
  1598. }
  1599. }
  1600. }, artistName);
  1601. },
  1602. "click #save-song-button": function() {
  1603. var newSong = {};
  1604. newSong.id = $("#id").val();
  1605. newSong.likes = Number($("#likes").val());
  1606. newSong.dislikes = Number($("#dislikes").val());
  1607. newSong.title = $("#title").val();
  1608. newSong.artist = $("#artist").val();
  1609. newSong.img = $("#img").val();
  1610. newSong.type = $("#type").val();
  1611. newSong.duration = Number($("#duration").val());
  1612. newSong.skipDuration = $("#skip-duration").val();
  1613. if(newSong.skipDuration === undefined){
  1614. newSong.skipDuration = 0;
  1615. };
  1616. if (Session.get("type") === "playlist") {
  1617. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1618. $('#editModal').modal('hide');
  1619. });
  1620. } else {
  1621. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1622. $('#editModal').modal('hide');
  1623. });
  1624. }
  1625. },
  1626. "click .delete-room": function(){
  1627. var typeDel = $(this)[0].type;
  1628. Meteor.call("deleteRoom", typeDel);
  1629. }
  1630. });