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

182 lines
5.9 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 <o> Options
self <o> Shared private variable
2012-05-31 10:32:54 +00:00
([options[, self]]) -> <o:Ox.SplitPanel> Example Panel
change <!> Change event
value <s> 'source' or 'live'
load <!> Load event
select <!> Select event
2012-06-17 22:38:26 +00:00
id <s> selected example
2012-05-21 10:38:18 +00:00
@*/
Ox.ExamplePanel = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
2012-04-09 08:42:00 +00:00
element: '',
examples: [],
mode: 'source',
path: '',
references: null,
replaceCode: [],
replaceComment: [],
selected: '',
size: 256
})
.options(options || {})
2012-05-28 19:35:41 +00:00
.update({
mode: function() {
if (self.options.selected) {
self.$page.options({selected: self.options.mode});
}
},
2012-05-28 19:35:41 +00:00
selected: function() {
self.options.mode = 'source';
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,
2014-09-26 12:31:20 +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], '/')) {
self.options.mode = 'source';
2012-06-22 07:23:38 +00:00
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', {items: self.items});
});
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) {
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="(.+)"\/>/);
2013-05-09 13:03:33 +00:00
item.sectionTitle = match ? match[1] : Ox._('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) {
var item = id ? getItemByName(id) : null,
selected = self.options.selected;
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,
selected: self.options.mode,
2012-06-22 07:23:38 +00:00
title: item.title,
width: window.innerWidth - self.options.size
})
.bindEvent({
change: function(data) {
that.triggerEvent('change', data);
},
2012-06-22 07:23:38 +00:00
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);
}
if (self.options.selected != selected) {
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
};