Personal wiki notebook (not under development)

Note_tree_area.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import re
  2. from model.Notebook import Notebook
  3. from Tags import Div, Span, H4, A, Table, Tbody, Tr, Td, Img, P, Br, Input
  4. from Search_form import Search_form
  5. class Note_tree_area( Div ):
  6. LINK_PATTERN = re.compile( u'<a\s+(?:[^>]+\s)?href="[^"]+"[^>]*>', re.IGNORECASE )
  7. def __init__( self, notebook, root_notes, recent_notes, total_notes_count, user ):
  8. tags = [ tag for tag in notebook.tags if tag.name == u"forum" ]
  9. if tags:
  10. forum_name = tags[ 0 ].value
  11. forum_tag = True
  12. else:
  13. forum_name = None
  14. forum_tag = False
  15. Div.__init__(
  16. self,
  17. Div(
  18. Div(
  19. Search_form(),
  20. class_ = u"link_area_item",
  21. ),
  22. H4(
  23. forum_tag and u"posts" or u"notes",
  24. Span(
  25. Span( total_notes_count, id = u"total_notes_count" ), u"total",
  26. class_ = u"small_text link_area_item",
  27. ),
  28. notebook.read_write != Notebook.READ_ONLY and Input(
  29. type = u"button",
  30. class_ = u"note_button small_text",
  31. id = u"save_button",
  32. value = u"saved",
  33. disabled = u"true",
  34. title = u"save your work",
  35. ) or None,
  36. id = u"note_tree_area_title",
  37. ) or None,
  38. ( not forum_tag ) and self.make_tree(
  39. [ self.make_item(
  40. title = note.title,
  41. link_attributes = u'href="/notebooks/%s?note_id=%s"' % ( notebook.object_id, note.object_id ),
  42. link_class = u"note_tree_link",
  43. has_children = ( notebook.name != u"trash" ) and self.LINK_PATTERN.search( note.contents ) or False,
  44. root_note_id = note.object_id,
  45. ) for note in root_notes ],
  46. Tr(
  47. Td(),
  48. ( notebook.name != u"trash" and notebook.read_write == Notebook.READ_WRITE ) and Td(
  49. Input(
  50. type = u"button",
  51. class_ = u"note_button",
  52. id = u"new_note_tree_link_button",
  53. value = u"+ note link",
  54. title = u"Add one of your notes to this list.",
  55. ),
  56. Span( id = u"new_note_tree_link_area" ),
  57. ) or None,
  58. id = u"new_note_tree_link_row",
  59. ) or None,
  60. tree_id = "note_tree_root_table",
  61. ) or None,
  62. ( not forum_tag and recent_notes is not None and notebook.name != u"trash" ) and Span(
  63. H4( u"recent updates",
  64. id = u"recent_notes_area_title",
  65. ),
  66. self.make_tree(
  67. Tr( id = "recent_notes_top" ),
  68. [ self.make_item(
  69. title = note.title,
  70. link_attributes = u'href="/notebooks/%s?note_id=%s"' % ( notebook.object_id, note.object_id ),
  71. link_class = u"recent_note_link",
  72. has_children = False,
  73. root_note_id = note.object_id,
  74. base_name = u"recent_note",
  75. ) for note in recent_notes ],
  76. navigation = Tbody( Tr( id = "recent_notes_spacer" ), Tr(
  77. Td(),
  78. Td(
  79. A( u"more", href = u"#", id = u"recent_notes_more_link", class_ = u"undisplayed" ),
  80. A( u"less", href = u"#", id = u"recent_notes_less_link", class_ = u"undisplayed" ),
  81. ),
  82. ), id = u"recent_notes_navigation" ),
  83. tree_id = "recent_notes_table",
  84. ),
  85. ) or None,
  86. ( user.username is None ) and P(
  87. A( u"Download", href = u"/download", class_ = u"hook_action" ),
  88. Span( u" or ", class_ = u"hook_action_or" ),
  89. A( u"Sign up", href = u"/pricing", class_ = u"hook_action" ), Br(),
  90. Span( "Get started in seconds.", class_ = u"hook_action_detail" ),
  91. class_ = u"hook_action_area",
  92. separator = u"",
  93. ) or None,
  94. id = u"note_tree_area_holder",
  95. ),
  96. Span( id = "tree_arrow_hover_preload" ),
  97. Span( id = "tree_arrow_down_preload" ),
  98. Span( id = "tree_arrow_down_hover_preload" ),
  99. id = u"note_tree_area",
  100. )
  101. @staticmethod
  102. def make_item( title, link_attributes, link_class, has_children = False, root_note_id = None, target = None, base_name = None ):
  103. if base_name is None:
  104. base_name = u"note_tree"
  105. return Tr(
  106. has_children and \
  107. Td( Div( id = root_note_id and u"%s_expander_%s" % ( base_name, root_note_id ) or None, class_ = u"tree_expander" ) ) or
  108. Td( Div( id = root_note_id and u"%s_expander_%s" % ( base_name, root_note_id ) or None, class_ = u"tree_expander_empty" ) ),
  109. Td(
  110. u'<a %s%s%s class="%s">%s</a>' % (
  111. link_attributes,
  112. root_note_id and u' id="%s_link_%s"' % ( base_name, root_note_id ) or "",
  113. target and u' target="%s"' % target or "",
  114. link_class,
  115. title or u"untitled note",
  116. ),
  117. ),
  118. id = root_note_id and u"%s_item_%s" % ( base_name, root_note_id ) or None,
  119. class_ = u"%s_item" % base_name,
  120. )
  121. @staticmethod
  122. def make_tree( first_node, second_node = None, third_node = None, navigation = None, tree_id = None ):
  123. return Table(
  124. Tbody(
  125. first_node,
  126. second_node,
  127. third_node,
  128. id = tree_id and tree_id + "_body" or None,
  129. ),
  130. navigation,
  131. id = tree_id or None,
  132. class_ = u"note_tree_table",
  133. )