Files
phabricator/webroot/rsrc/js/application/repository/repository-crossreference.js
Alan Huang 1855ed4bee Support symbol linking in Remarkup code blocks
Summary:
Trigger the crossreference behavior on code blocks. Limited to
Differential, where we know what the project is, but includes regular
comments, inline comments, and previews of both.

(Hopefully event handlers on deleted elements also get deleted, so we
don't leak memory? Also, caching is a problem, and I didn't find a way
to mark existing cache entries as stale, like
`DifferentialChangesetParser::CACHE_VERSION`...)

Test Plan:
Load Differential revision, make lots of comments, click on
things.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T1602

Differential Revision: https://secure.phabricator.com/D3283
2012-08-14 14:03:26 -07:00

77 lines
2.2 KiB
JavaScript

/**
* @provides javelin-behavior-repository-crossreference
* @requires javelin-behavior
* javelin-dom
* javelin-uri
*/
JX.behavior('repository-crossreference', function(config) {
// NOTE: Pretty much everything in this file is a worst practice. We're
// constrained by the markup generated by the syntax highlighters.
function link(element, lang) {
JX.DOM.alterClass(element, 'repository-crossreference', true);
JX.DOM.listen(
element,
'click',
'tag:span',
function(e) {
if (window.getSelection && !window.getSelection().isCollapsed) {
return;
}
var target = e.getTarget();
var map = {nc : 'class', nf : 'function', na : null};
while (target !== document.body) {
if (JX.DOM.isNode(target, 'span') && (target.className in map)) {
var symbol = target.textContent || target.innerText;
var query = {
lang : lang,
projects : config.projects.join(','),
jump : true
};
if (map[target.className]) {
query.type = map[target.className];
}
if (target.hasAttribute('data-symbol-context')) {
query.context = target.getAttribute('data-symbol-context');
}
if (target.hasAttribute('data-symbol-name')) {
symbol = target.getAttribute('data-symbol-name');
}
var uri = JX.$U('/diffusion/symbol/' + symbol + '/');
uri.addQueryParams(query);
window.open(uri);
e.kill();
break;
}
target = target.parentNode;
}
});
}
function linkAll(section) {
var blocks = section.getElementsByClassName('remarkup-code-block');
for (var i = 0; i < blocks.length; ++i) {
if (blocks[i].hasAttribute('data-code-lang')) {
var lang = blocks[i].getAttribute('data-code-lang');
link(blocks[i], lang);
}
}
}
if (config.container) {
link(JX.$(config.container), config.lang);
} else if (config.section) {
linkAll(JX.$(config.section));
}
JX.Stratcom.listen(
'differential-preview-update',
null,
function(e) {
linkAll(e.getData().container);
});
});