main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Routing.
  2. Router.configure({
  3. layoutTemplate: "ApplicationLayout"
  4. });
  5. Router.route("/", function () {
  6. this.render("welcome", { to: "main" });
  7. });
  8. Router.route("/images", function () {
  9. this.render("navbar", { to: "navbar" });
  10. this.render("images", { to: "main" });
  11. });
  12. Router.route("/image/:id", function (vars) {
  13. this.render("navbar", { to: "navbar" });
  14. this.render("image", {
  15. to: "main",
  16. data: function () {
  17. const id = this.params.id;
  18. const doc = Images.findOne({ _id: id });
  19. return doc;
  20. }
  21. });
  22. });
  23. // Infiniscroll.
  24. Session.set("imageLimit", 8);
  25. let lastScrollTop = 0;
  26. $(window).scroll(function (event) {
  27. let $window = $(window);
  28. // Current position in the page.
  29. let scrollTop = $window.scrollTop();
  30. // If we are near the bottom of the window.
  31. if (scrollTop + $window.height() > $(document).height() - 100) {
  32. // If we are heading down.
  33. if (scrollTop > lastScrollTop) {
  34. Session.set("imageLimit", Session.get("imageLimit") + 4);
  35. console.log('Increased image count');
  36. }
  37. lastScrollTop = scrollTop;
  38. }
  39. });
  40. // Accounts configuration.
  41. Accounts.ui.config({
  42. passwordSignupFields: "USERNAME_AND_EMAIL"
  43. });
  44. // Helpers.
  45. const getUser = (userId) => {
  46. let user = Meteor.users.findOne({ _id: userId });
  47. return user ? user.username : "anonymous";
  48. };
  49. Template.images.helpers({ getUser,
  50. images: function () {
  51. const createdBy = Session.get("userFilter");
  52. const selector = createdBy ? { createdBy } : {};
  53. return Images.find(selector, {
  54. sort: { createdOn: -1, rating: -1 },
  55. limit: Session.get("imageLimit")
  56. });
  57. },
  58. filtering_images: function () {
  59. const userFilter = Session.get("userFilter");
  60. return !!userFilter;
  61. },
  62. image_id: function () {
  63. return "rating-" + this._id;
  64. },
  65. getFilterUser: function () {
  66. const userFilter = Session.get("userFilter");
  67. return getUser(userFilter);
  68. }
  69. });
  70. Template.body.helpers({
  71. username: function () {
  72. let user = Meteor.user();
  73. let name = user ? user.username : "anon";
  74. return name;
  75. }
  76. });
  77. // Events.
  78. Template.images.events({
  79. "click .js-image": function (event) {
  80. $(event.target).css("width", "50px");
  81. },
  82. "click .js-del-image": function (event) {
  83. let imageId = this._id;
  84. console.log(imageId);
  85. // use jquery to hide the image component
  86. // then remove it at the end of the animation
  87. $("#" + imageId).hide("slow", function () {
  88. Images.remove({ _id: imageId });
  89. });
  90. },
  91. "click .js-rate-image": function (event) {
  92. let rating = $(event.currentTarget).data("userrating");
  93. let imageId = this.id.substring(7); // "rating-".length = 7.
  94. console.log(imageId);
  95. Images.update({ _id: imageId },
  96. { $set: { rating: rating }}
  97. );
  98. },
  99. "click .js-show-image-form": function () {
  100. $("#image_add_form").modal("show");
  101. },
  102. "click .js-set-image-filter": function () {
  103. Session.set("userFilter", this.createdBy);
  104. },
  105. "click .js-unset-image-filter": () => {
  106. Session.delete("userFilter");
  107. }
  108. });
  109. Template.image_add_form.events({
  110. 'submit .js-add-image': function (event) {
  111. let img_src = event.target.img_src.value;
  112. let img_alt = event.target.img_alt.value;
  113. console.log("src: " + img_src + " alt: " + img_alt);
  114. if (Meteor.user()) {
  115. Images.insert({
  116. img_src: img_src,
  117. img_alt: img_alt,
  118. createdOn: new Date(),
  119. createdBy: Meteor.userId()
  120. });
  121. }
  122. $("#image_add_form").modal("hide");
  123. return false;
  124. }
  125. });