image_share.js 1.7 KB

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