image_share.js 3.1 KB

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