oxjs/source/Ox.UI/js/Core/Ox.DocPage.js

176 lines
7.6 KiB
JavaScript
Raw Normal View History

2011-05-16 08:24:46 +00:00
/*@
Ox.DocPage <f> DocPage
() -> <o> DocPage object
(options) -> <o> DocPage object
(options, self) -> <o> DocPage object
options <o> Options object
2011-05-16 10:49:48 +00:00
item <o> doc item
2011-05-16 08:24:46 +00:00
self <o> Shared private variable
@*/
2011-05-06 17:40:26 +00:00
Ox.DocPage = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
item: {}
2011-05-06 17:40:26 +00:00
})
.options(options || {})
2011-05-12 03:29:35 +00:00
.addClass('OxDocPage OxDocument')
2011-05-06 17:40:26 +00:00
.css({
overflow: 'auto'
2011-05-06 17:40:26 +00:00
});
that.append(
$('<h1>')
.css({
marginTop: 0,
WebkitMarginBefore: 0
})
.html('<code>' + self.options.item.name + '</code>')
);
2011-05-06 17:40:26 +00:00
getItem(self.options.item, 0).forEach(function($element) {
2011-05-06 17:40:26 +00:00
that.append($element);
});
2011-05-06 17:40:26 +00:00
function getItem(item, level, name) {
Ox.print('getItem', item, level, name)
2011-05-06 17:40:26 +00:00
var $elements = [$('<div>')
.css({paddingLeft: (level ? level * 32 - 16 : 0) + 'px'})
2011-05-06 17:40:26 +00:00
.html(
2011-11-03 15:42:41 +00:00
'<code><b>' + (name || item.name) + '</b> '
+ '&lt;' + item.types.join('&gt;</code> or <code>&lt;') + '&gt; </code>'
+ (item['default'] ? '(default: <code>' + item['default'] + '</code>) ' : '')
+ Ox.parseHTML(item.summary)
2011-05-06 17:40:26 +00:00
)
];
[
'description', 'usage', 'arguments', 'properties',
'events', 'examples', 'source'
2011-05-06 17:40:26 +00:00
].forEach(function(section) {
var className = 'OxLine' + Ox.uid();
if (item[section]) {
if (section == 'description') {
$elements.push($('<div>')
.css({
paddingTop: (level ? 0 : 8) + 'px',
2011-05-11 12:46:28 +00:00
borderTopWidth: level ? 0 : '1px',
marginTop: (level ? 0 : 8) + 'px',
marginLeft: (level * 32) + 'px',
})
.html(Ox.parseHTML(item.description))
2011-05-06 17:40:26 +00:00
);
} else {
$elements.push($('<div>')
.css({
paddingTop: (level ? 0 : 8) + 'px',
2011-05-11 12:46:28 +00:00
borderTopWidth: level ? 0 : '1px',
marginTop: (level ? 0 : 8) + 'px',
marginLeft: (level * 32) + 'px',
})
2011-05-06 17:40:26 +00:00
.append(
$('<img>')
2011-08-09 17:00:39 +00:00
.attr({src: Ox.UI.getImageURL('symbolDown')})
2011-05-06 17:40:26 +00:00
.css({
width: '12px',
height: '12px',
margin: '0 4px -1px 0'
})
.click(function() {
var $this = $(this),
2011-08-09 17:00:39 +00:00
isExpanded = $this.attr('src') == Ox.UI.getImageURL('symbolDown');
2011-05-06 17:40:26 +00:00
$this.attr({
src: isExpanded ?
2011-08-09 17:00:39 +00:00
Ox.UI.getImageURL('symbolRight') :
Ox.UI.getImageURL('symbolDown')
2011-05-06 17:40:26 +00:00
});
$('.' + className).each(function() {
var $this = $(this);
$this[isExpanded ? 'addClass' : 'removeClass'](className + 'Hidden');
if (isExpanded) {
$this.hide();
} else {
var hidden = false;
Ox.forEach(this.className.split(' '), function(v) {
if (/Hidden$/.test(v)) {
hidden = true;
return false;
}
});
if (!hidden) {
$this.show()
}
}
});
})
)
.append('<b>' + Ox.toTitleCase(section) + '</b>')
);
if (section == 'examples') {
item.examples.forEach(function(example) {
$elements.push($('<div>')
.addClass(className)
.css({marginLeft: (level * 32 + 16) + 'px'})
2011-05-06 17:40:26 +00:00
.html(
2011-11-03 15:42:41 +00:00
'<code><b>&gt;&nbsp;'
+ Ox.encodeHTML(example.statement)
.replace(/ /g, '&nbsp;')
.replace(/\n/g, '<br/>\n&nbsp;&nbsp;')
+ '</b></code>'
2011-05-06 17:40:26 +00:00
)
);
example.result && $elements.push($('<div>')
.addClass(className)
.css({marginLeft: (level * 32 + 16) + 'px'})
2011-05-06 17:40:26 +00:00
.html(
'<code>' + Ox.encodeHTML(example.result) + '</code>'
2011-11-03 15:42:41 +00:00
)
2011-05-06 17:40:26 +00:00
)
});
} else if (section == 'source') {
// fixme: not the right place to fix path
2011-05-06 17:40:26 +00:00
$elements.push($('<div>')
.addClass(className)
.css({marginLeft: 16 + 'px'})
.html(
2011-11-03 15:42:41 +00:00
'<code><b>' + self.options.item.file.replace(Ox.PATH, '')
+ '</b> line ' + self.options.item.line + '</code>'
)
);
$elements.push(
Ox.SyntaxHighlighter({
showLineNumbers: true,
// fixme: silly
source: item.source.map(function(token) {
return token.source;
}).join(''),
offset: self.options.item.line
})
.addClass(className)
.css({
2011-05-11 12:46:28 +00:00
borderWidth: '1px',
marginTop: '8px',
})
2011-05-06 17:40:26 +00:00
);
} else {
item[section].forEach(function(v) {
var name = section == 'usage' ?
item.name + v.name + ' </b></code>returns<code> <b>' : null;
2011-05-06 17:40:26 +00:00
$elements = Ox.merge(
$elements,
Ox.map(getItem(v, level + 1, name), function($element) {
2011-05-06 17:40:26 +00:00
return $element.addClass(className);
})
);
});
}
}
}
})
return $elements;
}
return that;
2011-05-16 08:24:46 +00:00
};