load comments from source

This commit is contained in:
j 2010-12-06 18:40:27 +01:00
parent d0b4727f72
commit 24af1f65d4

View file

@ -7,6 +7,25 @@ $(function() {
app.$document = $(document); app.$document = $(document);
app.$window = $(window); app.$window = $(window);
app.docstrings = {};
$("<div>").load('../build/js/ox.ui.js', function(data) {
app.source = data;
var docstrings = app.source.match(/\/\*\*\n([\s\S]+?\*\/\n.*?)\n/gm)
docstrings.forEach(function(doc) {
doc = Ox.trim(doc);
var name = doc.match(/Ox\.(.+) = function/);
if(name) {
doc = doc.split('\n');
doc.splice(0, 1);
doc.pop();
doc.pop();
//remove whitespace
var offset = /^\s+/.exec(doc[0]);
if(offset)
doc = Ox.map(doc, function(line) { return line.substring(offset[0].length)});
app.docstrings[name[1]] = Ox.trim(doc.join('\n'));
}
})
app.$ui = {}; app.$ui = {};
app.docs = getDocsJSON(); app.docs = getDocsJSON();
app.$ui.actionList = constructList(); app.$ui.actionList = constructList();
@ -27,6 +46,7 @@ $(function() {
}); });
$main.appendTo(app.$body); $main.appendTo(app.$body);
});
}); });
function constructList() { function constructList() {
@ -48,8 +68,14 @@ function constructList() {
request: function(data, callback) { request: function(data, callback) {
var items = []; var items = [];
app.docs.forEach(function(v) { app.docs.forEach(function(v) {
items.push({'name': v.name}); items.push(v.name);
}); });
Ox.keys(app.docstrings).forEach(function(v) {
if(!$.inArray(v.name, items)) {
items.push(v.name);
}
});
items = Ox.map(items, function(i) { return {name: i}});
items.sort(function(a, b) { if(a.name < b.name) { return -1 } else if( a.name == b.name) { return 0 }else { return 1 } }) items.sort(function(a, b) { if(a.name < b.name) { return -1 } else if( a.name == b.name) { return 0 }else { return 1 } })
if(!data.keys) { if(!data.keys) {
var result = {'data': {'items': items.length}}; var result = {'data': {'items': items.length}};
@ -80,35 +106,53 @@ function constructList() {
} }
function getDocHtml(doc) { function getDocHtml(doc) {
var wrapper = $('<div>'); function cell(content) {
var title = $('<h2>').html(doc.name).appendTo(wrapper); return $('<td>').css({'padding-right': '4px'}).html(content);
var $options = $('<div>').html("Options: ").css({'marginBottom': '20px'}).appendTo(wrapper); }
var $div = $('<div>');
var title = $('<h2>').html('Ox.'+doc.name)
.appendTo($div);
if(app.docstrings[doc.name])
$('<pre>').html(app.docstrings[doc.name]).appendTo($div);
var $options = $('<div>').html("Options: ")
.css({'marginBottom': '20px'})
.appendTo($div);
var $table = $('<table>').attr({'cellpadding': '4'})
.appendTo($options);
Ox.keys(doc.options).forEach(function(k) { Ox.keys(doc.options).forEach(function(k) {
var $option = $('<div>'); var $option = $('<tr>');
var optionKey = $('<b>').html(k + ": ").appendTo($option); cell(k).appendTo($option);
var optionVal = $('<span>').html(typeof doc.options[k]).appendTo($option); if(typeof(doc.options[k]) != 'undefined') {
$('<span>').html(' <b>' + doc.options[k].toString() + '</b>').appendTo($option); cell(typeof doc.options[k]).appendTo($option);
if(Ox.isNull(doc.options[k]))
cell(' <b>null</b>').appendTo($option);
else
cell(' <b>' + doc.options[k].toString() + '</b>').appendTo($option);
} else {
cell('&nbsp;').appendTo($option);
cell('<b>required, no default value</b>').appendTo($option);
}
$option.appendTo($options); $option.appendTo($options);
}); });
var methods = Ox.keys(doc.methods); var methods = Ox.keys(doc.methods);
if (methods.length > 0) { if (methods.length > 0) {
var $methods = $('<div>').html("Methods: <br>").appendTo(wrapper); var $methods = $('<div>').html("Methods:").appendTo($div);
methods.forEach(function(m) { methods.forEach(function(m) {
var $m = $('<div>').appendTo($methods); var $method = $('<div>').appendTo($methods);
var f = $('<pre>').html(doc.methods[m]).hide(); var $code = $('<pre>').html(doc.methods[m]).hide();
var title = m;
var options = /function \((.*?)\)/.exec(doc.methods[m]); var options = /function \((.*?)\)/.exec(doc.methods[m]);
if(options) {
title = title + '('+options[1]+')';
}
var lable = new Ox.Label({ var lable = new Ox.Label({
title: title, title: m + '('+options[1]+')',
width: 190 width: 190
}) })
.css({'float': 'left', 'margin-right': '4px'}) .css({'float': 'left', 'margin-right': '4px'})
.addClass("margin") .addClass("margin")
.appendTo($m); .appendTo($method);
var b = new Ox.Button({ var $button = new Ox.Button({
title: [ title: [
{id: "one", title: "expand"}, {id: "one", title: "expand"},
{id: "two", title: "collapse"}, {id: "two", title: "collapse"},
@ -116,13 +160,27 @@ function getDocHtml(doc) {
type: "image" type: "image"
}) })
.addClass("margin") .addClass("margin")
.appendTo($m) .appendTo($method)
.click(function() { f.toggle()}); .click(function() { $code.toggle()});
f.appendTo($m); $code.appendTo($method);
}); });
} }
var $functionString = $('<pre>').html(doc.functionString).appendTo(wrapper);
return wrapper; var $methodcode = $('<pre>').html(doc.functionString.replace(/</g, '&lt;'))
.hide();
var $button = new Ox.Button({
title: [
{id: "one", title: "expand"},
{id: "two", title: "collapse"},
],
type: "image"
})
.addClass("margin")
.click(function() { $methodcode.toggle()})
.appendTo($div)
$('<span>').html(' View Source').appendTo($div)
$methodcode.appendTo($div);
return $div;
} }
function getObjectByName(obj, name) { function getObjectByName(obj, name) {