image_share.js 2.0 KB

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