main.js 3.0 KB

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