events.js 70 KB

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