pandora/static/js/entitiesDialog.js

562 lines
18 KiB
JavaScript
Raw Permalink Normal View History

2014-11-19 16:04:45 +00:00
'use strict';
2016-04-25 16:52:58 +00:00
(function() {
// cribbed from documentsPanel.js, TODO: refactor
var entitiesInput = function(width, defaultType, max) {
var labelWidth = 80;
if (!defaultType) {
defaultType = pandora.site.entities[0].id;
}
return Ox.ArrayInput({
input: {
get: function(width) {
var $input = Ox.FormElementGroup({
elements: [
Ox.Select({
items: pandora.site.entities.map(function(entity) {
return {
id: entity.id,
title: entity.title
};
}),
overlap: 'right',
width: labelWidth
})
.bindEvent({
change: function() {
var v = $input.value();
$input.value({type: v.type, name: ''});
$input.options('elements')[1].focusInput();
}
}),
Ox.Input({
autocomplete: function(value, callback) {
pandora.api.autocompleteEntities({
key: $input.value().type,
operator: '=',
range: [0, 10],
value: value
}, function(result) {
callback(result.data.items);
});
},
autocompleteReplace: true,
autocompleteSelect: true,
autocompleteSelectSubmit: true,
width: width - labelWidth
})
],
width: width,
split: function(value) {
return [value.type || defaultType, value.name || ''];
},
join: function(value) {
return {type: value[0], name: value[1]};
}
});
return $input;
},
getEmpty: function(value) {
var type = (value && value[0]) || defaultType;
return {type: type, name: ''};
},
isEmpty: function(value) {
return !value.name;
},
setWidth: function($input, width) {
$input.options('elements')[1].options({
width: width - labelWidth
});
}
},
width: width,
max: max || 0,
});
}
2014-11-19 16:04:45 +00:00
pandora.ui.entitiesDialog = function(options) {
var dialogHeight = Math.round((window.innerHeight - 48) * 0.9),
dialogWidth = Math.round(window.innerWidth * 0.9),
2014-11-20 20:26:59 +00:00
ui = pandora.user.ui,
type = ui.entitiesType || (
pandora.site.entities.length ? pandora.site.entities[0].id : ''
),
2014-12-17 16:30:51 +00:00
selected = ui.entitiesSelection[type] || [],
2014-11-20 20:26:59 +00:00
2014-11-19 17:55:43 +00:00
$entitiesSelect = Ox.Select({
items: pandora.site.entities.map(function(type) {
return {
id: type.id,
title: type.title
};
}),
2014-12-16 17:33:43 +00:00
value: type,
2014-11-19 18:37:13 +00:00
width: 122
2014-11-19 16:04:45 +00:00
})
.bindEvent({
2014-11-19 17:55:43 +00:00
change: function(data) {
2014-12-16 15:52:59 +00:00
type = data.value;
pandora.UI.set({entitiesType: type});
$list.reloadList();
selectEntities();
2014-11-19 17:55:43 +00:00
}
})
.css({
2014-11-19 18:37:13 +00:00
float: 'left',
margin: '4px 2px 4px 4px'
2014-11-19 17:55:43 +00:00
}),
$findInput = Ox.Input({
2014-11-20 18:54:04 +00:00
changeOnKeypress: true,
2014-11-19 17:55:43 +00:00
clear: true,
placeholder: 'Find',
2014-11-19 18:37:13 +00:00
width: 122
2014-11-19 17:55:43 +00:00
})
.css({
2014-11-19 19:21:59 +00:00
float: 'right',
2014-11-19 18:37:13 +00:00
margin: '4px 4px 4px 2px'
2014-11-19 17:55:43 +00:00
})
.bindEvent({
2014-11-19 18:11:05 +00:00
change: function(data) {
$list.options({
query: {
2014-11-20 16:13:08 +00:00
conditions: data.value ? [
{key: '*', operator: '=', value: data.value}
2014-11-20 16:13:08 +00:00
] : [],
2014-11-19 18:11:05 +00:00
operator: '&'
}
});
2014-11-19 17:55:43 +00:00
}
2014-11-19 16:04:45 +00:00
}),
2014-11-19 17:55:43 +00:00
$listBar = Ox.Bar({size: 24})
2014-11-19 18:37:13 +00:00
.append($entitiesSelect)
2014-11-19 17:55:43 +00:00
.append($findInput),
$list = Ox.TableList({
columns: [
2014-11-20 10:03:22 +00:00
{
id: 'id',
title: 'ID',
operator: '+'
},
{
id: 'name',
title: 'Name',
operator: '+',
visible: true,
width: 256 - Ox.SCROLLBAR_SIZE
}
2014-11-19 17:55:43 +00:00
],
items: function(options, callback) {
2014-11-20 18:54:04 +00:00
pandora.api.findEntities(Ox.extend(Ox.clone(options), {
2014-11-19 17:55:43 +00:00
query: {
conditions: [
{key: 'type', operator: '==', value: type}
].concat(options.query.conditions),
operator: '&'
2014-11-20 18:54:04 +00:00
}
}), callback);
2014-11-19 17:55:43 +00:00
},
2014-12-16 17:55:20 +00:00
sort: [{key: 'name', operator: '+'}],
2014-11-19 17:55:43 +00:00
scrollbarVisible: true,
2014-12-17 16:18:22 +00:00
selected: selected,
2014-11-19 17:55:43 +00:00
unique: 'id',
2014-11-19 18:05:08 +00:00
width: 256 - Ox.SCROLLBAR_SIZE
})
.bindEvent({
add: function() {
addEntity();
},
'delete': function() {
deleteEntities();
},
2014-11-19 18:05:08 +00:00
init: function(data) {
2014-11-19 18:31:06 +00:00
var text = Ox.formatCount(
data.items,
Ox._('entity'),
Ox._('entities')
)
$listStatus.html(text[0].toUpperCase() + text.slice(1));
2014-11-20 10:03:22 +00:00
},
select: function(data) {
2014-11-20 19:49:05 +00:00
pandora.UI.set('entitiesSelection.' + type, data.ids);
2014-11-19 18:05:08 +00:00
}
2014-11-19 17:55:43 +00:00
}),
2014-11-19 16:04:45 +00:00
2014-11-19 17:55:43 +00:00
$listStatus = Ox.Element()
.css({
fontSize: '9px',
marginTop: '2px',
textAlign: 'center'
})
.html(Ox._('Loading...')),
2014-11-19 16:04:45 +00:00
2014-11-19 17:55:43 +00:00
$listStatusbar = Ox.Bar({size: 16})
.append($listStatus),
2014-11-19 16:04:45 +00:00
$listPanel = Ox.SplitPanel({
elements: [
2014-11-19 17:58:08 +00:00
{element: $listBar, size: 24},
2014-11-19 17:55:43 +00:00
{element: $list},
{element: $listStatusbar, size: 16}
2014-11-19 16:04:45 +00:00
],
orientation: 'vertical'
2014-11-19 19:21:59 +00:00
})
.bindEvent({
2014-11-20 15:39:33 +00:00
resize: updateList
2014-11-19 16:04:45 +00:00
}),
$entity = Ox.Element()
.css({overflowY: 'auto'}),
2014-11-19 16:04:45 +00:00
2014-11-19 17:55:43 +00:00
$itemMenu = Ox.MenuButton({
items: [
2014-11-19 18:37:13 +00:00
{
id: 'add',
2014-11-19 18:37:13 +00:00
title: Ox._('Add Entity'),
keyboard: 'control n'
},
{
id: 'delete',
2014-11-19 18:37:13 +00:00
title: Ox._('Delete Entity...'),
disabled: true,
keyboard: 'delete'
}
2014-11-19 17:55:43 +00:00
],
title: 'set',
tooltip: Ox._('Options'),
type: 'image'
})
.css({
2014-11-19 18:05:08 +00:00
float: 'left',
2014-11-19 17:55:43 +00:00
margin: '4px'
})
.bindEvent({
click: function(data) {
if (data.id == 'add') {
addEntity();
2014-11-19 17:55:43 +00:00
} else if (data.id == 'delete') {
2014-11-20 21:33:21 +00:00
deleteEntities();
2014-11-19 17:55:43 +00:00
}
}
2014-11-19 18:05:08 +00:00
}),
2014-11-19 17:55:43 +00:00
2014-11-19 18:31:06 +00:00
$deselectButton = Ox.Button({
2014-11-19 18:55:32 +00:00
title: 'close',
2014-11-19 18:31:06 +00:00
tooltip: Ox._('Done'),
type: 'image'
})
.css({
float: 'right',
margin: '4px'
})
.hide()
.bindEvent({
click: function() {
2014-11-20 19:49:05 +00:00
pandora.UI.set('entitiesSelection.' + type, []);
2014-11-19 18:31:06 +00:00
}
2014-11-19 18:55:32 +00:00
}),
2014-11-19 18:31:06 +00:00
2014-11-19 17:55:43 +00:00
$itemBar = Ox.Bar({size: 24})
2014-11-19 18:31:06 +00:00
.append($itemMenu)
.append($deselectButton),
2014-11-19 17:55:43 +00:00
2014-11-20 10:03:22 +00:00
$labels = [],
$inputs = [],
2014-11-20 16:13:08 +00:00
$form = Ox.Element()
.css({
overflowY: 'scroll',
2014-11-20 18:30:57 +00:00
padding: '4px'
2014-11-20 16:13:08 +00:00
}),
2014-11-20 10:03:22 +00:00
2014-11-19 17:55:43 +00:00
$itemStatus = Ox.Element()
.css({
fontSize: '9px',
marginTop: '2px',
textAlign: 'center'
}),
2014-11-19 17:55:43 +00:00
$itemStatusbar = Ox.Bar({size: 16})
.append($itemStatus),
2014-11-19 16:04:45 +00:00
2014-11-19 17:55:43 +00:00
$itemPanel = Ox.SplitPanel({
2014-11-19 16:04:45 +00:00
elements: [
2014-11-19 17:55:43 +00:00
{element: $itemBar, size: 24},
2014-11-20 10:03:22 +00:00
{element: $form},
2014-11-19 17:55:43 +00:00
{element: $itemStatusbar, size: 16}
2014-11-19 16:04:45 +00:00
],
orientation: 'vertical'
2014-11-19 19:21:59 +00:00
})
.bindEvent({
2014-11-20 15:39:33 +00:00
resize: updateForm
2014-11-19 16:04:45 +00:00
}),
$content = pandora.site.entities.length ? Ox.SplitPanel({
2014-11-19 16:04:45 +00:00
elements: [
{
2014-11-19 18:37:13 +00:00
element: $listPanel,
2014-11-19 16:04:45 +00:00
resizable: true,
resize: [256, 384, 512],
size: 256
},
{
element: $entity
},
{
2014-11-19 17:55:43 +00:00
element: $itemPanel,
2014-11-19 16:04:45 +00:00
resizable: true,
resize: [256, 384, 512],
size: 256
}
],
orientation: 'horizontal'
}) : Ox.Element()
.css({
fontSize: '12px',
paddingTop: '32px',
textAlign: 'center'
})
2014-12-16 17:07:21 +00:00
.html(Ox._('No entities defined.')),
2014-11-19 16:04:45 +00:00
2014-11-19 17:55:43 +00:00
that = Ox.Dialog({
buttons: [
Ox.Button({
title: Ox._('Manage Documents...')
})
.bindEvent({
click: function() {
that.close();
(pandora.$ui.documentsDialog || (
pandora.$ui.documentsDialog = pandora.ui.documentsDialog()
)).open();
}
}),
{},
Ox.Button({
title: Ox._('Done'),
width: 48
})
.bindEvent({
click: function() {
that.close();
}
})
],
closeButton: true,
content: $content,
height: dialogHeight,
maximizeButton: true,
minHeight: 256,
minWidth: 512,
padding: 0,
removeOnClose: true,
title: Ox._('Manage Entities'),
width: dialogWidth
})
.bindEvent({
// resize: ...
2014-11-20 19:49:05 +00:00
pandora_entitiesselection: function() {
selectEntities();
},
pandora_entitiestype: function() {
type = ui.entitiesType || (
pandora.site.entities.length ? pandora.site.entities[0].id : ''
);
$entitiesSelect.options({value: type});
$list.reloadList();
selectEntities();
2014-11-20 19:49:05 +00:00
}
2014-11-19 17:55:43 +00:00
});
2014-11-19 16:04:45 +00:00
2014-12-22 22:21:59 +00:00
that.superClose = that.close;
that.close = function() {
Ox.Request.clearCache('findEntities');
that.superClose();
2014-12-22 22:26:40 +00:00
delete pandora.$ui.entitiesDialog;
2014-12-22 22:21:59 +00:00
};
selectEntities();
function addEntity() {
pandora.api.addEntity({
type: type
}, function(result) {
Ox.Request.clearCache('findEntities');
$list.reloadList();
2014-12-01 16:44:07 +00:00
pandora.UI.set('entitiesSelection.' + type, [result.data.id]);
});
}
2014-11-20 13:38:28 +00:00
function deleteEntities() {
pandora.ui.deleteEntityDialog(
$list.options('selected').map(function(id) {
return $list.value(id);
}),
function() {
Ox.Request.clearCache();
$list.reloadList();
pandora.UI.set('entitiesSelection.' + type, []);
2014-11-20 13:38:28 +00:00
}
).open();
}
2014-11-20 10:03:22 +00:00
function renderEntity() {
2014-11-20 20:39:38 +00:00
var id = ui.entitiesSelection[type]
? ui.entitiesSelection[type][0]
2014-11-20 20:31:41 +00:00
: null;
2014-11-20 10:03:22 +00:00
if (!id) {
$entity.empty();
return;
}
2014-11-21 14:51:11 +00:00
pandora.entity({
2014-11-20 10:03:22 +00:00
id: id,
type: type,
view: 'entity'
}, function(html) {
2014-11-20 20:39:38 +00:00
if (id != ui.entitiesSelection[type][0]) {
2014-11-20 10:03:22 +00:00
return;
}
$entity.html(html);
2014-11-20 20:54:22 +00:00
pandora.createLinks($entity);
2014-11-20 10:03:22 +00:00
});
}
function renderForm() {
2014-11-20 20:35:47 +00:00
var id = ui.entitiesSelection[type]
2014-12-15 19:49:01 +00:00
? ui.entitiesSelection[type][0]
: null,
width = $content.options('elements')[2].size
- 16 - Ox.SCROLLBAR_SIZE;
2014-11-20 10:03:22 +00:00
if (!id) {
$form.empty();
return;
}
pandora.api.getEntity({
id: id
2014-11-20 16:58:22 +00:00
}, function(result) {
2014-11-20 20:37:27 +00:00
if (id != ui.entitiesSelection[type][0]) {
2014-11-20 10:03:22 +00:00
return;
}
var keys = Ox.getObjectById(pandora.site.entities, type).keys;
$form.empty()
2014-11-20 18:30:57 +00:00
keys.forEach(function(key, index) {
2014-12-01 16:44:07 +00:00
var defaultValue = void 0,
2016-04-25 16:52:58 +00:00
value = result.data[key.id],
2014-12-01 16:44:07 +00:00
$label = Ox.Label({
2014-11-20 10:03:22 +00:00
title: Ox._(key.title),
2014-12-15 19:49:01 +00:00
width: width
2014-11-20 18:30:57 +00:00
})
.css({
2014-11-20 18:34:50 +00:00
margin: (index == 0 ? 4 : 16) + 'px 4px 8px 4px'
2014-11-20 10:03:22 +00:00
})
.appendTo($form),
$input;
2014-11-20 17:13:49 +00:00
if (key.type === 'document') {
2014-11-20 16:31:25 +00:00
$input = Ox.Input({autovalidate: /^[A-Z]+?$/});
2014-11-20 17:13:49 +00:00
} else if (key.type === 'float') {
2014-11-20 10:03:22 +00:00
$input = Ox.Input({type: 'float'});
2014-11-20 17:13:49 +00:00
} else if (key.type === 'int') {
2014-11-20 10:03:22 +00:00
$input = Ox.Input({type: 'int'});
2014-11-20 17:13:49 +00:00
} else if (key.type === 'string') {
2014-11-20 10:03:22 +00:00
$input = Ox.Input();
2014-11-20 17:13:49 +00:00
} else if (key.type[0] === 'string') {
2014-11-20 10:03:22 +00:00
$input = Ox.ArrayInput();
2014-12-01 16:44:07 +00:00
defaultValue = [];
2014-11-20 17:13:49 +00:00
} else if (key.type === 'text') {
2014-11-20 16:13:08 +00:00
$input = Ox.Input({
2014-12-15 19:49:01 +00:00
height: width,
2014-11-20 16:13:08 +00:00
type: 'textarea'
});
2016-04-25 16:52:58 +00:00
} else if (key.type[0] === 'entity') {
$input = entitiesInput(width, type, key.max || 0);
defaultValue = [];
$input.bindEvent({
submit: function(data) {
$input.triggerEvent('change', data);
}
})
2014-11-20 10:03:22 +00:00
}
2016-04-25 16:52:58 +00:00
var change = function(data, eventName) {
console.log(eventName, data);
pandora.api.editEntity(Ox.extend({
id: id
}, key.id, data.value), function(result) {
Ox.Request.clearCache('findEntities');
Ox.Request.clearCache('getEntity');
if (key.id == 'name') {
$input.value(result.data.name);
$list.reloadList(true);
} else if (key.id == 'alternativeNames') {
$input.value(result.data.alternativeNames);
}
renderEntity();
});
};
2014-11-20 10:03:22 +00:00
$input.options({
2014-11-20 16:31:25 +00:00
disabled: key.id == 'id',
2016-04-25 16:52:58 +00:00
value: value || defaultValue,
2014-12-15 19:49:01 +00:00
width: width
2014-11-20 10:03:22 +00:00
})
2014-11-20 18:30:57 +00:00
.css({margin: '4px'})
2014-11-20 10:03:22 +00:00
.bindEvent({
2016-04-25 16:52:58 +00:00
change: change,
2014-11-20 15:34:12 +00:00
})
.appendTo($form);
2014-11-20 10:03:22 +00:00
$labels.push($label);
$inputs.push($input);
});
});
}
2014-11-20 17:13:49 +00:00
function selectEntities() {
2014-12-01 16:24:11 +00:00
var entitiesLength = ui.entitiesSelection[type]
? ui.entitiesSelection[type].length
: 0,
text = Ox.formatCount(entitiesLength, Ox._('entity'), Ox._('entities'))
+ ' ' + Ox._('selected');
$list.options({selected: ui.entitiesSelection[type] || []})
2014-11-20 19:49:05 +00:00
renderEntity();
2014-11-20 20:35:47 +00:00
$itemMenu[
2014-12-01 16:24:11 +00:00
entitiesLength ? 'enableItem' : 'disableItem'
2014-11-20 20:35:47 +00:00
]('delete');
2014-11-20 19:49:05 +00:00
$itemMenu.setItemTitle('delete', Ox._('Delete {0}...', [
2014-12-01 16:24:11 +00:00
Ox._(entitiesLength < 2 ? 'Entity' : 'Entities')
2014-11-20 19:49:05 +00:00
]));
2014-12-01 16:24:11 +00:00
$deselectButton[entitiesLength ? 'show' : 'hide']();
2014-11-20 19:49:05 +00:00
renderForm();
$itemStatus.html(text[0].toUpperCase() + text.slice(1));
2014-11-20 17:13:49 +00:00
}
2014-11-20 15:39:33 +00:00
function updateForm() {
2014-11-20 19:52:41 +00:00
var width = $content.options('elements')[2].size - 16 - Ox.SCROLLBAR_SIZE;
2014-11-20 15:39:33 +00:00
$labels.forEach(function($label) {
$label.options({width: width});
});
$inputs.forEach(function($input) {
$input.options({width: width});
2015-02-14 15:21:05 +00:00
});
2014-11-20 15:39:33 +00:00
}
function updateList() {
var width = $content.options('elements')[0].size;
2014-11-20 19:52:41 +00:00
$entitiesSelect.options({width: Math.ceil((width - 12) / 2)});
$findInput.options({width: Math.floor((width - 12) / 2)});
2014-11-20 15:39:33 +00:00
$list.size();
}
2014-11-19 16:04:45 +00:00
return that;
2014-11-20 13:19:46 +00:00
};
2016-04-25 16:52:58 +00:00
}());