support html in documentDialog

This commit is contained in:
j 2017-01-27 13:19:42 +01:00
parent 85c63d228f
commit 385bf61084
4 changed files with 142 additions and 409 deletions

View file

@ -7,7 +7,7 @@ pandora.ui.document = function() {
.bindEvent({ .bindEvent({
doubleclick: function(e) { doubleclick: function(e) {
if ($(e.target).is('.OxBar')) { if ($(e.target).is('.OxBar')) {
pandora.$ui.text && pandora.$ui.text.animate({scrollTop:0}, 250); pandora.$ui.textPanel && pandora.$ui.textPanel.scrollTextTop();
} }
} }
}), }),
@ -29,11 +29,7 @@ pandora.ui.document = function() {
that.update(); that.update();
} }
}), }),
item, item;
$find,
$nextButton,
$currentButton,
$previousButton;
pandora.api.getDocument({ pandora.api.getDocument({
id: pandora.user.ui.document id: pandora.user.ui.document
@ -71,8 +67,7 @@ pandora.ui.document = function() {
zoom: 'fit' zoom: 'fit'
}) })
: item.extension == 'html' : item.extension == 'html'
? pandora.$ui.textPanel = textPanel(item).css({ ? pandora.$ui.textPanel = pandora.ui.textPanel(item, $toolbar)
})
: Ox.ImageViewer({ : Ox.ImageViewer({
area: pandora.user.ui.documents[item.id] area: pandora.user.ui.documents[item.id]
? pandora.user.ui.documents[item.id].position ? pandora.user.ui.documents[item.id].position
@ -120,149 +115,6 @@ pandora.ui.document = function() {
} }
} }
function textPanel(text) {
var textElement,
embedURLs = getEmbedURLs(text.text),
that = Ox.SplitPanel({
elements: [
{
element: pandora.$ui.text = textElement = pandora.ui.textHTML(text)
},
{
element: pandora.$ui.textEmbed = pandora.ui.textEmbed(),
collapsed: !embedURLs.length,
size: pandora.user.ui.embedSize,
resizable: true,
resize: [192, 256, 320, 384, 448, 512]
}
],
orientation: 'horizontal'
}),
selected = -1,
selectedURL;
/*
$find = Ox.Input({
clear: true,
placeholder: Ox._('Find in Texts'),
value: pandora.user.ui.textFind,
width: 188
})
.css({
float: 'right',
})
.bindEvent({
submit: function(data) {
Ox.print('SUBMIT', data);
}
})
.appendTo($toolbar);
*/
$nextButton = Ox.Button({
disabled: embedURLs.length < 2,
title: 'arrowRight',
tooltip: Ox._('Next Reference'),
type: 'image'
})
.css({
'margin-right': (pandora.user.ui.embedSize + Ox.SCROLLBAR_SIZE) + 'px',
float: 'right',
})
.bindEvent({
click: function() {
that.selectEmbed(
selected < embedURLs.length - 1 ? selected + 1 : 0,
true
);
}
})
.appendTo($toolbar);
$currentButton = Ox.Button({
disabled: embedURLs.length < 1,
title: 'center',
tooltip: Ox._('Current Reference'),
type: 'image'
})
.css({
float: 'right',
})
.bindEvent({
click: scrollToSelectedEmbed
})
.appendTo($toolbar);
$previousButton = Ox.Button({
disabled: embedURLs.length < 2,
title: 'arrowLeft',
tooltip: Ox._('Previous Reference'),
type: 'image'
})
.css({
float: 'right',
})
.bindEvent({
click: function() {
that.selectEmbed(
selected ? selected - 1 : embedURLs.length - 1,
true
);
}
})
.appendTo($toolbar);
function getEmbedURLs(text) {
var matches = text.match(/<a [^<>]*?href="(.+?)".*?>/gi),
urls = [];
if (matches) {
matches.forEach(function(match) {
var url = match.match(/"(.+?)"/)[1];
if (pandora.isEmbedURL(url)) {
urls.push(url);
}
});
}
return urls;
}
function scrollToSelectedEmbed() {
var scrollTop = Math.max(
textElement[0].scrollTop + $('#embed' + selected).offset().top - (
pandora.user.ui.showBrowser
? pandora.$ui.documentContentPanel.options().elements[0].size
: 0
) - 48,
0),
position = 100 * scrollTop / Math.max(1, textElement[0].scrollHeight);
textElement.scrollTo(position);
window.text = textElement;
}
that.selectEmbed = function(index, scroll) {
if (index != selected) {
selected = index;
selectedURL = embedURLs[selected]
$('.OxSpecialLink').removeClass('OxActive');
selected > -1 && $('#embed' + selected).addClass('OxActive');
pandora.$ui.textEmbed.update(selectedURL);
scroll && scrollToSelectedEmbed();
}
};
that.update = function(text) {
var index;
embedURLs = getEmbedURLs(text);
index = embedURLs.indexOf(selectedURL);
if (embedURLs.length && (index == -1 || index >= embedURLs.length)) {
index = 0;
}
selected = -1;
that.selectEmbed(index);
};
embedURLs.length && that.selectEmbed(0);
return that;
}
that.info = function() { that.info = function() {
return item; return item;
}; };
@ -272,9 +124,7 @@ pandora.ui.document = function() {
height: that.height(), height: that.height(),
width: that.width() width: that.width()
}); });
$nextButton && $nextButton.css({ pandora.$ui.textPanel && pandora.$ui.textPanel.update();
'margin-right': (pandora.user.ui.embedSize + Ox.SCROLLBAR_SIZE) + 'px',
});
}; };
return that; return that;

View file

@ -19,7 +19,7 @@ pandora.openDocumentDialog = function(options) {
operator: '|' operator: '|'
}, },
range: [0, options.ids.length], range: [0, options.ids.length],
keys: ['description', 'dimensions', 'extension', 'id', 'title', 'modified'] keys: ['description', 'dimensions', 'extension', 'id', 'title', 'modified', 'text']
}, function(result) { }, function(result) {
var i = 0, var i = 0,
documents = Ox.sort(result.data.items, function(item) { documents = Ox.sort(result.data.items, function(item) {
@ -202,6 +202,9 @@ pandora.ui.documentDialog = function(options) {
width: dialogWidth, width: dialogWidth,
zoom: 'fit' zoom: 'fit'
}) })
: item.extension == 'html'
? pandora.ui.textPanel(item).css({
})
: Ox.ImageViewer({ : Ox.ImageViewer({
area: pandora.user.ui.documents[item.id] area: pandora.user.ui.documents[item.id]
? pandora.user.ui.documents[item.id].position ? pandora.user.ui.documents[item.id].position

View file

@ -1,178 +1,104 @@
'use strict'; 'use strict';
pandora.ui.textPanel = function() { pandora.ui.textPanel = function(text, $toolbar) {
var textElement,
var that = Ox.SplitPanel({ textEmbed,
embedURLs = getEmbedURLs(text.text),
that = Ox.SplitPanel({
elements: [ elements: [
{element: Ox.Element(), size: 24}, {
{element: Ox.Element()}, element: textElement = pandora.ui.textHTML(text)
{element: Ox.Element(), size: 16} },
{
element: textEmbed = pandora.ui.textEmbed(textElement),
collapsed: !embedURLs.length,
size: pandora.user.ui.embedSize,
resizable: true,
resize: [192, 256, 320, 384, 448, 512]
}
], ],
orientation: 'vertical' orientation: 'horizontal'
}), }),
embedURLs,
scrolling = false,
selected = -1, selected = -1,
selectedURL; selectedURL,
$find,
$nextButton,
$currentButton,
$previousButton;
pandora.api.getText({id: pandora.user.ui.text}, function(result) { textElement.panel = that;
var text = result.data; if ($toolbar) {
embedURLs = text.type == 'html' /*
? getEmbedURLs(text.text) $find = Ox.Input({
: []; clear: true,
placeholder: Ox._('Find in Texts'),
value: pandora.user.ui.textFind,
width: 188
})
.css({
float: 'right',
})
.bindEvent({
submit: function(data) {
Ox.print('SUBMIT', data);
}
})
.appendTo($toolbar);
*/
$nextButton = Ox.Button({
disabled: embedURLs.length < 2,
title: 'arrowRight',
tooltip: Ox._('Next Reference'),
type: 'image'
})
.css({
'margin-right': (pandora.user.ui.embedSize + Ox.SCROLLBAR_SIZE) + 'px',
float: 'right',
})
.bindEvent({
click: function() {
that.selectEmbed(
selected < embedURLs.length - 1 ? selected + 1 : 0,
true
);
}
})
.appendTo($toolbar);
var $toolbar = Ox.Bar({size: 24}), $currentButton = Ox.Button({
disabled: embedURLs.length < 1,
title: 'center',
tooltip: Ox._('Current Reference'),
type: 'image'
})
.css({
float: 'right',
})
.bindEvent({
click: scrollToSelectedEmbed
})
.appendTo($toolbar);
$editMenu, $uploadButton, $previousButton = Ox.Button({
disabled: embedURLs.length < 2,
$find = Ox.Input({ title: 'arrowLeft',
clear: true, tooltip: Ox._('Previous Reference'),
placeholder: Ox._('Find in Texts'), type: 'image'
value: pandora.user.ui.textFind, })
width: 188 .css({
}) float: 'right',
.css({ })
float: 'right', .bindEvent({
margin: '4px 4px 4px 2px' click: function() {
}) that.selectEmbed(
.bindEvent({ selected ? selected - 1 : embedURLs.length - 1,
submit: function(data) { true
Ox.print('SUBMIT', data); );
} }
}) })
.appendTo($toolbar), .appendTo($toolbar);
}
$nextButton = Ox.Button({
disabled: embedURLs.length < 2,
title: 'arrowRight',
tooltip: Ox._('Next Reference'),
type: 'image'
})
.css({
float: 'right',
margin: '4px 2px 4px 2px'
})
.bindEvent({
click: function() {
that.selectEmbed(
selected < embedURLs.length - 1 ? selected + 1 : 0,
true
);
}
})
.appendTo($toolbar),
$currentButton = Ox.Button({
disabled: embedURLs.length < 1,
title: 'center',
tooltip: Ox._('Current Reference'),
type: 'image'
})
.css({
float: 'right',
margin: '4px 2px 4px 2px'
})
.bindEvent({
click: scrollToSelectedEmbed
})
.appendTo($toolbar),
$previousButton = Ox.Button({
disabled: embedURLs.length < 2,
title: 'arrowLeft',
tooltip: Ox._('Previous Reference'),
type: 'image'
})
.css({
float: 'right',
margin: '4px 2px 4px 2px'
})
.bindEvent({
click: function() {
that.selectEmbed(
selected ? selected - 1 : embedURLs.length - 1,
true
);
}
})
.appendTo($toolbar),
$statusbar = Ox.Bar({size: 16}),
$panel = Ox.SplitPanel({
elements: [
{
element: pandora.$ui.text = text.type == 'html'
? pandora.ui.textHTML(text)
: pandora.ui.textPDF(text)
},
{
element: pandora.$ui.textEmbed = pandora.ui.textEmbed(),
// fixme: at some point pdf will also have a sidebar
size: text.type == 'html' ? pandora.user.ui.embedSize : 0,
resizable: text.type == 'html',
resize: [192, 256, 320, 384, 448, 512]
}
],
orientation: 'horizontal'
});
if (text.editable) {
if (text.type == 'html') {
$editMenu = Ox.MenuButton({
items: [
{id: 'insertHTML', title: Ox._('Insert HTML...')},
{id: 'insertEmbed', title: Ox._('Insert Embed...')}
],
title: 'edit',
tooltip: Ox._('Editing Options'),
type: 'image'
})
.css({
float: 'left',
margin: '4px 2px 4px 4px'
})
.bindEvent({
click: function(data) {
if (data.id == 'insertEmbed') {
pandora.$ui.insertEmbedDialog = pandora.ui.insertEmbedDialog(function() {
// ...
}).open();
}
}
})
.appendTo($toolbar);
} else {
$uploadButton = Ox.FileButton({
image: 'upload',
tooltip: Ox._('Upload PDF'),
type: 'image'
})
.css({
float: 'left',
margin: '4px 2px 4px 4px'
})
.bindEvent({
click: function(data) {
if (data.files.length) {
pandora.$ui.uploadPDFDialog = pandora.ui.uploadPDFDialog({
file: data.files[0],
id: pandora.user.ui.text
}).open();
}
}
})
.appendTo($toolbar);
}
}
that.replaceElement(0, $toolbar);
that.replaceElement(1, $panel);
that.replaceElement(2, $statusbar);
embedURLs.length && that.selectEmbed(0);
});
function getEmbedURLs(text) { function getEmbedURLs(text) {
var matches = text.match(/<a [^<>]*?href="(.+?)".*?>/gi), var matches = text.match(/<a [^<>]*?href="(.+?)".*?>/gi),
@ -189,10 +115,16 @@ pandora.ui.textPanel = function() {
} }
function scrollToSelectedEmbed() { function scrollToSelectedEmbed() {
var scrollTop = Math.max(pandora.$ui.text[0].scrollTop + $('#embed' + selected).offset().top - 48, 0), var scrollTop = Math.max(
position = 100 * scrollTop / Math.max(1, textElement[0].scrollTop + $('#embed' + selected).offset().top - (
pandora.$ui.text[0].scrollHeight); pandora.user.ui.showBrowser
pandora.$ui.text.scrollTo(position); ? pandora.$ui.documentContentPanel.options().elements[0].size
: 0
) - 48,
0),
position = 100 * scrollTop / Math.max(1, textElement[0].scrollHeight);
textElement.scrollTo(position);
window.text = textElement;
} }
that.selectEmbed = function(index, scroll) { that.selectEmbed = function(index, scroll) {
@ -201,11 +133,16 @@ pandora.ui.textPanel = function() {
selectedURL = embedURLs[selected] selectedURL = embedURLs[selected]
$('.OxSpecialLink').removeClass('OxActive'); $('.OxSpecialLink').removeClass('OxActive');
selected > -1 && $('#embed' + selected).addClass('OxActive'); selected > -1 && $('#embed' + selected).addClass('OxActive');
pandora.$ui.textEmbed.update(selectedURL); textEmbed.update(selectedURL);
scroll && scrollToSelectedEmbed(); scroll && scrollToSelectedEmbed();
} }
}; };
that.scrollTextTop = function() {
textElement && textElement.animate({scrollTop:0}, 250);
return that;
};
that.update = function(text) { that.update = function(text) {
var index; var index;
embedURLs = getEmbedURLs(text); embedURLs = getEmbedURLs(text);
@ -215,14 +152,16 @@ pandora.ui.textPanel = function() {
} }
selected = -1; selected = -1;
that.selectEmbed(index); that.selectEmbed(index);
$nextButton && $nextButton.css({
'margin-right': (pandora.user.ui.embedSize + Ox.SCROLLBAR_SIZE) + 'px',
});
}; };
embedURLs.length && that.selectEmbed(0);
return that; return that;
}
};
pandora.ui.textHTML = function(text) { pandora.ui.textHTML = function(text) {
var height = getHeight(), var height = getHeight(),
width = getWidth(), width = getWidth(),
@ -234,23 +173,19 @@ pandora.ui.textHTML = function(text) {
scroll: function(event) { scroll: function(event) {
var position = Math.round(100 * that[0]. scrollTop / Math.max(1, var position = Math.round(100 * that[0]. scrollTop / Math.max(1,
that[0].scrollHeight - that.height())), that[0].scrollHeight - that.height())),
settings; settings, key;
if (pandora.user.ui.section == 'texts') { if (pandora.user.ui.part.document) {
settings = pandora.user.ui.texts[pandora.user.ui.text]; settings = pandora.user.ui.documents[pandora.user.ui.part.document] || {};
key = 'documents.' + pandora.user.ui.part.document;
} else { } else {
settings = pandora.user.ui.documents[pandora.user.ui.document] || {}; settings = pandora.user.ui.documents[pandora.user.ui.document] || {};
key = 'documents.' + pandora.user.ui.document;
} }
position = position - position % 10; position = position - position % 10;
if (!scrolling && settings && (settings.name || (position != settings.position))) { if (!scrolling && settings && (settings.name || (position != settings.position))) {
if (pandora.user.ui.section == 'documents') { pandora.UI.set(key, {
pandora.UI.set('documents.' + pandora.user.ui.document, { position: position ? position : 0
position: position ? position : 0 });
});
} else {
pandora.UI.set('texts.' + pandora.UI.encode(pandora.user.ui.text), {
position: position ? position : 0
});
}
} }
scrolling = false; scrolling = false;
}, },
@ -320,7 +255,7 @@ pandora.ui.textHTML = function(text) {
.appendTo($content), .appendTo($content),
$text = Ox.EditableContent({ $text = Ox.EditableContent({
clickLink: pandora.clickLink, clickLink: clickLink,
collapseToEnd: false, collapseToEnd: false,
editable: text.editable, editable: text.editable,
format: function(text) { format: function(text) {
@ -417,7 +352,7 @@ pandora.ui.textHTML = function(text) {
id: pandora.user.ui.text, id: pandora.user.ui.text,
text: data.value text: data.value
}); });
pandora.$ui.textPanel.update(data.value); that.panel.update(data.value);
} }
} }
}) })
@ -425,6 +360,9 @@ pandora.ui.textHTML = function(text) {
setTimeout(scrollToPosition); setTimeout(scrollToPosition);
function clickLink(e) {
pandora.clickLink(e, that.panel.selectEmbed);
}
function getHeight() { function getHeight() {
// 24 menu + 24 toolbar + 16 statusbar + 32 title + 32 margins // 24 menu + 24 toolbar + 16 statusbar + 32 title + 32 margins
// + 1px to get rid of scrollbar // + 1px to get rid of scrollbar
@ -444,9 +382,11 @@ pandora.ui.textHTML = function(text) {
} }
function scrollToPosition() { function scrollToPosition() {
var settings = (pandora.user.ui.section == 'documents' var settings = (pandora.user.ui.part.document
? pandora.user.ui.documents[pandora.user.ui.part.document]
: pandora.user.ui.section == 'documents'
? pandora.user.ui.documents[pandora.user.ui.document] ? pandora.user.ui.documents[pandora.user.ui.document]
: pandora.user.ui.texts[pandora.user.ui.text]) || {}, : {}),
position = settings.position || 0, position = settings.position || 0,
element, element,
scrollTop; scrollTop;
@ -482,62 +422,7 @@ pandora.ui.textHTML = function(text) {
}; };
pandora.ui.textPDF = function(text) { pandora.ui.textEmbed = function(textElement) {
var that = Ox.Element(),
$iframe,
page = pandora.user.ui.texts[pandora.user.ui.text].position || 1,
url = '/texts/' + pandora.user.ui.text + '/text.pdf.html#page=' + page;
if (text.uploaded) {
$iframe = Ox.Element('<iframe>')
.attr({
frameborder: 0,
height: '100%',
src: url,
width: '100%'
})
.bindMessage({
edit: function(data) {
pandora.ui.insertEmbedDialog(data.src, function(url) {
data.src = url;
var embed = text.embeds.filter(function(embed) {
return embed.id == data.id
&& embed.type == data.type
&& embed.page == data.page;
})[0];
if (embed) {
embed.src = url;
} else {
text.embeds.push(data);
//fixme sort embeds by page/id
}
pandora.api.editText({
id: text.id,
embeds: text.embeds
}, function(result) {
$iframe.postMessage('update', data);
});
}).open();
},
page: function(data) {
pandora.UI.set('texts.' + pandora.UI.encode(pandora.user.ui.text), {
'position': data.page
});
}
})
.appendTo(that);
that.setPage = function(page) {
$iframe && $iframe.postMessage('page', {page: page});
}
} else {
that.html('Please upload PDF');
}
return that;
};
pandora.ui.textEmbed = function() {
var that = Ox.Element() var that = Ox.Element()
.bindEvent({ .bindEvent({
@ -546,7 +431,7 @@ pandora.ui.textEmbed = function() {
}, },
resize: function(data) { resize: function(data) {
pandora.user.ui.embedSize = data.size; pandora.user.ui.embedSize = data.size;
pandora.$ui.text.update(); textElement.update();
pandora.$ui.document && pandora.$ui.document.update(); pandora.$ui.document && pandora.$ui.document.update();
}, },
resizeend: function(data) { resizeend: function(data) {

View file

@ -361,10 +361,10 @@ pandora.changeFolderItemStatus = function(id, status, callback) {
} }
}; };
pandora.clickLink = function(e) { pandora.clickLink = function(e, selectEmbed) {
var match = e.target.id.match(/^embed(\d+)$/); var match = e.target.id.match(/^embed(\d+)$/);
if (match) { if (match) {
pandora.$ui.textPanel.selectEmbed(parseInt(match[1])); (selectEmbed || pandora.$ui.textPanel.selectEmbed)(parseInt(match[1]));
} else if ( } else if (
e.target.hostname == document.location.hostname e.target.hostname == document.location.hostname
&& !Ox.startsWith(e.target.pathname, '/static') && !Ox.startsWith(e.target.pathname, '/static')
@ -2679,9 +2679,6 @@ pandora.resizeFolders = function(section) {
pandora.$ui.folder[pos].updatePanel(); pandora.$ui.folder[pos].updatePanel();
} }
}); });
if (pandora.user.ui.section == 'texts') {
pandora.$ui.text && pandora.$ui.text.update();
}
}; };
pandora.resizeWindow = function() { pandora.resizeWindow = function() {
@ -2775,8 +2772,6 @@ pandora.resizeWindow = function() {
- pandora.$ui.mainPanel.size(0) - 1 - pandora.$ui.mainPanel.size(0) - 1
}); });
} }
} else if (pandora.user.ui.section == 'texts') {
pandora.$ui.text && pandora.$ui.text.update();
} }
}; };