events.js 69 KB

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