helpers.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var Helpers = {
  2. error: function() {
  3. return Session.get('error');
  4. },
  5. toLowerCase: function(text) {
  6. return text && text.toLowerCase();
  7. },
  8. toUpperCase: function(text) {
  9. return text && text.toUpperCase();
  10. },
  11. firstChar: function(text) {
  12. return text && text[0].toUpperCase();
  13. },
  14. session: function(prop) {
  15. return Session.get(prop);
  16. },
  17. getUser: function(userId) {
  18. return Users.findOne(userId);
  19. }
  20. };
  21. // Register all Helpers
  22. _.each(Helpers, function(fn, name) { Blaze.registerHelper(name, fn); });
  23. // XXX I believe we should compute a HTML rendered field on the server that
  24. // would handle markdown, emojies and user mentions. We can simply have two
  25. // fields, one source, and one compiled version (in HTML) and send only the
  26. // compiled version to most users -- who don't need to edit.
  27. // In the meantime, all the transformation are done on the client using the
  28. // Blaze API.
  29. var at = HTML.CharRef({html: '@', str: '@'});
  30. Blaze.Template.registerHelper('mentions', new Template('mentions', function() {
  31. var view = this;
  32. var content = Blaze.toHTML(view.templateContentBlock);
  33. var currentBoard = Session.get('currentBoard');
  34. var knowedUsers = _.map(currentBoard.members, function(member) {
  35. member.username = Users.findOne(member.userId).username;
  36. return member;
  37. });
  38. var mentionRegex = /\B@(\w*)/gi;
  39. var currentMention, knowedUser, href, linkClass, linkValue, link;
  40. while (currentMention = mentionRegex.exec(content)) {
  41. knowedUser = _.findWhere(knowedUsers, { username: currentMention[1] });
  42. if (! knowedUser)
  43. continue;
  44. linkValue = [' ', at, knowedUser.username];
  45. href = Router.url('Profile', { username: knowedUser.username });
  46. linkClass = 'atMention' + (knowedUser.userId === Meteor.userId() ? ' me' : '');
  47. link = HTML.A({ href: href, 'class': linkClass }, linkValue);
  48. content = content.replace(currentMention[0], Blaze.toHTML(link));
  49. }
  50. return HTML.Raw(content);
  51. }));