PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 1ms RepoModel.GetById 1ms 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
 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" title="Add tags"/>
 81                %endif
 82            %endif
 83        </div>
 84    </div>
 85</%def>
 86
 87## Render tool tagging elements
 88<%def name="render_tool_tagging_elements()">
 89    <%
 90        elt_id = int ( floor ( random()*maxint ) )
 91        tags = trans.app.tag_handler.get_tool_tags( trans )
 92    %>
 93    ${self.render_tagging_element_html(elt_id=elt_id, \
 94                                        tags=tags, \
 95                                        editable=False, \
 96                                        use_toggle_link=False )}
 97    <script type="text/javascript">
 98        init_tag_click_function($('#${elt_id}'), tool_tag_click);
 99    </script>
100</%def>
101
102## Render community tagging element.
103<%def name="render_community_tagging_element(tagged_item=None, elt_context=None, use_toggle_link=False, tag_click_fn='default_tag_click_fn')">
104    ## Build HTML.
105    <% 
106        elt_id = int ( floor ( random()*maxint ) ) 
107        community_tags = trans.app.tag_handler.get_community_tags( trans, item=tagged_item, limit=5 )
108    %>
109    ${self.render_tagging_element_html(elt_id=elt_id, \
110                                        tags=community_tags, \
111                                        use_toggle_link=use_toggle_link, \
112                                        editable=False, tag_type="community")}
113    
114    ## Set up tag click function.
115    <script type="text/javascript">
116        init_tag_click_function($('#${elt_id}'), ${tag_click_fn});
117    </script>
118</%def>
119
120
121## Render individual tagging element.
122<%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)">
123    ## Useful attributes.
124    <%
125        # Useful ids.
126        tagged_item_id = str( trans.security.encode_id ( tagged_item.id ) )
127        elt_id = int ( floor ( random()*maxint ) )
128        
129        # Get list of user's item tags. TODO: implement owner_tags for all taggable objects and use here.
130        item_tags = [ tag for tag in tagged_item.tags if ( tag.user == user ) ]
131    %>
132    
133    ## Build HTML.
134    ${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)}
135    
136    ## Build script that augments tags using progressive javascript.
137    <script type="text/javascript">
138        //
139        // Set up autocomplete tagger.
140        //
141
142        //
143        // Default function get text to display on the toggle link.
144        //
145        var default_get_toggle_link_text_fn = function(tags)
146        {
147            var text = "";
148            var num_tags = obj_length(tags);
149            if (num_tags != 0)
150              {
151                text = num_tags + (num_tags != 1 ? " Tags" : " Tag");
152                /*
153                // Show first N tags; hide the rest.
154                var max_to_show = 1;
155    
156                // Build tag string.
157                var tag_strs = new Array();
158                var count = 0;
159                for (tag_name in tags)
160                  {
161                    tag_value = tags[tag_name];
162                    tag_strs[tag_strs.length] = build_tag_str(tag_name, tag_value);
163                    if (++count == max_to_show)
164                      break;
165                  }
166                tag_str = tag_strs.join(", ");
167            
168                // Finalize text.
169                var num_tags_hiding = num_tags - max_to_show;
170                text = "Tags: " + tag_str + 
171                  (num_tags_hiding != 0 ? " and " + num_tags_hiding + " more" : "");
172                */
173              }
174            else
175              {
176                // No tags.
177                text = "Add tags";
178              }
179            return text;
180        };
181        
182        // Default function to handle a tag click.
183        var default_tag_click_fn = function(tag_name, tag_value) { };
184        
185        <%
186            ## Build dict of tag name, values.
187            tag_names_and_values = dict()
188            for tag in item_tags:
189                tag_name = tag.user_tname
190                tag_value = ""
191                if tag.value is not None:
192                    tag_value = tag.user_value
193                ## Tag names and values may be string or unicode object.
194                if isinstance( tag_name, str ):
195                    tag_names_and_values[unicode(tag_name, 'utf-8')] = unicode(tag_value, 'utf-8')
196                else: ## isInstance( tag_name, unicode ):
197                    tag_names_and_values[tag_name] = tag_value
198        %>
199        var options =
200        {
201            tags : ${h.to_json_string(tag_names_and_values)},
202            editable : ${iff( editable, 'true', 'false' )},
203            get_toggle_link_text_fn: ${get_toggle_link_text_fn},
204            tag_click_fn: ${tag_click_fn},
205            ## Use forward slash in controller to suppress route memory.
206            ajax_autocomplete_tag_url: "${h.url_for( controller='/tag', action='tag_autocomplete_data', item_id=tagged_item_id, item_class=tagged_item.__class__.__name__ )}",
207            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 )}",
208            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 )}",
209            delete_tag_img: "${h.url_for('/static/images/delete_tag_icon_gray.png')}",
210            delete_tag_img_rollover: "${h.url_for('/static/images/delete_tag_icon_white.png')}",
211            use_toggle_link: ${iff( use_toggle_link, 'true', 'false' )}
212         };
213         
214        $('#${elt_id}').find( 'input' ).autocomplete_tagging(options);
215    </script>
216    
217    ## Use style to hide/display the tag area.
218    <style>
219    .tag-area {
220        display: ${iff( use_toggle_link, "none", "block" )};
221    }
222    </style>
223
224    <noscript>
225    <style>
226    .tag-area {
227        display: block;
228    }
229    </style>
230    </noscript>
231</%def>