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