oxjs/source/Ox.UI/js/Code/ExamplePanel.js

165 lines
5.2 KiB
JavaScript
Raw Normal View History

'use strict';
2012-05-21 10:38:18 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.ExamplePanel <f> Example Panel
([options[, self]]) -> <o:Ox.SplitPanel> Example Panel
2012-06-17 22:38:26 +00:00
load <!> load
select <!> select
id <s> selected example
2012-05-21 10:38:18 +00:00
options <o> Options
self <o> Shared private variable
@*/
Ox.ExamplePanel = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
2012-04-09 08:42:00 +00:00
element: '',
examples: [],
path: '',
references: null,
replaceCode: [],
replaceComment: [],
selected: '',
size: 256
})
.options(options || {})
2012-05-28 19:35:41 +00:00
.update({
selected: function() {
2012-06-22 07:23:38 +00:00
selectItem(self.options.selected);
2012-05-28 19:35:41 +00:00
}
});
self.$list = Ox.Element();
2012-04-16 07:13:22 +00:00
self.$page = Ox.Element();
that.setElement(
self.$panel = Ox.SplitPanel({
elements: [
{
element: self.$list,
size: self.options.size
},
{
element: self.$page
}
],
orientation: 'horizontal'
})
);
2012-06-22 07:23:38 +00:00
loadItems(function(items) {
var treeItems = [];
self.items = items;
2012-06-22 07:23:38 +00:00
items.forEach(function(item) {
var sectionIndex = Ox.getIndexById(treeItems, item.section + '/');
if (sectionIndex == -1) {
treeItems.push({
id: item.section + '/',
items: [],
title: item.sectionTitle
});
sectionIndex = treeItems.length - 1;
}
treeItems[sectionIndex].items.push(item);
});
self.$list = Ox.TreeList({
expanded: true,
2012-06-22 10:53:04 +00:00
icon: Ox.UI.getImageURL('symbolCenter'),
2012-06-22 07:23:38 +00:00
items: treeItems,
selected: self.options.selected ? [self.options.selected] : [],
width: self.options.size
})
.bindEvent({
select: function(data) {
2012-06-22 07:23:38 +00:00
if (!data.ids[0] || !Ox.endsWith(data.ids[0], '/')) {
selectItem(
data.ids[0] ? data.ids[0].split('/').pop() : ''
);
}
}
});
self.$panel.replaceElement(0, self.$list);
2012-04-16 07:13:22 +00:00
selectItem(self.options.selected);
that.triggerEvent('load', {});
});
2012-06-22 07:23:38 +00:00
function getItemByName(name) {
var item = null;
Ox.forEach(self.items, function(v) {
if (v.id.split('/').pop() == name) {
item = v;
Ox.Break();
}
});
return item;
}
function loadItems(callback) {
var items = [];
self.options.examples.forEach(function(example) {
var item = {
html: self.options.path + example + '/index.html',
id: example,
2012-06-22 07:23:38 +00:00
js: self.options.path + example + '/js/example.js',
section: example.split('/').shift()
};
Ox.get(item.html, function(html) {
2012-06-22 07:23:38 +00:00
var match = html.match(/<title>(.+)<\/title>/);
item.title = match ? match[1] : 'Untitled';
match = html.match(/<meta http-equiv="Keywords" content="(.+)"\/>/);
item.sectionTitle = match ? match[1] : 'Untitled';
Ox.get(item.js, function(js) {
var references = js.match(self.options.references);
item.references = references ? Ox.unique(references).sort(function(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a < b ? -1 : a > b ? 1 : 0;
}) : [];
items.push(item);
if (items.length == self.options.examples.length) {
callback(items.sort(sortById));
}
});
});
});
}
function selectItem(id) {
2012-06-22 07:23:38 +00:00
var item = id ? getItemByName(id) : null;
if (item) {
self.options.selected = id;
2012-06-22 07:23:38 +00:00
self.$list.options({selected: [item.section + '/' + id]});
self.$page = Ox.ExamplePage({
height: window.innerHeight,
html: item.html,
js: item.js,
references: item.references,
replaceCode: self.options.replaceCode,
replaceComment: self.options.replaceComment,
title: item.title,
width: window.innerWidth - self.options.size
})
.bindEvent({
close: function() {
selectItem();
}
});
self.$panel.replaceElement(1, self.$page);
} else {
self.options.selected = '';
self.$list.options({selected: []});
2012-04-16 07:13:22 +00:00
self.$page.empty().append(self.options.element);
}
that.triggerEvent('select', {id: self.options.selected});
}
function sortById(a, b) {
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
}
return that;
2012-05-21 10:38:18 +00:00
};