in doc panel, parse examples and references

This commit is contained in:
rolux 2012-06-12 14:32:27 +02:00
parent b71d9148d0
commit cc67b1110b
2 changed files with 98 additions and 33 deletions

View file

@ -31,35 +31,39 @@ Ox.DocPage = function(options, self) {
.css({float: 'left', height: '13px', paddingTop: '1px', margin: '4px'}) .css({float: 'left', height: '13px', paddingTop: '1px', margin: '4px'})
.appendTo(self.$toolbar) .appendTo(self.$toolbar)
self.$examplesMenu = Ox.MenuButton({ if (self.options.item.references) {
items: [ self.$referencesMenu = Ox.MenuButton({
{id: 'foo', title: 'Not yet...', disabled: true} items: self.options.item.references.map(function(reference) {
], return {id: reference, title: reference};
title: 'Examples...', }),
width: 128 title: 'References...',
}) width: 128
.css({float: 'right', margin: '4px 4px 4px 2px'}) })
.bindEvent({ .css({float: 'right', margin: '4px 2px 4px 2px'})
click: function(data) { .bindEvent({
that.triggerEvent('select', {id: data.id}); click: function(data) {
} that.triggerEvent('select', {id: data.id});
}) }
.appendTo(self.$toolbar); })
.appendTo(self.$toolbar);
}
self.$referencesMenu = Ox.MenuButton({ if (self.options.item.examples) {
items: [ self.$examplesMenu = Ox.MenuButton({
{id: 'foo', title: 'Not yet...', disabled: true} items: self.options.item.examples.map(function(example) {
], return {id: example, title: example};
title: 'References...', }),
width: 128 title: 'Examples...',
}) width: 128
.css({float: 'right', margin: '4px 2px 4px 2px'}) })
.bindEvent({ .css({float: 'right', margin: '4px 4px 4px 2px'})
click: function(data) { .bindEvent({
that.triggerEvent('select', {id: data.id}); click: function(data) {
} that.triggerEvent('select', {id: data.id});
}) }
.appendTo(self.$toolbar); })
.appendTo(self.$toolbar);
}
self.$page = Ox.Container() self.$page = Ox.Container()
.addClass('OxDocPage OxDocument'); .addClass('OxDocPage OxDocument');

View file

@ -11,6 +11,7 @@ Ox.DocPanel <f> Documentation Panel
getSection <f> Returns section for given item getSection <f> Returns section for given item
items <a|[]> Array of doc items (alternative to files option) items <a|[]> Array of doc items (alternative to files option)
path <s|''> Path prefix path <s|''> Path prefix
references <r|null> RegExp to find references
replace <[[]]|[]> See Ox.SyntaxHighlighter replace <[[]]|[]> See Ox.SyntaxHighlighter
resizable <b|true> If true, list is resizable resizable <b|true> If true, list is resizable
resize <a|[128, 256, 384]> List resize positions resize <a|[128, 256, 384]> List resize positions
@ -31,6 +32,8 @@ Ox.DocPanel = function(options, self) {
.defaults({ .defaults({
collapsible: false, collapsible: false,
element: '', element: '',
examples: [],
examplesPath: '',
files: [], files: [],
getModule: function(item) { getModule: function(item) {
return item.file.replace(self.options.path, ''); return item.file.replace(self.options.path, '');
@ -40,6 +43,7 @@ Ox.DocPanel = function(options, self) {
}, },
items: [], items: [],
path: '', path: '',
references: null,
replace: [], replace: [],
resizable: false, resizable: false,
resize: [128, 256, 384], resize: [128, 256, 384],
@ -85,13 +89,51 @@ Ox.DocPanel = function(options, self) {
return self.options.path + file; return self.options.path + file;
}), function(docItems) { }), function(docItems) {
self.options.items = docItems; self.options.items = docItems;
renderList(); getReferences();
self.options.runTests && runTests(); getExamples(function() {
that.triggerEvent('load', {items: docItems}); renderList();
self.options.runTests && runTests();
that.triggerEvent('load', {items: docItems});
});
}); });
} else { } else {
renderList(); getReferences();
self.options.runTests && runTests(); getExamples(function() {
renderList();
self.options.runTests && runTests();
});
}
function getExamples(callback) {
var i = 0;
if (self.options.examples.length) {
self.options.examples.forEach(function(example) {
var path = self.options.examplesPath + example;
Ox.get(path + '/index.html', function(html) {
var match = html.match(/<title>(.+)<\/title>/),
title = match ? match[1] : 'Untitled';
Ox.get(path + '/js/example.js', function(js) {
var references = js.match(self.options.references);
references && Ox.unique(references).forEach(function(reference) {
var item = getItemByName(reference);
item.examples = (item.examples || []).concat(title);
});
if (++i == self.options.examples.length) {
self.options.items.forEach(function(item) {
item.examples && item.examples.sort(function(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a < b ? -1 : a > b ? 1 : 0;
});
});
callback();
}
});
});
});
} else {
callback();
}
} }
function getIcon(id, expanded) { function getIcon(id, expanded) {
@ -119,6 +161,25 @@ Ox.DocPanel = function(options, self) {
return item; return item;
} }
function getReferences() {
self.options.items.forEach(function(item, i) {
var references = item.source.map(function(token) {
return token.value;
}).join('').match(self.options.references);
if (references) {
self.options.items[i].references = Ox.unique(references)
.filter(function(reference) {
return reference != item.name;
})
.sort(function(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a < b ? -1 : a > b ? 1 : 0;
});
}
});
}
function getTooltip(results) { function getTooltip(results) {
return results return results
? results.passed + ' test' ? results.passed + ' test'