events.js 69 KB

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