unrhodecode/rhodecode/templates/files/files.mako

445 lines
No EOL
15 KiB
Mako

<%inherit file="/base/base.mako"/>
<%def name="title(*args)">
${_('{} Files').format(c.repo_name)}
%if hasattr(c,'file'):
&middot; ${(h.safe_str(c.file.path) or '\\')}
%endif
%if c.rhodecode_name:
&middot; ${h.branding(c.rhodecode_name)}
%endif
</%def>
<%def name="breadcrumbs_links()">
${_('Files')}
%if c.file:
@ ${h.show_id(c.commit)}
%endif
</%def>
<%def name="menu_bar_nav()">
${self.menu_items(active='repositories')}
</%def>
<%def name="menu_bar_subnav()">
${self.repo_menu(active='files')}
</%def>
<%def name="main()">
<script type="text/javascript">
var fileSourcePage = ${c.file_source_page};
var atRef = '${request.GET.get('at', '')}';
// global state for fetching metadata
metadataRequest = null;
// global metadata about URL
filesUrlData = ${h.files_url_data(request)|n};
</script>
<div>
<div>
<%include file='files_pjax.mako'/>
</div>
</div>
<style>
.select2-container--default .select2-selection--single {
border-radius: 0 !important;
}
.select2-container--default .select2-selection--single {
margin: 0 5px; /* left/right margin */
height: 32px;
line-height: 32px;
border-radius: 0 !important;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
height: 32px;
right: 6px;
}
</style>
<script type="text/javascript">
var initFileJS = function () {
var state = getFileState();
// select code link event
$("#hlcode").on('mouseup', getSelectionLink);
// file history select2 used for history of file, and switch to
var initialCommitData = {
at_ref: atRef,
id: null,
text: '${c.commit.raw_id}',
type: 'sha',
raw_id: '${c.commit.raw_id}',
idx: ${c.commit.idx},
files_url: null,
};
// check if we have ref info.
var selectedRef = fileTreeRefs[atRef];
if (selectedRef !== undefined) {
$.extend(initialCommitData, selectedRef)
}
var loadUrl = pyroutes.url('repo_file_history', {'repo_name': templateContext.repo_name, 'commit_id': state.commit_id,'f_path': state.f_path});
var cacheKey = '__SINGLE_FILE_REFS__';
var cachedDataSource = {};
var loadRefsData = function (query) {
$.ajax({
url: loadUrl,
data: {},
dataType: 'json',
type: 'GET',
success: function (data) {
cachedDataSource[cacheKey] = data;
query.callback({results: data.results});
}
});
};
var feedRefsData = function (query, cachedData) {
var data = {results: []};
//filter results
$.each(cachedData.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(this)
}
});
data.results.push({
'text': section,
'children': children
})
});
query.callback(data);
};
var select2FileHistorySwitcher = function (targetElement, loadUrl, initialData) {
var formatResult = function (result, container, query) {
return formatSelect2SelectionRefs(result);
};
var formatSelection = function (data, container) {
var commit_ref = data;
var tmpl = '';
if (commit_ref.type === 'sha') {
tmpl = (commit_ref.raw_id || "").substr(0,8);
} else if (commit_ref.type === 'branch') {
tmpl = tmpl.concat('<i class="icon-branch"></i> ');
tmpl = tmpl.concat(escapeHtml(commit_ref.text));
} else if (commit_ref.type === 'tag') {
tmpl = tmpl.concat('<i class="icon-tag"></i> ');
tmpl = tmpl.concat(escapeHtml(commit_ref.text));
} else if (commit_ref.type === 'book') {
tmpl = tmpl.concat('<i class="icon-bookmark"></i> ');
tmpl = tmpl.concat(escapeHtml(commit_ref.text));
}
var idx = commit_ref.idx || 0;
if (idx !== 0) {
tmpl = tmpl.concat('<span class="select-index-number">r{0}</span>'.format(idx));
}
return tmpl
};
$(targetElement).select2({
dropdownAutoWidth: true,
width: "resolve",
containerCssClass: "drop-menu",
dropdownCssClass: "drop-menu-dropdown",
query: function(query) {
var cachedData = cachedDataSource[cacheKey];
if (cachedData) {
feedRefsData(query, cachedData)
} else {
loadRefsData(query)
}
},
initSelection: function(element, callback) {
callback(initialData);
},
formatResult: formatResult,
formatSelection: formatSelection
});
};
select2FileHistorySwitcher('#file_refs_filter', loadUrl, initialCommitData);
// switcher for files
$('#file_refs_filter').on('change', function(e) {
var data = $('#file_refs_filter').select2('data');
var commit_id = data.id;
var params = {
'repo_name': templateContext.repo_name,
'commit_id': commit_id,
'f_path': state.f_path
};
if(data.at_rev !== undefined && data.at_rev !== "") {
params['at'] = data.at_rev;
}
if ("${c.annotate}" === "True") {
var url = pyroutes.url('repo_files:annotated', params);
} else {
var url = pyroutes.url('repo_files', params);
}
window.location = url;
});
// load file short history
$('#file_history_overview').on('click', function(e) {
e.preventDefault();
path = state.f_path;
if (path.indexOf("#") >= 0) {
path = path.slice(0, path.indexOf("#"));
}
var url = pyroutes.url('repo_commits_file',
{'repo_name': templateContext.repo_name,
'commit_id': state.commit_id, 'f_path': path, 'limit': 6});
$('#file_history_container').show();
$('#file_history_container').html('<div class="file-history-inner">{0}</div>'.format(_gettext('Loading ...')));
$.pjax({
url: url,
container: '#file_history_container',
push: false,
timeout: 5000
}).complete(function () {
tooltipActivate();
});
});
};
var initTreeJS = function () {
var state = getFileState();
getFilesMetadata();
// fuzzy file filter
fileBrowserListeners(state.node_list_url, state.url_base);
// switch to widget
var initialCommitData = {
at_ref: atRef,
id: null,
text: '${c.commit.raw_id}',
type: 'sha',
raw_id: '${c.commit.raw_id}',
idx: ${c.commit.idx},
files_url: null
};
// check if we have ref info.
var selectedRef = fileTreeRefs[atRef];
if (selectedRef !== undefined) {
$.extend(initialCommitData, selectedRef);
}
var loadUrl = pyroutes.url('repo_refs_data', {repo_name: templateContext.repo_name});
var cacheKey = '__ALL_FILE_REFS__';
var cachedDataSource = {};
/**
* Build filtered data (similar to old feedRefsData) based on term and cached data.
*/
var buildFilteredRefsData = function (term, cachedData) {
var data = {results: []};
term = term || '';
// filter results
$.each(cachedData.results, function () {
var section = this.text;
var children = [];
$.each(this.children, function () {
if (!term.length || this.text.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
children.push(this);
}
});
data.results.push({
text: section,
children: children
});
});
// push the typed-in commit idx (numeric commit)
if (term && !isNaN(term)) {
var files_url = pyroutes.url('repo_files', {
repo_name: templateContext.repo_name,
commit_id: term,
f_path: state.f_path
});
data.results.push({
text: _gettext('go to numeric commit'),
children: [{
at_ref: null,
id: null,
text: 'r{0}'.format(term),
type: 'sha',
raw_id: term,
idx: term,
files_url: files_url
}]
});
}
return data;
};
/**
* Ensure refs data is loaded and filtered, then call callback(data).
* Replaces old loadRefsData + feedRefsData + query().
*/
var ensureRefsData = function (term, callback) {
var cachedData = cachedDataSource[cacheKey];
if (cachedData) {
var filtered = buildFilteredRefsData(term, cachedData);
callback(filtered);
} else {
$.ajax({
url: loadUrl,
data: {},
dataType: 'json',
type: 'GET',
success: function (data) {
cachedDataSource[cacheKey] = data;
var filtered = buildFilteredRefsData(term, data);
callback(filtered);
}
});
}
};
var select2RefFileSwitcher = function (targetElement, loadUrl, initialData) {
var formatResult = function (result) {
return formatSelect2SelectionRefs(result);
};
var formatSelection = function (data) {
var commit_ref = data || {};
var tmpl = '';
if (commit_ref.type === 'sha') {
tmpl = (commit_ref.raw_id || commit_ref.text || "").substr(0, 8);
} else if (commit_ref.type === 'branch') {
tmpl = tmpl.concat('<i class="icon-branch"></i> ');
tmpl = tmpl.concat(escapeHtml(commit_ref.text || ''));
} else if (commit_ref.type === 'tag') {
tmpl = tmpl.concat('<i class="icon-tag"></i> ');
tmpl = tmpl.concat(escapeHtml(commit_ref.text || ''));
} else if (commit_ref.type === 'book') {
tmpl = tmpl.concat('<i class="icon-bookmark"></i> ');
tmpl = tmpl.concat(escapeHtml(commit_ref.text || ''));
} else {
// fallback
tmpl = escapeHtml(commit_ref.text || '');
}
var idx = commit_ref.idx || 0;
if (idx !== 0) {
tmpl = tmpl.concat('<span class="select-index-number">r{0}</span>'.format(idx));
}
return tmpl;
};
var $element = $(targetElement);
// --- initSelection replacement: pre-populate selected option ---
if (initialData) {
// Ensure there is an id; Select2 v4 needs an id field
if (!initialData.id) {
initialData.id = initialData.raw_id || initialData.text;
}
var option = new Option(initialData.text, initialData.id, true, true);
// Attach full data object so templateSelection has access to type/raw_id/idx
$(option).data('data', initialData);
$element.append(option).trigger('change');
}
$element.select2({
dropdownAutoWidth: true,
width: "100%",
containerCssClass: "drop-menu",
dropdownCssClass: "drop-menu-dropdown",
// --- Select2 v4 ajax API instead of "query" ---
ajax: {
transport: function (params, success, failure) {
var term = (params.data && params.data.term) ? params.data.term : '';
ensureRefsData(term, function (data) {
success(data); // data already in {results: [...] } format
});
},
processResults: function (data /*, params */) {
// data is already in the expected {results: [...]} format
return data;
}
},
// --- v4 template callbacks (replacing formatResult/formatSelection) ---
templateResult: formatResult,
templateSelection: formatSelection,
// allow HTML from our templates
escapeMarkup: function (markup) { return markup; }
});
};
select2RefFileSwitcher('#refs_filter', loadUrl, initialCommitData);
// switcher for file tree
// In v4, prefer the "select2:select" event and e.params.data
$('#refs_filter').on('select2:select', function (e) {
var data = e.params && e.params.data;
if (data && data.files_url) {
window.location = data.files_url;
}
});
};
$(document).ready(function() {
timeagoActivate();
tooltipActivate();
if ($('#trimmed_message_box').height() < 50) {
$('#message_expand').hide();
}
$('#message_expand').on('click', function(e) {
$('#trimmed_message_box').css('max-height', 'none');
$(this).hide();
});
if (fileSourcePage) {
initFileJS()
} else {
initTreeJS()
}
var search_GET = "${request.GET.get('search','')}";
if (search_GET === "1") {
NodeFilter.initFilter();
NodeFilter.focus();
}
});
</script>
</%def>