Mercurial > kallithea
changeset 5564:21f80c6cdf0c
js: wrap repo_switcher setup code in $(document).ready
Without wrapping in a function, some variables were made global, such as cache,
format, visual_show_public_icon.
author | Takumi IINO <trot.thunder@gmail.com> |
---|---|
date | Sun, 25 Oct 2015 18:32:18 +0000 |
parents | 59edb20f88ad |
children | ebc4cc82cce2 |
files | kallithea/templates/base/base.html |
diffstat | 1 files changed, 84 insertions(+), 82 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/templates/base/base.html Sat Oct 24 16:19:20 2015 +0100 +++ b/kallithea/templates/base/base.html Sun Oct 25 18:32:18 2015 +0000 @@ -353,95 +353,97 @@ </li> <script type="text/javascript"> - var visual_show_public_icon = "${c.visual.show_public_icon}" == "True"; - var cache = {} - /*format the look of items in the list*/ - var format = function(state){ - if (!state.id){ - return state.text; // optgroup - } - var obj_dict = state.obj; - var tmpl = ''; - - if(obj_dict && state.type == 'repo'){ - tmpl += '<span class="repo-icons">'; - if(obj_dict['repo_type'] === 'hg'){ - tmpl += '<span class="repotag">hg</span> '; + $(document).ready(function(){ + var visual_show_public_icon = "${c.visual.show_public_icon}" == "True"; + var cache = {} + /*format the look of items in the list*/ + var format = function(state){ + if (!state.id){ + return state.text; // optgroup } - else if(obj_dict['repo_type'] === 'git'){ - tmpl += '<span class="repotag">git</span> '; - } - if(obj_dict['private']){ - tmpl += '<i class="icon-keyhole-circled"></i> '; - } - else if(visual_show_public_icon){ - tmpl += '<i class="icon-globe"></i> '; - } - tmpl += '</span>'; - } - if(obj_dict && state.type == 'group'){ - tmpl += '<i class="icon-folder"></i> '; - } - tmpl += state.text; - return tmpl; - } + var obj_dict = state.obj; + var tmpl = ''; - $("#repo_switcher").select2({ - placeholder: '<i class="icon-database"></i> ${_('Repositories')}', - dropdownAutoWidth: true, - formatResult: format, - formatSelection: format, - formatNoMatches: function(term){ - return "${_('No matches found')}"; - }, - containerCssClass: "repo-switcher", - dropdownCssClass: "repo-switcher-dropdown", - escapeMarkup: function(m){ - // don't escape our custom placeholder - if(m.substr(0,29) == '<i class="icon-database"></i>'){ - return m; + if(obj_dict && state.type == 'repo'){ + tmpl += '<span class="repo-icons">'; + if(obj_dict['repo_type'] === 'hg'){ + tmpl += '<span class="repotag">hg</span> '; + } + else if(obj_dict['repo_type'] === 'git'){ + tmpl += '<span class="repotag">git</span> '; + } + if(obj_dict['private']){ + tmpl += '<i class="icon-keyhole-circled"></i> '; + } + else if(visual_show_public_icon){ + tmpl += '<i class="icon-globe"></i> '; + } + tmpl += '</span>'; } + if(obj_dict && state.type == 'group'){ + tmpl += '<i class="icon-folder"></i> '; + } + tmpl += state.text; + return tmpl; + } - return Select2.util.escapeMarkup(m); - }, - query: function(query){ - var key = 'cache'; - var cached = cache[key] ; - if(cached) { - var data = {results: []}; - //filter results - $.each(cached.results, function(){ - var section = this.text; - var children = []; - $.each(this.children, function(){ - if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){ - children.push({'id': this.id, 'text': this.text, 'type': this.type, 'obj': this.obj}); - } - }); - if(children.length !== 0){ - data.results.push({'text': section, 'children': children}); + $("#repo_switcher").select2({ + placeholder: '<i class="icon-database"></i> ${_('Repositories')}', + dropdownAutoWidth: true, + formatResult: format, + formatSelection: format, + formatNoMatches: function(term){ + return "${_('No matches found')}"; + }, + containerCssClass: "repo-switcher", + dropdownCssClass: "repo-switcher-dropdown", + escapeMarkup: function(m){ + // don't escape our custom placeholder + if(m.substr(0,29) == '<i class="icon-database"></i>'){ + return m; } - }); - query.callback(data); - }else{ - $.ajax({ - url: "${h.url('repo_switcher_data')}", - data: {}, - dataType: 'json', - type: 'GET', - success: function(data) { - cache[key] = data; - query.callback({results: data.results}); - } - }); - } - } - }); + return Select2.util.escapeMarkup(m); + }, + query: function(query){ + var key = 'cache'; + var cached = cache[key] ; + if(cached) { + var data = {results: []}; + //filter results + $.each(cached.results, function(){ + var section = this.text; + var children = []; + $.each(this.children, function(){ + if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){ + children.push({'id': this.id, 'text': this.text, 'type': this.type, 'obj': this.obj}); + } + }); + if(children.length !== 0){ + data.results.push({'text': section, 'children': children}); + } - $("#repo_switcher").on('select2-selecting', function(e){ - e.preventDefault(); - window.location = pyroutes.url('summary_home', {'repo_name': e.val}); + }); + query.callback(data); + }else{ + $.ajax({ + url: "${h.url('repo_switcher_data')}", + data: {}, + dataType: 'json', + type: 'GET', + success: function(data) { + cache[key] = data; + query.callback({results: data.results}); + } + }); + } + } + }); + + $("#repo_switcher").on('select2-selecting', function(e){ + e.preventDefault(); + window.location = pyroutes.url('summary_home', {'repo_name': e.val}); + }); }); ## Global mouse bindings ##