Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to collapse nodes in the outline for XML. #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ define(function (require, exports, module) {
var $entry = $(document.createElement("li"));
$entry.addClass("outline-entry");
$entry.addClass(entry.classes);
$entry.attr("id", entry.id);
$entry.append(entry.$html);
$entry.click({
line: entry.line,
Expand Down
33 changes: 31 additions & 2 deletions src/languages/XML.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ define(function (require, exports, module) {
var $elements = [];
if (indent) {
var $indentation = $(document.createElement("span"));
$indentation.addClass("outline-entry-indent");
var interpunct = "";
for (var i = 0; i < indent; i++) {
interpunct += "·";
}
$indentation.text(interpunct);
$indentation.addClass("outline-entry-indent open")
.text(interpunct)
.attr("data-indent", indent)
.click(function(){
_controlCollapse(this, indent);
});
$elements.push($indentation);
}
if (namespace) {
Expand All @@ -39,6 +43,26 @@ define(function (require, exports, module) {
};
}

function _controlCollapse(el, indent) {
// Collapse the child nodes when this node is clicked.
var indentElementParent = $(el).parent();
var nextParent = indentElementParent.next();
// Look at all subsequent nodes with a greater indent than the current node
// to see if they should be hidden or shown.
while (Number(nextParent.children(".outline-entry-indent").attr("data-indent")) > indent ){
if(nextParent.attr("data-closed-by") === indentElementParent[0].id){
nextParent.removeAttr("data-closed-by");
indentElementParent.children(".outline-entry-indent.closed").addClass("open").removeClass("closed");
nextParent.show();
} else if(nextParent.attr("data-closed-by") === undefined) {
nextParent.attr("data-closed-by", indentElementParent[0].id);
indentElementParent.children(".outline-entry-indent.open").addClass("closed").removeClass("open");
nextParent.hide();
}
nextParent = nextParent.next();
}
}

function _getIndentationLevel(whitespace) {
if (!whitespace) {
return 0;
Expand All @@ -62,9 +86,11 @@ define(function (require, exports, module) {
var lines = text.split("\n");
var regex = /^(\s*)<([\w]+:)?([\w.:-]+)(?:[^>]*?(id|class)=["']([\w- ]+)["'])?/g;
var result = [];
var idCounter = 0;
lines.forEach(function (line, index) {
var match = regex.exec(line);
while (match !== null) {
idCounter += 1;
var whitespace = match[1];
var namespace = showArguments ? (match[2] || "").trim() : "";
var name = match[3].trim();
Expand All @@ -73,6 +99,9 @@ define(function (require, exports, module) {
var entry = _createListEntry(namespace, name, type, args, _getIndentationLevel(whitespace));
entry.line = index;
entry.ch = line.length;
// Add an id so the collapse can mark the collapsed nodes and so control
// what can reopen them.
entry.id = "ListEntry" + idCounter.toString();
result.push(entry);
match = regex.exec(line);
}
Expand Down
10 changes: 10 additions & 0 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,13 @@
.outline-entry-xml-class {
color: #adb9bd;
}

.outline-entry-indent.open:before {
content: "\f30f";
font-family: Ionicons;
}

.outline-entry-indent.closed:before {
content: "\f366";
font-family: Ionicons;
}