image_share.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // this is image_share.js
  2. Images = new Mongo.Collection("images");
  3. if (Meteor.isClient) {
  4. Accounts.ui.config({
  5. passwordSignupFields: "USERNAME_AND_EMAIL"
  6. });
  7. Template.images.helpers({
  8. images: function () {
  9. const createdBy = Session.get("userFilter");
  10. const selector = createdBy ? { createdBy } : {};
  11. return Images.find(selector, { sort: { createdOn: -1, rating: -1 }});
  12. },
  13. image_id: function () {
  14. return "rating-" + this._id;
  15. },
  16. getUser: function (userId) {
  17. let user = Meteor.users.findOne({ _id: userId });
  18. return user ? user.username : "anonymous";
  19. }
  20. });
  21. Template.body.helpers({
  22. username: function () {
  23. let user = Meteor.user();
  24. let name = user ? user.username : "anon";
  25. return name;
  26. }
  27. });
  28. Template.images.events({
  29. "click .js-image": function (event) {
  30. $(event.target).css("width", "50px");
  31. },
  32. "click .js-del-image": function (event) {
  33. let imageId = this._id;
  34. console.log(imageId);
  35. // use jquery to hide the image component
  36. // then remove it at the end of the animation
  37. $("#" + imageId).hide("slow", function () {
  38. Images.remove({ _id: imageId });
  39. });
  40. },
  41. "click .js-rate-image": function (event) {
  42. let rating = $(event.currentTarget).data("userrating");
  43. let imageId = this.id.substring(7); // "rating-".length = 7.
  44. console.log(imageId);
  45. Images.update({ _id: imageId },
  46. { $set: { rating: rating }}
  47. );
  48. },
  49. "click .js-show-image-form": function () {
  50. $("#image_add_form").modal("show");
  51. },
  52. "click .js-set-image-filter": function () {
  53. Session.set("userFilter", this.createdBy);
  54. }
  55. });
  56. Template.image_add_form.events({
  57. 'submit .js-add-image': function (event) {
  58. let img_src = event.target.img_src.value;
  59. let img_alt = event.target.img_alt.value;
  60. console.log("src: " + img_src + " alt: " + img_alt);
  61. if (Meteor.user()) {
  62. Images.insert({
  63. img_src: img_src,
  64. img_alt: img_alt,
  65. createdOn: new Date(),
  66. createdBy: Meteor.userId()
  67. });
  68. }
  69. $("#image_add_form").modal("hide");
  70. return false;
  71. }
  72. });
  73. }