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