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