2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-05-07 15:56:29 +00:00
|
|
|
/*@
|
2012-05-22 13:14:40 +00:00
|
|
|
Ox.DocPanel <f:Ox.Element> Documentation Panel
|
2011-05-16 08:24:46 +00:00
|
|
|
() -> <f> Documentation Panel
|
|
|
|
(options) -> <f> Documentation Panel
|
|
|
|
(options, self) -> <f> Documentation Panel
|
|
|
|
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-04-09 08:39:56 +00:00
|
|
|
files <a|[]> Files to parse for docs
|
|
|
|
getModule <f> Returns module for given item
|
|
|
|
getSection <f> Returns section for given item
|
|
|
|
items <a|[]> Array of doc items (alternative to files options)
|
|
|
|
path <s|''> Path prefix
|
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
|
|
|
|
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: '',
|
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-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-04-08 18:20:58 +00:00
|
|
|
selected: '',
|
2011-05-07 15:56:29 +00:00
|
|
|
size: 256
|
|
|
|
})
|
|
|
|
.options(options || {});
|
|
|
|
|
|
|
|
self.$list = Ox.Element();
|
2012-04-16 07:13:22 +00:00
|
|
|
self.$page = Ox.Element();
|
2012-04-08 18:20:58 +00:00
|
|
|
|
|
|
|
that.setElement(
|
|
|
|
self.$panel = Ox.SplitPanel({
|
|
|
|
elements: [
|
|
|
|
{
|
|
|
|
collapsible: self.options.collapsible,
|
|
|
|
element: self.$list,
|
|
|
|
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) {
|
|
|
|
self.options.items = Ox.doc(self.options.files.map(function(file) {
|
|
|
|
return self.options.path + file;
|
|
|
|
}), function(docItems) {
|
|
|
|
self.options.items = docItems;
|
|
|
|
renderList();
|
|
|
|
that.triggerEvent('load', {items: docItems});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
renderList();
|
|
|
|
}
|
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) {
|
|
|
|
docItems = Ox.merge(docItems, 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) {
|
|
|
|
var moduleIndex, sectionIndex;
|
|
|
|
docItem.module = self.options.getModule(docItem);
|
|
|
|
moduleIndex = Ox.getIndexById(treeItems, '_' + docItem.module);
|
|
|
|
if (moduleIndex == -1) {
|
|
|
|
treeItems.push({
|
|
|
|
id: '_' + docItem.module,
|
|
|
|
items: [],
|
|
|
|
title: docItem.module
|
|
|
|
});
|
|
|
|
moduleIndex = treeItems.length - 1;
|
|
|
|
}
|
|
|
|
docItem.section = self.options.getSection(docItem);
|
|
|
|
if (docItem.section) {
|
|
|
|
sectionIndex = Ox.getIndexById(
|
|
|
|
treeItems[moduleIndex].items,
|
|
|
|
'_' + docItem.module + '_' + docItem.section
|
|
|
|
);
|
|
|
|
if (sectionIndex == -1) {
|
|
|
|
treeItems[moduleIndex].items.push({
|
|
|
|
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({
|
|
|
|
id: docItem.name,
|
|
|
|
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({
|
|
|
|
items: treeItems,
|
|
|
|
selected: self.options.selected
|
|
|
|
? [self.options.selected] : '',
|
|
|
|
width: self.options.size - Ox.UI.SCROLLBAR_SIZE
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
select: function(data) {
|
|
|
|
selectItem(data.ids.length ? data.ids[0] : '')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
that.$element.replaceElement(0, self.$list);
|
|
|
|
selectItem(self.options.selected);
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getItemByName(name) {
|
|
|
|
var item = {};
|
2012-04-09 08:39:56 +00:00
|
|
|
Ox.forEach(self.options.items, function(v) {
|
2011-05-07 15:56:29 +00:00
|
|
|
if (v.name == name) {
|
|
|
|
item = v;
|
2012-05-22 07:11:26 +00:00
|
|
|
Ox.break();
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2012-04-08 18:20:58 +00:00
|
|
|
function selectItem(id) {
|
|
|
|
if (id) {
|
|
|
|
self.options.selected = id;
|
|
|
|
if (self.options.selected[0] != '_') {
|
|
|
|
self.$list.options({selected: [id]});
|
2011-05-12 03:29:35 +00:00
|
|
|
self.$page = Ox.DocPage({
|
2012-04-08 18:20:58 +00:00
|
|
|
item: getItemByName(self.options.selected),
|
2012-04-06 12:24:17 +00:00
|
|
|
replace: self.options.replace
|
2011-05-12 03:29:35 +00:00
|
|
|
});
|
|
|
|
that.$element.replaceElement(1, self.$page);
|
2012-04-08 18:20:58 +00:00
|
|
|
that.triggerEvent('select', {id: self.options.selected});
|
2011-05-12 03:29:35 +00:00
|
|
|
}
|
2012-04-16 07:13:22 +00:00
|
|
|
} else {
|
|
|
|
self.$page.empty().append(self.options.element);
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-11 13:53:29 +00:00
|
|
|
function sortByTitle(a, b) {
|
2012-04-09 08:39:56 +00:00
|
|
|
return a.title < b.title ? -1
|
|
|
|
: a.title > b.title ? 1
|
|
|
|
: 0;
|
2011-05-11 13:53:29 +00:00
|
|
|
}
|
2012-04-08 18:20:58 +00:00
|
|
|
|
|
|
|
self.setOption = function(key, value) {
|
|
|
|
if (key == 'selected') {
|
|
|
|
selectItem(value);
|
|
|
|
}
|
2011-12-29 12:24: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
|
|
|
};
|