select2_widgets.js
82 lines
| 2.3 KiB
| application/javascript
|
JavascriptLexer
r1 | /* COMMON */ | |||
var select2RefFilterResults = function(queryTerm, data) { | ||||
var filteredData = {results: []}; | ||||
//filter results | ||||
$.each(data.results, function() { | ||||
var section = this.text; | ||||
var children = []; | ||||
$.each(this.children, function() { | ||||
if (queryTerm.length === 0 || this.text.toUpperCase().indexOf(queryTerm.toUpperCase()) >= 0) { | ||||
children.push(this); | ||||
} | ||||
}); | ||||
if (children.length > 0) { | ||||
filteredData.results.push({ | ||||
'text': section, | ||||
'children': children | ||||
}); | ||||
} | ||||
}); | ||||
return filteredData | ||||
}; | ||||
var select2RefBaseSwitcher = function(targetElement, loadUrl, initialData){ | ||||
var formatResult = function(result, container, query) { | ||||
return formatSelect2SelectionRefs(result); | ||||
}; | ||||
var formatSelection = function(data, container) { | ||||
return formatSelect2SelectionRefs(data); | ||||
}; | ||||
$(targetElement).select2({ | ||||
cachedDataSource: {}, | ||||
dropdownAutoWidth: true, | ||||
width: "resolve", | ||||
containerCssClass: "drop-menu", | ||||
dropdownCssClass: "drop-menu-dropdown", | ||||
query: function(query) { | ||||
var self = this; | ||||
var cacheKey = '__ALLREFS__'; | ||||
var cachedData = self.cachedDataSource[cacheKey]; | ||||
if (cachedData) { | ||||
var data = select2RefFilterResults(query.term, cachedData); | ||||
query.callback({results: data.results}); | ||||
} else { | ||||
$.ajax({ | ||||
url: loadUrl, | ||||
data: {}, | ||||
dataType: 'json', | ||||
type: 'GET', | ||||
success: function(data) { | ||||
self.cachedDataSource[cacheKey] = data; | ||||
query.callback({results: data.results}); | ||||
} | ||||
}); | ||||
} | ||||
}, | ||||
initSelection: function(element, callback) { | ||||
callback(initialData); | ||||
}, | ||||
r2196 | formatResult: formatResult, | |||
r1 | formatSelection: formatSelection | |||
}); | ||||
}; | ||||
/* WIDGETS */ | ||||
var select2RefSwitcher = function(targetElement, initialData) { | ||||
var loadUrl = pyroutes.url('repo_refs_data', | ||||
{'repo_name': templateContext.repo_name}); | ||||
select2RefBaseSwitcher(targetElement, loadUrl, initialData); | ||||
}; | ||||
var select2FileHistorySwitcher = function(targetElement, initialData, state) { | ||||
r1927 | var loadUrl = pyroutes.url('repo_file_history', | |||
{'repo_name': templateContext.repo_name, 'commit_id': state.rev, | ||||
r1 | 'f_path': state.f_path}); | |||
select2RefBaseSwitcher(targetElement, loadUrl, initialData); | ||||
}; | ||||