1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 17:02:41 +01:00
phorge-phorge/webroot/rsrc/js/application/differential/behavior-toggle-files.js
Alan Huang dbde4b9ff2 Fix issues with Differential file toggling
Summary:
See D2714#15.

 # Give anchors an `id` equal to their `name` so JX.$ can find them.
 # Listen for `click` s on `<a>` s instead of `hashchange` s to make
   toggling a bit more correct and consistent.
 # Add a placeholder menu item when the file is unloaded. A previous
   patch by @jungejason had left the item blank in that case.
 # In fixing (1) I found another exception when clicking on ToC links.
   Since those links are `differential-load`, they try to load the file
   before jumping to it. However, loading destroys the node it's looking
   for, so if you jump to an already-loaded file JX.$ complains at you.

Test Plan: Make a giant diff. Click on links and try toggling files.

Reviewers: epriestley, vrana

Reviewed By: vrana

CC: vrana, aran, Korvin

Maniphest Tasks: T370

Differential Revision: https://secure.phabricator.com/D3185
2012-08-09 17:53:05 -07:00

85 lines
2.3 KiB
JavaScript

/**
* @provides javelin-behavior-differential-toggle-files
* @requires javelin-behavior
* javelin-dom
* javelin-stratcom
* phabricator-keyboard-shortcut
*/
JX.behavior('differential-toggle-files', function(config) {
JX.Stratcom.listen(
'differential-toggle-file',
null,
function(e) {
if (e.getData().diff.length != 1) {
return;
}
var diff = e.getData().diff[0],
data = JX.Stratcom.getData(diff);
if(data.hidden) {
data.hidden = false;
JX.DOM.show(diff);
} else {
data.hidden = true;
JX.DOM.hide(diff);
}
JX.Stratcom.invoke('differential-toggle-file-toggled');
});
JX.Stratcom.listen(
'differential-toggle-file-request',
null,
function(e) {
var elt = e.getData().element;
while (elt !== document.body) {
if (JX.Stratcom.hasSigil(elt, 'differential-changeset')) {
var diffs = JX.DOM.scry(elt, 'table', 'differential-diff');
for (var i = 0; i < diffs.length; ++i) {
if (JX.Stratcom.getData(diffs[i]).hidden) {
JX.Stratcom.invoke('differential-toggle-file', null, {
diff: [ diffs[i] ],
});
}
}
return;
}
elt = elt.parentNode;
}
});
JX.Stratcom.listen(
'click',
'tag:a',
function(e) {
var link = e.getNode('tag:a');
var id = link.getAttribute('href');
if (!id.match(/^#.+/)) {
return;
}
// The target may have either a matching name or a matching id.
var target;
try {
target = JX.$(id.substr(1));
} catch(err) {
var named = document.getElementsByName(id.substr(1));
var matches = [];
for (var i = 0; i < named.length; ++i) {
if (named[i].tagName.toLowerCase() == 'a') {
matches.push(named[i]);
}
}
if (matches.length == 1) {
target = matches[0];
} else {
return;
}
}
JX.Stratcom.invoke('differential-toggle-file-request', null, {
element: target,
});
// This event is processed after the hash has changed, so it doesn't
// automatically jump there like we want.
JX.DOM.scrollTo(target);
});
});