Restore "h" to hide or show files, and modernize file visibility toggling

Summary:
Ref T12616. This puts "h" back to collapse or expand the current file.

This removes some very complicated/messy code around following links in the table of contents and getting files auto-expanded. I suspect no one will miss this, but we can restore it if ayone notices.

Test Plan: Pressed "h" to collapse/expand a file. Also used the menu items.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T12616

Differential Revision: https://secure.phabricator.com/D17940
This commit is contained in:
epriestley
2017-05-17 12:51:29 -07:00
parent 1e3c8df1c8
commit f78ce156f1
7 changed files with 114 additions and 177 deletions

View File

@@ -55,6 +55,9 @@ JX.install('DiffChangeset', {
_rightID: null,
_inlines: null,
_visible: true,
_undoNode: null,
getLeftChangesetID: function() {
return this._leftID;
@@ -345,6 +348,10 @@ JX.install('DiffChangeset', {
}
});
if (!this._visible) {
return items;
}
var rows = JX.DOM.scry(this._node, 'tr');
var blocks = [];
@@ -635,8 +642,60 @@ JX.install('DiffChangeset', {
// them to this Changeset's list of inlines.
this.getInlineForRow(row);
}
}
},
toggleVisibility: function() {
this._visible = !this._visible;
var diff = JX.DOM.find(this._node, 'table', 'differential-diff');
var undo = this._getUndoNode();
if (this._visible) {
JX.DOM.show(diff);
JX.DOM.remove(undo);
} else {
JX.DOM.hide(diff);
JX.DOM.appendContent(diff.parentNode, undo);
}
JX.Stratcom.invoke('resize');
},
_getUndoNode: function() {
if (!this._undoNode) {
var pht = this.getChangesetList().getTranslations();
var link_attributes = {
href: '#'
};
var undo_link = JX.$N('a', link_attributes, pht('Show Content'));
var onundo = JX.bind(this, this._onundo);
JX.DOM.listen(undo_link, 'click', null, onundo);
var node_attributes = {
className: 'differential-collapse-undo'
};
var node_content = [
pht('This file content has been collapsed.'),
' ',
undo_link
];
var undo_node = JX.$N('div', node_attributes, node_content);
this._undoNode = undo_node;
}
return this._undoNode;
},
_onundo: function(e) {
e.kill();
this.toggleVisibility();
}
},
statics: {

View File

@@ -161,6 +161,9 @@ JX.install('DiffChangesetList', {
'Jump to previous inline comment, including hidden comments.');
this._installJumpKey('P', label, -1, 'comment', true);
label = pht('Hide or show the current file.');
this._installKey('h', label, this._onkeytogglefile);
label = pht('Jump to the table of contents.');
this._installKey('t', label, this._ontoc);
@@ -385,6 +388,20 @@ JX.install('DiffChangesetList', {
this._warnUser(pht('You must select a comment to mark done.'));
},
_onkeytogglefile: function() {
var cursor = this._cursorItem;
if (cursor) {
if (cursor.type == 'file') {
cursor.changeset.toggleVisibility();
return;
}
}
var pht = this.getTranslations();
this._warnUser(pht('You must select a file to hide or show.'));
},
_onkeyhide: function() {
var cursor = this._cursorItem;
@@ -616,14 +633,10 @@ JX.install('DiffChangesetList', {
var visible_item = new JX.PHUIXActionView()
.setHandler(function(e) {
var diff = JX.DOM.scry(
JX.$(data.containerID),
'table',
'differential-diff');
JX.Stratcom.invoke('differential-toggle-file', null, {diff: diff});
e.prevent();
menu.close();
changeset.toggleVisibility();
});
list.addItem(visible_item);

View File

@@ -1,128 +0,0 @@
/**
* @provides javelin-behavior-differential-toggle-files
* @requires javelin-behavior
* javelin-dom
* javelin-stratcom
* phabricator-phtize
*/
JX.behavior('differential-toggle-files', function(config) {
var pht = JX.phtize(config.pht);
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);
JX.DOM.remove(data.undo);
data.undo = null;
} else {
data.hidden = true;
data.undo = render_collapse_undo();
JX.DOM.hide(diff);
JX.DOM.listen(
data.undo,
'click',
'differential-collapse-undo',
function(e) {
e.kill();
data.hidden = false;
JX.DOM.show(diff);
JX.DOM.remove(data.undo);
data.undo = null;
});
JX.DOM.appendContent(diff.parentNode, data.undo);
}
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');
var invoked = false;
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] ]
});
invoked = true;
}
}
if (!invoked) {
e.prevent();
}
return;
}
elt = elt.parentNode;
}
e.prevent();
});
JX.Stratcom.listen(
'click',
'tag:a',
function(e) {
var link = e.getNode('tag:a');
var id = link.getAttribute('href');
if (!id || !id.match(/^#.+/)) {
return;
}
var raw = e.getRawEvent();
if (raw.altKey || raw.ctrlKey || raw.metaKey || raw.shiftKey) {
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));
for (var i = 0; i < named.length; ++i) {
if (named[i].tagName.toLowerCase() == 'a') {
if (target) {
return;
}
target = named[i];
}
}
if (!target) {
return;
}
}
var event = JX.Stratcom.invoke('differential-toggle-file-request', null, {
element: target
});
if (!event.getPrevented()) {
// This event is processed after the hash has changed, so it doesn't
// automatically jump there like we want.
JX.DOM.scrollTo(target);
}
});
var render_collapse_undo = function() {
var link = JX.$N(
'a',
{href: '#', sigil: 'differential-collapse-undo'},
pht('undo'));
return JX.$N(
'div',
{className: 'differential-collapse-undo',
sigil: 'differential-collapse-undo-div'},
[pht('collapsed'), ' ', link]);
};
});