events.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. var feedbackData;
  2. function gup( name, url ) {
  3. if (!url) url = location.href;
  4. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  5. var regexS = "[\\?&]"+name+"=([^&#]*)";
  6. var regex = new RegExp( regexS );
  7. var results = regex.exec( url );
  8. return results == null ? null : results[1];
  9. }
  10. function getSpotifyInfo(title, cb, artist) {
  11. var q = "";
  12. q = title;
  13. if (artist !== undefined) {
  14. q += " artist:" + artist;
  15. }
  16. $.ajax({
  17. type: "GET",
  18. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  19. applicationType: "application/json",
  20. contentType: "json",
  21. success: function (data) {
  22. cb(data);
  23. }
  24. });
  25. }
  26. function executeCommand(command, params){
  27. if (command === "help" || command === "commands") {
  28. $('#helpModal').modal('show');
  29. return true;
  30. } else if (command === "volume") {
  31. if (params.length === 1) {
  32. var volume = Number(params[0]);
  33. if (volume >= 0 || volume <= 100) {
  34. if (volume === 0) {
  35. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  36. } else {
  37. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  38. }
  39. $("#volume-slider").slider("setValue", volume);
  40. if (YTPlayer !== undefined) {
  41. YTPlayer.setVolume(volume);
  42. localStorage.setItem("volume", volume);
  43. }
  44. return true;
  45. }
  46. }
  47. } else if(command === "mute"){
  48. $("#volume-slider").slider("setValue", 0);
  49. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off");
  50. if (YTPlayer !== undefined) {
  51. YTPlayer.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 a": function(e){
  181. var id = e.currentTarget.id;
  182. console.log(id.toLowerCase());
  183. Session.set("playlistToEdit", id);
  184. },
  185. "click #croom_create": function() {
  186. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#croom_private").prop("checked"), $("#croom_desc").val(), function (err, res) {
  187. if (err) {
  188. var $toastContent = $('<span><strong>Room not added.</strong> ' + err.reason + '</span>');
  189. Materialize.toast($toastContent, 8000);
  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. $("#desc_text").val(Rooms.findOne({type: Session.get("roomDesc")}).roomDesc);
  204. },
  205. "click #submit_desc": function(){
  206. var description = $("#desc_text").val();
  207. Meteor.call("editRoomDesc", Session.get("roomDesc"), description);
  208. $("#desc-modal").closeModal();
  209. },
  210. "click #submit-alert": function(){
  211. var alertDesc = $("#alert-desc").val()
  212. if(alertDesc !== ""){
  213. Meteor.call("addAlert", alertDesc);
  214. }
  215. },
  216. "click #remove-alerts": function(){
  217. Meteor.call("removeAlerts");
  218. }
  219. });
  220. Template.feedback.events({
  221. "click #feedback_submit": function(){
  222. if($("#feedback_message").val().length !== 0 && $("#feedback_message").hasClass("invalid") === false){
  223. Meteor.call("sendFeedback", $("#feedback_message").val());
  224. $("#feedback_message").val("");
  225. $("#modal1").closeModal()
  226. } else{
  227. var $toastContent = $('<span><strong>Feedback not sent.</strong> Possible reasons include:<ul><li>- Empty Feedback Message</li><li>- Feedback is more than 500 words</li></ul></span>');
  228. Materialize.toast($toastContent, 8000);
  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 $toastContent = $('<span><strong>Oh snap!</strong> ' + err.reason + '</span>');
  274. Materialize.toast($toastContent, 8000);
  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. $("#genres").val(this.genres).change();
  425. $("#genres").material_select();
  426. $("#previewPlayerContainer").addClass("hide-preview");
  427. Session.set("image_url", this.img);
  428. Session.set("editing", true);
  429. $("#editModal").openModal({
  430. complete : function() {
  431. Session.set("editing", false);
  432. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  433. YTPlayer.stopVideo();
  434. }
  435. }
  436. });
  437. },
  438. "click .add-song-button": function(e){
  439. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  440. Meteor.call("addSongToPlaylist", this, function(err) {
  441. console.log(err);
  442. if (err) {
  443. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  444. Materialize.toast($toastContent, 8000);
  445. }
  446. });
  447. },
  448. "click .deny-song-button": function(e){
  449. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  450. Meteor.call("removeSongFromQueue", this.mid);
  451. },
  452. "click #play": function() {
  453. var duration = Session.get("song").duration;
  454. var d = moment.duration(parseInt(duration), 'seconds');
  455. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  456. $("#previewPlayerContainer").removeClass("hide-preview");
  457. var song = Session.get("song");
  458. var id = song.id;
  459. var volume = localStorage.getItem("volume") || 20;
  460. if (song.duration !== 0) {
  461. $("#play").attr("disabled", true);
  462. $("#stop").attr("disabled", false);
  463. $("#pause").attr("disabled", false);
  464. $("#forward").attr("disabled", false);
  465. if (YTPlayer === undefined) {
  466. YTPlayer = new YT.Player("previewPlayer", {
  467. height: 540,
  468. width: 568,
  469. videoId: id,
  470. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  471. events: {
  472. 'onReady': function(event) {
  473. event.target.seekTo(Number(song.skipDuration));
  474. event.target.playVideo();
  475. event.target.setVolume(volume);
  476. },
  477. 'onStateChange': function(event){
  478. if (event.data == YT.PlayerState.PAUSED) {
  479. if (seekerBarInterval !== undefined) {
  480. Meteor.clearInterval(seekerBarInterval);
  481. seekerBarInterval = undefined;
  482. }
  483. }
  484. // if (event.data == YT.PlayerState.UNSTARTED) {
  485. // if (seekerBarInterval !== undefined) {
  486. // Meteor.clearInterval(seekerBarInterval);
  487. // seekerBarInterval = undefined;
  488. // }
  489. // $(".seeker-bar").css({width: "0"});
  490. // $("#time-elapsed").text("0:00");
  491. // $("#previewPlayerContainer").addClass("hide-preview");
  492. // console.log("HIDE MEY STACY!!!!")
  493. // }
  494. if (event.data == YT.PlayerState.PLAYING) {
  495. seekerBarInterval = Meteor.setInterval(function() {
  496. var duration = Session.get("song").duration;
  497. var timeElapsed = YTPlayer.getCurrentTime();
  498. var skipDuration = Session.get("song").skipDuration;
  499. if (duration <= (timeElapsed - skipDuration)) {
  500. YTPlayer.stopVideo();
  501. $("#play").attr("disabled", false);
  502. $("#stop").attr("disabled", true);
  503. $("#pause").attr("disabled", true);
  504. $("#forward").attr("disabled", true);
  505. $("#previewPlayerContainer").addClass("hide-preview");
  506. $(".seeker-bar").css({width: "0"});
  507. $("#time-elapsed").text("0:00");
  508. Meteor.clearInterval(seekerBarInterval);
  509. } else {
  510. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  511. $(".seeker-bar").css({width: percentComplete + "%"});
  512. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  513. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  514. }
  515. }, 100);
  516. $("#play").attr("disabled", true);
  517. $("#stop").attr("disabled", false);
  518. $("#pause").attr("disabled", false);
  519. $("#forward").attr("disabled", false);
  520. } else {
  521. $("#play").attr("disabled", false);
  522. $("#stop").attr("disabled", true);
  523. $("#pause").attr("disabled", true);
  524. $("#forward").attr("disabled", true);
  525. }
  526. }
  527. }
  528. });
  529. } else {
  530. if (YTPlayer.getPlayerState() === 2) {
  531. YTPlayer.playVideo();
  532. } else {
  533. console.log(id, song.skipDuration, song.duration);
  534. YTPlayer.loadVideoById(id);
  535. YTPlayer.seekTo(Number(song.skipDuration));
  536. }
  537. }
  538. $("#previewPlayerContainer").removeClass("hide-preview");
  539. }
  540. },
  541. "click #stop": function() {
  542. $("#play").attr("disabled", false);
  543. $("#stop").attr("disabled", true);
  544. $("#pause").attr("disabled", true);
  545. $("#forward").attr("disabled", true);
  546. if (previewEndSongTimeout !== undefined) {
  547. Meteor.clearTimeout(previewEndSongTimeout);
  548. }
  549. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  550. YTPlayer.stopVideo();
  551. }
  552. },
  553. "click #pause": function() {
  554. $("#play").attr("disabled", false);
  555. $("#stop").attr("disabled", false);
  556. $("#pause").attr("disabled", true);
  557. $("#forward").attr("disabled", true);
  558. if (previewEndSongTimeout !== undefined) {
  559. Meteor.clearTimeout(previewEndSongTimeout);
  560. }
  561. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  562. YTPlayer.pauseVideo();
  563. }
  564. },
  565. "click #forward": function() {
  566. var error = false;
  567. if (YTPlayer !== undefined) {
  568. var duration = Number(Session.get("song").duration) | 0;
  569. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  570. if (YTPlayer.getDuration() < duration + skipDuration) {
  571. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  572. Materialize.toast($toastContent, 8000);
  573. error = true;
  574. } else {
  575. YTPlayer.seekTo(skipDuration + duration - 10);
  576. }
  577. }
  578. if (!error) {
  579. if (previewEndSongTimeout !== undefined) {
  580. Meteor.clearTimeout(previewEndSongTimeout);
  581. }
  582. previewEndSongTimeout = Meteor.setTimeout(function() {
  583. if (YTPlayer !== undefined) {
  584. YTPlayer.stopVideo();
  585. }
  586. $("#play").attr("disabled", false);
  587. $("#stop").attr("disabled", true);
  588. $("#pause").attr("disabled", true);
  589. $("#forward").attr("disabled", true);
  590. $("#previewPlayerContainer").addClass("hide-preview");
  591. }, 10000);
  592. }
  593. },
  594. "click #get-spotify-info": function() {
  595. var search = $("#title").val();
  596. var artistName = $("#artist").val();
  597. getSpotifyInfo(search, function(data) {
  598. for(var i in data){
  599. for(var j in data[i].items){
  600. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  601. $("#img").val(data[i].items[j].album.images[2].url).change();
  602. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  603. return;
  604. }
  605. }
  606. }
  607. }, artistName);
  608. },
  609. "click #save-song-button": function() {
  610. var newSong = {};
  611. newSong.mid = $("#mid").val();
  612. newSong.id = $("#id").val();
  613. newSong.likes = Number($("#likes").val());
  614. newSong.dislikes = Number($("#dislikes").val());
  615. newSong.title = $("#title").val();
  616. newSong.artist = $("#artist").val();
  617. newSong.img = $("#img").val();
  618. newSong.duration = Number($("#duration").val());
  619. newSong.skipDuration = $("#skip-duration").val();
  620. newSong.requestedBy = Session.get("song").requestedBy;
  621. newSong.genres = $("#genres").val();
  622. if(newSong.skipDuration === undefined){
  623. newSong.skipDuration = 0;
  624. }
  625. Meteor.call("updateQueueSong", newSong.mid, newSong, function(err, res) {
  626. if (err) {
  627. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  628. Materialize.toast($toastContent, 8000);
  629. } else {
  630. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  631. Materialize.toast($toastContent, 4000);
  632. Session.set("song", newSong);
  633. }
  634. });
  635. }
  636. });
  637. Template.manageStation.events({
  638. /* TODO Add undo delete button */
  639. "input #id": function() {
  640. $("#previewPlayerContainer").addClass("hide-preview");
  641. },
  642. "input #img": function() {
  643. var url = $("#img").val();
  644. Session.set("image_url", url);
  645. },
  646. "click .preview-button": function(e){
  647. Session.set("song", this);
  648. $("#previewModal").openModal();
  649. },
  650. "click #previewImageButton": function() {
  651. $("#preview-image").attr("src", Session.get("song").img);
  652. },
  653. "click .edit-song-button": function(e){
  654. Session.set("song", this);
  655. Session.set("genre", $(e.target).data("genre"));
  656. $("#mid").val(this.mid).change();
  657. $("#artist").val(this.artist).change();
  658. $("#title").val(this.title).change();
  659. $("#img").val(this.img).change();
  660. $("#id").val(this.id).change();
  661. $("#likes").val(this.likes).change();
  662. $("#dislikes").val(this.dislikes).change();
  663. $("#duration").val(this.duration).change();
  664. $("#skip-duration").val(this.skipDuration).change();
  665. $("#genres").val(this.genres).change();
  666. $("#genres").material_select();
  667. $("#previewPlayerContainer").addClass("hide-preview");
  668. Session.set("image_url", this.img);
  669. Session.set("editing", true);
  670. $("#editModal").openModal({
  671. complete : function() {
  672. Session.set("editing", false);
  673. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  674. YTPlayer.stopVideo();
  675. }
  676. }
  677. });
  678. },
  679. "click .remove-song-button": function(e){
  680. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  681. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  682. },
  683. "click #play": function() {
  684. var duration = Session.get("song").duration;
  685. var d = moment.duration(parseInt(duration), 'seconds');
  686. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  687. $("#previewPlayerContainer").removeClass("hide-preview");
  688. var song = Session.get("song");
  689. var id = song.id;
  690. var volume = localStorage.getItem("volume") || 20;
  691. if (song.duration !== 0) {
  692. $("#play").attr("disabled", true);
  693. $("#stop").attr("disabled", false);
  694. $("#pause").attr("disabled", false);
  695. $("#forward").attr("disabled", false);
  696. if (YTPlayer === undefined) {
  697. YTPlayer = new YT.Player("previewPlayer", {
  698. height: 540,
  699. width: 568,
  700. videoId: id,
  701. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  702. events: {
  703. 'onReady': function(event) {
  704. event.target.seekTo(Number(song.skipDuration));
  705. event.target.playVideo();
  706. event.target.setVolume(volume);
  707. },
  708. 'onStateChange': function(event){
  709. if (event.data == YT.PlayerState.PAUSED) {
  710. if (seekerBarInterval !== undefined) {
  711. Meteor.clearInterval(seekerBarInterval);
  712. seekerBarInterval = undefined;
  713. }
  714. }
  715. // if (event.data == YT.PlayerState.UNSTARTED) {
  716. // if (seekerBarInterval !== undefined) {
  717. // Meteor.clearInterval(seekerBarInterval);
  718. // seekerBarInterval = undefined;
  719. // }
  720. // $(".seeker-bar").css({width: "0"});
  721. // $("#time-elapsed").text("0:00");
  722. // $("#previewPlayerContainer").addClass("hide-preview");
  723. // }
  724. if (event.data == YT.PlayerState.PLAYING) {
  725. seekerBarInterval = Meteor.setInterval(function() {
  726. var duration = Session.get("song").duration;
  727. var timeElapsed = YTPlayer.getCurrentTime();
  728. var skipDuration = Session.get("song").skipDuration;
  729. if (duration <= (timeElapsed - skipDuration)) {
  730. YTPlayer.stopVideo();
  731. $("#play").attr("disabled", false);
  732. $("#stop").attr("disabled", true);
  733. $("#pause").attr("disabled", true);
  734. $("#forward").attr("disabled", true);
  735. $("#previewPlayerContainer").addClass("hide-preview");
  736. $(".seeker-bar").css({width: "0"});
  737. $("#time-elapsed").text("0:00");
  738. Meteor.clearInterval(seekerBarInterval);
  739. } else {
  740. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  741. $(".seeker-bar").css({width: percentComplete + "%"});
  742. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  743. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  744. }
  745. }, 100);
  746. $("#play").attr("disabled", true);
  747. $("#stop").attr("disabled", false);
  748. $("#pause").attr("disabled", false);
  749. $("#forward").attr("disabled", false);
  750. } else {
  751. $("#play").attr("disabled", false);
  752. $("#stop").attr("disabled", true);
  753. $("#pause").attr("disabled", true);
  754. $("#forward").attr("disabled", true);
  755. }
  756. }
  757. }
  758. });
  759. } else {
  760. if (YTPlayer.getPlayerState() === 2) {
  761. YTPlayer.playVideo();
  762. } else {
  763. console.log(id, song.skipDuration, song.duration);
  764. YTPlayer.loadVideoById(id);
  765. YTPlayer.seekTo(Number(song.skipDuration));
  766. }
  767. }
  768. $("#previewPlayerContainer").removeClass("hide-preview");
  769. }
  770. },
  771. "click #stop": function() {
  772. $("#play").attr("disabled", false);
  773. $("#stop").attr("disabled", true);
  774. $("#pause").attr("disabled", true);
  775. $("#forward").attr("disabled", true);
  776. if (previewEndSongTimeout !== undefined) {
  777. Meteor.clearTimeout(previewEndSongTimeout);
  778. }
  779. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  780. YTPlayer.stopVideo();
  781. }
  782. },
  783. "click #pause": function() {
  784. $("#play").attr("disabled", false);
  785. $("#stop").attr("disabled", false);
  786. $("#pause").attr("disabled", true);
  787. $("#forward").attr("disabled", true);
  788. if (previewEndSongTimeout !== undefined) {
  789. Meteor.clearTimeout(previewEndSongTimeout);
  790. }
  791. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  792. YTPlayer.pauseVideo();
  793. }
  794. },
  795. "click #forward": function() {
  796. var error = false;
  797. if (YTPlayer !== undefined) {
  798. var duration = Number(Session.get("song").duration) | 0;
  799. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  800. if (YTPlayer.getDuration() < duration + skipDuration) {
  801. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  802. Materialize.toast($toastContent, 8000);
  803. error = true;
  804. } else {
  805. YTPlayer.seekTo(skipDuration + duration - 10);
  806. }
  807. }
  808. if (!error) {
  809. if (previewEndSongTimeout !== undefined) {
  810. Meteor.clearTimeout(previewEndSongTimeout);
  811. }
  812. previewEndSongTimeout = Meteor.setTimeout(function() {
  813. if (YTPlayer !== undefined) {
  814. YTPlayer.stopVideo();
  815. }
  816. $("#play").attr("disabled", false);
  817. $("#stop").attr("disabled", true);
  818. $("#pause").attr("disabled", true);
  819. $("#forward").attr("disabled", true);
  820. $("#previewPlayerContainer").addClass("hide-preview");
  821. }, 10000);
  822. }
  823. },
  824. "click #get-spotify-info": function() {
  825. var search = $("#title").val();
  826. var artistName = $("#artist").val();
  827. getSpotifyInfo(search, function(data) {
  828. for(var i in data){
  829. for(var j in data[i].items){
  830. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  831. $("#img").val(data[i].items[j].album.images[2].url).change();
  832. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  833. return;
  834. }
  835. }
  836. }
  837. }, artistName);
  838. },
  839. "click #save-song-button": function() {
  840. var newSong = {};
  841. newSong.mid = $("#mid").val();
  842. newSong.id = $("#id").val();
  843. newSong.likes = Number($("#likes").val());
  844. newSong.dislikes = Number($("#dislikes").val());
  845. newSong.title = $("#title").val();
  846. newSong.artist = $("#artist").val();
  847. newSong.img = $("#img").val();
  848. newSong.duration = Number($("#duration").val());
  849. newSong.skipDuration = $("#skip-duration").val();
  850. newSong.requestedBy = Session.get("song").requestedBy;
  851. newSong.genres = $("#genres").val() || [];
  852. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  853. console.log(err, res);
  854. if (err) {
  855. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  856. Materialize.toast($toastContent, 8000);
  857. } else {
  858. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  859. Materialize.toast($toastContent, 4000);
  860. Session.set("song", newSong);
  861. }
  862. });
  863. }
  864. });
  865. Template.manageSongs.events({
  866. /* TODO Add undo delete button */
  867. "change #show_genres_cb": function() {
  868. var selected = $("#show_genres_cb").is(":checked");
  869. Session.set("showGenres", selected);
  870. },
  871. "change #show_no_genres_cb": function() {
  872. var selected = $("#show_no_genres_cb").is(":checked");
  873. Session.set("showNoGenres", selected);
  874. },
  875. "input #id": function() {
  876. $("#previewPlayerContainer").addClass("hide-preview");
  877. },
  878. "input #img": function() {
  879. var url = $("#img").val();
  880. Session.set("image_url", url);
  881. },
  882. "click .preview-button": function(e){
  883. Session.set("song", this);
  884. $("#previewModal").openModal();
  885. },
  886. "click #previewImageButton": function() {
  887. $("#preview-image").attr("src", Session.get("song").img);
  888. },
  889. "click .edit-song-button": function(e){
  890. Session.set("song", this);
  891. Session.set("genre", $(e.target).data("genre"));
  892. $("#mid").val(this.mid).change();
  893. $("#artist").val(this.artist).change();
  894. $("#title").val(this.title).change();
  895. $("#img").val(this.img).change();
  896. $("#id").val(this.id).change();
  897. $("#likes").val(this.likes).change();
  898. $("#dislikes").val(this.dislikes).change();
  899. $("#duration").val(this.duration).change();
  900. $("#skip-duration").val(this.skipDuration).change();
  901. $("#genres").val(this.genres).change();
  902. $("#genres").material_select();
  903. $("#previewPlayerContainer").addClass("hide-preview");
  904. Session.set("image_url", this.img);
  905. Session.set("editing", true);
  906. $("#editModal").openModal({
  907. complete : function() {
  908. Session.set("editing", false);
  909. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  910. YTPlayer.stopVideo();
  911. }
  912. }
  913. });
  914. },
  915. "click .remove-song-button": function(){
  916. console.log(this.mid);
  917. console.log("TEST!");
  918. Meteor.call("deleteSong", this.mid);
  919. },
  920. "click #play": function() {
  921. var duration = Session.get("song").duration;
  922. var d = moment.duration(parseInt(duration), 'seconds');
  923. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  924. $("#previewPlayerContainer").removeClass("hide-preview");
  925. var song = Session.get("song");
  926. var id = song.id;
  927. var volume = localStorage.getItem("volume") || 20;
  928. if (song.duration !== 0) {
  929. $("#play").attr("disabled", true);
  930. $("#stop").attr("disabled", false);
  931. $("#pause").attr("disabled", false);
  932. $("#forward").attr("disabled", false);
  933. if (YTPlayer === undefined) {
  934. YTPlayer = new YT.Player("previewPlayer", {
  935. height: 540,
  936. width: 568,
  937. videoId: id,
  938. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  939. events: {
  940. 'onReady': function(event) {
  941. event.target.seekTo(Number(song.skipDuration));
  942. event.target.playVideo();
  943. event.target.setVolume(volume);
  944. },
  945. 'onStateChange': function(event){
  946. if (event.data == YT.PlayerState.PAUSED) {
  947. if (seekerBarInterval !== undefined) {
  948. Meteor.clearInterval(seekerBarInterval);
  949. seekerBarInterval = undefined;
  950. }
  951. }
  952. // if (event.data == YT.PlayerState.UNSTARTED) {
  953. // if (seekerBarInterval !== undefined) {
  954. // Meteor.clearInterval(seekerBarInterval);
  955. // seekerBarInterval = undefined;
  956. // }
  957. // $(".seeker-bar").css({width: "0"});
  958. // $("#time-elapsed").text("0:00");
  959. // $("#previewPlayerContainer").addClass("hide-preview");
  960. // }
  961. if (event.data == YT.PlayerState.PLAYING) {
  962. seekerBarInterval = Meteor.setInterval(function() {
  963. var duration = Session.get("song").duration;
  964. var timeElapsed = YTPlayer.getCurrentTime();
  965. var skipDuration = Session.get("song").skipDuration;
  966. if (duration <= (timeElapsed - skipDuration)) {
  967. YTPlayer.stopVideo();
  968. $("#play").attr("disabled", false);
  969. $("#stop").attr("disabled", true);
  970. $("#pause").attr("disabled", true);
  971. $("#forward").attr("disabled", true);
  972. $("#previewPlayerContainer").addClass("hide-preview");
  973. $(".seeker-bar").css({width: "0"});
  974. $("#time-elapsed").text("0:00");
  975. Meteor.clearInterval(seekerBarInterval);
  976. } else {
  977. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  978. $(".seeker-bar").css({width: percentComplete + "%"});
  979. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  980. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  981. }
  982. }, 100);
  983. $("#play").attr("disabled", true);
  984. $("#stop").attr("disabled", false);
  985. $("#pause").attr("disabled", false);
  986. $("#forward").attr("disabled", false);
  987. } else {
  988. $("#play").attr("disabled", false);
  989. $("#stop").attr("disabled", true);
  990. $("#pause").attr("disabled", true);
  991. $("#forward").attr("disabled", true);
  992. }
  993. }
  994. }
  995. });
  996. } else {
  997. if (YTPlayer.getPlayerState() === 2) {
  998. YTPlayer.playVideo();
  999. } else {
  1000. console.log(id, song.skipDuration, song.duration);
  1001. YTPlayer.loadVideoById(id);
  1002. YTPlayer.seekTo(Number(song.skipDuration));
  1003. }
  1004. }
  1005. $("#previewPlayerContainer").removeClass("hide-preview");
  1006. }
  1007. },
  1008. "click #stop": function() {
  1009. $("#play").attr("disabled", false);
  1010. $("#stop").attr("disabled", true);
  1011. $("#pause").attr("disabled", true);
  1012. $("#forward").attr("disabled", true);
  1013. if (previewEndSongTimeout !== undefined) {
  1014. Meteor.clearTimeout(previewEndSongTimeout);
  1015. }
  1016. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  1017. YTPlayer.stopVideo();
  1018. }
  1019. },
  1020. "click #pause": function() {
  1021. $("#play").attr("disabled", false);
  1022. $("#stop").attr("disabled", false);
  1023. $("#pause").attr("disabled", true);
  1024. $("#forward").attr("disabled", true);
  1025. if (previewEndSongTimeout !== undefined) {
  1026. Meteor.clearTimeout(previewEndSongTimeout);
  1027. }
  1028. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  1029. YTPlayer.pauseVideo();
  1030. }
  1031. },
  1032. "click #forward": function() {
  1033. var error = false;
  1034. if (YTPlayer !== undefined) {
  1035. var duration = Number(Session.get("song").duration) | 0;
  1036. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1037. if (YTPlayer.getDuration() < duration + skipDuration) {
  1038. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  1039. Materialize.toast($toastContent, 8000);
  1040. error = true;
  1041. } else {
  1042. YTPlayer.seekTo(skipDuration + duration - 10);
  1043. }
  1044. }
  1045. if (!error) {
  1046. if (previewEndSongTimeout !== undefined) {
  1047. Meteor.clearTimeout(previewEndSongTimeout);
  1048. }
  1049. previewEndSongTimeout = Meteor.setTimeout(function() {
  1050. if (YTPlayer !== undefined) {
  1051. YTPlayer.stopVideo();
  1052. }
  1053. $("#play").attr("disabled", false);
  1054. $("#stop").attr("disabled", true);
  1055. $("#pause").attr("disabled", true);
  1056. $("#forward").attr("disabled", true);
  1057. $("#previewPlayerContainer").addClass("hide-preview");
  1058. }, 10000);
  1059. }
  1060. },
  1061. "click #get-spotify-info": function() {
  1062. var search = $("#title").val();
  1063. var artistName = $("#artist").val();
  1064. getSpotifyInfo(search, function(data) {
  1065. for(var i in data){
  1066. for(var j in data[i].items){
  1067. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1068. $("#img").val(data[i].items[j].album.images[2].url).change();
  1069. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  1070. return;
  1071. }
  1072. }
  1073. }
  1074. }, artistName);
  1075. },
  1076. "click #save-song-button": function() {
  1077. var newSong = {};
  1078. newSong.mid = $("#mid").val();
  1079. newSong.id = $("#id").val();
  1080. newSong.likes = Number($("#likes").val());
  1081. newSong.dislikes = Number($("#dislikes").val());
  1082. newSong.title = $("#title").val();
  1083. newSong.artist = $("#artist").val();
  1084. newSong.img = $("#img").val();
  1085. newSong.duration = Number($("#duration").val());
  1086. newSong.skipDuration = $("#skip-duration").val();
  1087. newSong.requestedBy = Session.get("song").requestedBy;
  1088. newSong.genres = $("#genres").val() || [];
  1089. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  1090. console.log(err, res);
  1091. if (err) {
  1092. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  1093. Materialize.toast($toastContent, 8000);
  1094. } else {
  1095. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  1096. Materialize.toast($toastContent, 4000);
  1097. Session.set("song", newSong);
  1098. }
  1099. });
  1100. }
  1101. });
  1102. Template.register.events({
  1103. "submit form": function(e){
  1104. e.preventDefault();
  1105. var username = $("#username").val();
  1106. var email = $("#email").val();
  1107. var password = $("#password").val();
  1108. var acceptedTermsAndPrivacy = $("#termsPrivacyBTN").is(":checked");
  1109. var captchaData = grecaptcha.getResponse();
  1110. if (acceptedTermsAndPrivacy) {
  1111. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  1112. grecaptcha.reset();
  1113. if (err) {
  1114. console.log(err);
  1115. var $toastContent = $('<span><strong>Oh snap!</strong> ' + err.reason + '</span>');
  1116. Materialize.toast($toastContent, 8000);
  1117. } else {
  1118. Meteor.loginWithPassword(username, password);
  1119. Accounts.onLogin(function(){
  1120. window.location.href = "/";
  1121. })
  1122. }
  1123. });
  1124. } else {
  1125. var $toastContent = $('<span><strong>Oh snap!</strong> Please read and accept the Terms and Conditions and the Privacy Policy.</span>');
  1126. Materialize.toast($toastContent, 8000);
  1127. }
  1128. },
  1129. "click #github-login": function(){
  1130. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  1131. console.log(err, res);
  1132. });
  1133. }
  1134. });
  1135. Template.news.events({
  1136. "click #createArticleButton": function() {
  1137. var title = $("#title").val();
  1138. var content = $("#content").val();
  1139. var anonymous = $("#anonymous").is(":checked");
  1140. Meteor.call("createArticle", {title: title, content: content, anonymous: anonymous}, function(err, res) {
  1141. if (err) {
  1142. var $toastContent = $('<span><strong>Article not created.</strong> ' + err.reason + '</span>');
  1143. Materialize.toast($toastContent, 8000);
  1144. } else {
  1145. $('#createArticle').closeModal()
  1146. $("#title").val("").change();
  1147. $("#content").val("").change();
  1148. $("#anonymous").prop("checked", false).change();
  1149. }
  1150. });
  1151. }
  1152. });
  1153. Template.room.events({
  1154. "input #volume_slider": function() {
  1155. console.log("Test3");
  1156. var volume = Number($("#volume_slider").val());
  1157. localStorage.setItem("volume", volume);
  1158. if (YTPlayer !== undefined) {
  1159. YTPlayer.setVolume(volume);
  1160. }
  1161. },
  1162. "click #add-song-modal-button": function() {
  1163. Session.set("songResults", []);
  1164. },
  1165. "click #return-button": function() {
  1166. Session.set("editingSong", false);
  1167. },
  1168. "click #removeSong": function(e) {
  1169. var id = $(e.target).data("result");
  1170. var songs = Session.get("songResults");
  1171. var currentSong;
  1172. songs = songs.filter(function(song) {
  1173. return id !== song.id;
  1174. });
  1175. Session.set("songResults", []);
  1176. Session.set("songResults", songs);
  1177. },
  1178. "click #addSong": function(e) {
  1179. var id = $(e.target).data("result");
  1180. var songs = Session.get("songResults");
  1181. var currentSong;
  1182. songs.forEach(function(song) {
  1183. if (song.id === id) {
  1184. currentSong = song;
  1185. }
  1186. });
  1187. Session.set("editingSong", true);
  1188. var title = currentSong.title;
  1189. var artist = currentSong.artist;
  1190. var img = currentSong.img;
  1191. getSpotifyInfo(title.replace(/\[.*\]/g, ""), function (data) {
  1192. if (data.tracks.items.length > 0) {
  1193. title = data.tracks.items[0].name;
  1194. var artists = [];
  1195. img = data.tracks.items[0].album.images[2].url;
  1196. data.tracks.items[0].artists.forEach(function (artist) {
  1197. artists.push(artist.name);
  1198. });
  1199. artist = artists.join(", ");
  1200. $("#title").val(title).change();
  1201. $("#artist").val(artist).change();
  1202. $("#img").val(img).change();
  1203. $("#id").val(id).change();
  1204. $("#genres").val(null).change();
  1205. } else {
  1206. $("#title").val(title).change();
  1207. $("#artist").val(artist).change();
  1208. $("#img").val(img).change();
  1209. $("#id").val(id).change();
  1210. $("#genres").val(null).change();
  1211. // I give up for now... Will fix this later. -Kris
  1212. }
  1213. });
  1214. },
  1215. "click #import-playlist-button": function () {
  1216. if (!Session.get("importingPlaylist")) {
  1217. Session.set("songResults", []);
  1218. var playlist_link = $("#playlist-url").val();
  1219. var playlist_id = gup("list", playlist_link);
  1220. var ytImportQueue = [];
  1221. var totalVideos = 0;
  1222. var videosInvalid = 0;
  1223. var videosInQueue = 0;
  1224. var videosInPlaylist = 0;
  1225. var ranOnce = false;
  1226. Session.set("importingPlaylist", true);
  1227. $("#import-playlist-button").attr("disabled", "");
  1228. $("#import-playlist-button").addClass("disabled");
  1229. $("#playlist-url").attr("disabled", "");
  1230. $("#playlist-url").addClass("disabled");
  1231. $("#import-progress").css({width: "0%"});
  1232. function makeAPICall(playlist_id, nextPageToken) {
  1233. if (nextPageToken !== undefined) {
  1234. nextPageToken = "&pageToken=" + nextPageToken;
  1235. } else {
  1236. nextPageToken = "";
  1237. }
  1238. $.ajax({
  1239. type: "GET",
  1240. url: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlist_id + nextPageToken + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  1241. applicationType: "application/json",
  1242. contentType: "json",
  1243. success: function (data) {
  1244. if (!ranOnce) {
  1245. ranOnce = true;
  1246. totalVideos = data.pageInfo.totalResults;
  1247. }
  1248. var nextToken = data.nextPageToken;
  1249. for (var i in data.items) {
  1250. var item = data.items[i];
  1251. if (item.snippet.thumbnails !== undefined) {
  1252. var genre = Session.get("type");
  1253. if (Playlists.find({
  1254. type: genre,
  1255. "songs.id": item.snippet.resourceId.videoId
  1256. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  1257. videosInPlaylist++;
  1258. } else if (Queues.find({
  1259. type: genre,
  1260. "songs.id": item.snippet.resourceId.videoId
  1261. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  1262. videosInQueue++;
  1263. } else {
  1264. var percentage = ytImportQueue.length / (totalVideos - videosInvalid) * 100;
  1265. $("#import-progress").css({width: percentage + "%"});
  1266. ytImportQueue.push({title: item.snippet.title, artist: item.snippet.channelTitle, id: item.snippet.resourceId.videoId, image: item.snippet.thumbnails.medium.url});
  1267. }
  1268. } else {
  1269. videosInvalid++;
  1270. }
  1271. }
  1272. if (nextToken !== undefined) {
  1273. makeAPICall(playlist_id, nextToken);
  1274. } else {
  1275. /*$("#playlist-import-queue > div > i").click(function () {
  1276. var title = $(this).parent().find("div > .song-result-title").text();
  1277. for (var i in ytImportQueue) {
  1278. if (ytImportQueue[i].title === title) {
  1279. ytImportQueue.splice(i, 1);
  1280. }
  1281. }
  1282. $(this).parent().remove();
  1283. Session.set("YTImportQueue", ytImportQueue);
  1284. });*/
  1285. Session.set("importingPlaylist", false);
  1286. $("#import-progress").css({width: "100%"});
  1287. $("#import-playlist-button").removeAttr("disabled");
  1288. $("#import-playlist-button").removeClass("disabled");
  1289. $("#playlist-url").removeAttr("disabled");
  1290. $("#playlist-url").removeClass("disabled");
  1291. Session.set("YTImportQueue", ytImportQueue);
  1292. Session.set("songResults", ytImportQueue);
  1293. }
  1294. },
  1295. error: function() {
  1296. Session.set("importingPlaylist", false);
  1297. $("#import-progress").css({width: "0%"});
  1298. $("#import-playlist-button").removeAttr("disabled");
  1299. $("#import-playlist-button").removeClass("disabled");
  1300. $("#playlist-url").removeAttr("disabled");
  1301. $("#playlist-url").removeClass("disabled");
  1302. }
  1303. })
  1304. }
  1305. makeAPICall(playlist_id);
  1306. }
  1307. },
  1308. "click #confirm-import": function () {
  1309. var YTImportQueue = Session.get("YTImportQueue");
  1310. $("#import-playlist-button").attr("disabled", "");
  1311. $("#import-playlist-button").addClass("disabled");
  1312. $("#confirm-import").addClass("disabled");
  1313. $("#confirm-import").addClass("disabled");
  1314. $("#playlist-url").attr("disabled", "");
  1315. $("#playlist-url").addClass("disabled");
  1316. $("#import-progress").css({width: "0%"});
  1317. var genres = $("#genres_pl").val() || [];
  1318. var failed = 0;
  1319. var success = 0;
  1320. var processed = 0;
  1321. var total = YTImportQueue.length;
  1322. YTImportQueue.forEach(function (song) {
  1323. var songData = {id: song.id, title: song.title, artist: "Unknown", genres: genres};
  1324. Meteor.call("addSongToQueue", songData, function (err, res) {
  1325. if (err) {
  1326. console.log(err);
  1327. failed++;
  1328. } else {
  1329. success++;
  1330. }
  1331. processed++;
  1332. var percent = processed / total * 100;
  1333. $("#import-progress").css({width: percent + "%"});
  1334. if (processed === total) {
  1335. $("#import-playlist-button").removeAttr("disabled");
  1336. $("#import-playlist-button").removeClass("disabled");
  1337. $("#confirm-import").removeAttr("disabled");
  1338. $("#confirm-import").removeClass("disabled");
  1339. $("#playlist-url").removeAttr("disabled", "");
  1340. $("#playlist-url").removeClass("disabled");
  1341. $("#import-progress").css({width: "0%"});
  1342. Session.set("songResults", []);
  1343. Session.set("YTImportQueue", [])
  1344. }
  1345. });
  1346. });
  1347. },
  1348. "click #chat-tab": function () {
  1349. $("#chat-tab").removeClass("unread-messages");
  1350. },
  1351. "click #global-chat-tab": function () {
  1352. $("#global-chat-tab").removeClass("unread-messages");
  1353. },
  1354. "click #sync": function () {
  1355. if (Session.get("currentSong") !== undefined) {
  1356. var room = Rooms.findOne({type: Session.get("type")});
  1357. if (room !== undefined) {
  1358. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  1359. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  1360. if (YTPlayer !== undefined) {
  1361. YTPlayer.seekTo(skipDuration + timeIn / 1000);
  1362. }
  1363. }
  1364. }
  1365. },
  1366. "click #lock": function () {
  1367. Meteor.call("lockRoom", Session.get("type"));
  1368. },
  1369. "click #unlock": function () {
  1370. Meteor.call("unlockRoom", Session.get("type"));
  1371. },
  1372. "click #chat-tab": function (e) {
  1373. Meteor.setTimeout(function () {
  1374. $("#chat-ul").scrollTop(100000);
  1375. }, 1);
  1376. },
  1377. "click #global-chat-tab": function (e) {
  1378. Meteor.setTimeout(function () {
  1379. $("#global-chat-ul").scrollTop(100000);
  1380. }, 1);
  1381. },
  1382. "click #submit": function () {
  1383. sendMessage();
  1384. Meteor.setTimeout(function () {
  1385. $("#chat-ul").scrollTop(100000);
  1386. }, 1000)
  1387. },
  1388. "click #global-submit": function () {
  1389. sendMessageGlobal();
  1390. Meteor.setTimeout(function () {
  1391. $("#global-chat-ul").scrollTop(100000);
  1392. }, 1000)
  1393. },
  1394. "keyup #chat-input": function (e) {
  1395. if (e.type === "keyup" && e.which === 13) {
  1396. e.preventDefault();
  1397. if (!$('#chat-input').data('dropdownshown')) {
  1398. sendMessage();
  1399. Meteor.setTimeout(function () {
  1400. $("#chat-ul").scrollTop(100000);
  1401. }, 1000)
  1402. }
  1403. }
  1404. },
  1405. "keyup #global-chat-input": function (e) {
  1406. if (e.type === "keyup" && e.which === 13) {
  1407. e.preventDefault();
  1408. if (!$('#global-chat-input').data('dropdownshown')) {
  1409. sendMessageGlobal();
  1410. Meteor.setTimeout(function () {
  1411. $("#global-chat-ul").scrollTop(100000);
  1412. }, 1000)
  1413. }
  1414. }
  1415. },
  1416. "click #like": function (e) {
  1417. $("#like").blur();
  1418. Meteor.call("likeSong", Session.get("currentSong").mid);
  1419. },
  1420. "click #dislike": function (e) {
  1421. $("#dislike").blur();
  1422. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  1423. },
  1424. "click #vote-skip": function () {
  1425. Meteor.call("voteSkip", type, function (err, res) {
  1426. $("#vote-skip").attr("disabled", true);
  1427. });
  1428. },
  1429. "click #report-prev": function (e) {
  1430. if (Session.get("previousSong") !== undefined) {
  1431. Session.set("reportPrevious", true);
  1432. $("#report-prev").prop("disabled", true);
  1433. $("#report-curr").prop("disabled", false);
  1434. }
  1435. },
  1436. "click #report-curr": function (e) {
  1437. Session.set("reportPrevious", false);
  1438. $("#report-prev").prop("disabled", false);
  1439. $("#report-curr").prop("disabled", true);
  1440. },
  1441. "click #report-modal": function () {
  1442. Session.set("currentSongR", Session.get("currentSong"));
  1443. Session.set("previousSongR", Session.get("previousSong"));
  1444. },
  1445. "click #add-song-button": function (e) {
  1446. e.preventDefault();
  1447. id = $("#id").val();
  1448. var title = $("#title").val();
  1449. var artist = $("#artist").val();
  1450. var genres = $("#genres").val() || [];
  1451. var songData = {id: id, title: title, artist: artist, genres: genres};
  1452. if (Songs.find({id: id}).count() > 0) {
  1453. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been added.</span>');
  1454. Materialize.toast($toastContent, 8000);
  1455. } else if (Queues.find({id: id}).count() > 0) {
  1456. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been requested.</span>');
  1457. Materialize.toast($toastContent, 8000);
  1458. } else {
  1459. Meteor.call("addSongToQueue", songData, function (err, res) {
  1460. console.log(err, res);
  1461. if (err) {
  1462. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  1463. Materialize.toast($toastContent, 8000);
  1464. } else {
  1465. var $toastContent = $('<span><strong>Song added.</strong> Your song has succesfully been added to the queue.</span>');
  1466. Materialize.toast($toastContent, 8000);
  1467. $('#add_song_modal').closeModal();
  1468. Session.set("editingSong", false);
  1469. }
  1470. });
  1471. }
  1472. },
  1473. "click #toggle-video": function (e) {
  1474. e.preventDefault();
  1475. if (Session.get("mediaHidden")) {
  1476. $("#media-container").removeClass("hidden");
  1477. $("#toggle-video").text("Hide video");
  1478. Session.set("mediaHidden", false);
  1479. } else {
  1480. $("#media-container").addClass("hidden");
  1481. $("#toggle-video").text("Show video");
  1482. Session.set("mediaHidden", true);
  1483. }
  1484. },
  1485. "click #return": function (e) {
  1486. $("#add-info").hide();
  1487. $("#search-info").show();
  1488. },
  1489. "click #search-song": function () {
  1490. var songs = [];
  1491. Session.set("songResults", songs);
  1492. $.ajax({
  1493. type: "GET",
  1494. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY&type=video&maxResults=25",
  1495. applicationType: "application/json",
  1496. contentType: "json",
  1497. success: function (data) {
  1498. for (var i in data.items) {
  1499. var item = data.items[i];
  1500. console.log(item);
  1501. songs.push({title: item.snippet.title, artist: item.snippet.channelTitle, id: item.id.videoId, image: item.snippet.thumbnails.medium.url});
  1502. }
  1503. Session.set("songResults", songs);
  1504. /*$("#song-results > div").click(function () {
  1505. $("#search-info").hide();
  1506. $("#add-info").show();
  1507. var title = $(this).find("div > .song-result-title").text();
  1508. for (var i in songs) {
  1509. if (songs[i].title === title) {
  1510. var songObj = {
  1511. id: songs[i].id,
  1512. title: songs[i].title,
  1513. type: "youtube"
  1514. };
  1515. $("#title").val(songObj.title);
  1516. $("#artist").val("");
  1517. $("#id").val(songObj.id);
  1518. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function (data) {
  1519. if (data.tracks.items.length > 0) {
  1520. $("#title").val(data.tracks.items[0].name);
  1521. var artists = [];
  1522. $("#img").val(data.tracks.items[0].album.images[2].url);
  1523. data.tracks.items[0].artists.forEach(function (artist) {
  1524. artists.push(artist.name);
  1525. });
  1526. $("#artist").val(artists.join(", "));
  1527. }
  1528. });
  1529. }
  1530. }
  1531. })*/
  1532. }
  1533. })
  1534. },
  1535. "click #volume-icon": function () {
  1536. var volume = 0;
  1537. var slider = $("#volume-slider").slider();
  1538. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1539. if (YTPlayer !== undefined) {
  1540. YTPlayer.setVolume(volume);
  1541. localStorage.setItem("volume", volume);
  1542. $("#volume-slider").slider("setValue", volume);
  1543. }
  1544. },
  1545. "click #play": function () {
  1546. Meteor.call("resumeRoom", type);
  1547. },
  1548. "click #pause": function () {
  1549. Meteor.call("pauseRoom", type);
  1550. },
  1551. "click #skip": function () {
  1552. Meteor.call("skipSong", type);
  1553. },
  1554. "click #shuffle": function () {
  1555. Meteor.call("shufflePlaylist", type);
  1556. },
  1557. "change input": function (e) {
  1558. /*if (e.target && e.target.id) {
  1559. var partsOfId = e.target.id.split("-");
  1560. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  1561. var camelCase = partsOfId.join("");
  1562. Session.set(camelCase, e.target.checked);
  1563. }*/
  1564. },
  1565. "click #report-song-button": function () {
  1566. var room = Session.get("type");
  1567. var reportData = {};
  1568. reportData.song = Session.get("currentSong").mid;
  1569. reportData.type = [];
  1570. reportData.reason = [];
  1571. $(".report-layer-1 > .checkbox input:checked").each(function () {
  1572. reportData.type.push(this.id);
  1573. if (this.id == "report-other") {
  1574. var otherText = $(".other-textarea").val();
  1575. }
  1576. });
  1577. $(".report-layer-2 input:checked").each(function () {
  1578. reportData.reason.push(this.id);
  1579. });
  1580. console.log(reportData);
  1581. Meteor.call("submitReport", room, reportData, Session.get("id"), function () {
  1582. $("#close-modal-r").click();
  1583. });
  1584. },
  1585. "change #si_or_pl": function () {
  1586. Session.set("songResults", []);
  1587. Session.set("si_or_pl", $("#si_or_pl").val());
  1588. },
  1589. "click #close-modal-a": function () {
  1590. $("#select_single").attr("selected", true);
  1591. $("#search-info").show();
  1592. $("#playlist-import").hide();
  1593. }
  1594. });
  1595. // Settings Template
  1596. Template.settings.events({
  1597. "click #save-settings": function() {
  1598. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  1599. },
  1600. "click #delete-account": function(){
  1601. $("#delete-account").text("Click to confirm");
  1602. $("#delete-account").click(function(){
  1603. var bool = confirm("Are you sure you want to delete your account?");
  1604. if(bool) {
  1605. Meteor.call("deleteAccount");
  1606. } else{
  1607. $("#delete-account").text("Delete");
  1608. }
  1609. })
  1610. },
  1611. "click #change-password": function(){
  1612. var oldPassword = $("#old-password").val();
  1613. var newPassword= $("#new-password").val();
  1614. var confirmPassword = $("#confirm-password").val();
  1615. if(newPassword === confirmPassword){
  1616. Accounts.changePassword(oldPassword, newPassword, function(err){
  1617. if(err){
  1618. $("#old-password").val("");
  1619. $("#new-password").val("");
  1620. $("#confirm-password").val("");
  1621. var $toastContent = $('<span><strong>Password not changed.</strong> ' + err.reason + '</span>');
  1622. Materialize.toast($toastContent, 8000);
  1623. } else {
  1624. $("#old-password").val("");
  1625. $("#new-password").val("");
  1626. $("#confirm-password").val("");
  1627. var $toastContent = $('<span><strong>Password changed.</strong></span>');
  1628. Materialize.toast($toastContent, 8000);
  1629. }
  1630. });
  1631. }
  1632. }
  1633. });
  1634. var previewEndSongTimeout = undefined;