pandora/static/js/documentDialog.js

263 lines
8.9 KiB
JavaScript
Raw Permalink Normal View History

2014-01-07 08:24:00 +00:00
'use strict';
2014-02-04 05:53:04 +00:00
// fixme: this is a very uncommon pattern
2014-01-17 13:49:11 +00:00
2014-02-04 05:53:04 +00:00
pandora.openDocumentDialog = function(options) {
2014-02-03 06:12:55 +00:00
if (
pandora.$ui.documentDialog && options.ids && options.ids.length == 1
&& Ox.getObjectById(pandora.$ui.documentDialog.getItems(), options.ids[0])
) {
pandora.UI.set({'part.document': options.ids[0]});
2014-02-03 06:12:55 +00:00
} else if (options.ids) {
2014-01-20 11:13:07 +00:00
pandora.api.findDocuments({
query: {
conditions: options.ids.map(function(id) {
return {key: 'id', value: id, operator: '=='}
}),
operator: '|'
},
range: [0, options.ids.length],
2017-01-27 12:19:42 +00:00
keys: ['description', 'dimensions', 'extension', 'id', 'title', 'modified', 'text']
2014-01-20 11:13:07 +00:00
}, function(result) {
2014-02-03 06:12:55 +00:00
var i = 0,
documents = Ox.sort(result.data.items, function(item) {
return options.ids.indexOf(item.id);
}).map(function(document) {
return Ox.extend({index: i++}, document);
});
2014-01-20 11:13:07 +00:00
pandora.openDocumentDialog({documents: documents});
2014-01-17 13:49:11 +00:00
});
2014-01-20 11:13:07 +00:00
} else {
2014-01-17 13:49:11 +00:00
if (!pandora.$ui.documentDialog) {
pandora.$ui.documentDialog = pandora.ui.documentDialog({
index: 0,
2014-01-20 11:13:07 +00:00
items: options.documents,
2014-01-17 13:49:11 +00:00
})
.bindEvent({
close: function() {
pandora.user.ui.page == 'document' && pandora.UI.set({page: ''});
2014-01-17 13:49:11 +00:00
}
})
.open();
} else {
pandora.$ui.documentDialog.update({
index: 0,
2014-01-20 11:13:07 +00:00
items: options.documents
2014-01-17 13:49:11 +00:00
});
}
pandora.UI.set({
page: 'document',
'part.document': options.documents[0].id
2014-01-17 13:49:11 +00:00
});
2014-01-20 11:13:07 +00:00
return pandora.$ui.documentDialog;
}
2014-01-17 13:49:11 +00:00
};
2014-01-07 11:38:19 +00:00
pandora.ui.documentDialog = function(options) {
2014-12-17 16:52:21 +00:00
var dialogHeight = Math.round((window.innerHeight - 48) * 0.9) - 24,
dialogWidth = Math.round(window.innerWidth * 0.9) - 48,
isItemView = !pandora.$ui.documentsDialog,
2014-01-07 11:38:19 +00:00
items = options.items,
item = items[options.index],
2014-01-07 08:24:00 +00:00
$content = Ox.Element(),
that = Ox.Dialog({
closeButton: true,
content: $content,
2014-01-07 11:38:19 +00:00
focus: false,
2014-01-07 08:24:00 +00:00
height: dialogHeight,
maximizeButton: true,
minHeight: 256,
minWidth: 512,
padding: 0,
removeOnClose: true,
2014-01-07 12:40:33 +00:00
title: '',
2014-01-07 08:24:00 +00:00
width: dialogWidth
})
.bindEvent({
close: function() {
pandora.UI.set({'part.documents': ''});
2014-01-07 12:40:33 +00:00
delete pandora.$ui.documentDialog;
2014-01-07 08:24:00 +00:00
},
resize: function(data) {
2014-01-07 12:40:33 +00:00
dialogHeight = data.height;
dialogWidth = data.width;
2014-01-07 08:24:00 +00:00
$content.options({
2014-01-07 12:40:33 +00:00
height: dialogHeight,
width: dialogWidth
2014-01-07 08:24:00 +00:00
});
2015-02-05 08:18:40 +00:00
$content.getArea && pandora.UI.set(
'documents.' + item.id,
{position: $content.getArea().map(Math.round)}
);
2014-01-07 08:24:00 +00:00
},
'pandora_part.documents': function(data) {
2014-01-07 14:29:12 +00:00
if (data.value) {
2014-01-07 12:40:33 +00:00
if (Ox.getObjectById(items, data.value)) {
2014-01-20 11:13:07 +00:00
item = Ox.getObjectById(items, data.value);
setTitle();
setContent();
2014-01-07 12:40:33 +00:00
} else {
2014-01-20 12:23:25 +00:00
// ...
2014-01-07 12:40:33 +00:00
}
2014-01-07 08:24:00 +00:00
} else {
2014-01-07 12:40:33 +00:00
that.close();
2014-01-07 08:24:00 +00:00
}
2014-01-07 12:40:33 +00:00
},
pandora_item: function(data) {
pandora.UI.set({'part.documents': ''});
2014-01-07 08:24:00 +00:00
}
}),
$infoButton = Ox.Button({
title: 'info',
tooltip: Ox._('Info'),
type: 'image'
})
.css({
position: 'absolute',
right: '24px',
top: '4px'
})
.bindEvent({
click: function(data) {
var id = options.items[options.index].id;
that.close();
pandora.UI.set({
section: 'documents',
document: id,
documentView: 'info'
});
}
}),
2015-02-05 08:14:56 +00:00
$embedButton = Ox.Button({
title: 'embed',
tooltip: Ox._('Embed'),
type: 'image'
})
.css({
position: 'absolute',
right: '4px',
top: '4px'
})
.bindEvent({
click: function(data) {
var id = options.items[options.index].id;
pandora.$ui.embedDocumentDialog = pandora.ui.embedDocumentDialog(
id,
pandora.user.ui.documents[id]
? pandora.user.ui.documents[id].position
: null
).open();
}
}),
2014-01-07 08:24:00 +00:00
$selectButton = Ox.ButtonGroup({
buttons: [
{id: 'previous', title: 'left', tooltip: Ox._('Previous')},
{id: 'next', title: 'right', tooltip: Ox._('Next')}
2014-01-07 08:24:00 +00:00
],
type: 'image'
})
2014-01-07 11:38:19 +00:00
.css({
position: 'absolute',
right: '44px',
2014-01-07 11:38:19 +00:00
top: '4px'
})
2014-01-07 14:29:12 +00:00
[items.length > 1 ? 'show' : 'hide']()
2014-01-07 08:24:00 +00:00
.bindEvent({
click: function(data) {
pandora.$ui[
isItemView ? 'documents' : 'documentsDialogPanel'
].selectSelected(data.id == 'previous' ? -1 : 1);
2014-01-07 08:24:00 +00:00
}
});
$(that.find('.OxBar')[0])
.append($infoButton)
.append($embedButton)
.append($selectButton);
2014-01-07 11:38:19 +00:00
// fixme: why is this needed?
$(that.find('.OxContent')[0]).css({overflow: 'hidden'});
2014-01-07 08:24:00 +00:00
2014-01-07 12:40:33 +00:00
setTitle();
2014-01-07 08:24:00 +00:00
setContent();
function setContent() {
$content.replaceWith(
$content = (
item.extension == 'pdf'
2014-01-07 14:29:12 +00:00
? Ox.PDFViewer({
height: dialogHeight,
page: pandora.user.ui.documents[item.id]
? pandora.user.ui.documents[item.id].position
: 1,
url: '/documents/' + item.id + '/'
2019-12-01 15:10:08 +00:00
+ pandora.safeDocumentName(item.title) + '.' + item.extension,
2014-01-16 04:50:27 +00:00
width: dialogWidth,
zoom: 'fit'
2014-01-07 14:29:12 +00:00
})
2017-01-27 12:19:42 +00:00
: item.extension == 'html'
2020-01-19 11:48:03 +00:00
? pandora.$ui.textPanel = pandora.ui.textPanel(item)
2014-01-07 08:24:00 +00:00
: Ox.ImageViewer({
area: pandora.user.ui.documents[item.id]
? pandora.user.ui.documents[item.id].position
: [],
2014-01-07 11:38:19 +00:00
height: dialogHeight,
2014-01-07 08:24:00 +00:00
imageHeight: item.dimensions[1],
imagePreviewURL: pandora.getMediaURL('/documents/' + item.id + '/256p.jpg?' + item.modified),
2014-11-18 13:38:37 +00:00
imageURL: pandora.getMediaURL('/documents/' + item.id + '/'
2019-12-01 15:10:08 +00:00
+ pandora.safeDocumentName(item.title) + '.' + item.extension + '?' + item.modified),
2014-01-07 08:24:00 +00:00
imageWidth: item.dimensions[0],
width: dialogWidth
2014-01-07 08:24:00 +00:00
})
)
.bindEvent({
2014-01-07 12:40:33 +00:00
center: function(data) {
pandora.UI.set(
'documents.' + item.id,
2015-02-05 07:41:25 +00:00
{position: $content.getArea().map(Math.round)}
);
2014-01-07 08:24:00 +00:00
},
2014-01-07 12:40:33 +00:00
key_escape: function() {
pandora.$ui.documentDialog.close();
},
page: function(data) {
pandora.UI.set(
'documents.' + item.id,
2015-02-05 07:30:56 +00:00
{position: data.page}
);
2014-01-07 08:24:00 +00:00
},
zoom: function(data) {
pandora.UI.set(
'documents.' + item.id,
2015-02-05 07:41:25 +00:00
{position: $content.getArea().map(Math.round)}
);
2014-01-07 08:24:00 +00:00
}
})
);
}
2014-01-07 12:40:33 +00:00
function setTitle() {
2020-01-19 11:48:03 +00:00
that.options({title: item.title + (item.extension == 'html' ? '' : '.' + item.extension)});
2014-01-07 12:40:33 +00:00
}
2014-01-20 11:13:07 +00:00
that.getItems = function() {
return items;
};
2014-02-03 06:12:55 +00:00
2014-01-07 12:40:33 +00:00
that.update = function(options) {
items = options.items;
item = items[options.index];
setTitle();
$selectButton[items.length > 1 ? 'show' : 'hide']();
2014-01-07 12:40:33 +00:00
setContent();
};
2014-01-07 08:24:00 +00:00
return that;
};