pandora/static/js/apiDialog.js

271 lines
9.6 KiB
JavaScript
Raw Normal View History

2013-03-04 07:26:01 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict';
pandora.ui.apiDialog = function() {
var selected = pandora.user.ui.part.api,
2013-03-04 12:00:00 +00:00
actions,
2013-03-04 07:26:01 +00:00
$panel, $list, $text,
that = Ox.Dialog({
buttons: [
2013-03-04 12:53:13 +00:00
Ox.Button({
id: 'switch',
2013-05-09 10:13:58 +00:00
title: Ox._('Help...')
2013-03-04 12:53:13 +00:00
}).bindEvent({
click: function() {
pandora.UI.set({page: 'help'});
2013-03-04 12:53:13 +00:00
}
}),
2013-03-04 15:22:26 +00:00
{},
2013-03-04 07:26:01 +00:00
Ox.Button({
id: 'close',
2013-05-09 10:13:58 +00:00
title: Ox._('Close')
2013-03-04 07:26:01 +00:00
}).bindEvent({
click: function() {
that.close();
}
})
],
closeButton: true,
2013-11-20 08:12:59 +00:00
content: Ox.LoadingScreen().start(),
2013-03-05 12:53:48 +00:00
height: 384,
2013-03-04 07:26:01 +00:00
keys: {escape: 'close'},
maximizeButton: true,
minHeight: 256,
2014-12-23 12:48:12 +00:00
minWidth: 544 + 2 + Ox.UI.SCROLLBAR_SIZE,
2013-03-04 12:00:00 +00:00
removeOnClose: true,
2013-05-09 10:13:58 +00:00
title: Ox._('API Documentation'),
2014-12-23 12:48:12 +00:00
width: 672 + 2 + Ox.UI.SCROLLBAR_SIZE
2013-03-04 07:26:01 +00:00
})
.bindEvent({
close: function() {
pandora.user.ui.page == 'api' && pandora.UI.set({page: ''});
2013-03-04 07:26:01 +00:00
},
resize: function() {
$list.size();
2013-03-04 12:00:00 +00:00
},
'pandora_part.api': function(data) {
2013-03-04 12:00:00 +00:00
pandora.user.ui.page == 'api' && that.select(data.value);
2013-03-04 07:26:01 +00:00
}
2013-03-07 11:14:10 +00:00
});
2013-03-04 07:26:01 +00:00
pandora.api.api({docs: true, code: true}, function(results) {
2013-03-04 12:00:00 +00:00
var items = [{
id: '',
2013-05-09 10:13:58 +00:00
title: Ox._('API Documentation'),
sort: 'aaa' // FIXME: what's this?
2013-03-04 12:00:00 +00:00
}];
2013-03-04 07:26:01 +00:00
actions = results.data.actions;
Ox.forEach(results.data.actions, function(v, k) {
items.push({
'id': k,
2013-03-04 12:00:00 +00:00
'title': k,
'sort': k
2013-03-04 07:26:01 +00:00
});
});
$list = Ox.TableList({
_tree: true,
columns: [
{
id: 'title',
visible: true,
2014-09-26 12:12:25 +00:00
width: 128 - Ox.UI.SCROLLBAR_SIZE
2013-03-04 07:26:01 +00:00
}
],
items: items,
max: 1,
min: 1,
scrollbarVisible: true,
2013-03-04 15:22:26 +00:00
selected: [selected],
2013-03-04 12:00:00 +00:00
sort: [{key: 'sort', operator: '+'}],
2013-03-04 07:26:01 +00:00
unique: 'id'
})
.bindEvent({
2013-03-04 12:00:00 +00:00
select: function(data) {
var id = data.ids[0];
pandora.UI.set({'part.api': id});
2013-03-04 12:00:00 +00:00
}
2013-03-04 07:26:01 +00:00
});
$text = Ox.Element()
.css({
padding: '16px',
2013-03-07 11:14:10 +00:00
lineHeight: '16px',
2013-03-04 07:26:01 +00:00
overflowY: 'auto'
2013-03-07 11:14:10 +00:00
});
2013-03-04 07:26:01 +00:00
$panel = Ox.SplitPanel({
elements: [
2013-03-07 11:14:10 +00:00
{element: $list, size: 128},
2013-03-04 07:26:01 +00:00
{element: $text}
],
orientation: 'horizontal'
});
2013-03-04 15:22:26 +00:00
that.select(selected).options({content: $panel});
$list.gainFocus();
2013-03-04 07:26:01 +00:00
});
2014-12-21 12:55:06 +00:00
function getDoc(id) {
2014-12-18 13:38:08 +00:00
/*
API Documentation format:
Description
takes {
key: value, // comment
key: value, // comment
...
}
returns {
key: value, // comment
key: value, // comment
...
}
notes: Notes
see: action, action, ...
*/
2014-12-21 12:55:06 +00:00
var string = actions[id].doc + '\nsource: ' + actions[id].code[0],
$doc = Ox.SyntaxHighlighter({
2014-12-18 15:57:06 +00:00
source: '\n' + string.replace(
2014-12-21 12:55:06 +00:00
/\n(?=(takes \{|returns \{|notes: |see: |source: ))/g,
'\n\n'
2014-12-18 14:25:02 +00:00
).replace(
2014-12-21 12:55:06 +00:00
/(takes|returns|notes|see|source)(?=( \{|: ))/g,
'BOLD$1BOLD'
2014-12-18 14:25:02 +00:00
).replace(
2014-12-21 12:55:06 +00:00
/`/g,
'BOLD'
2014-12-18 14:06:33 +00:00
)
})
.css({backgroundColor: 'rgba(0, 0, 0, 0)'}),
2014-12-18 14:25:02 +00:00
parts, parts_,
2014-12-18 15:54:50 +00:00
colon = '</span><span class="OxOperator">:</span>',
comma = '<span class="OxOperator">,</span>',
2014-12-18 15:42:05 +00:00
linebreak = '<span class="OxLinebreak"><br></span>',
whitespace = '<span class="OxWhitespace">&nbsp;</span>';
2014-12-18 13:38:08 +00:00
['Keyword', 'Method', 'Property'].forEach(function(type) {
2014-12-18 14:48:57 +00:00
$doc.find('.Ox' + type)
.removeClass('Ox' + type)
.addClass('OxIdentifier');
2014-12-18 13:38:08 +00:00
});
$doc.html(
2014-12-18 14:45:54 +00:00
$doc.html().replace(/BOLD(\w+)BOLD/g, '<b>$1</b>')
2014-12-18 13:38:08 +00:00
);
2014-12-18 15:54:50 +00:00
parts = $doc.html().split('<b>see</b>' + colon + whitespace);
2014-12-18 13:53:25 +00:00
if (parts.length == 2) {
parts_ = parts[1].split('<b>source</b>' + colon + whitespace);
parts_[0] = parts_[0]
.split(comma + whitespace)
.map(function(action) {
action = Ox.stripTags(action);
return '<span class="OxMethod"><a href="/api/'
+ action + '">' + action + '</a></span>';
2014-12-18 15:42:05 +00:00
})
.join(comma + whitespace);
parts[1] = parts_.join(
linebreak + linebreak + '<b>source</b>' + colon + whitespace
);
2014-12-18 15:54:50 +00:00
$doc.html(parts.join('<b>see</b>' + colon + whitespace));
2014-12-18 13:38:08 +00:00
}
pandora.createLinks($doc);
return $doc;
}
2013-03-07 11:14:10 +00:00
function getIndex() {
var $index = Ox.Element()
.html(Ox._(
2013-03-07 11:14:10 +00:00
'<h1><b>API Documentation</b></h1>\n'
+ '<p><b>{0}</b> uses a JSON API'
2013-03-07 11:14:10 +00:00
+ ' to communicate between the browser and the server.'
+ ' This API is 100% public, which means that there is'
+ ' virtually no limit to what you can do with the site,'
+ ' or build on top of it &mdash; from writing simple scripts'
+ ' to read or write specific information to including'
+ ' data from <b>{0}</b>'
2013-03-07 11:14:10 +00:00
+ ' (not just videos, but also metadata, annotations, lists,'
+ ' or a custom search interface) in your own website.</p>\n'
+ '<p>If you are using the API in JavaScript, you may want to'
+ ' take a look at <a href="https://oxjs.org/#doc/Ox.API">'
+ ' OxJS</a>, and if you are using Python, there is'
+ ' <a href="https://wiki.0x2620.org/wiki/python-ox">'
+ ' python-ox</a>, which is used by'
+ ' <a href="https://wiki.0x2620.org/wiki/pandora_client">'
+ ' pandora_client</a> to automate certain tasks.</p>\n'
+ '<p>To get started, just open the console and paste the'
+ ' following snippet. For the first ten items that are'
2014-12-18 19:26:22 +00:00
+ ' both shorter than one hour and whose title starts with'
+ ' either "a" or "the" (sorted by duration, then title, both'
2013-03-07 11:14:10 +00:00
+ ' in ascending order), it will return their duration,'
+ ' id and title properties.</p>',
[pandora.site.site.name]))
2013-03-07 11:14:10 +00:00
.append(
Ox.SyntaxHighlighter({
source: "pandora.api.find({\n"
+ " keys: ['duration', 'id', 'title'],\n"
+ " query: {\n"
+ " conditions: [\n"
+ " {key: 'duration', operator: '<', value: '01:00:00'},\n"
2014-12-18 19:26:22 +00:00
+ " {\n"
+ " conditions: [\n"
+ " {key: 'title', operator: '=', value: 'a*'}\n"
+ " {key: 'title', operator: '=', value: 'the*'}\n"
+ " ],\n"
+ " operator: '|'\n"
+ " }\n"
2013-03-07 11:14:10 +00:00
+ " ],\n"
+ " operator: '&'\n"
+ " },\n"
+ " range: [0, 10],\n"
+ " sort: [\n"
+ " {key: 'duration', operator: '+'},\n"
+ " {key: 'title', operator: '+'}\n"
+ " ]\n"
+ "}, function(result) {\n"
+ " console.log(\n"
+ " result.status.code == 200 ? result.data : result.status\n"
+ " );\n"
+ "});"
})
);
pandora.createLinks($index);
return $index;
}
2013-03-04 12:00:00 +00:00
that.select = function(id) {
2014-12-21 12:55:06 +00:00
var $code;
2013-03-04 15:22:26 +00:00
if (id && actions[id]) {
2014-12-18 15:42:05 +00:00
$text.empty();
2014-12-18 15:31:19 +00:00
$('<h1>')
.css({paddingLeft: '4px'})
2014-12-18 15:33:58 +00:00
.html('<tt><b>' + id + '</b></tt>')
2014-12-18 15:31:30 +00:00
.appendTo($text);
2014-12-21 12:55:06 +00:00
getDoc(id).appendTo($text);
2014-12-18 15:31:19 +00:00
$code = Ox.SyntaxHighlighter({
2014-12-21 12:55:06 +00:00
source: actions[id].code[1].replace(
/\s*?'''[\s\S]+?'''/g, ''
),
2014-12-18 15:31:19 +00:00
})
.css({
backgroundColor: 'rgba(0, 0, 0, 0)',
borderWidth: '1px',
})
.appendTo($text);
// fix decorators
$code.find('.OxError')
.removeClass('OxError')
.addClass('OxOperator');
2013-03-04 07:26:01 +00:00
} else {
2013-03-07 11:14:10 +00:00
$text.empty().append(getIndex());
2013-03-04 07:26:01 +00:00
}
$text.scrollTop(0);
2013-03-04 12:00:00 +00:00
return that;
2013-03-04 07:26:01 +00:00
}
2013-03-04 12:00:00 +00:00
2013-03-04 07:26:01 +00:00
return that;
};