events.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. var feedbackData;
  2. function getSpotifyInfo(title, cb, artist) {
  3. var q = "";
  4. q = title;
  5. if (artist !== undefined) {
  6. q += " artist:" + artist;
  7. }
  8. $.ajax({
  9. type: "GET",
  10. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  11. applicationType: "application/json",
  12. contentType: "json",
  13. success: function (data) {
  14. cb(data);
  15. }
  16. });
  17. }
  18. function executeCommand(command, params){
  19. if (command === "help" || command === "commands") {
  20. $('#helpModal').modal('show');
  21. return true;
  22. } else if (command === "volume") {
  23. if (params.length === 1) {
  24. var volume = Number(params[0]);
  25. if (volume >= 0 || volume <= 100) {
  26. if (volume === 0) {
  27. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  28. } else {
  29. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  30. }
  31. $("#volume-slider").slider("setValue", volume);
  32. if (YTPlayer !== undefined) {
  33. YTPlayer.setVolume(volume);
  34. localStorage.setItem("volume", volume);
  35. }
  36. return true;
  37. }
  38. }
  39. } else if(command === "mute"){
  40. $("#volume-slider").slider("setValue", 0);
  41. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off");
  42. if (YTPlayer !== undefined) {
  43. YTPlayer.setVolume(0);
  44. localStorage.setItem("volume", 0);
  45. }
  46. } else if(command === "ban"){
  47. var user = params[0];
  48. var time = params[1];
  49. var reason = params[2];
  50. Meteor.call("banUser", user, time, reason, function(err, res){
  51. if(err){
  52. console.log(err);
  53. }
  54. });
  55. } else if(command === "silence"){
  56. var user = params[0];
  57. var time = params[1];
  58. Meteor.call("muteUser", user, time, function(err, res){
  59. if(err){
  60. console.log(err);
  61. }
  62. });
  63. } else if(command === "unban"){
  64. var user = params[0];
  65. Meteor.call("unbanUser", user, function(err, res){
  66. if(err){
  67. console.log(err);
  68. }
  69. });
  70. } else if(command === "unsilence"){
  71. var user = params[0];
  72. Meteor.call("unsilenceUser", user, function(err, res){
  73. if(err){
  74. console.log(err);
  75. }
  76. });
  77. } else if(command === "pause"){
  78. Meteor.call("pauseRoom", Session.get("type"), function(err, res){
  79. if(err){
  80. console.log(err);
  81. }
  82. });
  83. } else if(command === "resume"){
  84. Meteor.call("resumeRoom", Session.get("type"), function(err, res){
  85. if(err){
  86. console.log(err);
  87. }
  88. });
  89. } else if(command === "shuffle"){
  90. Meteor.call("shufflePlaylist", Session.get("type"), function(err, res){
  91. if(err){
  92. console.log(err);
  93. }
  94. });
  95. } else if(command === "skip"){
  96. Meteor.call("skipSong", Session.get("type"), function(err, res){
  97. if(err){
  98. console.log(err);
  99. }
  100. });
  101. }
  102. }
  103. function sendMessage() {
  104. var message = $("#chat-input").val();
  105. if (!$("#chat-input").hasClass("disabled")) {
  106. if (message.length > 0 && message[0] !== " ") {
  107. if (message[0] === "/") {
  108. message = message.split("");
  109. message.shift();
  110. message = message.join("");
  111. var params = message.split(" ");
  112. params = params.map(function(param) {
  113. return param.replace(/\r?\n|\r/g, "");
  114. });
  115. var command = params.shift();
  116. command = command.replace(/\r?\n|\r/g, "");
  117. if (executeCommand(command, params)) {
  118. $("#chat-input").val("");
  119. } else {
  120. $("#chat-input").val("");
  121. }
  122. } else {
  123. $("#chat-input").addClass("disabled");
  124. $("#chat-input").attr("disabled", "");
  125. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  126. if(err){
  127. $("#chat-input").val("");
  128. $("#chat-input").removeAttr("disabled");
  129. $("#chat-input").removeClass("disabled");
  130. }
  131. if (res) {
  132. $("#chat-input").val("");
  133. $("#chat-input").removeAttr("disabled");
  134. $("#chat-input").removeClass("disabled");
  135. }
  136. });
  137. }
  138. }
  139. }
  140. }
  141. function sendMessageGlobal() {
  142. var message = $("#global-chat-input").val();
  143. if (!$("#global-chat-input").hasClass("disabled")) {
  144. if (message.length > 0 && message[0] !== " ") {
  145. if (message[0] === "/") {
  146. message = message.split("");
  147. message.shift();
  148. message = message.join("");
  149. var params = message.split(" ");
  150. var command = params.shift();
  151. command = command.replace(/\r?\n|\r/g, "");
  152. if (executeCommand(command, params)) {
  153. $("#global-chat-input").val("");
  154. } else {
  155. $("#global-chat-input").val("");
  156. }
  157. } else {
  158. $("#global-chat-input").addClass("disabled");
  159. $("#global-chat-input").attr("disabled", "");
  160. Meteor.call("sendMessage", "global", message, function (err, res) {
  161. if (res) {
  162. $("#global-chat-input").val("");
  163. }
  164. $("#global-chat-input").removeClass("disabled");
  165. $("#global-chat-input").removeAttr("disabled");
  166. });
  167. }
  168. }
  169. }
  170. }
  171. Template.admin.events({
  172. "click #croom_create": function() {
  173. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  174. if (err) {
  175. alert("Error " + err.error + ": " + err.reason);
  176. } else {
  177. window.location = "/" + $("#croom_tag").val();
  178. }
  179. });
  180. },
  181. "click a": function(e){
  182. var id = e.currentTarget.id;
  183. console.log(id.toLowerCase());
  184. Session.set("playlistToEdit", id);
  185. },
  186. "click #croom_create": function() {
  187. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#two").prop("checked"), function (err, res) {
  188. if (err) {
  189. alert("Error " + err.error + ": " + err.reason);
  190. } else {
  191. window.location = "/" + $("#croom_tag").val();
  192. }
  193. });
  194. },
  195. "click #rreset_confirm": function(){
  196. $('#confirmModal').modal('hide');
  197. Meteor.call("resetRating");
  198. },
  199. "click #edit_desc": function(){
  200. console.log($(this));
  201. console.log($(this)[0].type);
  202. Session.set("roomDesc", $(this)[0].type);
  203. },
  204. "click #submit_desc": function(){
  205. var description = $("#desc_text").val();
  206. Meteor.call("editRoomDesc", Session.get("roomDesc"), description);
  207. }
  208. });
  209. Template.alertsDashboard.events({
  210. "click #calart-create": function() {
  211. Meteor.call("addAlert", $("#calert-description").val(), $("#calert-priority").val().toLowerCase(), function (err, res) {
  212. if (err) {
  213. alert("Error " + err.error + ": " + err.reason);
  214. } else {
  215. $("#calert-description").val("");
  216. }
  217. });
  218. },
  219. "click #ralert-button": function() {
  220. Meteor.call("removeAlerts");
  221. }
  222. });
  223. Template.feedback.events({
  224. "click #feedback_submit": function(){
  225. if($("#feedback_message").val().length !== 0){
  226. Meteor.call("sendFeedback", $("#feedback_message").val());
  227. $("#feedback_message").val("");
  228. $("#modal1").closeModal()
  229. }
  230. },
  231. "click .upvote": function(){
  232. var message = $(this).parent("card").prevObject[0].message;
  233. Meteor.call("upvoteFeedback", message);
  234. },
  235. "click #delete": function(){
  236. var message = $(this).parent("card").prevObject[0].message;
  237. Meteor.call("deleteFeedback", message);
  238. },
  239. "click #edit": function(){
  240. $("#editModal").click()
  241. var data = Feedback.findOne({"message": $(this).parent("card").prevObject[0].message});
  242. feedbackData = data.message;
  243. $("#edit_feedback_message").val(data.message);
  244. },
  245. "click #edit_feedback_submit": function(){
  246. var oldMessage = feedbackData;
  247. var newMessage = $("#edit_feedback_message").val()
  248. $("#edit_feedback_message").val("")
  249. Meteor.call("updateFeedback", oldMessage, newMessage);
  250. $("#editFeedback").closeModal();
  251. }
  252. });
  253. Template.header.events({
  254. "click .logout": function(e){
  255. e.preventDefault();
  256. Meteor.logout();
  257. if (hpSound !== undefined) {
  258. hpSound.stop();
  259. }
  260. },
  261. "click #profile": function(){
  262. window.location = "/u/" + Meteor.user().profile.username;
  263. }
  264. });
  265. Template.login.events({
  266. "submit form": function(e){
  267. e.preventDefault();
  268. Session.set("github", false);
  269. var username = $("#username").val()
  270. var password = $("#password").val();
  271. Meteor.loginWithPassword(username, password, function(err) {
  272. if (err) {
  273. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  274. $(".landing").before(errAlert);
  275. Meteor.setTimeout(function() {
  276. errAlert.fadeOut(5000, function() {
  277. errAlert.remove();
  278. });
  279. }, 5000);
  280. } else {
  281. window.location.href = "/";
  282. }
  283. });
  284. },
  285. "click #github-login": function(){
  286. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  287. console.log(err, res);
  288. });
  289. }
  290. });
  291. Template.playlist.events({
  292. "keyup #search-playlist": function(){
  293. if($("#search-playlist").val().length === 0){
  294. $(".pl-item").show();
  295. } else {
  296. $(".pl-item").hide();
  297. var input = $("#search-playlist").val().toLowerCase();
  298. $(".pl-item strong").each(function(i, el){
  299. if($(el).text().toLowerCase().indexOf(input) !== -1){
  300. $(el).parent(".pl-item").show();
  301. }
  302. })
  303. $(".pl-item #pl-artist").each(function(i, el){
  304. if($(el).text().toLowerCase().indexOf(input) !== -1){
  305. $(el).parent(".pl-item").show();
  306. }
  307. })
  308. }
  309. },
  310. "click #pl-item": function(){
  311. console.log($(this).text());
  312. }
  313. });
  314. Template.profile.events({
  315. //Edit real name
  316. "click #edit-name": function(){
  317. $("#name").hide();
  318. $("#name-div").show();
  319. $("#edit-name").hide();
  320. $("#cancel-edit").show();
  321. },
  322. "click #submit-name": function(){
  323. var user = Meteor.user();
  324. $("#name").show();
  325. $("#name-div").hide();
  326. $("#edit-name").show();
  327. $("#cancel-edit").hide();
  328. var realname = $("#input-name").val();
  329. var username = user.profile.username;
  330. $("#name").text("Name: " + realname);
  331. $("#input-name").val("")
  332. Meteor.call("updateRealName", realname);
  333. },
  334. "click #cancel-edit": function(){
  335. $("#name").show();
  336. $("#name-div").hide();
  337. $("#edit-name").show();
  338. $("#cancel-edit").hide();
  339. $("#input-name").val("");
  340. },
  341. //Edit username
  342. "click #edit-username": function(){
  343. $("#username").hide();
  344. $("#username-div").show();
  345. $("#edit-username").hide();
  346. $("#cancel-username").show();
  347. },
  348. "click #submit-username": function(){
  349. var user = Meteor.user()
  350. $("#username").show();
  351. $("#username-div").hide();
  352. $("#edit-username").show();
  353. $("#cancel-username").hide();
  354. var username = user.username;
  355. var newUserName = $("#input-username").val();
  356. $("#profile-name").text(newUserName)
  357. $("#username").text("Username: " + newUserName);
  358. $("#input-username").val("")
  359. Meteor.call("updateUserName", newUserName);
  360. window.location = "/u/" + newUserName;
  361. },
  362. "click #cancel-username": function(){
  363. $("#username").show();
  364. $("#username-div").hide();
  365. $("#edit-username").show();
  366. $("#cancel-username").hide();
  367. $("#input-username").val("");
  368. },
  369. // Admins only Edit Rank
  370. "click #edit-rank": function() {
  371. $("#rank").hide();
  372. $("#rank-div").show();
  373. $("#edit-rank").hide();
  374. $("#cancel-rank").show();
  375. },
  376. "click #submit-rank": function() {
  377. $("#rank").show();
  378. $("#rank-div").hide();
  379. $("#edit-rank").show();
  380. $("#cancel-rank").hide();
  381. var newRank = $("#select-rank option:selected").val();
  382. var username = Session.get("username");
  383. console.log(username, newRank);
  384. },
  385. "click #cancel-rank": function() {
  386. $("#rank").show();
  387. $("#rank-div").hide();
  388. $("#edit-rank").show();
  389. $("#cancel-rank").hide();
  390. }
  391. });
  392. var seekerBarInterval = undefined;
  393. Template.queues.events({
  394. /* TODO Add undo delete button */
  395. "input #id": function() {
  396. console.log("Change!");
  397. $("#previewPlayerContainer").addClass("hide-preview");
  398. },
  399. "input #img": function() {
  400. var url = $("#img").val();
  401. console.log(url);
  402. Session.set("image_url", url);
  403. },
  404. "click .preview-button": function(e){
  405. Session.set("song", this);
  406. $("#previewModal").openModal();
  407. },
  408. "click #previewImageButton": function() {
  409. $("#preview-image").attr("src", Session.get("song").img);
  410. },
  411. "click .edit-queue-button": function(e){
  412. Session.set("song", this);
  413. Session.set("genre", $(e.target).data("genre"));
  414. Session.set("type", "queue");
  415. $("#mid").val(this.mid).change();
  416. $("#artist").val(this.artist).change();
  417. $("#title").val(this.title).change();
  418. $("#img").val(this.img).change();
  419. $("#id").val(this.id).change();
  420. $("#likes").val(this.likes).change();
  421. $("#dislikes").val(this.dislikes).change();
  422. $("#duration").val(this.duration).change();
  423. $("#skip-duration").val(this.skipDuration).change();
  424. $("#previewPlayerContainer").addClass("hide-preview");
  425. Session.set("image_url", this.img);
  426. Session.set("editing", true);
  427. $("#editModal").openModal({
  428. complete : function() {
  429. Session.set("editing", false);
  430. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  431. YTPlayer.stopVideo();
  432. }
  433. }
  434. });
  435. },
  436. "click .add-song-button": function(e){
  437. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  438. Meteor.call("addSongToPlaylist", genre, this, function(err) {
  439. console.log(err);
  440. if (err) {
  441. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  442. Materialize.toast($toastContent, 8000);
  443. }
  444. });
  445. },
  446. "click .deny-song-button": function(e){
  447. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  448. Meteor.call("removeSongFromQueue", genre, this.mid);
  449. },
  450. "click #play": function() {
  451. var duration = Session.get("song").duration;
  452. var d = moment.duration(parseInt(duration), 'seconds');
  453. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  454. $("#previewPlayerContainer").removeClass("hide-preview");
  455. var song = Session.get("song");
  456. var id = song.id;
  457. var volume = localStorage.getItem("volume") || 20;
  458. if (song.duration !== 0) {
  459. $("#play").attr("disabled", true);
  460. $("#stop").attr("disabled", false);
  461. $("#pause").attr("disabled", false);
  462. $("#forward").attr("disabled", false);
  463. if (YTPlayer === undefined) {
  464. YTPlayer = new YT.Player("previewPlayer", {
  465. height: 540,
  466. width: 568,
  467. videoId: id,
  468. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  469. events: {
  470. 'onReady': function(event) {
  471. event.target.seekTo(Number(song.skipDuration));
  472. event.target.playVideo();
  473. event.target.setVolume(volume);
  474. },
  475. 'onStateChange': function(event){
  476. if (event.data == YT.PlayerState.PAUSED) {
  477. if (seekerBarInterval !== undefined) {
  478. Meteor.clearInterval(seekerBarInterval);
  479. seekerBarInterval = undefined;
  480. }
  481. }
  482. if (event.data == YT.PlayerState.UNSTARTED) {
  483. if (seekerBarInterval !== undefined) {
  484. Meteor.clearInterval(seekerBarInterval);
  485. seekerBarInterval = undefined;
  486. }
  487. $(".seeker-bar").css({width: "0"});
  488. $("#time-elapsed").text("0:00");
  489. $("#previewPlayerContainer").addClass("hide-preview");
  490. }
  491. if (event.data == YT.PlayerState.PLAYING) {
  492. seekerBarInterval = Meteor.setInterval(function() {
  493. var duration = Session.get("song").duration;
  494. var timeElapsed = YTPlayer.getCurrentTime();
  495. var skipDuration = Session.get("song").skipDuration;
  496. if (duration <= (timeElapsed - skipDuration)) {
  497. YTPlayer.stopVideo();
  498. $("#play").attr("disabled", false);
  499. $("#stop").attr("disabled", true);
  500. $("#pause").attr("disabled", true);
  501. $("#forward").attr("disabled", true);
  502. $("#previewPlayerContainer").addClass("hide-preview");
  503. $(".seeker-bar").css({width: "0"});
  504. $("#time-elapsed").text("0:00");
  505. Meteor.clearInterval(seekerBarInterval);
  506. } else {
  507. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  508. $(".seeker-bar").css({width: percentComplete + "%"});
  509. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  510. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  511. }
  512. }, 100);
  513. $("#play").attr("disabled", true);
  514. $("#stop").attr("disabled", false);
  515. $("#pause").attr("disabled", false);
  516. $("#forward").attr("disabled", false);
  517. } else {
  518. $("#play").attr("disabled", false);
  519. $("#stop").attr("disabled", true);
  520. $("#pause").attr("disabled", true);
  521. $("#forward").attr("disabled", true);
  522. }
  523. }
  524. }
  525. });
  526. } else {
  527. if (YTPlayer.getPlayerState() === 2) {
  528. YTPlayer.playVideo();
  529. } else {
  530. console.log(id, song.skipDuration, song.duration);
  531. YTPlayer.loadVideoById(id);
  532. YTPlayer.seekTo(Number(song.skipDuration));
  533. }
  534. }
  535. $("#previewPlayerContainer").removeClass("hide-preview");
  536. }
  537. },
  538. "click #stop": function() {
  539. $("#play").attr("disabled", false);
  540. $("#stop").attr("disabled", true);
  541. $("#pause").attr("disabled", true);
  542. $("#forward").attr("disabled", true);
  543. if (previewEndSongTimeout !== undefined) {
  544. Meteor.clearTimeout(previewEndSongTimeout);
  545. }
  546. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  547. YTPlayer.stopVideo();
  548. }
  549. },
  550. "click #pause": function() {
  551. $("#play").attr("disabled", false);
  552. $("#stop").attr("disabled", false);
  553. $("#pause").attr("disabled", true);
  554. $("#forward").attr("disabled", true);
  555. if (previewEndSongTimeout !== undefined) {
  556. Meteor.clearTimeout(previewEndSongTimeout);
  557. }
  558. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  559. YTPlayer.pauseVideo();
  560. }
  561. },
  562. "click #forward": function() {
  563. var error = false;
  564. if (YTPlayer !== undefined) {
  565. var duration = Number(Session.get("song").duration) | 0;
  566. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  567. if (YTPlayer.getDuration() < duration + skipDuration) {
  568. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  569. Materialize.toast($toastContent, 8000);
  570. error = true;
  571. } else {
  572. YTPlayer.seekTo(skipDuration + duration - 10);
  573. }
  574. }
  575. if (!error) {
  576. if (previewEndSongTimeout !== undefined) {
  577. Meteor.clearTimeout(previewEndSongTimeout);
  578. }
  579. previewEndSongTimeout = Meteor.setTimeout(function() {
  580. if (YTPlayer !== undefined) {
  581. YTPlayer.stopVideo();
  582. }
  583. $("#play").attr("disabled", false);
  584. $("#stop").attr("disabled", true);
  585. $("#pause").attr("disabled", true);
  586. $("#forward").attr("disabled", true);
  587. $("#previewPlayerContainer").addClass("hide-preview");
  588. }, 10000);
  589. }
  590. },
  591. "click #get-spotify-info": function() {
  592. var search = $("#title").val();
  593. var artistName = $("#artist").val();
  594. getSpotifyInfo(search, function(data) {
  595. for(var i in data){
  596. for(var j in data[i].items){
  597. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  598. $("#img").val(data[i].items[j].album.images[2].url).change();
  599. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  600. return;
  601. }
  602. }
  603. }
  604. }, artistName);
  605. },
  606. "click #save-song-button": function() {
  607. var newSong = {};
  608. newSong.mid = $("#mid").val();
  609. newSong.id = $("#id").val();
  610. newSong.likes = Number($("#likes").val());
  611. newSong.dislikes = Number($("#dislikes").val());
  612. newSong.title = $("#title").val();
  613. newSong.artist = $("#artist").val();
  614. newSong.img = $("#img").val();
  615. newSong.duration = Number($("#duration").val());
  616. newSong.skipDuration = $("#skip-duration").val();
  617. newSong.requestedBy = Session.get("song").requestedBy;
  618. if(newSong.skipDuration === undefined){
  619. newSong.skipDuration = 0;
  620. }
  621. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function(err, res) {
  622. console.log(err, res);
  623. if (err) {
  624. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  625. Materialize.toast($toastContent, 8000);
  626. } else {
  627. Session.set("song", newSong);
  628. }
  629. });
  630. }
  631. });
  632. Template.manageStation.events({
  633. /* TODO Add undo delete button */
  634. "input #id": function() {
  635. $("#previewPlayerContainer").addClass("hide-preview");
  636. },
  637. "input #img": function() {
  638. var url = $("#img").val();
  639. Session.set("image_url", url);
  640. },
  641. "click .preview-button": function(e){
  642. Session.set("song", this);
  643. $("#previewModal").openModal();
  644. },
  645. "click #previewImageButton": function() {
  646. $("#preview-image").attr("src", Session.get("song").img);
  647. },
  648. "click .edit-song-button": function(e){
  649. Session.set("song", this);
  650. Session.set("genre", $(e.target).data("genre"));
  651. $("#mid").val(this.mid).change();
  652. $("#artist").val(this.artist).change();
  653. $("#title").val(this.title).change();
  654. $("#img").val(this.img).change();
  655. $("#id").val(this.id).change();
  656. $("#likes").val(this.likes).change();
  657. $("#dislikes").val(this.dislikes).change();
  658. $("#duration").val(this.duration).change();
  659. $("#skip-duration").val(this.skipDuration).change();
  660. $("#previewPlayerContainer").addClass("hide-preview");
  661. Session.set("image_url", this.img);
  662. Session.set("editing", true);
  663. $("#editModal").openModal({
  664. complete : function() {
  665. Session.set("editing", false);
  666. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  667. YTPlayer.stopVideo();
  668. }
  669. }
  670. });
  671. },
  672. "click .remove-song-button": function(e){
  673. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  674. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  675. },
  676. "click #play": function() {
  677. var duration = Session.get("song").duration;
  678. var d = moment.duration(parseInt(duration), 'seconds');
  679. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  680. $("#previewPlayerContainer").removeClass("hide-preview");
  681. var song = Session.get("song");
  682. var id = song.id;
  683. var volume = localStorage.getItem("volume") || 20;
  684. if (song.duration !== 0) {
  685. $("#play").attr("disabled", true);
  686. $("#stop").attr("disabled", false);
  687. $("#pause").attr("disabled", false);
  688. $("#forward").attr("disabled", false);
  689. if (YTPlayer === undefined) {
  690. YTPlayer = new YT.Player("previewPlayer", {
  691. height: 540,
  692. width: 568,
  693. videoId: id,
  694. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  695. events: {
  696. 'onReady': function(event) {
  697. event.target.seekTo(Number(song.skipDuration));
  698. event.target.playVideo();
  699. event.target.setVolume(volume);
  700. },
  701. 'onStateChange': function(event){
  702. if (event.data == YT.PlayerState.PAUSED) {
  703. if (seekerBarInterval !== undefined) {
  704. Meteor.clearInterval(seekerBarInterval);
  705. seekerBarInterval = undefined;
  706. }
  707. }
  708. if (event.data == YT.PlayerState.UNSTARTED) {
  709. if (seekerBarInterval !== undefined) {
  710. Meteor.clearInterval(seekerBarInterval);
  711. seekerBarInterval = undefined;
  712. }
  713. $(".seeker-bar").css({width: "0"});
  714. $("#time-elapsed").text("0:00");
  715. $("#previewPlayerContainer").addClass("hide-preview");
  716. }
  717. if (event.data == YT.PlayerState.PLAYING) {
  718. seekerBarInterval = Meteor.setInterval(function() {
  719. var duration = Session.get("song").duration;
  720. var timeElapsed = YTPlayer.getCurrentTime();
  721. var skipDuration = Session.get("song").skipDuration;
  722. if (duration <= (timeElapsed - skipDuration)) {
  723. YTPlayer.stopVideo();
  724. $("#play").attr("disabled", false);
  725. $("#stop").attr("disabled", true);
  726. $("#pause").attr("disabled", true);
  727. $("#forward").attr("disabled", true);
  728. $("#previewPlayerContainer").addClass("hide-preview");
  729. $(".seeker-bar").css({width: "0"});
  730. $("#time-elapsed").text("0:00");
  731. Meteor.clearInterval(seekerBarInterval);
  732. } else {
  733. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  734. $(".seeker-bar").css({width: percentComplete + "%"});
  735. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  736. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  737. }
  738. }, 100);
  739. $("#play").attr("disabled", true);
  740. $("#stop").attr("disabled", false);
  741. $("#pause").attr("disabled", false);
  742. $("#forward").attr("disabled", false);
  743. } else {
  744. $("#play").attr("disabled", false);
  745. $("#stop").attr("disabled", true);
  746. $("#pause").attr("disabled", true);
  747. $("#forward").attr("disabled", true);
  748. }
  749. }
  750. }
  751. });
  752. } else {
  753. if (YTPlayer.getPlayerState() === 2) {
  754. YTPlayer.playVideo();
  755. } else {
  756. console.log(id, song.skipDuration, song.duration);
  757. YTPlayer.loadVideoById(id);
  758. YTPlayer.seekTo(Number(song.skipDuration));
  759. }
  760. }
  761. $("#previewPlayerContainer").removeClass("hide-preview");
  762. }
  763. },
  764. "click #stop": function() {
  765. $("#play").attr("disabled", false);
  766. $("#stop").attr("disabled", true);
  767. $("#pause").attr("disabled", true);
  768. $("#forward").attr("disabled", true);
  769. if (previewEndSongTimeout !== undefined) {
  770. Meteor.clearTimeout(previewEndSongTimeout);
  771. }
  772. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  773. YTPlayer.stopVideo();
  774. }
  775. },
  776. "click #pause": function() {
  777. $("#play").attr("disabled", false);
  778. $("#stop").attr("disabled", false);
  779. $("#pause").attr("disabled", true);
  780. $("#forward").attr("disabled", true);
  781. if (previewEndSongTimeout !== undefined) {
  782. Meteor.clearTimeout(previewEndSongTimeout);
  783. }
  784. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  785. YTPlayer.pauseVideo();
  786. }
  787. },
  788. "click #forward": function() {
  789. var error = false;
  790. if (YTPlayer !== undefined) {
  791. var duration = Number(Session.get("song").duration) | 0;
  792. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  793. if (YTPlayer.getDuration() < duration + skipDuration) {
  794. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  795. Materialize.toast($toastContent, 8000);
  796. error = true;
  797. } else {
  798. YTPlayer.seekTo(skipDuration + duration - 10);
  799. }
  800. }
  801. if (!error) {
  802. if (previewEndSongTimeout !== undefined) {
  803. Meteor.clearTimeout(previewEndSongTimeout);
  804. }
  805. previewEndSongTimeout = Meteor.setTimeout(function() {
  806. if (YTPlayer !== undefined) {
  807. YTPlayer.stopVideo();
  808. }
  809. $("#play").attr("disabled", false);
  810. $("#stop").attr("disabled", true);
  811. $("#pause").attr("disabled", true);
  812. $("#forward").attr("disabled", true);
  813. $("#previewPlayerContainer").addClass("hide-preview");
  814. }, 10000);
  815. }
  816. },
  817. "click #get-spotify-info": function() {
  818. var search = $("#title").val();
  819. var artistName = $("#artist").val();
  820. getSpotifyInfo(search, function(data) {
  821. for(var i in data){
  822. for(var j in data[i].items){
  823. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  824. $("#img").val(data[i].items[j].album.images[2].url).change();
  825. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  826. return;
  827. }
  828. }
  829. }
  830. }, artistName);
  831. },
  832. "click #save-song-button": function() {
  833. var newSong = {};
  834. newSong.mid = $("#mid").val();
  835. newSong.id = $("#id").val();
  836. newSong.likes = Number($("#likes").val());
  837. newSong.dislikes = Number($("#dislikes").val());
  838. newSong.title = $("#title").val();
  839. newSong.artist = $("#artist").val();
  840. newSong.img = $("#img").val();
  841. newSong.duration = Number($("#duration").val());
  842. newSong.skipDuration = $("#skip-duration").val();
  843. newSong.requestedBy = Session.get("song").requestedBy;
  844. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function(err, res) {
  845. console.log(err, res);
  846. if (err) {
  847. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  848. Materialize.toast($toastContent, 8000);
  849. } else {
  850. Session.set("song", newSong);
  851. }
  852. });
  853. }
  854. });
  855. Template.register.events({
  856. "submit form": function(e){
  857. e.preventDefault();
  858. var username = $("#username").val()
  859. var email = $("#email").val()
  860. var password = $("#password").val();
  861. var captchaData = grecaptcha.getResponse();
  862. console.log(captchaData)
  863. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  864. grecaptcha.reset();
  865. if (err) {
  866. console.log(err);
  867. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  868. $(".landing").before(errAlert);
  869. Meteor.setTimeout(function() {
  870. errAlert.fadeOut(5000, function() {
  871. errAlert.remove();
  872. });
  873. }, 5000);
  874. } else {
  875. Meteor.loginWithPassword(username, password);
  876. Accounts.onLogin(function(){
  877. window.location.href = "/";
  878. })
  879. }
  880. });
  881. },
  882. "click #github-login": function(){
  883. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  884. console.log(err, res);
  885. });
  886. }
  887. });
  888. Template.room.events({
  889. "click #youtube-playlist-button": function () {
  890. if (!Session.get("importingPlaylist")) {
  891. var playlist_link = $("#youtube-playlist-input").val();
  892. var playlist_id = gup("list", playlist_link);
  893. var ytImportQueue = [];
  894. var totalVideos = 0;
  895. var videosInvalid = 0;
  896. var videosInQueue = 0;
  897. var videosInPlaylist = 0;
  898. var ranOnce = false;
  899. Session.set("importingPlaylist", true);
  900. $("#youtube-playlist-button").attr("disabled", "");
  901. $("#youtube-playlist-button").addClass("disabled");
  902. $("#youtube-playlist-input").attr("disabled", "");
  903. $("#youtube-playlist-input").addClass("disabled");
  904. $("#playlist-import-queue").empty();
  905. $("#playlist-import-queue").hide();
  906. $("#add-youtube-playlist").addClass("hidden-2");
  907. $("#import-progress").attr("aria-valuenow", 0);
  908. $("#import-progress").css({width: "0%"});
  909. $("#import-progress").text("0%");
  910. function makeAPICall(playlist_id, nextPageToken) {
  911. if (nextPageToken !== undefined) {
  912. nextPageToken = "&pageToken=" + nextPageToken;
  913. } else {
  914. nextPageToken = "";
  915. }
  916. $.ajax({
  917. type: "GET",
  918. url: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlist_id + nextPageToken + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  919. applicationType: "application/json",
  920. contentType: "json",
  921. success: function (data) {
  922. if (!ranOnce) {
  923. ranOnce = true;
  924. totalVideos = data.pageInfo.totalResults;
  925. }
  926. var nextToken = data.nextPageToken;
  927. for (var i in data.items) {
  928. var item = data.items[i];
  929. if (item.snippet.thumbnails !== undefined) {
  930. var genre = Session.get("type");
  931. if (Playlists.find({
  932. type: genre,
  933. "songs.id": item.snippet.resourceId.videoId
  934. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  935. videosInPlaylist++;
  936. } else if (Queues.find({
  937. type: genre,
  938. "songs.id": item.snippet.resourceId.videoId
  939. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  940. videosInQueue++;
  941. } else {
  942. $("#playlist-import-queue").append(
  943. "<div class='youtube-import-queue-item'>" +
  944. "<img src='" + item.snippet.thumbnails.medium.url + "' class='song-result-thumbnail'/>" +
  945. "<div>" +
  946. "<span class='song-result-title'>" + item.snippet.title + "</span>" +
  947. "<span class='song-result-channel'>" + item.snippet.channelTitle + "</span>" +
  948. "</div>" +
  949. "<i class='fa fa-times remove-import-song'></i>" +
  950. "</div>"
  951. );
  952. var percentage = ytImportQueue.length / (totalVideos - videosInvalid) * 100;
  953. $("#import-progress").attr("aria-valuenow", percentage.toFixed(2));
  954. $("#import-progress").css({width: percentage + "%"});
  955. $("#import-progress").text(percentage.toFixed(1) + "%");
  956. ytImportQueue.push({
  957. title: item.snippet.title,
  958. id: item.snippet.resourceId.videoId
  959. });
  960. }
  961. } else {
  962. videosInvalid++;
  963. }
  964. }
  965. if (nextToken !== undefined) {
  966. makeAPICall(playlist_id, nextToken);
  967. } else {
  968. $("#playlist-import-queue > div > i").click(function () {
  969. var title = $(this).parent().find("div > .song-result-title").text();
  970. for (var i in ytImportQueue) {
  971. if (ytImportQueue[i].title === title) {
  972. ytImportQueue.splice(i, 1);
  973. }
  974. }
  975. $(this).parent().remove();
  976. Session.set("YTImportQueue", ytImportQueue);
  977. });
  978. Session.set("importingPlaylist", false);
  979. $("#import-progress").attr("aria-valuenow", 100);
  980. $("#import-progress").css({width: "100%"});
  981. $("#import-progress").text("100%");
  982. $("#youtube-playlist-button").removeAttr("disabled");
  983. $("#youtube-playlist-button").removeClass("disabled");
  984. $("#youtube-playlist-input").removeAttr("disabled");
  985. $("#youtube-playlist-input").removeClass("disabled");
  986. $("#playlist-import-queue").show();
  987. $("#add-youtube-playlist").removeClass("hidden-2");
  988. Session.set("YTImportQueue", ytImportQueue);
  989. }
  990. }
  991. })
  992. }
  993. makeAPICall(playlist_id);
  994. }
  995. },
  996. "click #add-youtube-playlist": function () {
  997. var YTImportQueue = Session.get("YTImportQueue");
  998. $("#youtube-playlist-button").attr("disabled", "");
  999. $("#youtube-playlist-button").addClass("disabled");
  1000. $("#youtube-playlist-input").attr("disabled", "");
  1001. $("#youtube-playlist-input").addClass("disabled");
  1002. $("#import-progress").attr("aria-valuenow", 0);
  1003. $("#import-progress").css({width: "0%"});
  1004. $("#import-progress").text("0%");
  1005. var failed = 0;
  1006. var success = 0;
  1007. var processed = 0;
  1008. var total = YTImportQueue.length;
  1009. YTImportQueue.forEach(function (song) {
  1010. var songData = {type: "YouTube", id: song.id, title: song.title, artist: "", img: ""};
  1011. Meteor.call("addSongToQueue", Session.get("type"), songData, function (err, res) {
  1012. if (err) {
  1013. console.log(err);
  1014. failed++;
  1015. } else {
  1016. success++;
  1017. }
  1018. processed++;
  1019. var percentage = processed / total * 100;
  1020. $("#import-progress").attr("aria-valuenow", percentage.toFixed(2));
  1021. $("#import-progress").css({width: percentage + "%"});
  1022. $("#import-progress").text(percentage.toFixed(1) + "%");
  1023. });
  1024. });
  1025. },
  1026. "click #chat-tab": function () {
  1027. $("#chat-tab").removeClass("unread-messages");
  1028. },
  1029. "click #global-chat-tab": function () {
  1030. $("#global-chat-tab").removeClass("unread-messages");
  1031. },
  1032. "click #sync": function () {
  1033. if (Session.get("currentSong") !== undefined) {
  1034. var room = Rooms.findOne({type: Session.get("type")});
  1035. if (room !== undefined) {
  1036. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  1037. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  1038. if (YTPlayer !== undefined) {
  1039. YTPlayer.seekTo(skipDuration + timeIn / 1000);
  1040. }
  1041. }
  1042. }
  1043. },
  1044. "click #lock": function () {
  1045. Meteor.call("lockRoom", Session.get("type"));
  1046. },
  1047. "click #unlock": function () {
  1048. Meteor.call("unlockRoom", Session.get("type"));
  1049. },
  1050. "click #chat-tab": function (e) {
  1051. Meteor.setTimeout(function () {
  1052. $("#chat-ul").scrollTop(100000);
  1053. }, 1);
  1054. },
  1055. "click #global-chat-tab": function (e) {
  1056. Meteor.setTimeout(function () {
  1057. $("#global-chat-ul").scrollTop(100000);
  1058. }, 1);
  1059. },
  1060. "click #submit": function () {
  1061. sendMessage();
  1062. Meteor.setTimeout(function () {
  1063. $("#chat-ul").scrollTop(100000);
  1064. }, 1000)
  1065. },
  1066. "click #global-submit": function () {
  1067. sendMessageGlobal();
  1068. Meteor.setTimeout(function () {
  1069. $("#global-chat-ul").scrollTop(100000);
  1070. }, 1000)
  1071. },
  1072. "keyup #chat-input": function (e) {
  1073. if (e.type === "keyup" && e.which === 13) {
  1074. e.preventDefault();
  1075. if (!$('#chat-input').data('dropdownshown')) {
  1076. sendMessage();
  1077. Meteor.setTimeout(function () {
  1078. $("#chat-ul").scrollTop(100000);
  1079. }, 1000)
  1080. }
  1081. }
  1082. },
  1083. "keyup #global-chat-input": function (e) {
  1084. if (e.type === "keyup" && e.which === 13) {
  1085. e.preventDefault();
  1086. if (!$('#global-chat-input').data('dropdownshown')) {
  1087. sendMessageGlobal();
  1088. Meteor.setTimeout(function () {
  1089. $("#global-chat-ul").scrollTop(100000);
  1090. }, 1000)
  1091. }
  1092. }
  1093. },
  1094. "click #like": function (e) {
  1095. $("#like").blur();
  1096. Meteor.call("likeSong", Session.get("currentSong").mid);
  1097. },
  1098. "click #dislike": function (e) {
  1099. $("#dislike").blur();
  1100. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  1101. },
  1102. "click #vote-skip": function () {
  1103. Meteor.call("voteSkip", type, function (err, res) {
  1104. $("#vote-skip").attr("disabled", true);
  1105. });
  1106. },
  1107. "click #report-prev": function (e) {
  1108. if (Session.get("previousSong") !== undefined) {
  1109. Session.set("reportPrevious", true);
  1110. $("#report-prev").prop("disabled", true);
  1111. $("#report-curr").prop("disabled", false);
  1112. }
  1113. },
  1114. "click #report-curr": function (e) {
  1115. Session.set("reportPrevious", false);
  1116. $("#report-prev").prop("disabled", false);
  1117. $("#report-curr").prop("disabled", true);
  1118. },
  1119. "click #report-modal": function () {
  1120. Session.set("currentSongR", Session.get("currentSong"));
  1121. Session.set("previousSongR", Session.get("previousSong"));
  1122. },
  1123. "click #add-song-button": function (e) {
  1124. e.preventDefault();
  1125. parts = location.href.split('/');
  1126. var roomType = parts.pop();
  1127. var genre = roomType.toLowerCase();
  1128. var type = $("#type").val();
  1129. id = $("#id").val();
  1130. var title = $("#title").val();
  1131. var artist = $("#artist").val();
  1132. var img = $("#img").val();
  1133. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  1134. if (Playlists.find({
  1135. type: genre,
  1136. "songs.id": songData.id
  1137. }, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  1138. $("<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 () {
  1139. $(this).remove();
  1140. });
  1141. } else if (Queues.find({
  1142. type: genre,
  1143. "songs.id": songData.id
  1144. }, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  1145. $("<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 () {
  1146. $(this).remove();
  1147. });
  1148. } else {
  1149. Meteor.call("addSongToQueue", genre, songData, function (err, res) {
  1150. console.log(err, res);
  1151. if (err) {
  1152. $("<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 () {
  1153. $(this).remove();
  1154. });
  1155. } else {
  1156. $("<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 () {
  1157. $(this).remove();
  1158. });
  1159. }
  1160. });
  1161. }
  1162. $("#close-modal-a").click();
  1163. },
  1164. "click #toggle-video": function (e) {
  1165. e.preventDefault();
  1166. if (Session.get("mediaHidden")) {
  1167. $("#media-container").removeClass("hidden");
  1168. $("#toggle-video").text("Hide video");
  1169. Session.set("mediaHidden", false);
  1170. } else {
  1171. $("#media-container").addClass("hidden");
  1172. $("#toggle-video").text("Show video");
  1173. Session.set("mediaHidden", true);
  1174. }
  1175. },
  1176. "click #return": function (e) {
  1177. $("#add-info").hide();
  1178. $("#search-info").show();
  1179. },
  1180. "click #search-song": function () {
  1181. var songs = [];
  1182. $("#song-results").empty();
  1183. $.ajax({
  1184. type: "GET",
  1185. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  1186. applicationType: "application/json",
  1187. contentType: "json",
  1188. success: function (data) {
  1189. for (var i in data.items) {
  1190. var item = data.items[i];
  1191. $("#song-results").append(
  1192. "<div>" +
  1193. "<img src='" + item.snippet.thumbnails.medium.url + "' class='song-result-thumbnail'/>" +
  1194. "<div>" +
  1195. "<span class='song-result-title'>" + item.snippet.title + "</span>" +
  1196. "<span class='song-result-channel'>" + item.snippet.channelTitle + "</span>" +
  1197. "</div>" +
  1198. "</div>"
  1199. );
  1200. songs.push({title: item.snippet.title, id: item.id.videoId});
  1201. }
  1202. $("#song-results > div").click(function () {
  1203. $("#search-info").hide();
  1204. $("#add-info").show();
  1205. var title = $(this).find("div > .song-result-title").text();
  1206. for (var i in songs) {
  1207. if (songs[i].title === title) {
  1208. var songObj = {
  1209. id: songs[i].id,
  1210. title: songs[i].title,
  1211. type: "youtube"
  1212. };
  1213. $("#title").val(songObj.title);
  1214. $("#artist").val("");
  1215. $("#id").val(songObj.id);
  1216. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function (data) {
  1217. if (data.tracks.items.length > 0) {
  1218. $("#title").val(data.tracks.items[0].name);
  1219. var artists = [];
  1220. $("#img").val(data.tracks.items[0].album.images[2].url);
  1221. data.tracks.items[0].artists.forEach(function (artist) {
  1222. artists.push(artist.name);
  1223. });
  1224. $("#artist").val(artists.join(", "));
  1225. }
  1226. });
  1227. }
  1228. }
  1229. })
  1230. }
  1231. })
  1232. },
  1233. "click #volume-icon": function () {
  1234. var volume = 0;
  1235. var slider = $("#volume-slider").slider();
  1236. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1237. if (YTPlayer !== undefined) {
  1238. YTPlayer.setVolume(volume);
  1239. localStorage.setItem("volume", volume);
  1240. $("#volume-slider").slider("setValue", volume);
  1241. }
  1242. },
  1243. "click #play": function () {
  1244. Meteor.call("resumeRoom", type);
  1245. },
  1246. "click #pause": function () {
  1247. Meteor.call("pauseRoom", type);
  1248. },
  1249. "click #skip": function () {
  1250. Meteor.call("skipSong", type);
  1251. },
  1252. "click #shuffle": function () {
  1253. Meteor.call("shufflePlaylist", type);
  1254. },
  1255. "change input": function (e) {
  1256. if (e.target && e.target.id) {
  1257. var partsOfId = e.target.id.split("-");
  1258. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  1259. var camelCase = partsOfId.join("");
  1260. Session.set(camelCase, e.target.checked);
  1261. }
  1262. },
  1263. "click #report-song-button": function () {
  1264. var room = Session.get("type");
  1265. var reportData = {};
  1266. reportData.song = Session.get("currentSong").mid;
  1267. reportData.type = [];
  1268. reportData.reason = [];
  1269. $(".report-layer-1 > .checkbox input:checked").each(function () {
  1270. reportData.type.push(this.id);
  1271. if (this.id == "report-other") {
  1272. var otherText = $(".other-textarea").val();
  1273. }
  1274. });
  1275. $(".report-layer-2 input:checked").each(function () {
  1276. reportData.reason.push(this.id);
  1277. });
  1278. console.log(reportData);
  1279. Meteor.call("submitReport", room, reportData, Session.get("id"), function () {
  1280. $("#close-modal-r").click();
  1281. });
  1282. },
  1283. "change #si_or_pl": function () {
  1284. if ($("#select_playlist").is(':selected')) {
  1285. $("#search-info").hide();
  1286. $("#playlist-import").show();
  1287. }
  1288. if ($("#select_single").is(':selected')) {
  1289. $("#search-info").show();
  1290. $("#playlist-import").hide();
  1291. }
  1292. },
  1293. "click #close-modal-a": function () {
  1294. $("#select_single").attr("selected", true);
  1295. $("#search-info").show();
  1296. $("#playlist-import").hide();
  1297. }
  1298. });
  1299. Template.room.helpers({
  1300. singleVideo: function() {
  1301. return true;
  1302. },
  1303. chat: function() {
  1304. Meteor.setTimeout(function() {
  1305. var elem = document.getElementById('chat');
  1306. if (elem !== undefined && elem !== null) {
  1307. elem.scrollTop = elem.scrollHeight;
  1308. }
  1309. }, 100);
  1310. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1311. },
  1312. globalChat: function() {
  1313. Meteor.setTimeout(function() {
  1314. var elem = document.getElementById('global-chat');
  1315. if (elem !== undefined && elem !== null) {
  1316. elem.scrollTop = elem.scrollHeight;
  1317. }
  1318. }, 100);
  1319. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  1320. },
  1321. likes: function() {
  1322. var playlist = Playlists.findOne({type: Session.get("type")});
  1323. var likes = 0;
  1324. playlist.songs.forEach(function(song) {
  1325. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1326. likes = song.likes;
  1327. return;
  1328. }
  1329. });
  1330. return likes;
  1331. },
  1332. dislikes: function() {
  1333. var playlist = Playlists.findOne({type: Session.get("type")});
  1334. var dislikes = 0;
  1335. playlist.songs.forEach(function(song) {
  1336. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1337. dislikes = song.dislikes;
  1338. return;
  1339. }
  1340. });
  1341. return dislikes;
  1342. },
  1343. liked: function() {
  1344. if (Meteor.userId()) {
  1345. var currentSong = Session.get("currentSong");
  1346. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  1347. return "active";
  1348. } else {
  1349. return "";
  1350. }
  1351. } else {
  1352. "";
  1353. }
  1354. },
  1355. disliked: function() {
  1356. if (Meteor.userId()) {
  1357. var currentSong = Session.get("currentSong");
  1358. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  1359. return "active";
  1360. } else {
  1361. return "";
  1362. }
  1363. } else {
  1364. "";
  1365. }
  1366. },
  1367. type: function() {
  1368. var parts = location.href.split('/');
  1369. var id = parts.pop().toLowerCase();
  1370. return Rooms.findOne({type: id}).display;
  1371. },
  1372. users: function() {
  1373. var parts = location.href.split('/');
  1374. var id = parts.pop().toLowerCase();
  1375. return Rooms.findOne({type: id}).users;
  1376. },
  1377. title: function(){
  1378. return Session.get("title");
  1379. },
  1380. artist: function(){
  1381. return Session.get("artist");
  1382. },
  1383. loaded: function() {
  1384. return Session.get("loaded");
  1385. },
  1386. paused: function() {
  1387. return Session.get("state") === "paused";
  1388. },
  1389. private: function() {
  1390. return Rooms.findOne({type: Session.get("type")}).private === true;
  1391. },
  1392. report: function() {
  1393. return Session.get("reportObj");
  1394. },
  1395. reportSong: function() {
  1396. return Session.get("reportSong");
  1397. },
  1398. reportTitle: function() {
  1399. return Session.get("reportTitle");
  1400. },
  1401. reportAuthor: function() {
  1402. return Session.get("reportAuthor");
  1403. },
  1404. reportDuration: function() {
  1405. return Session.get("reportDuration");
  1406. },
  1407. reportAudio: function() {
  1408. return Session.get("reportAudio");
  1409. },
  1410. reportAlbumart: function() {
  1411. return Session.get("reportAlbumart");
  1412. },
  1413. reportOther: function() {
  1414. return Session.get("reportOther");
  1415. },
  1416. currentSong: function() {
  1417. return Session.get("currentSong");
  1418. },
  1419. previousSong: function() {
  1420. return Session.get("previousSong");
  1421. },
  1422. currentSongR: function() {
  1423. return Session.get("currentSongR");
  1424. },
  1425. previousSongR: function() {
  1426. return Session.get("previousSongR");
  1427. },
  1428. reportingSong: function() {
  1429. if (Session.get("reportPrevious")) {
  1430. return Session.get("previousSongR");
  1431. } else {
  1432. return Session.get("currentSongR");
  1433. }
  1434. },
  1435. votes: function(){
  1436. return Rooms.findOne({type: Session.get("type")}).votes;
  1437. }
  1438. });
  1439. Template.room.onCreated(function () {
  1440. Chat.after.find(function(userId, selector) {
  1441. if (selector.type === "global") {
  1442. if (!$("#global-chat-tab").hasClass("active")) {
  1443. $("#global-chat-tab").addClass("unread-messages");
  1444. }
  1445. } else if(selector.type === Session.get("type")) {
  1446. if (!$("#chat-tab").hasClass("active")) {
  1447. $("#chat-tab").addClass("unread-messages");
  1448. }
  1449. }
  1450. });
  1451. Session.set("reportSong", false);
  1452. Session.set("reportTitle", false);
  1453. Session.set("reportAuthor", false);
  1454. Session.set("reportDuration", false);
  1455. Session.set("reportAudio", false);
  1456. Session.set("reportAlbumart", false);
  1457. Session.set("reportOther", false);
  1458. if (resizeSeekerbarInterval !== undefined) {
  1459. Meteor.clearInterval(resizeSeekerbarInterval);
  1460. resizeSeekerbarInterval = undefined;
  1461. }
  1462. YTPlayer = undefined;
  1463. Session.set("videoHidden", false);
  1464. var tag = document.createElement("script");
  1465. tag.src = "https://www.youtube.com/iframe_api";
  1466. var firstScriptTag = document.getElementsByTagName('script')[0];
  1467. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1468. Session.set("singleVideo", true);
  1469. var currentSong = undefined;
  1470. var currentSongR = undefined;
  1471. function getTimeElapsed() {
  1472. if (currentSong !== undefined) {
  1473. var room = Rooms.findOne({type: type});
  1474. if (room !== undefined) {
  1475. return Date.now() - currentSong.started - room.timePaused;
  1476. }
  1477. }
  1478. return 0;
  1479. }
  1480. function getSongInfo(songData){
  1481. Session.set("title", songData.title);
  1482. Session.set("artist", songData.artist);
  1483. Session.set("id", songData.id);
  1484. $("#song-img").attr("src", songData.img);
  1485. Session.set("duration", parseInt(songData.duration));
  1486. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1487. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1488. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1489. }
  1490. function resizeSeekerbar() {
  1491. if (Session.get("state") === "playing") {
  1492. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1493. }
  1494. }
  1495. function startSong() {
  1496. $("#time-elapsed").text("0:00");
  1497. $("#vote-skip").attr("disabled", false);
  1498. if (currentSong !== undefined) {
  1499. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  1500. var volume = localStorage.getItem("volume") || 20;
  1501. if ($("#player").length !== 1) {
  1502. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1503. }
  1504. $("#player").show();
  1505. function loadVideo() {
  1506. if (!Session.get("YTLoaded")) {
  1507. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  1508. loadVideo();
  1509. }, 500));
  1510. } else {
  1511. if (YTPlayer === undefined) {
  1512. YTPlayer = new YT.Player("player", {
  1513. height: 540,
  1514. width: 960,
  1515. videoId: currentSong.id,
  1516. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1517. events: {
  1518. 'onReady': function (event) {
  1519. if (currentSong.skipDuration === undefined) {
  1520. currentSong.skipDuration = 0;
  1521. }
  1522. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1523. event.target.playVideo();
  1524. event.target.setVolume(volume);
  1525. resizeSeekerbar();
  1526. },
  1527. 'onStateChange': function (event) {
  1528. if (Session.get("YTLoaded")) {
  1529. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1530. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1531. event.target.playVideo();
  1532. }
  1533. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1534. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1535. event.target.pauseVideo();
  1536. }
  1537. }
  1538. }
  1539. }
  1540. });
  1541. } else {
  1542. YTPlayer.loadVideoById(currentSong.id);
  1543. if (currentSong.skipDuration === undefined) {
  1544. currentSong.skipDuration = 0;
  1545. }
  1546. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1547. }
  1548. Session.set("pauseVideo", false);
  1549. getSongInfo(currentSong);
  1550. }
  1551. }
  1552. loadVideo();
  1553. }
  1554. }
  1555. Session.set("loaded", false);
  1556. Meteor.subscribe("rooms", function() {
  1557. var parts = location.href.split('/');
  1558. var id = parts.pop();
  1559. var type = id.toLowerCase();
  1560. Session.set("type", type);
  1561. if (Rooms.find({type: type}).count() !== 1) {
  1562. window.location = "/";
  1563. } else {
  1564. StationSubscription = Meteor.subscribe(type);
  1565. Session.set("loaded", true);
  1566. minterval = Meteor.setInterval(function () {
  1567. var room = Rooms.findOne({type: type});
  1568. if (room !== undefined) {
  1569. if (room.state === "paused" || Session.get("pauseVideo")) {
  1570. Session.set("state", "paused");
  1571. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  1572. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  1573. YTPlayer.pauseVideo();
  1574. }
  1575. } else {
  1576. Session.set("state", "playing");
  1577. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  1578. YTPlayer.playVideo();
  1579. }
  1580. }
  1581. }
  1582. if (room.currentSong.song !== undefined && (currentSongR === undefined || room.currentSong.started !== currentSongR.started)) {
  1583. Session.set("previousSong", currentSong);
  1584. currentSongR = room.currentSong;
  1585. currentSong = room.currentSong.song;
  1586. currentSong.started = room.currentSong.started;
  1587. Session.set("currentSong", currentSong);
  1588. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  1589. startSong();
  1590. }
  1591. if (currentSong !== undefined) {
  1592. if (room !== undefined) {
  1593. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1594. var song_duration = currentSong.duration;
  1595. if (song_duration <= duration) {
  1596. Session.set("pauseVideo", true);
  1597. }
  1598. var d = moment.duration(duration, 'seconds');
  1599. if (Session.get("state") === "playing") {
  1600. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1601. }
  1602. }
  1603. }
  1604. }, 100);
  1605. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1606. resizeSeekerbar();
  1607. }, 500);
  1608. }
  1609. });
  1610. });
  1611. // Settings Template
  1612. Template.settings.events({
  1613. "click #save-settings": function() {
  1614. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  1615. },
  1616. "click #delete-account": function(){
  1617. $("#delete-account").text("Click to confirm");
  1618. $("#delete-account").click(function(){
  1619. var bool = confirm("Are you sure you want to delete your account?");
  1620. if(bool) {
  1621. Meteor.call("deleteAccount");
  1622. } else{
  1623. $("#delete-account").text("Delete");
  1624. }
  1625. })
  1626. },
  1627. "click #change-password": function(){
  1628. var oldPassword = $("#old-password").val();
  1629. var newPassword= $("#new-password").val();
  1630. var confirmPassword = $("#confirm-password").val();
  1631. if(newPassword === confirmPassword){
  1632. Accounts.changePassword(oldPassword, newPassword, function(err){
  1633. if(err){
  1634. $("#old-password").val("");
  1635. $("#new-password").val("");
  1636. $("#confirm-password").val("");
  1637. $("<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(); });
  1638. } else {
  1639. $("#old-password").val("");
  1640. $("#new-password").val("");
  1641. $("#confirm-password").val("");
  1642. $("<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(); });
  1643. }
  1644. });
  1645. }
  1646. }
  1647. });
  1648. var previewEndSongTimeout = undefined;