events.js 70 KB

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