2
0

events.js 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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 = $("#chat-message").val();
  151. if (!$("#chat-message").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. $("#chat-message").val("");
  162. } else {
  163. $("#chat-message").val("");
  164. }
  165. } else {
  166. Meteor.call("sendMessage", "global", message, function (err, res) {
  167. if (res) {
  168. $("#chat-message").val("");
  169. }
  170. });
  171. }
  172. }
  173. }
  174. }
  175. Template.admin.events({
  176. "click a": function(e){
  177. var id = e.currentTarget.id;
  178. console.log(id.toLowerCase());
  179. Session.set("playlistToEdit", id);
  180. },
  181. "click #croom_create": function() {
  182. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#croom_private").prop("checked"), $("#croom_desc").val(), function (err, res) {
  183. if (err) {
  184. alert("Error " + err.error + ": " + err.reason);
  185. } else {
  186. window.location = "/" + $("#croom_tag").val();
  187. }
  188. });
  189. },
  190. "click #rreset_confirm": function(){
  191. $('#confirmModal').modal('hide');
  192. Meteor.call("resetRating");
  193. },
  194. "click #edit_desc": function(){
  195. console.log($(this));
  196. console.log($(this)[0].type);
  197. Session.set("roomDesc", $(this)[0].type);
  198. $("#desc_text").val(Rooms.findOne({type: Session.get("roomDesc")}).roomDesc);
  199. },
  200. "click #submit_desc": function(){
  201. var description = $("#desc_text").val();
  202. Meteor.call("editRoomDesc", Session.get("roomDesc"), description);
  203. $("#desc-modal").closeModal();
  204. },
  205. "click #submit-alert": function(){
  206. var alertDesc = $("#alert-desc").val()
  207. if(alertDesc !== ""){
  208. Meteor.call("addAlert", alertDesc);
  209. }
  210. },
  211. "click #remove-alerts": function(){
  212. Meteor.call("removeAlerts");
  213. }
  214. });
  215. Template.feedback.events({
  216. "click #feedback_submit": function(){
  217. if(Meteor.userId()){
  218. if($("#feedback_message").val().length !== 0 && $("#feedback_message").hasClass("invalid") === false){
  219. Meteor.call("sendFeedback", $("#feedback_message").val());
  220. $("#feedback_message").val("");
  221. $("#modal1").closeModal()
  222. } else{
  223. 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>');
  224. Materialize.toast($toastContent, 8000);
  225. }
  226. } else {
  227. var $toastContent = $('<span><strong>Feedback not sent.</strong> You must be logged in.</span>');
  228. Materialize.toast($toastContent, 4000);
  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>Login Failed.</strong> ' + err.reason + '</span>');
  274. Materialize.toast($toastContent, 4000);
  275. } else {
  276. window.location.href = "/";
  277. }
  278. });
  279. },
  280. "click #github-login": function(){
  281. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  282. console.log(err, res);
  283. });
  284. }
  285. });
  286. Template.playlist.events({
  287. "keyup #search-playlist": function(){
  288. if($("#search-playlist").val().length === 0){
  289. $(".pl-item").show();
  290. } else {
  291. $(".pl-item").hide();
  292. var input = $("#search-playlist").val().toLowerCase();
  293. $(".pl-item strong").each(function(i, el){
  294. if($(el).text().toLowerCase().indexOf(input) !== -1){
  295. $(el).parent(".pl-item").show();
  296. }
  297. })
  298. $(".pl-item #pl-artist").each(function(i, el){
  299. if($(el).text().toLowerCase().indexOf(input) !== -1){
  300. $(el).parent(".pl-item").show();
  301. }
  302. })
  303. }
  304. },
  305. "click #pl-item": function(){
  306. console.log($(this).text());
  307. }
  308. });
  309. Template.profile.events({
  310. //Edit real name
  311. "click #edit-name": function(){
  312. $("#name").hide();
  313. $("#name-div").show();
  314. $("#edit-name").hide();
  315. $("#cancel-edit").show();
  316. },
  317. "click #submit-name": function(){
  318. var user = Meteor.user();
  319. $("#name").show();
  320. $("#name-div").hide();
  321. $("#edit-name").show();
  322. $("#cancel-edit").hide();
  323. var realname = $("#input-name").val();
  324. var username = user.profile.username;
  325. $("#name").text("Name: " + realname);
  326. $("#input-name").val("")
  327. Meteor.call("updateRealName", realname);
  328. },
  329. "click #cancel-edit": function(){
  330. $("#name").show();
  331. $("#name-div").hide();
  332. $("#edit-name").show();
  333. $("#cancel-edit").hide();
  334. $("#input-name").val("");
  335. },
  336. //Edit username
  337. "click #edit-username": function(){
  338. $("#username").hide();
  339. $("#username-div").show();
  340. $("#edit-username").hide();
  341. $("#cancel-username").show();
  342. },
  343. "click #submit-username": function(){
  344. var user = Meteor.user()
  345. $("#username").show();
  346. $("#username-div").hide();
  347. $("#edit-username").show();
  348. $("#cancel-username").hide();
  349. var username = user.username;
  350. var newUserName = $("#input-username").val();
  351. $("#profile-name").text(newUserName)
  352. $("#username").text("Username: " + newUserName);
  353. $("#input-username").val("")
  354. Meteor.call("updateUserName", newUserName);
  355. window.location = "/u/" + newUserName;
  356. },
  357. "click #cancel-username": function(){
  358. $("#username").show();
  359. $("#username-div").hide();
  360. $("#edit-username").show();
  361. $("#cancel-username").hide();
  362. $("#input-username").val("");
  363. },
  364. // Admins only Edit Rank
  365. "click #edit-rank": function() {
  366. $("#rank").hide();
  367. $("#rank-div").show();
  368. $("#edit-rank").hide();
  369. $("#cancel-rank").show();
  370. },
  371. "click #submit-rank": function() {
  372. $("#rank").show();
  373. $("#rank-div").hide();
  374. $("#edit-rank").show();
  375. $("#cancel-rank").hide();
  376. var newRank = $("#select-rank option:selected").val();
  377. var username = Session.get("username");
  378. console.log(username, newRank);
  379. },
  380. "click #cancel-rank": function() {
  381. $("#rank").show();
  382. $("#rank-div").hide();
  383. $("#edit-rank").show();
  384. $("#cancel-rank").hide();
  385. }
  386. });
  387. var seekerBarInterval = undefined;
  388. Template.queues.events({
  389. /* TODO Add undo delete button */
  390. "input #id": function() {
  391. console.log("Change!");
  392. $("#previewPlayerContainer").addClass("hide-preview");
  393. },
  394. "input #img": function() {
  395. var url = $("#img").val();
  396. console.log(url);
  397. Session.set("image_url", url);
  398. },
  399. "click .preview-button": function(e){
  400. Session.set("song", this);
  401. $("#previewModal").openModal();
  402. },
  403. "click #previewImageButton": function() {
  404. $("#preview-image").attr("src", Session.get("song").img);
  405. },
  406. "click .edit-queue-button": function(e){
  407. Session.set("song", this);
  408. Session.set("genre", $(e.target).data("genre"));
  409. Session.set("type", "queue");
  410. $("#mid").val(this.mid).change();
  411. $("#artist").val(this.artist).change();
  412. $("#title").val(this.title).change();
  413. $("#img").val(this.img).change();
  414. $("#id").val(this.id).change();
  415. $("#likes").val(this.likes).change();
  416. $("#dislikes").val(this.dislikes).change();
  417. $("#duration").val(this.duration).change();
  418. $("#skip-duration").val(this.skipDuration).change();
  419. $("#genres").val(this.genres).change();
  420. $("#genres").material_select();
  421. $("#previewPlayerContainer").addClass("hide-preview");
  422. Session.set("image_url", this.img);
  423. Session.set("editing", true);
  424. $("#editModal").openModal({
  425. complete : function() {
  426. Session.set("editing", false);
  427. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  428. YTPlayer.stopVideo();
  429. }
  430. }
  431. });
  432. },
  433. "click .add-song-button": function(e){
  434. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  435. Meteor.call("addSongToPlaylist", this, function(err) {
  436. console.log(err);
  437. if (err) {
  438. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  439. Materialize.toast($toastContent, 8000);
  440. }
  441. });
  442. },
  443. "click .deny-song-button": function(e){
  444. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  445. Meteor.call("removeSongFromQueue", this.mid);
  446. },
  447. "click #play": function() {
  448. var duration = Session.get("song").duration;
  449. var d = moment.duration(parseInt(duration), 'seconds');
  450. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  451. $("#previewPlayerContainer").removeClass("hide-preview");
  452. var song = Session.get("song");
  453. var id = song.id;
  454. var volume = localStorage.getItem("volume") || 20;
  455. if (song.duration !== 0) {
  456. $("#play").attr("disabled", true);
  457. $("#stop").attr("disabled", false);
  458. $("#pause").attr("disabled", false);
  459. $("#forward").attr("disabled", false);
  460. if (YTPlayer === undefined) {
  461. YTPlayer = new YT.Player("previewPlayer", {
  462. height: 540,
  463. width: 568,
  464. videoId: id,
  465. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  466. events: {
  467. 'onReady': function(event) {
  468. event.target.seekTo(Number(song.skipDuration));
  469. event.target.playVideo();
  470. event.target.setVolume(volume);
  471. },
  472. 'onStateChange': function(event){
  473. if (event.data == YT.PlayerState.PAUSED) {
  474. if (seekerBarInterval !== undefined) {
  475. Meteor.clearInterval(seekerBarInterval);
  476. seekerBarInterval = undefined;
  477. }
  478. }
  479. // if (event.data == YT.PlayerState.UNSTARTED) {
  480. // if (seekerBarInterval !== undefined) {
  481. // Meteor.clearInterval(seekerBarInterval);
  482. // seekerBarInterval = undefined;
  483. // }
  484. // $(".seeker-bar").css({width: "0"});
  485. // $("#time-elapsed").text("0:00");
  486. // $("#previewPlayerContainer").addClass("hide-preview");
  487. // console.log("HIDE MEY STACY!!!!")
  488. // }
  489. if (event.data == YT.PlayerState.PLAYING) {
  490. seekerBarInterval = Meteor.setInterval(function() {
  491. var duration = Session.get("song").duration;
  492. var timeElapsed = YTPlayer.getCurrentTime();
  493. var skipDuration = Session.get("song").skipDuration;
  494. if (duration <= (timeElapsed - skipDuration)) {
  495. YTPlayer.stopVideo();
  496. $("#play").attr("disabled", false);
  497. $("#stop").attr("disabled", true);
  498. $("#pause").attr("disabled", true);
  499. $("#forward").attr("disabled", true);
  500. $("#previewPlayerContainer").addClass("hide-preview");
  501. $(".seeker-bar").css({width: "0"});
  502. $("#time-elapsed").text("0:00");
  503. Meteor.clearInterval(seekerBarInterval);
  504. } else {
  505. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  506. $(".seeker-bar").css({width: percentComplete + "%"});
  507. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  508. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  509. }
  510. }, 100);
  511. $("#play").attr("disabled", true);
  512. $("#stop").attr("disabled", false);
  513. $("#pause").attr("disabled", false);
  514. $("#forward").attr("disabled", false);
  515. } else {
  516. $("#play").attr("disabled", false);
  517. $("#stop").attr("disabled", true);
  518. $("#pause").attr("disabled", true);
  519. $("#forward").attr("disabled", true);
  520. }
  521. }
  522. }
  523. });
  524. } else {
  525. if (YTPlayer.getPlayerState() === 2) {
  526. YTPlayer.playVideo();
  527. } else {
  528. console.log(id, song.skipDuration, song.duration);
  529. YTPlayer.loadVideoById(id);
  530. YTPlayer.seekTo(Number(song.skipDuration));
  531. }
  532. }
  533. $("#previewPlayerContainer").removeClass("hide-preview");
  534. }
  535. },
  536. "click #stop": function() {
  537. $("#play").attr("disabled", false);
  538. $("#stop").attr("disabled", true);
  539. $("#pause").attr("disabled", true);
  540. $("#forward").attr("disabled", true);
  541. if (previewEndSongTimeout !== undefined) {
  542. Meteor.clearTimeout(previewEndSongTimeout);
  543. }
  544. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  545. YTPlayer.stopVideo();
  546. }
  547. },
  548. "click #pause": function() {
  549. $("#play").attr("disabled", false);
  550. $("#stop").attr("disabled", false);
  551. $("#pause").attr("disabled", true);
  552. $("#forward").attr("disabled", true);
  553. if (previewEndSongTimeout !== undefined) {
  554. Meteor.clearTimeout(previewEndSongTimeout);
  555. }
  556. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  557. YTPlayer.pauseVideo();
  558. }
  559. },
  560. "click #forward": function() {
  561. var error = false;
  562. if (YTPlayer !== undefined) {
  563. var duration = Number(Session.get("song").duration) | 0;
  564. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  565. if (YTPlayer.getDuration() < duration + skipDuration) {
  566. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  567. Materialize.toast($toastContent, 8000);
  568. error = true;
  569. } else {
  570. YTPlayer.seekTo(skipDuration + duration - 10);
  571. }
  572. }
  573. if (!error) {
  574. if (previewEndSongTimeout !== undefined) {
  575. Meteor.clearTimeout(previewEndSongTimeout);
  576. }
  577. previewEndSongTimeout = Meteor.setTimeout(function() {
  578. if (YTPlayer !== undefined) {
  579. YTPlayer.stopVideo();
  580. }
  581. $("#play").attr("disabled", false);
  582. $("#stop").attr("disabled", true);
  583. $("#pause").attr("disabled", true);
  584. $("#forward").attr("disabled", true);
  585. $("#previewPlayerContainer").addClass("hide-preview");
  586. }, 10000);
  587. }
  588. },
  589. "click #get-spotify-info": function() {
  590. var search = $("#title").val();
  591. var artistName = $("#artist").val();
  592. getSpotifyInfo(search, function(data) {
  593. for(var i in data){
  594. for(var j in data[i].items){
  595. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  596. $("#img").val(data[i].items[j].album.images[2].url).change();
  597. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  598. return;
  599. }
  600. }
  601. }
  602. }, artistName);
  603. },
  604. "click #save-song-button": function() {
  605. var newSong = {};
  606. newSong.mid = $("#mid").val();
  607. newSong.id = $("#id").val();
  608. newSong.likes = Number($("#likes").val());
  609. newSong.dislikes = Number($("#dislikes").val());
  610. newSong.title = $("#title").val();
  611. newSong.artist = $("#artist").val();
  612. newSong.img = $("#img").val();
  613. newSong.duration = Number($("#duration").val());
  614. newSong.skipDuration = $("#skip-duration").val();
  615. newSong.requestedBy = Session.get("song").requestedBy;
  616. newSong.genres = $("#genres").val();
  617. if(newSong.skipDuration === undefined){
  618. newSong.skipDuration = 0;
  619. }
  620. Meteor.call("updateQueueSong", newSong.mid, newSong, function(err, res) {
  621. if (err) {
  622. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  623. Materialize.toast($toastContent, 8000);
  624. } else {
  625. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  626. Materialize.toast($toastContent, 4000);
  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. newSong.genres = $("#genres").val();
  845. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  846. console.log(err, res);
  847. if (err) {
  848. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  849. Materialize.toast($toastContent, 8000);
  850. } else {
  851. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  852. Materialize.toast($toastContent, 4000);
  853. Session.set("song", newSong);
  854. }
  855. });
  856. }
  857. });
  858. Template.manageSongs.events({
  859. /* TODO Add undo delete button */
  860. "change #show_genres_cb": function() {
  861. var selected = $("#show_genres_cb").is(":checked");
  862. Session.set("showGenres", selected);
  863. },
  864. "change #show_no_genres_cb": function() {
  865. var selected = $("#show_no_genres_cb").is(":checked");
  866. Session.set("showNoGenres", selected);
  867. },
  868. "input #id": function() {
  869. $("#previewPlayerContainer").addClass("hide-preview");
  870. },
  871. "input #img": function() {
  872. var url = $("#img").val();
  873. Session.set("image_url", url);
  874. },
  875. "click .preview-button": function(e){
  876. Session.set("song", this);
  877. $("#previewModal").openModal();
  878. },
  879. "click #previewImageButton": function() {
  880. $("#preview-image").attr("src", Session.get("song").img);
  881. },
  882. "click .edit-song-button": function(e){
  883. Session.set("song", this);
  884. Session.set("genre", $(e.target).data("genre"));
  885. $("#mid").val(this.mid).change();
  886. $("#artist").val(this.artist).change();
  887. $("#title").val(this.title).change();
  888. $("#img").val(this.img).change();
  889. $("#id").val(this.id).change();
  890. $("#likes").val(this.likes).change();
  891. $("#dislikes").val(this.dislikes).change();
  892. $("#duration").val(this.duration).change();
  893. $("#skip-duration").val(this.skipDuration).change();
  894. $("#genres").val(this.genres).change();
  895. $("#genres").material_select();
  896. $("#previewPlayerContainer").addClass("hide-preview");
  897. Session.set("image_url", this.img);
  898. Session.set("editing", true);
  899. $("#editModal").openModal({
  900. complete : function() {
  901. Session.set("editing", false);
  902. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  903. YTPlayer.stopVideo();
  904. }
  905. }
  906. });
  907. },
  908. "click .remove-song-button": function(){
  909. console.log(this.mid);
  910. console.log("TEST!");
  911. Meteor.call("deleteSong", this.mid);
  912. },
  913. "click #play": function() {
  914. var duration = Session.get("song").duration;
  915. var d = moment.duration(parseInt(duration), 'seconds');
  916. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  917. $("#previewPlayerContainer").removeClass("hide-preview");
  918. var song = Session.get("song");
  919. var id = song.id;
  920. var volume = localStorage.getItem("volume") || 20;
  921. if (song.duration !== 0) {
  922. $("#play").attr("disabled", true);
  923. $("#stop").attr("disabled", false);
  924. $("#pause").attr("disabled", false);
  925. $("#forward").attr("disabled", false);
  926. if (YTPlayer === undefined) {
  927. YTPlayer = new YT.Player("previewPlayer", {
  928. height: 540,
  929. width: 568,
  930. videoId: id,
  931. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  932. events: {
  933. 'onReady': function(event) {
  934. event.target.seekTo(Number(song.skipDuration));
  935. event.target.playVideo();
  936. event.target.setVolume(volume);
  937. },
  938. 'onStateChange': function(event){
  939. if (event.data == YT.PlayerState.PAUSED) {
  940. if (seekerBarInterval !== undefined) {
  941. Meteor.clearInterval(seekerBarInterval);
  942. seekerBarInterval = undefined;
  943. }
  944. }
  945. // if (event.data == YT.PlayerState.UNSTARTED) {
  946. // if (seekerBarInterval !== undefined) {
  947. // Meteor.clearInterval(seekerBarInterval);
  948. // seekerBarInterval = undefined;
  949. // }
  950. // $(".seeker-bar").css({width: "0"});
  951. // $("#time-elapsed").text("0:00");
  952. // $("#previewPlayerContainer").addClass("hide-preview");
  953. // }
  954. if (event.data == YT.PlayerState.PLAYING) {
  955. seekerBarInterval = Meteor.setInterval(function() {
  956. var duration = Session.get("song").duration;
  957. var timeElapsed = YTPlayer.getCurrentTime();
  958. var skipDuration = Session.get("song").skipDuration;
  959. if (duration <= (timeElapsed - skipDuration)) {
  960. YTPlayer.stopVideo();
  961. $("#play").attr("disabled", false);
  962. $("#stop").attr("disabled", true);
  963. $("#pause").attr("disabled", true);
  964. $("#forward").attr("disabled", true);
  965. $("#previewPlayerContainer").addClass("hide-preview");
  966. $(".seeker-bar").css({width: "0"});
  967. $("#time-elapsed").text("0:00");
  968. Meteor.clearInterval(seekerBarInterval);
  969. } else {
  970. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  971. $(".seeker-bar").css({width: percentComplete + "%"});
  972. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  973. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  974. }
  975. }, 100);
  976. $("#play").attr("disabled", true);
  977. $("#stop").attr("disabled", false);
  978. $("#pause").attr("disabled", false);
  979. $("#forward").attr("disabled", false);
  980. } else {
  981. $("#play").attr("disabled", false);
  982. $("#stop").attr("disabled", true);
  983. $("#pause").attr("disabled", true);
  984. $("#forward").attr("disabled", true);
  985. }
  986. }
  987. }
  988. });
  989. } else {
  990. if (YTPlayer.getPlayerState() === 2) {
  991. YTPlayer.playVideo();
  992. } else {
  993. console.log(id, song.skipDuration, song.duration);
  994. YTPlayer.loadVideoById(id);
  995. YTPlayer.seekTo(Number(song.skipDuration));
  996. }
  997. }
  998. $("#previewPlayerContainer").removeClass("hide-preview");
  999. }
  1000. },
  1001. "click #stop": function() {
  1002. $("#play").attr("disabled", false);
  1003. $("#stop").attr("disabled", true);
  1004. $("#pause").attr("disabled", true);
  1005. $("#forward").attr("disabled", true);
  1006. if (previewEndSongTimeout !== undefined) {
  1007. Meteor.clearTimeout(previewEndSongTimeout);
  1008. }
  1009. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  1010. YTPlayer.stopVideo();
  1011. }
  1012. },
  1013. "click #pause": function() {
  1014. $("#play").attr("disabled", false);
  1015. $("#stop").attr("disabled", false);
  1016. $("#pause").attr("disabled", true);
  1017. $("#forward").attr("disabled", true);
  1018. if (previewEndSongTimeout !== undefined) {
  1019. Meteor.clearTimeout(previewEndSongTimeout);
  1020. }
  1021. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  1022. YTPlayer.pauseVideo();
  1023. }
  1024. },
  1025. "click #forward": function() {
  1026. var error = false;
  1027. if (YTPlayer !== undefined) {
  1028. var duration = Number(Session.get("song").duration) | 0;
  1029. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1030. if (YTPlayer.getDuration() < duration + skipDuration) {
  1031. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  1032. Materialize.toast($toastContent, 8000);
  1033. error = true;
  1034. } else {
  1035. YTPlayer.seekTo(skipDuration + duration - 10);
  1036. }
  1037. }
  1038. if (!error) {
  1039. if (previewEndSongTimeout !== undefined) {
  1040. Meteor.clearTimeout(previewEndSongTimeout);
  1041. }
  1042. previewEndSongTimeout = Meteor.setTimeout(function() {
  1043. if (YTPlayer !== undefined) {
  1044. YTPlayer.stopVideo();
  1045. }
  1046. $("#play").attr("disabled", false);
  1047. $("#stop").attr("disabled", true);
  1048. $("#pause").attr("disabled", true);
  1049. $("#forward").attr("disabled", true);
  1050. $("#previewPlayerContainer").addClass("hide-preview");
  1051. }, 10000);
  1052. }
  1053. },
  1054. "click #get-spotify-info": function() {
  1055. var search = $("#title").val();
  1056. var artistName = $("#artist").val();
  1057. getSpotifyInfo(search, function(data) {
  1058. for(var i in data){
  1059. for(var j in data[i].items){
  1060. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1061. $("#img").val(data[i].items[j].album.images[2].url).change();
  1062. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  1063. return;
  1064. }
  1065. }
  1066. }
  1067. }, artistName);
  1068. },
  1069. "click #save-song-button": function() {
  1070. var newSong = {};
  1071. newSong.mid = $("#mid").val();
  1072. newSong.id = $("#id").val();
  1073. newSong.likes = Number($("#likes").val());
  1074. newSong.dislikes = Number($("#dislikes").val());
  1075. newSong.title = $("#title").val();
  1076. newSong.artist = $("#artist").val();
  1077. newSong.img = $("#img").val();
  1078. newSong.duration = Number($("#duration").val());
  1079. newSong.skipDuration = $("#skip-duration").val();
  1080. newSong.requestedBy = Session.get("song").requestedBy;
  1081. newSong.genres = $("#genres").val() || [];
  1082. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  1083. console.log(err, res);
  1084. if (err) {
  1085. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  1086. Materialize.toast($toastContent, 8000);
  1087. } else {
  1088. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  1089. Materialize.toast($toastContent, 4000);
  1090. Session.set("song", newSong);
  1091. }
  1092. });
  1093. }
  1094. });
  1095. Template.register.events({
  1096. "submit form": function(e){
  1097. e.preventDefault();
  1098. var username = $("#username").val()
  1099. var email = $("#email").val()
  1100. var password = $("#password").val();
  1101. var captchaData = grecaptcha.getResponse();
  1102. var hasAcceptedTerms = $("#termsPrivacyBTN").is(":checked");
  1103. if(hasAcceptedTerms){
  1104. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  1105. grecaptcha.reset();
  1106. if (err) {
  1107. var $toastContent = $('<span><strong>Signup Failed.</strong> ' + err.reason + '</span>');
  1108. Materialize.toast($toastContent, 4000);
  1109. } else {
  1110. Meteor.loginWithPassword(username, password);
  1111. Accounts.onLogin(function(){
  1112. window.location.href = "/";
  1113. })
  1114. }
  1115. });
  1116. } else{
  1117. var $toastContent = $('<span><strong>Signup Failed.</strong> You must accept the terms.</span>');
  1118. Materialize.toast($toastContent, 4000);
  1119. }
  1120. },
  1121. "click #github-login": function(){
  1122. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  1123. console.log(err, res);
  1124. });
  1125. }
  1126. });
  1127. Template.news.events({
  1128. "click #createArticleButton": function() {
  1129. var title = $("#title").val();
  1130. var content = $("#content").val();
  1131. var anonymous = $("#anonymous").is(":checked");
  1132. Meteor.call("createArticle", {title: title, content: content, anonymous: anonymous}, function(err, res) {
  1133. if (err) {
  1134. var $toastContent = $('<span><strong>Article not created.</strong> ' + err.reason + '</span>');
  1135. Materialize.toast($toastContent, 8000);
  1136. } else {
  1137. $('#createArticle').closeModal()
  1138. $("#title").val("").change();
  1139. $("#content").val("").change();
  1140. $("#anonymous").prop("checked", false).change();
  1141. }
  1142. });
  1143. }
  1144. });
  1145. Template.room.events({
  1146. "input #volume_slider": function() {
  1147. console.log("Test3");
  1148. var volume = Number($("#volume_slider").val());
  1149. localStorage.setItem("volume", volume);
  1150. if (YTPlayer !== undefined) {
  1151. YTPlayer.setVolume(volume);
  1152. }
  1153. },
  1154. "click #add-song-modal-button": function() {
  1155. Session.set("songResults", []);
  1156. },
  1157. "click #return-button": function() {
  1158. Session.set("editingSong", false);
  1159. },
  1160. "click #removeSong": function(e) {
  1161. var id = $(e.target).data("result");
  1162. var songs = Session.get("songResults");
  1163. var currentSong;
  1164. songs = songs.filter(function(song) {
  1165. return id !== song.id;
  1166. });
  1167. Session.set("songResults", []);
  1168. Session.set("songResults", songs);
  1169. },
  1170. "click #addSong": function(e) {
  1171. var id = $(e.target).data("result");
  1172. var songs = Session.get("songResults");
  1173. var currentSong;
  1174. songs.forEach(function(song) {
  1175. if (song.id === id) {
  1176. currentSong = song;
  1177. }
  1178. });
  1179. Session.set("editingSong", true);
  1180. var title = currentSong.title;
  1181. var artist = currentSong.artist;
  1182. var img = currentSong.img;
  1183. getSpotifyInfo(title.replace(/\[.*\]/g, ""), function (data) {
  1184. if (data.tracks.items.length > 0) {
  1185. title = data.tracks.items[0].name;
  1186. var artists = [];
  1187. img = data.tracks.items[0].album.images[2].url;
  1188. data.tracks.items[0].artists.forEach(function (artist) {
  1189. artists.push(artist.name);
  1190. });
  1191. artist = artists.join(", ");
  1192. $("#title").val(title).change();
  1193. $("#artist").val(artist).change();
  1194. $("#img").val(img).change();
  1195. $("#id").val(id).change();
  1196. $("#genres").val(null).change();
  1197. } else {
  1198. $("#title").val(title).change();
  1199. $("#artist").val(artist).change();
  1200. $("#img").val(img).change();
  1201. $("#id").val(id).change();
  1202. $("#genres").val(null).change();
  1203. // I give up for now... Will fix this later. -Kris
  1204. }
  1205. });
  1206. },
  1207. "click #import-playlist-button": function () {
  1208. if (!Session.get("importingPlaylist")) {
  1209. Session.set("songResults", []);
  1210. var playlist_link = $("#playlist-url").val();
  1211. var playlist_id = gup("list", playlist_link);
  1212. var ytImportQueue = [];
  1213. var totalVideos = 0;
  1214. var videosInvalid = 0;
  1215. var videosInQueue = 0;
  1216. var videosInPlaylist = 0;
  1217. var ranOnce = false;
  1218. Session.set("importingPlaylist", true);
  1219. $("#import-playlist-button").attr("disabled", "");
  1220. $("#import-playlist-button").addClass("disabled");
  1221. $("#playlist-url").attr("disabled", "");
  1222. $("#playlist-url").addClass("disabled");
  1223. $("#import-progress").css({width: "0%"});
  1224. function makeAPICall(playlist_id, nextPageToken) {
  1225. if (nextPageToken !== undefined) {
  1226. nextPageToken = "&pageToken=" + nextPageToken;
  1227. } else {
  1228. nextPageToken = "";
  1229. }
  1230. $.ajax({
  1231. type: "GET",
  1232. url: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlist_id + nextPageToken + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  1233. applicationType: "application/json",
  1234. contentType: "json",
  1235. success: function (data) {
  1236. if (!ranOnce) {
  1237. ranOnce = true;
  1238. totalVideos = data.pageInfo.totalResults;
  1239. }
  1240. var nextToken = data.nextPageToken;
  1241. for (var i in data.items) {
  1242. var item = data.items[i];
  1243. if (item.snippet.thumbnails !== undefined) {
  1244. var genre = Session.get("type");
  1245. if (Playlists.find({
  1246. type: genre,
  1247. "songs.id": item.snippet.resourceId.videoId
  1248. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  1249. videosInPlaylist++;
  1250. } else if (Queues.find({
  1251. type: genre,
  1252. "songs.id": item.snippet.resourceId.videoId
  1253. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  1254. videosInQueue++;
  1255. } else {
  1256. var percentage = ytImportQueue.length / (totalVideos - videosInvalid) * 100;
  1257. $("#import-progress").css({width: percentage + "%"});
  1258. ytImportQueue.push({title: item.snippet.title, artist: item.snippet.channelTitle, id: item.snippet.resourceId.videoId, image: item.snippet.thumbnails.medium.url});
  1259. }
  1260. } else {
  1261. videosInvalid++;
  1262. }
  1263. }
  1264. if (nextToken !== undefined) {
  1265. makeAPICall(playlist_id, nextToken);
  1266. } else {
  1267. /*$("#playlist-import-queue > div > i").click(function () {
  1268. var title = $(this).parent().find("div > .song-result-title").text();
  1269. for (var i in ytImportQueue) {
  1270. if (ytImportQueue[i].title === title) {
  1271. ytImportQueue.splice(i, 1);
  1272. }
  1273. }
  1274. $(this).parent().remove();
  1275. Session.set("YTImportQueue", ytImportQueue);
  1276. });*/
  1277. Session.set("importingPlaylist", false);
  1278. $("#import-progress").css({width: "100%"});
  1279. $("#import-playlist-button").removeAttr("disabled");
  1280. $("#import-playlist-button").removeClass("disabled");
  1281. $("#playlist-url").removeAttr("disabled");
  1282. $("#playlist-url").removeClass("disabled");
  1283. Session.set("YTImportQueue", ytImportQueue);
  1284. Session.set("songResults", ytImportQueue);
  1285. }
  1286. },
  1287. error: function() {
  1288. Session.set("importingPlaylist", false);
  1289. $("#import-progress").css({width: "0%"});
  1290. $("#import-playlist-button").removeAttr("disabled");
  1291. $("#import-playlist-button").removeClass("disabled");
  1292. $("#playlist-url").removeAttr("disabled");
  1293. $("#playlist-url").removeClass("disabled");
  1294. }
  1295. })
  1296. }
  1297. makeAPICall(playlist_id);
  1298. }
  1299. },
  1300. "click #confirm-import": function () {
  1301. var YTImportQueue = Session.get("YTImportQueue");
  1302. $("#import-playlist-button").attr("disabled", "");
  1303. $("#import-playlist-button").addClass("disabled");
  1304. $("#playlist-url").attr("disabled", "");
  1305. $("#playlist-url").addClass("disabled");
  1306. $("#import-progress").css({width: "0%"});
  1307. var failed = 0;
  1308. var success = 0;
  1309. var processed = 0;
  1310. var total = YTImportQueue.length;
  1311. YTImportQueue.forEach(function (song) {
  1312. var songData = {type: "YouTube", id: song.id, title: song.title, artist: "", img: "", genres: [Session.get("type")]};
  1313. Meteor.call("addSongToQueue", songData, function (err, res) {
  1314. if (err) {
  1315. console.log(err);
  1316. failed++;
  1317. } else {
  1318. success++;
  1319. }
  1320. processed++;
  1321. var percentage = processed / total * 100;
  1322. $("#import-progress").css({width: percentage + "%"});
  1323. });
  1324. });
  1325. $("#import-playlist-button").removeAttr("disabled");
  1326. $("#import-playlist-button").removeClass("disabled");
  1327. $("#playlist-url").removeAttr("disabled", "");
  1328. $("#playlist-url").removeClass("disabled");
  1329. Session.set("songResults", []);
  1330. Session.set("YTImportQueue", [])
  1331. },
  1332. "click #chat-tab": function () {
  1333. $("#chat-tab").removeClass("unread-messages");
  1334. },
  1335. "click #global-chat-tab": function () {
  1336. $("#global-chat-tab").removeClass("unread-messages");
  1337. },
  1338. "click #sync": function () {
  1339. if (Session.get("currentSong") !== undefined) {
  1340. var room = Rooms.findOne({type: Session.get("type")});
  1341. if (room !== undefined) {
  1342. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  1343. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  1344. if (YTPlayer !== undefined) {
  1345. YTPlayer.seekTo(skipDuration + timeIn / 1000);
  1346. }
  1347. }
  1348. }
  1349. },
  1350. "click #lock": function () {
  1351. Meteor.call("lockRoom", Session.get("type"));
  1352. },
  1353. "click #unlock": function () {
  1354. Meteor.call("unlockRoom", Session.get("type"));
  1355. },
  1356. "click #submit": function () {
  1357. sendMessageGlobal();
  1358. Meteor.setTimeout(function () {
  1359. $(".chat-ul").scrollTop(100000);
  1360. }, 1000)
  1361. },
  1362. "keyup #chat-message": function (e) {
  1363. if (e.type === "keyup" && e.which === 13) {
  1364. e.preventDefault();
  1365. if (!$('#chat-message').data('dropdownshown')) {
  1366. sendMessageGlobal();
  1367. Meteor.setTimeout(function () {
  1368. $(".chat-ul").scrollTop(100000);
  1369. }, 1000)
  1370. }
  1371. }
  1372. },
  1373. "click #like": function (e) {
  1374. $("#like").blur();
  1375. Meteor.call("likeSong", Session.get("currentSong").mid);
  1376. },
  1377. "click #dislike": function (e) {
  1378. $("#dislike").blur();
  1379. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  1380. },
  1381. "click #vote-skip": function () {
  1382. Meteor.call("voteSkip", type, function (err, res) {
  1383. $("#vote-skip").attr("disabled", true);
  1384. });
  1385. },
  1386. "click #report-prev": function (e) {
  1387. if (Session.get("previousSong") !== undefined) {
  1388. Session.set("reportPrevious", true);
  1389. $("#report-prev").prop("disabled", true);
  1390. $("#report-curr").prop("disabled", false);
  1391. }
  1392. },
  1393. "click #report-curr": function (e) {
  1394. Session.set("reportPrevious", false);
  1395. $("#report-prev").prop("disabled", false);
  1396. $("#report-curr").prop("disabled", true);
  1397. },
  1398. "click #report-modal": function () {
  1399. Session.set("currentSongR", Session.get("currentSong"));
  1400. Session.set("previousSongR", Session.get("previousSong"));
  1401. },
  1402. "click #add-song-button": function (e) {
  1403. e.preventDefault();
  1404. parts = location.href.split('/');
  1405. var roomType = parts.pop();
  1406. var genre = roomType.toLowerCase();
  1407. var type = $("#type").val();
  1408. id = $("#id").val();
  1409. var title = $("#title").val();
  1410. var artist = $("#artist").val();
  1411. var genres = $("#genres").val() || [];
  1412. var songData = {type: type, id: id, title: title, artist: artist, genres: genres};
  1413. if (Songs.find({"id": songData.id}).count() > 0) {
  1414. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been added.</span>');
  1415. Materialize.toast($toastContent, 8000);
  1416. } else if (Queues.find({"id": songData.id}).count() > 0) {
  1417. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been requested.</span>');
  1418. Materialize.toast($toastContent, 8000);
  1419. } else {
  1420. Meteor.call("addSongToQueue", songData, function (err, res) {
  1421. console.log(err, res);
  1422. if (err) {
  1423. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  1424. Materialize.toast($toastContent, 8000);
  1425. } else {
  1426. var $toastContent = $('<span><strong>Song added.</strong> Your song has succesfully been added to the queue.</span>');
  1427. Materialize.toast($toastContent, 8000);
  1428. $('#add_song_modal').closeModal();
  1429. Session.set("editingSong", false);
  1430. }
  1431. });
  1432. }
  1433. },
  1434. "click #toggle-video": function (e) {
  1435. e.preventDefault();
  1436. if (Session.get("mediaHidden")) {
  1437. $("#media-container").removeClass("hidden");
  1438. $("#toggle-video").text("Hide video");
  1439. Session.set("mediaHidden", false);
  1440. } else {
  1441. $("#media-container").addClass("hidden");
  1442. $("#toggle-video").text("Show video");
  1443. Session.set("mediaHidden", true);
  1444. }
  1445. },
  1446. "click #return": function (e) {
  1447. $("#add-info").hide();
  1448. $("#search-info").show();
  1449. },
  1450. "click #search-song": function () {
  1451. var songs = [];
  1452. Session.set("songResults", songs);
  1453. $.ajax({
  1454. type: "GET",
  1455. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY&type=video&maxResults=25",
  1456. applicationType: "application/json",
  1457. contentType: "json",
  1458. success: function (data) {
  1459. for (var i in data.items) {
  1460. var item = data.items[i];
  1461. console.log(item);
  1462. songs.push({title: item.snippet.title, artist: item.snippet.channelTitle, id: item.id.videoId, image: item.snippet.thumbnails.medium.url});
  1463. }
  1464. Session.set("songResults", songs);
  1465. /*$("#song-results > div").click(function () {
  1466. $("#search-info").hide();
  1467. $("#add-info").show();
  1468. var title = $(this).find("div > .song-result-title").text();
  1469. for (var i in songs) {
  1470. if (songs[i].title === title) {
  1471. var songObj = {
  1472. id: songs[i].id,
  1473. title: songs[i].title,
  1474. type: "youtube"
  1475. };
  1476. $("#title").val(songObj.title);
  1477. $("#artist").val("");
  1478. $("#id").val(songObj.id);
  1479. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function (data) {
  1480. if (data.tracks.items.length > 0) {
  1481. $("#title").val(data.tracks.items[0].name);
  1482. var artists = [];
  1483. $("#img").val(data.tracks.items[0].album.images[2].url);
  1484. data.tracks.items[0].artists.forEach(function (artist) {
  1485. artists.push(artist.name);
  1486. });
  1487. $("#artist").val(artists.join(", "));
  1488. }
  1489. });
  1490. }
  1491. }
  1492. })*/
  1493. }
  1494. })
  1495. },
  1496. "click #volume-icon": function () {
  1497. var volume = 0;
  1498. var slider = $("#volume-slider").slider();
  1499. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1500. if (YTPlayer !== undefined) {
  1501. YTPlayer.setVolume(volume);
  1502. localStorage.setItem("volume", volume);
  1503. $("#volume-slider").slider("setValue", volume);
  1504. }
  1505. },
  1506. "click #play": function () {
  1507. Meteor.call("resumeRoom", type);
  1508. },
  1509. "click #pause": function () {
  1510. Meteor.call("pauseRoom", type);
  1511. },
  1512. "click #skip": function () {
  1513. Meteor.call("skipSong", type);
  1514. },
  1515. "click #shuffle": function () {
  1516. Meteor.call("shufflePlaylist", type);
  1517. },
  1518. "change input": function (e) {
  1519. /*if (e.target && e.target.id) {
  1520. var partsOfId = e.target.id.split("-");
  1521. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  1522. var camelCase = partsOfId.join("");
  1523. Session.set(camelCase, e.target.checked);
  1524. }*/
  1525. },
  1526. "click #report-song-button": function () {
  1527. var room = Session.get("type");
  1528. var reportData = {};
  1529. reportData.song = Session.get("currentSong").mid;
  1530. reportData.type = [];
  1531. reportData.reason = [];
  1532. $(".report-layer-1 > .checkbox input:checked").each(function () {
  1533. reportData.type.push(this.id);
  1534. if (this.id == "report-other") {
  1535. var otherText = $(".other-textarea").val();
  1536. }
  1537. });
  1538. $(".report-layer-2 input:checked").each(function () {
  1539. reportData.reason.push(this.id);
  1540. });
  1541. console.log(reportData);
  1542. Meteor.call("submitReport", room, reportData, Session.get("id"), function () {
  1543. $("#close-modal-r").click();
  1544. });
  1545. },
  1546. "change #si_or_pl": function () {
  1547. Session.set("songResults", []);
  1548. Session.set("si_or_pl", $("#si_or_pl").val());
  1549. },
  1550. "click #close-modal-a": function () {
  1551. $("#select_single").attr("selected", true);
  1552. $("#search-info").show();
  1553. $("#playlist-import").hide();
  1554. }
  1555. });
  1556. // Settings Template
  1557. Template.settings.events({
  1558. "click #save-settings": function() {
  1559. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  1560. },
  1561. "click #delete-account": function(){
  1562. $("#delete-account").text("Click to confirm");
  1563. $("#delete-account").click(function(){
  1564. var bool = confirm("Are you sure you want to delete your account?");
  1565. if(bool) {
  1566. Meteor.call("deleteAccount");
  1567. } else{
  1568. $("#delete-account").text("Delete");
  1569. }
  1570. })
  1571. },
  1572. "click #change-password": function(){
  1573. var oldPassword = $("#old-password").val();
  1574. var newPassword= $("#new-password").val();
  1575. var confirmPassword = $("#confirm-password").val();
  1576. if(newPassword === confirmPassword){
  1577. Accounts.changePassword(oldPassword, newPassword, function(err){
  1578. if(err){
  1579. $("#old-password").val("");
  1580. $("#new-password").val("");
  1581. $("#confirm-password").val("");
  1582. $("<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(); });
  1583. } else {
  1584. $("#old-password").val("");
  1585. $("#new-password").val("");
  1586. $("#confirm-password").val("");
  1587. $("<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(); });
  1588. }
  1589. });
  1590. }
  1591. }
  1592. });
  1593. var previewEndSongTimeout = undefined;