pandora/static/js/documentDialog.js

221 lines
7.3 KiB
JavaScript
Raw Normal View History

2014-01-07 08:24:00 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
'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])
) {
2014-01-20 11:13:07 +00:00
pandora.UI.set({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],
keys: ['description', 'dimensions', 'extension', 'id', 'name']
}, 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 == 'documents' && pandora.UI.set({page: ''});
}
})
.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: 'documents',
2014-01-20 11:13:07 +00:00
'part.documents': 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-20 11:13:07 +00:00
settings = getSettings(),
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({document: ''});
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
});
},
pandora_document: 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);
settings = getSettings();
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({document: ''});
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: '4px',
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) {
var offset = data.id == 'previous' ? -1 : 1;
options.index = Ox.mod(options.index + offset, items.length);
pandora.$ui[
isItemView ? 'documents' : 'documentsDialogPanel'
].selectSelected(offset);
2014-01-07 11:38:19 +00:00
pandora.UI.set({document: items[options.index].id});
2014-01-07 08:24:00 +00:00
}
});
$(that.find('.OxBar')[0]).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();
2014-01-20 11:13:07 +00:00
function getSettings() {
return Ox.extend(item.extension == 'pdf' ? {
page: 1,
zoom: 'fit'
} : {
center: 'auto',
zoom: 'fit'
}, pandora.user.ui.documents[item.id] || {});
}
2014-01-07 08:24:00 +00:00
function setContent() {
$content.replaceWith(
$content = (
item.extension == 'pdf'
2014-01-07 14:29:12 +00:00
? Ox.PDFViewer({
height: dialogHeight,
2014-01-16 04:50:27 +00:00
page: settings.page,
url: '/documents/' + item.id + '/'
+ item.name + '.' + item.extension,
2014-01-16 04:50:27 +00:00
width: dialogWidth,
zoom: settings.zoom
2014-01-07 14:29:12 +00:00
})
2014-01-07 08:24:00 +00:00
: Ox.ImageViewer({
2014-01-16 04:50:27 +00:00
center: settings.center,
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'),
2014-11-18 13:38:37 +00:00
imageURL: pandora.getMediaURL('/documents/' + item.id + '/'
+ item.name + '.' + item.extension),
2014-01-07 08:24:00 +00:00
imageWidth: item.dimensions[0],
2014-01-16 04:50:27 +00:00
width: dialogWidth,
zoom: settings.zoom
2014-01-07 08:24:00 +00:00
})
)
.bindEvent({
2014-01-07 12:40:33 +00:00
center: function(data) {
2014-01-16 13:51:10 +00:00
pandora.UI.set('documents.' + item.id, Ox.extend(settings, {
2014-01-16 04:50:27 +00:00
center: data.center
}));
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) {
2014-01-16 13:51:10 +00:00
pandora.UI.set('documents.' + item.id, Ox.extend(settings, {
2014-01-16 04:50:27 +00:00
page: data.page
}));
2014-01-07 08:24:00 +00:00
},
zoom: function(data) {
2014-01-16 13:51:10 +00:00
pandora.UI.set('documents.' + item.id, Ox.extend(settings, {
2014-01-16 04:50:27 +00:00
zoom: data.zoom
}));
2014-01-07 08:24:00 +00:00
}
})
);
}
2014-01-07 12:40:33 +00:00
function setTitle() {
that.options({title: item.name + '.' + item.extension});
}
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;
};