PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/templates/tagging_common.mako

https://bitbucket.org/cistrome/cistrome-harvard/
Mako | 231 lines | 190 code | 20 blank | 21 comment | 4 complexity | 34ec2357ab7f288686891bee8281580e MD5 | raw file
  1. <%!
  2. from cgi import escape
  3. from galaxy.web.framework.helpers import iff
  4. from random import random
  5. from sys import maxint
  6. from math import floor
  7. from galaxy.model import Tag, ItemTagAssociation
  8. %>
  9. ## Render a tagging element if there is a tagged_item.
  10. %if tagged_item is not None:
  11. %if tag_type == "individual":
  12. ${render_individual_tagging_element( user=user, tagged_item=tagged_item, elt_context=elt_context, in_form=in_form, input_size=input_size, tag_click_fn=tag_click_fn, use_toggle_link=use_toggle_link )}
  13. %elif tag_type == "community":
  14. ${render_community_tagging_element(tagged_item=tagged_item, elt_context=elt_context, tag_click_fn=tag_click_fn)}
  15. %endif
  16. %endif
  17. ## Render HTML for a list of tags.
  18. <%def name="render_tagging_element_html(elt_id=None, tags=None, editable=True, use_toggle_link=True, input_size='15', in_form=False, tag_type='individual', render_add_tag_button=True)">
  19. ## Useful attributes.
  20. <%
  21. num_tags = len( tags )
  22. %>
  23. <div class="tag-element"
  24. %if elt_id:
  25. id="${elt_id}"
  26. %endif
  27. ## Do not display element if there are no tags and it is not editable.
  28. %if num_tags == 0 and not editable:
  29. style="display: none"
  30. %endif
  31. >
  32. %if use_toggle_link:
  33. <a class="toggle-link" href="#">${num_tags} Tag${iff( num_tags == 1, "", "s")}</a>
  34. %endif
  35. <div class="tag-area
  36. %if tag_type == 'individual':
  37. individual-tag-area
  38. %endif
  39. ">
  40. ## Build buttons for current tags.
  41. %for tag in tags:
  42. <%
  43. ## Handle both Tag and ItemTagAssociation objects.
  44. if isinstance( tag, Tag ):
  45. tag_name = tag.name
  46. tag_value = None
  47. elif isinstance( tag, ItemTagAssociation ):
  48. tag_name = tag.user_tname
  49. tag_value = tag.user_value
  50. ## Convert tag name, value to unicode.
  51. if isinstance( tag_name, str ):
  52. tag_name = unicode( escape( tag_name ), 'utf-8' )
  53. if tag_value:
  54. tag_value = unicode( escape( tag_value ), 'utf-8' )
  55. if tag_value:
  56. tag_str = tag_name + ":" + tag_value
  57. else:
  58. tag_str = tag_name
  59. %>
  60. <span class="tag-button">
  61. <span class="tag-name">${tag_str}</span>
  62. %if editable:
  63. <img class="delete-tag-img" src="${h.url_for('/static/images/delete_tag_icon_gray.png')}"/>
  64. %endif
  65. </span>
  66. %endfor
  67. ## Add tag input field. If element is in form, tag input is a textarea; otherwise element is a input type=text.
  68. %if editable:
  69. %if in_form:
  70. <textarea class="tag-input" rows='1' cols='${input_size}'></textarea>
  71. %else:
  72. <input class="tag-input" type='text' size='${input_size}'/>
  73. %endif
  74. ## Add "add tag" button.
  75. %if render_add_tag_button:
  76. <img src='${h.url_for('/static/images/fugue/tag--plus.png')}' class="add-tag-button" title="Add tags"/>
  77. %endif
  78. %endif
  79. </div>
  80. </div>
  81. </%def>
  82. ## Render tool tagging elements
  83. <%def name="render_tool_tagging_elements()">
  84. <%
  85. elt_id = int ( floor ( random()*maxint ) )
  86. tags = trans.app.tag_handler.get_tool_tags( trans )
  87. %>
  88. ${self.render_tagging_element_html(elt_id=elt_id, \
  89. tags=tags, \
  90. editable=False, \
  91. use_toggle_link=False )}
  92. <script type="text/javascript">
  93. init_tag_click_function($('#${elt_id}'), tool_tag_click);
  94. </script>
  95. </%def>
  96. ## Render community tagging element.
  97. <%def name="render_community_tagging_element(tagged_item=None, elt_context=None, use_toggle_link=False, tag_click_fn='default_tag_click_fn')">
  98. ## Build HTML.
  99. <%
  100. elt_id = int ( floor ( random()*maxint ) )
  101. community_tags = trans.app.tag_handler.get_community_tags( trans, item=tagged_item, limit=5 )
  102. %>
  103. ${self.render_tagging_element_html(elt_id=elt_id, \
  104. tags=community_tags, \
  105. use_toggle_link=use_toggle_link, \
  106. editable=False, tag_type="community")}
  107. ## Set up tag click function.
  108. <script type="text/javascript">
  109. init_tag_click_function($('#${elt_id}'), ${tag_click_fn});
  110. </script>
  111. </%def>
  112. ## Render individual tagging element.
  113. <%def name="render_individual_tagging_element(user=None, tagged_item=None, elt_context=None, use_toggle_link=True, in_form=False, input_size='15', tag_click_fn='default_tag_click_fn', get_toggle_link_text_fn='default_get_toggle_link_text_fn', editable=True, render_add_tag_button=True)">
  114. ## Useful attributes.
  115. <%
  116. # Useful ids.
  117. tagged_item_id = str( trans.security.encode_id ( tagged_item.id ) )
  118. elt_id = int ( floor ( random()*maxint ) )
  119. # Get list of user's item tags. TODO: implement owner_tags for all taggable objects and use here.
  120. item_tags = [ tag for tag in tagged_item.tags if ( tag.user == user ) ]
  121. %>
  122. ## Build HTML.
  123. ${self.render_tagging_element_html(elt_id=elt_id, tags=item_tags, editable=editable, use_toggle_link=use_toggle_link, input_size=input_size, in_form=in_form, render_add_tag_button=render_add_tag_button)}
  124. ## Build script that augments tags using progressive javascript.
  125. <script type="text/javascript">
  126. //
  127. // Set up autocomplete tagger.
  128. //
  129. //
  130. // Default function get text to display on the toggle link.
  131. //
  132. var default_get_toggle_link_text_fn = function(tags)
  133. {
  134. var text = "";
  135. var num_tags = obj_length(tags);
  136. if (num_tags != 0)
  137. {
  138. text = num_tags + (num_tags != 1 ? " Tags" : " Tag");
  139. /*
  140. // Show first N tags; hide the rest.
  141. var max_to_show = 1;
  142. // Build tag string.
  143. var tag_strs = new Array();
  144. var count = 0;
  145. for (tag_name in tags)
  146. {
  147. tag_value = tags[tag_name];
  148. tag_strs[tag_strs.length] = build_tag_str(tag_name, tag_value);
  149. if (++count == max_to_show)
  150. break;
  151. }
  152. tag_str = tag_strs.join(", ");
  153. // Finalize text.
  154. var num_tags_hiding = num_tags - max_to_show;
  155. text = "Tags: " + tag_str +
  156. (num_tags_hiding != 0 ? " and " + num_tags_hiding + " more" : "");
  157. */
  158. }
  159. else
  160. {
  161. // No tags.
  162. text = "Add tags";
  163. }
  164. return text;
  165. };
  166. // Default function to handle a tag click.
  167. var default_tag_click_fn = function(tag_name, tag_value) { };
  168. <%
  169. ## Build dict of tag name, values.
  170. tag_names_and_values = dict()
  171. for tag in item_tags:
  172. tag_name = tag.user_tname
  173. tag_value = ""
  174. if tag.value is not None:
  175. tag_value = tag.user_value
  176. ## Tag names and values may be string or unicode object.
  177. if isinstance( tag_name, str ):
  178. tag_names_and_values[unicode(tag_name, 'utf-8')] = unicode(tag_value, 'utf-8')
  179. else: ## isInstance( tag_name, unicode ):
  180. tag_names_and_values[tag_name] = tag_value
  181. %>
  182. var options =
  183. {
  184. tags : ${h.to_json_string(tag_names_and_values)},
  185. editable : ${iff( editable, 'true', 'false' )},
  186. get_toggle_link_text_fn: ${get_toggle_link_text_fn},
  187. tag_click_fn: ${tag_click_fn},
  188. ## Use forward slash in controller to suppress route memory.
  189. ajax_autocomplete_tag_url: "${h.url_for( controller='/tag', action='tag_autocomplete_data', item_id=tagged_item_id, item_class=tagged_item.__class__.__name__ )}",
  190. ajax_add_tag_url: "${h.url_for( controller='/tag', action='add_tag_async', item_id=tagged_item_id, item_class=tagged_item.__class__.__name__, context=elt_context )}",
  191. ajax_delete_tag_url: "${h.url_for( controller='/tag', action='remove_tag_async', item_id=tagged_item_id, item_class=tagged_item.__class__.__name__, context=elt_context )}",
  192. delete_tag_img: "${h.url_for('/static/images/delete_tag_icon_gray.png')}",
  193. delete_tag_img_rollover: "${h.url_for('/static/images/delete_tag_icon_white.png')}",
  194. use_toggle_link: ${iff( use_toggle_link, 'true', 'false' )}
  195. };
  196. $('#${elt_id}').find( 'input' ).autocomplete_tagging(options);
  197. </script>
  198. ## Use style to hide/display the tag area.
  199. <style>
  200. .tag-area {
  201. display: ${iff( use_toggle_link, "none", "block" )};
  202. }
  203. </style>
  204. <noscript>
  205. <style>
  206. .tag-area {
  207. display: block;
  208. }
  209. </style>
  210. </noscript>
  211. </%def>