search.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. {% extends 'base.html' %}
  2. {% block title %}{{ group.name }}{% if q %} > Search: {{ q }}{% endif %}{% endblock %}
  3. {% macro print_post(post, show_comments=False) %}
  4. <div class="post">
  5. <img src="http://graph.facebook.com/{{ post.from.id }}/picture?size=square" class="picture">
  6. <span class="name">{{ post.from.name }}</span>
  7. <span class="meta">{{ post.time|format_time }}{% if post.like_count %} &middot; {{ post.like_count }} likes{% endif %} &middot; <a href="{{ post.original_link }}" class="permalink">View original</a></span>
  8. <p class="body">{{ post.message|urlize|nl2br }}</p>
  9. {% if show_comments %}
  10. <ul class="comments">
  11. {% for comment in post.comments %}
  12. <li>
  13. <span class="comment-name">{{ comment.from.name }}</span>
  14. <span class="body">{{ comment.message|urlize|nl2br }}</span>
  15. <a href="{{ comment.link }}" class="permalink">#</a>
  16. </li>
  17. {% endfor %}
  18. </ul>
  19. {% endif %}
  20. </div>
  21. {% endmacro %}
  22. {% block content %}
  23. <form method="get" action="" class="search-form">
  24. <h1><img src="{{ group.icon }}"> <a href="{{ group.link }}">{{ group.name }}</a></h1>
  25. <input type="search" size="30" name="q" value="{{ q }}" placeholder="Search...">
  26. {% if error %}
  27. <p>{{ error }}</p>
  28. {% endif %}
  29. </form>
  30. {% if result %}
  31. <div class="result">
  32. <div class="sort">
  33. Sort by:
  34. <a href="{{ url_for('search', group_slug=group.slug, q=q, sort='popular') }}"{% if sort == 'popular' %} class="active"{% endif %}>Popularity</a>
  35. <a href="{{ url_for('search', group_slug=group.slug, q=q, sort='new') }}"{% if sort == 'new' %} class="active"{% endif %}>Date</a>
  36. </div>
  37. {% for item in result %}
  38. {% if item.model == 'Post' %}
  39. {{ print_post(item, show_comments=True) }}
  40. {% elif item.model == 'Comment' %}
  41. {{ print_post(item.post, show_comments=True) }}
  42. {% endif %}
  43. {% endfor %}
  44. {% if next_page or prev_page %}
  45. <div class="paging">
  46. {% if prev_page %}
  47. <a href="{{ url_for('search', group_slug=group.slug, q=q, sort=sort, page=prev_page) }}" class="prev">&larr; Previous</a>
  48. {% endif %}
  49. {% if next_page %}
  50. <a href="{{ url_for('search', group_slug=group.slug, q=q, sort=sort, page=next_page) }}" class="next">Next &rarr;</a>
  51. {% endif %}
  52. </div>
  53. {% endif %}
  54. </div>
  55. {% endif %}
  56. {% endblock %}
  57. {% block js %}
  58. {% if result %}
  59. <script type="text/javascript">
  60. function escapeRegExp(str) {
  61. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  62. }
  63. var re = new RegExp(escapeRegExp({{ q|escape|tojson|safe }}), 'gi');
  64. var repl = "<span class='hl'>$&</span>";
  65. $('.body').each(function() {
  66. var cell = this;
  67. if (cell.innerHTML.match(re))
  68. {
  69. var content = []
  70. for (var j = 0; j < cell.childNodes.length; j++)
  71. {
  72. var node = cell.childNodes[j]
  73. if (node.nodeType == 3)
  74. // XXX: too hacky... :(
  75. content.push($('<div />').text(node.nodeValue).html().replace(re, repl))
  76. else if (node.nodeType == 1) {
  77. if (node.tagName == 'A')
  78. node.innerHTML = node.innerHTML.replace(re, repl);
  79. content.push(node.outerHTML);
  80. }
  81. }
  82. cell.innerHTML = content.join('')
  83. }
  84. });
  85. </script>
  86. {% endif %}
  87. {% endblock %}