2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-05-07 15:56:29 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.DocPanel <f> Documentation Panel
|
|
|
|
([options[, self]]) -> <o:Ox.SplitPanel> Documentation Panel
|
2011-05-16 08:24:46 +00:00
|
|
|
options <o> Options object
|
2012-04-09 08:39:56 +00:00
|
|
|
collapsible <b|false> If true, the list can be collabsed
|
2012-04-08 18:20:58 +00:00
|
|
|
element <e> Default content
|
2012-06-12 11:19:09 +00:00
|
|
|
files <a|[]> Files to parse for docs (alternative to items option)
|
2012-04-09 08:39:56 +00:00
|
|
|
getModule <f> Returns module for given item
|
|
|
|
getSection <f> Returns section for given item
|
2012-06-12 11:19:09 +00:00
|
|
|
items <a|[]> Array of doc items (alternative to files option)
|
2012-04-09 08:39:56 +00:00
|
|
|
path <s|''> Path prefix
|
2012-06-12 12:32:27 +00:00
|
|
|
references <r|null> RegExp to find references
|
2012-04-06 12:24:17 +00:00
|
|
|
replace <[[]]|[]> See Ox.SyntaxHighlighter
|
2012-04-09 08:39:56 +00:00
|
|
|
resizable <b|true> If true, list is resizable
|
|
|
|
resize <a|[128, 256, 384]> List resize positions
|
2012-06-12 11:19:09 +00:00
|
|
|
runTests <b|false> If true, run tests
|
|
|
|
selected <s|''> Id of the selected item
|
|
|
|
showTests <b|false> If true, show test results in list
|
|
|
|
showTooltips <b|false> If true, show test result tooltips in list
|
2012-04-09 08:39:56 +00:00
|
|
|
size <s|256> Default list size
|
|
|
|
self <o> Shared private variable
|
|
|
|
load <!> Fires once all docs are loaded
|
|
|
|
items [o] Array of doc items
|
2011-05-07 15:56:29 +00:00
|
|
|
@*/
|
|
|
|
|
|
|
|
Ox.DocPanel = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
|
|
|
.defaults({
|
2012-04-08 18:20:58 +00:00
|
|
|
collapsible: false,
|
|
|
|
element: '',
|
2012-06-12 12:32:27 +00:00
|
|
|
examples: [],
|
|
|
|
examplesPath: '',
|
2011-05-07 15:56:29 +00:00
|
|
|
files: [],
|
|
|
|
getModule: function(item) {
|
|
|
|
return item.file.replace(self.options.path, '');
|
|
|
|
},
|
|
|
|
getSection: function(item) {
|
|
|
|
return item.section;
|
|
|
|
},
|
2012-04-09 08:39:56 +00:00
|
|
|
items: [],
|
2011-05-07 15:56:29 +00:00
|
|
|
path: '',
|
2012-06-12 12:32:27 +00:00
|
|
|
references: null,
|
2012-04-06 12:24:17 +00:00
|
|
|
replace: [],
|
2012-04-08 18:20:58 +00:00
|
|
|
resizable: false,
|
2011-05-07 15:56:29 +00:00
|
|
|
resize: [128, 256, 384],
|
2012-06-12 14:55:13 +00:00
|
|
|
results: null,
|
2012-06-12 11:19:09 +00:00
|
|
|
runTests: false,
|
2012-04-08 18:20:58 +00:00
|
|
|
selected: '',
|
2012-06-12 11:19:09 +00:00
|
|
|
showTests: false,
|
|
|
|
showTooltips: false,
|
|
|
|
size: 256,
|
2011-05-07 15:56:29 +00:00
|
|
|
})
|
2012-05-28 19:35:41 +00:00
|
|
|
.options(options || {})
|
|
|
|
.update({
|
|
|
|
selected: function() {
|
|
|
|
selectItem(self.options.selected);
|
|
|
|
}
|
|
|
|
});
|
2011-05-07 15:56:29 +00:00
|
|
|
|
2012-06-12 15:37:35 +00:00
|
|
|
self.$toolbar = Ox.Bar({size: 24}).css({textAlign: 'center'});
|
2011-05-07 15:56:29 +00:00
|
|
|
self.$list = Ox.Element();
|
2012-06-12 13:32:34 +00:00
|
|
|
self.$sidebar = Ox.SplitPanel({
|
|
|
|
elements: [
|
2012-06-12 15:24:58 +00:00
|
|
|
{element: Ox.Element()},
|
|
|
|
{element: Ox.Element(), size: 24}
|
2012-06-12 13:32:34 +00:00
|
|
|
],
|
|
|
|
orientation: 'vertical'
|
|
|
|
});
|
2012-04-16 07:13:22 +00:00
|
|
|
self.$page = Ox.Element();
|
2012-06-12 15:37:35 +00:00
|
|
|
self.$testsStatus = $('<div>')
|
|
|
|
.css({marginTop: '5px', textAlign: 'center'})
|
|
|
|
.appendTo(self.$toolbar);
|
2012-06-16 12:25:08 +00:00
|
|
|
if (!self.options.showTests) {
|
|
|
|
self.$testsStatus.hide();
|
|
|
|
} else if (!self.options.results) {
|
2012-06-12 15:24:58 +00:00
|
|
|
self.options.results = {};
|
|
|
|
self.$testsButton = Ox.Button({title: 'Run Tests'})
|
2012-06-12 15:37:35 +00:00
|
|
|
.css({margin: '4px auto'})
|
2012-06-12 15:24:58 +00:00
|
|
|
.bindEvent({click: runTests})
|
|
|
|
.appendTo(self.$toolbar);
|
2012-06-12 15:37:35 +00:00
|
|
|
self.$testsStatus.hide();
|
2012-06-12 15:24:58 +00:00
|
|
|
} else {
|
2012-06-12 15:37:35 +00:00
|
|
|
self.$testsStatus.html(formatResults());
|
2012-06-12 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
2012-04-08 18:20:58 +00:00
|
|
|
that.setElement(
|
|
|
|
self.$panel = Ox.SplitPanel({
|
|
|
|
elements: [
|
|
|
|
{
|
|
|
|
collapsible: self.options.collapsible,
|
2012-06-12 13:32:34 +00:00
|
|
|
element: self.$sidebar,
|
2012-04-08 18:20:58 +00:00
|
|
|
resizable: self.options.resizable,
|
|
|
|
resize: self.options.resize,
|
|
|
|
size: self.options.size
|
|
|
|
},
|
|
|
|
{
|
|
|
|
element: self.$page
|
|
|
|
}
|
|
|
|
],
|
|
|
|
orientation: 'horizontal'
|
|
|
|
})
|
|
|
|
);
|
2011-05-07 15:56:29 +00:00
|
|
|
|
2012-04-09 08:39:56 +00:00
|
|
|
if (self.options.files) {
|
2012-05-30 17:24:45 +00:00
|
|
|
self.options.files = Ox.makeArray(self.options.files);
|
2012-04-09 08:39:56 +00:00
|
|
|
self.options.items = Ox.doc(self.options.files.map(function(file) {
|
|
|
|
return self.options.path + file;
|
|
|
|
}), function(docItems) {
|
|
|
|
self.options.items = docItems;
|
2012-06-12 12:32:27 +00:00
|
|
|
getExamples(function() {
|
2012-06-12 15:24:58 +00:00
|
|
|
self.$sidebar.replaceElement(1, self.$toolbar);
|
2012-06-12 12:32:27 +00:00
|
|
|
renderList();
|
|
|
|
self.options.runTests && runTests();
|
2012-06-12 14:55:13 +00:00
|
|
|
that.triggerEvent('load', {items: self.options.items});
|
2012-06-12 12:32:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
getExamples(function() {
|
2012-06-12 15:24:58 +00:00
|
|
|
self.$sidebar.replaceElement(1, self.$toolbar);
|
2012-04-09 08:39:56 +00:00
|
|
|
renderList();
|
2012-06-12 11:19:09 +00:00
|
|
|
self.options.runTests && runTests();
|
2012-04-09 08:39:56 +00:00
|
|
|
});
|
2012-06-12 12:32:27 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 15:24:58 +00:00
|
|
|
function formatResults() {
|
|
|
|
var results = self.options.results[''],
|
|
|
|
tests = results.passed + results.failed;
|
|
|
|
return tests + ' test' + (tests == 1 ? '' : 's') + ', '
|
|
|
|
+ results.passed + ' passed, ' + results.failed + ' failed';
|
|
|
|
}
|
|
|
|
|
2012-06-12 12:32:27 +00:00
|
|
|
function getExamples(callback) {
|
|
|
|
var i = 0;
|
2012-06-12 14:26:55 +00:00
|
|
|
if (self.options.examples && self.options.examples.length) {
|
2012-06-12 12:32:27 +00:00
|
|
|
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);
|
2012-06-13 10:05:32 +00:00
|
|
|
if (item) {
|
|
|
|
item.examples = (item.examples || []).concat(
|
|
|
|
{id: example, title: title}
|
|
|
|
);
|
|
|
|
}
|
2012-06-12 12:32:27 +00:00
|
|
|
});
|
|
|
|
if (++i == self.options.examples.length) {
|
|
|
|
self.options.items.forEach(function(item) {
|
|
|
|
item.examples && item.examples.sort(function(a, b) {
|
2012-06-12 14:26:55 +00:00
|
|
|
a = a.title.toLowerCase();
|
|
|
|
b = b.title.toLowerCase();
|
2012-06-12 12:32:27 +00:00
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
2012-06-12 11:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getIcon(id, expanded) {
|
2012-06-12 14:55:13 +00:00
|
|
|
var $icon = null, results = self.options.results[id];
|
|
|
|
if (!Ox.isEmpty(self.options.results)) {
|
2012-06-12 11:19:09 +00:00
|
|
|
$icon = Ox.Theme.getColorImage(
|
|
|
|
'symbol' + (expanded === true ? 'Down' : expanded === false ? 'Right' : 'Center'),
|
|
|
|
!results ? 'none' : results.failed === 0 ? 'passed' : 'failed',
|
|
|
|
self.options.showTooltips ? getTooltip(results) : null
|
|
|
|
);
|
|
|
|
} else if (!Ox.endsWith(id, '/')) {
|
|
|
|
$icon = $('<img>').attr({src: Ox.UI.getImageURL('symbolCenter')});
|
|
|
|
}
|
|
|
|
return $icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getItemByName(name) {
|
2012-06-13 10:05:32 +00:00
|
|
|
var item = null;
|
2012-06-12 11:19:09 +00:00
|
|
|
Ox.forEach(self.options.items, function(v) {
|
|
|
|
if (v.name == name) {
|
|
|
|
item = v;
|
|
|
|
Ox.Break();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTooltip(results) {
|
|
|
|
return results
|
|
|
|
? results.passed + ' test'
|
|
|
|
+ (results.passed == 1 ? '' : 's') + ' passed'
|
|
|
|
+ (results.failed
|
|
|
|
? ', ' + results.failed + ' test'
|
|
|
|
+ (results.failed == 1 ? '' : 's') + ' failed'
|
|
|
|
: ''
|
|
|
|
)
|
|
|
|
: 'No tests';
|
2012-04-09 08:39:56 +00:00
|
|
|
}
|
2011-09-05 05:40:44 +00:00
|
|
|
|
2012-04-09 08:39:56 +00:00
|
|
|
function parseFiles(callback) {
|
2011-05-07 15:56:29 +00:00
|
|
|
var counter = 0,
|
2012-04-04 14:59:58 +00:00
|
|
|
docItems = [],
|
|
|
|
length = self.options.files.length;
|
2011-05-07 15:56:29 +00:00
|
|
|
self.options.files.forEach(function(file) {
|
|
|
|
Ox.doc(self.options.path + file, function(fileItems) {
|
2012-05-24 07:45:33 +00:00
|
|
|
docItems = docItems.concat(fileItems);
|
2012-04-09 08:39:56 +00:00
|
|
|
++counter == length && callback(docItems);
|
2011-05-07 15:56:29 +00:00
|
|
|
});
|
|
|
|
});
|
2012-04-09 08:39:56 +00:00
|
|
|
}
|
2011-05-16 08:24:46 +00:00
|
|
|
|
2012-04-09 08:39:56 +00:00
|
|
|
function renderList() {
|
|
|
|
var treeItems = [];
|
|
|
|
self.options.items.forEach(function(docItem) {
|
2012-06-12 11:19:09 +00:00
|
|
|
var moduleIndex, results, sectionIndex;
|
2012-04-09 08:39:56 +00:00
|
|
|
docItem.module = self.options.getModule(docItem);
|
2012-06-12 11:19:09 +00:00
|
|
|
moduleIndex = Ox.getIndexById(treeItems, docItem.module + '/');
|
2012-04-09 08:39:56 +00:00
|
|
|
if (moduleIndex == -1) {
|
|
|
|
treeItems.push({
|
2012-06-12 11:19:09 +00:00
|
|
|
id: docItem.module + '/',
|
2012-04-09 08:39:56 +00:00
|
|
|
items: [],
|
|
|
|
title: docItem.module
|
|
|
|
});
|
|
|
|
moduleIndex = treeItems.length - 1;
|
|
|
|
}
|
|
|
|
docItem.section = self.options.getSection(docItem);
|
|
|
|
if (docItem.section) {
|
|
|
|
sectionIndex = Ox.getIndexById(
|
|
|
|
treeItems[moduleIndex].items,
|
2012-06-12 11:19:09 +00:00
|
|
|
docItem.module + '/' + docItem.section + '/'
|
2012-04-09 08:39:56 +00:00
|
|
|
);
|
|
|
|
if (sectionIndex == -1) {
|
|
|
|
treeItems[moduleIndex].items.push({
|
2012-06-12 11:19:09 +00:00
|
|
|
id: docItem.module + '/' + docItem.section + '/',
|
2011-05-07 15:56:29 +00:00
|
|
|
items: [],
|
2012-04-09 08:39:56 +00:00
|
|
|
title: docItem.section
|
2011-05-07 15:56:29 +00:00
|
|
|
});
|
2012-04-09 08:39:56 +00:00
|
|
|
sectionIndex = treeItems[moduleIndex].items.length - 1;
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
2012-04-09 08:39:56 +00:00
|
|
|
}
|
|
|
|
(
|
|
|
|
docItem.section
|
|
|
|
? treeItems[moduleIndex].items[sectionIndex]
|
|
|
|
: treeItems[moduleIndex]
|
|
|
|
).items.push({
|
2012-06-12 11:19:09 +00:00
|
|
|
id: docItem.module + '/' + (
|
|
|
|
docItem.section ? docItem.section + '/' : ''
|
|
|
|
) + docItem.name,
|
2012-04-09 08:39:56 +00:00
|
|
|
title: docItem.name
|
2011-05-07 15:56:29 +00:00
|
|
|
});
|
2012-04-09 08:39:56 +00:00
|
|
|
});
|
|
|
|
treeItems.sort(sortByTitle);
|
|
|
|
treeItems.forEach(function(item) {
|
|
|
|
item.items.sort(sortByTitle);
|
|
|
|
item.items.forEach(function(subitem) {
|
|
|
|
subitem.items.sort(sortByTitle);
|
2011-05-11 13:53:29 +00:00
|
|
|
});
|
2012-04-09 08:39:56 +00:00
|
|
|
});
|
|
|
|
self.$list = Ox.TreeList({
|
2012-06-12 11:19:09 +00:00
|
|
|
icon: self.options.showTests
|
|
|
|
? getIcon
|
|
|
|
: Ox.UI.getImageURL('symbolCenter'),
|
2012-04-09 08:39:56 +00:00
|
|
|
items: treeItems,
|
|
|
|
selected: self.options.selected
|
|
|
|
? [self.options.selected] : '',
|
2012-06-12 11:19:09 +00:00
|
|
|
width: self.options.size
|
2012-04-09 08:39:56 +00:00
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
select: function(data) {
|
2012-06-13 10:05:32 +00:00
|
|
|
selectItem(
|
2012-06-13 10:08:54 +00:00
|
|
|
data.ids.length ? Ox.last(data.ids[0].split('/')) : '',
|
|
|
|
true
|
2012-06-13 10:05:32 +00:00
|
|
|
);
|
2012-04-09 08:39:56 +00:00
|
|
|
}
|
|
|
|
});
|
2012-06-12 15:24:58 +00:00
|
|
|
self.$sidebar.replaceElement(0, self.$list);
|
2012-04-09 08:39:56 +00:00
|
|
|
selectItem(self.options.selected);
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 11:19:09 +00:00
|
|
|
function runTests() {
|
2012-06-12 15:37:35 +00:00
|
|
|
self.$testsButton.remove();
|
|
|
|
self.$testsStatus.html('Running tests...').show();
|
2012-06-12 14:41:37 +00:00
|
|
|
Ox.load({Geo: {}, Image: {}, Unicode: {}}, function() {
|
|
|
|
Ox.test(self.options.items, function(results) {
|
|
|
|
results.forEach(function(result) {
|
|
|
|
var item = getItemByName(result.name),
|
|
|
|
passed = result.passed ? 'passed' : 'failed';
|
|
|
|
item.tests[Ox.indexOf(item.tests, function(test) {
|
|
|
|
return test.statement == result.statement;
|
|
|
|
})] = result;
|
|
|
|
['', item.module + '/'].concat(
|
|
|
|
item.section ? item.module + '/' + item.section + '/' : [],
|
|
|
|
item.module + '/' + (item.section ? item.section + '/' : '') + item.name
|
|
|
|
).forEach(function(key) {
|
2012-06-12 14:55:13 +00:00
|
|
|
self.options.results[key] = self.options.results[key] || {passed: 0, failed: 0};
|
|
|
|
self.options.results[key][passed]++;
|
2012-06-12 11:19:09 +00:00
|
|
|
});
|
|
|
|
});
|
2012-06-12 15:37:35 +00:00
|
|
|
self.$testsStatus.html(formatResults());
|
2012-06-12 14:41:37 +00:00
|
|
|
renderList();
|
2012-06-12 14:55:13 +00:00
|
|
|
that.triggerEvent('tests', {results: self.options.results});
|
2012-06-12 11:19:09 +00:00
|
|
|
});
|
2012-06-12 14:41:37 +00:00
|
|
|
});
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
2012-06-13 10:08:54 +00:00
|
|
|
function selectItem(id, fromList) {
|
2012-06-13 10:05:32 +00:00
|
|
|
var item = id ? getItemByName(id) : null;
|
|
|
|
if (item) {
|
2012-04-08 18:20:58 +00:00
|
|
|
self.options.selected = id;
|
2012-06-13 10:05:32 +00:00
|
|
|
self.$list.options({
|
|
|
|
selected: [item.module + '/' + (
|
|
|
|
item.section ? item.section + '/' : ''
|
|
|
|
) + item.name]
|
|
|
|
});
|
|
|
|
self.$page = Ox.DocPage({
|
|
|
|
item: item,
|
|
|
|
replace: self.options.replace
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
example: function(data) {
|
|
|
|
that.triggerEvent('example', data);
|
|
|
|
},
|
|
|
|
select: function(data) {
|
|
|
|
selectItem(data.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
that.$element.replaceElement(1, self.$page);
|
2012-04-16 07:13:22 +00:00
|
|
|
} else {
|
2012-06-13 10:05:32 +00:00
|
|
|
self.options.selected = '';
|
2012-06-13 10:08:54 +00:00
|
|
|
!fromList && self.$list.options({selected: []});
|
2012-04-16 07:13:22 +00:00
|
|
|
self.$page.empty().append(self.options.element);
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
2012-06-13 10:05:32 +00:00
|
|
|
that.triggerEvent('select', {id: self.options.selected});
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
2011-05-11 13:53:29 +00:00
|
|
|
function sortByTitle(a, b) {
|
2012-06-12 11:19:09 +00:00
|
|
|
var a = Ox.stripTags(a.title).toLowerCase(),
|
|
|
|
b = Ox.stripTags(b.title).toLowerCase();
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
2011-05-11 13:53:29 +00:00
|
|
|
}
|
2012-04-08 18:20:58 +00:00
|
|
|
|
2011-05-07 15:56:29 +00:00
|
|
|
return that;
|
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
};
|