2012-04-04 14:59:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.ExamplePanel <f> Example Panel
|
2012-07-04 11:29:18 +00:00
|
|
|
options <o> Options
|
|
|
|
self <o> Shared private variable
|
2012-05-31 10:32:54 +00:00
|
|
|
([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
|
|
|
@*/
|
|
|
|
|
2012-04-04 14:59:58 +00:00
|
|
|
Ox.ExamplePanel = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
|
|
|
.defaults({
|
2012-04-09 08:42:00 +00:00
|
|
|
element: '',
|
2012-04-04 14:59:58 +00:00
|
|
|
examples: [],
|
|
|
|
path: '',
|
2012-06-12 12:31:45 +00:00
|
|
|
references: null,
|
2012-04-06 12:10:21 +00:00
|
|
|
replaceCode: [],
|
|
|
|
replaceComment: [],
|
2012-04-08 18:20:58 +00:00
|
|
|
selected: '',
|
2012-04-04 16:26:17 +00:00
|
|
|
size: 256
|
2012-04-04 14:59:58 +00:00
|
|
|
})
|
|
|
|
.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
|
|
|
}
|
|
|
|
});
|
2012-04-04 14:59:58 +00:00
|
|
|
|
|
|
|
self.$list = Ox.Element();
|
2012-04-16 07:13:22 +00:00
|
|
|
self.$page = Ox.Element();
|
2012-04-04 14:59:58 +00:00
|
|
|
|
|
|
|
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 = [];
|
2012-04-04 14:59:58 +00:00
|
|
|
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
|
2012-04-04 14:59:58 +00:00
|
|
|
})
|
|
|
|
.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() : ''
|
|
|
|
);
|
|
|
|
}
|
2012-04-04 14:59:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
self.$panel.replaceElement(0, self.$list);
|
2012-04-16 07:13:22 +00:00
|
|
|
selectItem(self.options.selected);
|
2012-04-04 14:59:58 +00:00
|
|
|
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;
|
2012-07-05 08:58:08 +00:00
|
|
|
return false; // break
|
2012-06-22 07:23:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadItems(callback) {
|
2012-04-04 14:59:58 +00:00
|
|
|
var items = [];
|
|
|
|
self.options.examples.forEach(function(example) {
|
2012-04-04 16:26:17 +00:00
|
|
|
var item = {
|
2012-06-12 12:31:45 +00:00
|
|
|
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()
|
2012-06-12 12:31:45 +00:00
|
|
|
};
|
2012-04-04 16:26:17 +00:00
|
|
|
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';
|
2012-04-04 16:26:17 +00:00
|
|
|
Ox.get(item.js, function(js) {
|
2012-06-12 12:31:45 +00:00
|
|
|
var references = js.match(self.options.references);
|
|
|
|
item.references = references ? Ox.unique(references).sort(function(a, b) {
|
2012-04-04 16:26:17 +00:00
|
|
|
a = a.toLowerCase();
|
|
|
|
b = b.toLowerCase();
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
}) : [];
|
|
|
|
items.push(item);
|
2012-06-22 07:54:27 +00:00
|
|
|
if (items.length == self.options.examples.length) {
|
|
|
|
callback(items.sort(sortById));
|
|
|
|
}
|
2012-04-04 14:59:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-08 18:20:58 +00:00
|
|
|
function selectItem(id) {
|
2012-06-22 07:23:38 +00:00
|
|
|
var item = id ? getItemByName(id) : null;
|
2012-06-20 09:03:37 +00:00
|
|
|
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);
|
2012-04-04 16:26:17 +00:00
|
|
|
} else {
|
2012-06-20 09:03:37 +00:00
|
|
|
self.options.selected = '';
|
2012-06-18 08:57:52 +00:00
|
|
|
self.$list.options({selected: []});
|
2012-04-16 07:13:22 +00:00
|
|
|
self.$page.empty().append(self.options.element);
|
2012-04-04 16:26:17 +00:00
|
|
|
}
|
2012-06-20 09:03:37 +00:00
|
|
|
that.triggerEvent('select', {id: self.options.selected});
|
2012-04-04 14:59:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 07:54:27 +00:00
|
|
|
function sortById(a, b) {
|
|
|
|
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 14:59:58 +00:00
|
|
|
return that;
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
};
|