events.js 70 KB

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