pandora/static/js/pandora/textPanel.js

398 lines
12 KiB
JavaScript
Raw Normal View History

2013-02-21 10:49:37 +00:00
'use strict';
pandora.ui.textPanel = function() {
2013-02-21 16:07:20 +00:00
var that = Ox.SplitPanel({
2013-02-21 12:28:10 +00:00
elements: [
{element: Ox.Element(), size: 24},
{element: Ox.Element()},
{element: Ox.Element(), size: 16}
],
orientation: 'vertical'
2013-02-21 16:07:20 +00:00
}),
embedURLs,
selected;
2013-02-21 10:49:37 +00:00
pandora.api.getText({id: pandora.user.ui.text}, function(result) {
2013-02-21 13:14:33 +00:00
Ox.print('TEXT:', result.data);
2013-02-21 16:07:20 +00:00
var text = result.data;
embedURLs = text.type == 'html'
? getEmbedURLs(text.text)
: [];
selected = -1;
2013-02-21 10:49:37 +00:00
2013-02-21 16:07:20 +00:00
var $toolbar = Ox.Bar({size: 24}),
2013-02-21 10:49:37 +00:00
$editMenu, $uploadButton,
$find = Ox.Input({
clear: true,
placeholder: 'Find in Texts',
value: pandora.user.ui.textFind,
width: 188
})
.css({
float: 'right',
margin: '4px 4px 4px 2px'
})
.bindEvent({
submit: function(data) {
Ox.print('SUBMIT', data);
}
})
.appendTo($toolbar),
$nextButton = Ox.Button({
2013-02-21 11:25:24 +00:00
disabled: embedURLs.length < 2,
2013-02-21 10:49:37 +00:00
title: 'arrowRight',
tooltip: 'Next Clip',
type: 'image'
})
.css({
float: 'right',
margin: '4px 2px 4px 2px'
})
2013-02-21 16:07:20 +00:00
.bindEvent({
click: function() {
2013-02-22 03:10:14 +00:00
that.selectEmbed(
2013-02-21 16:07:20 +00:00
selected < embedURLs.length - 1 ? selected + 1 : 0
);
}
})
2013-02-21 10:49:37 +00:00
.appendTo($toolbar),
$currentButton = Ox.Button({
2013-02-21 11:25:24 +00:00
disabled: embedURLs.length < 1,
2013-02-21 10:49:37 +00:00
title: 'center',
tooltip: 'Current Reference',
type: 'image'
})
.css({
float: 'right',
margin: '4px 2px 4px 2px'
})
.appendTo($toolbar),
$previousButton = Ox.Button({
2013-02-21 11:25:24 +00:00
disabled: embedURLs.length < 2,
2013-02-21 10:49:37 +00:00
title: 'arrowLeft',
tooltip: 'Previous Clip',
type: 'image'
})
.css({
float: 'right',
margin: '4px 2px 4px 2px'
})
2013-02-21 16:07:20 +00:00
.bindEvent({
click: function() {
2013-02-22 03:10:14 +00:00
that.selectEmbed(
2013-02-21 16:07:20 +00:00
selected ? selected - 1 : embedURLs.length - 1
);
}
})
2013-02-21 10:49:37 +00:00
.appendTo($toolbar),
$statusbar = Ox.Bar({size: 16}),
$panel = Ox.SplitPanel({
elements: [
{
element: pandora.$ui.text = text.type == 'html'
2013-02-21 16:07:20 +00:00
? pandora.ui.textHTML(text)
2013-02-21 10:49:37 +00:00
: pandora.ui.textPDF(text)
},
{
element: pandora.$ui.textEmbed = pandora.ui.textEmbed(),
2013-02-21 10:49:37 +00:00
size: pandora.user.ui.embedSize,
resizable: true,
resize: [192, 256, 320, 384, 448, 512]
}
],
orientation: 'horizontal'
});
2013-02-21 11:56:56 +00:00
if (text.editable) {
if (text.type == 'html') {
$editMenu = Ox.MenuButton({
items: [
{id: 'inserthtml', title: 'Insert HTML...'},
{id: 'insertembed', title: 'Insert Embed...'}
],
title: 'edit',
tooltip: 'Editing Options',
type: 'image'
})
.css({
float: 'left',
margin: '4px 2px 4px 4px'
})
.appendTo($toolbar);
} else {
2013-02-21 14:09:23 +00:00
$uploadButton = Ox.FileButton({
image: 'upload',
2013-02-21 11:56:56 +00:00
tooltip: 'Upload PDF',
type: 'image'
})
.css({
float: 'left',
margin: '4px 2px 4px 4px'
})
2013-02-21 14:09:23 +00:00
.bindEvent({
click: function(data) {
if(data.files.length) {
pandora.$ui.uploadPDFDialog = pandora.ui.uploadPDFDialog({
file: data.files[0],
id: pandora.user.ui.text
}).open();
}
}
})
2013-02-21 11:56:56 +00:00
.appendTo($toolbar);
}
2013-02-21 10:49:37 +00:00
}
2013-02-21 12:28:10 +00:00
that.replaceElement(0, $toolbar);
that.replaceElement(1, $panel);
that.replaceElement(2, $statusbar);
2013-02-21 10:49:37 +00:00
embedURLs.length && that.selectEmbed(0);
2013-02-21 10:49:37 +00:00
});
2013-02-21 11:25:24 +00:00
function getEmbedURLs(text) {
2013-02-21 16:07:20 +00:00
var matches = text.match(/<a [^<>]*?href="(.+?)".*?>/gi),
2013-02-21 11:25:24 +00:00
urls = [];
if (matches) {
matches.forEach(function(match) {
2013-02-21 16:07:20 +00:00
var url = match.match(/"(.+?)"/)[1];
if (pandora.isEmbedURL(url)) {
urls.push(url);
}
2013-02-21 11:25:24 +00:00
});
}
return urls;
2013-02-21 10:49:37 +00:00
}
2013-02-22 03:10:14 +00:00
that.selectEmbed = function(index) {
if (index != selected) {
selected = index;
$('.OxSpecialLink').removeClass('OxActive');
$('#embed' + selected).addClass('OxActive');
pandora.$ui.textEmbed.update(embedURLs[selected]);
}
};
2013-02-21 16:07:20 +00:00
2013-02-21 10:49:37 +00:00
return that;
};
2013-02-21 16:07:20 +00:00
pandora.ui.textHTML = function(text) {
2013-02-21 10:49:37 +00:00
var height = getHeight(),
width = getWidth(),
that = Ox.Element()
.css({margin: '16px'}),
$title = Ox.Editable({
2013-02-21 13:14:33 +00:00
editable: text.editable,
2013-02-21 10:49:37 +00:00
height: 32,
placeholder: text.editable ? 'Doubleclick to edit title' : 'Untitled',
tooltip: text.editable ? 'Doubleclick to edit title' : '',
2013-02-21 13:14:33 +00:00
value: text.name,
2013-02-21 10:49:37 +00:00
width: width
})
.css({
//position: 'absolute',
//width: width + 'px',
height: '32px',
fontSize: '18px',
})
.bindEvent({
submit: function(data) {
Ox.Request.clearCache('getText');
pandora.api.editText({
2013-02-21 11:56:56 +00:00
id: pandora.user.ui.text,
2013-02-21 10:49:37 +00:00
name: data.value
}, function(result) {
2013-02-21 11:56:56 +00:00
Ox.print('RESULT.DATA:', result.data);
2013-02-21 10:49:37 +00:00
});
}
})
.appendTo(that),
$spaceTop = Ox.Element()
.css({height: '16px'})
.appendTo(that),
$text = Ox.Editable({
clickLink: pandora.clickLink,
editable: text.editable,
2013-02-21 14:51:28 +00:00
format: function(text) {
2013-02-22 03:10:14 +00:00
var index = 0;
2013-02-21 14:51:28 +00:00
return text.replace(
/<a [^<>]*?href="(.+?)".*?>/gi,
function() {
2013-02-22 03:10:14 +00:00
var link = arguments[0], ret, url = arguments[1];
if (pandora.isEmbedURL(url)) {
ret = '<a id="embed' + index
+ '" class="OxSpecialLink" href="' + url
+ '">'
index++;
} else {
ret = link;
}
return ret;
2013-02-21 14:51:28 +00:00
}
);
},
2013-02-21 10:49:37 +00:00
height: height,
maxHeight: Infinity,
placeholder: text.editable ? 'Doubleclick to edit text' : '',
tooltip: text.editable ? 'Doubleclick to edit text' : '',
type: 'textarea',
2013-02-21 13:14:33 +00:00
width: width,
value: text.text
2013-02-21 10:49:37 +00:00
})
.css({
//position: 'absolute',
height: height + 'px',
width: width + 'px',
//marginTop: '48px',
fontSize: '12px'
})
.bindEvent({
submit: function(data) {
2013-02-21 14:51:28 +00:00
Ox.print('SUBMIT', data.value);
2013-02-21 10:49:37 +00:00
Ox.Request.clearCache('getText');
pandora.api.editText({
2013-02-21 12:00:45 +00:00
id: pandora.user.ui.text,
2013-02-21 10:49:37 +00:00
text: data.value
});
}
})
.appendTo(that);
function getHeight() {
// 24 menu + 24 toolbar + 16 statusbar + 32 title + 32 margins
return window.innerHeight - 128;
}
function getWidth() {
2013-02-21 12:00:45 +00:00
return window.innerWidth
- pandora.user.ui.showSidebar * pandora.user.ui.sidebarSize - 1
- pandora.user.ui.embedSize - 1
- 32;
2013-02-21 10:49:37 +00:00
}
that.update = function() {
$text.options({
height: getHeight(),
width: getWidth()
});
return that;
};
2013-02-21 11:56:56 +00:00
return that;
2013-02-21 10:49:37 +00:00
};
pandora.ui.textPDF = function(text) {
var that = Ox.Element();
2013-02-21 14:09:23 +00:00
if (text.uploaded) {
$('<iframe>')
.attr({
height: '100%',
frameborder: 0,
src: '/texts/' + pandora.user.ui.text + '/text.pdf.html',
width: '100%'
})
.appendTo(that);
2013-02-21 10:49:37 +00:00
2013-02-21 14:09:23 +00:00
} else {
that.html('UPLOADED: ' + text.uploaded);
}
2013-02-21 10:49:37 +00:00
return that;
};
pandora.ui.textEmbed = function() {
2013-02-21 10:49:37 +00:00
var that = Ox.Element()
.bindEvent({
resizestart: function() {
2013-02-21 11:25:24 +00:00
url && $overlay.show();
2013-02-21 10:49:37 +00:00
},
resize: function(data) {
pandora.user.ui.embedSize = data.size;
2013-02-21 11:56:56 +00:00
pandora.$ui.text.update();
2013-02-21 10:49:37 +00:00
},
resizeend: function() {
2013-02-21 11:25:24 +00:00
url && $overlay.hide();
2013-02-21 10:49:37 +00:00
}
}),
2013-02-21 11:56:56 +00:00
$message = $('<div>')
.css({marginTop: '16px', textAlign: 'center'})
.html('No Embeds')
.hide()
.appendTo(that),
2013-02-21 10:49:37 +00:00
$iframe = $('<iframe>')
.attr({
height: '100%',
id: 'embed',
2013-02-21 10:49:37 +00:00
frameborder: 0,
2013-02-21 11:25:24 +00:00
src: '',
2013-02-21 13:14:33 +00:00
width: '100%',
allowFullScreen: true,
mozallowfullscreen: true,
webkitAllowFullScreen: true
2013-02-21 10:49:37 +00:00
})
2013-02-21 11:25:24 +00:00
.hide()
2013-02-21 11:56:56 +00:00
.appendTo(that),
2013-02-21 10:49:37 +00:00
$overlay = $('<div>')
.css({
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0
})
.hide()
2013-02-21 11:56:56 +00:00
.appendTo(that);
2013-02-21 10:49:37 +00:00
that.update = function(url) {
var parsed, src;
2013-02-21 11:25:24 +00:00
if (url) {
2013-02-21 16:07:20 +00:00
url = url.replace(/&amp;/g, '&') + '&matchRatio=true';
src = $iframe.attr('src');
parsed = {src: Ox.parseURL(src), url: Ox.parseURL(url)};
if (
src
&& parsed.url.protocol == parsed.src.protocol
&& parsed.url.hostname == parsed.src.hostname
) {
$iframe[0].contentWindow.postMessage(JSON.stringify({
url: parsed.url.pathname + parsed.url.search + parsed.url.hash
}), '*');
} else {
$iframe.attr({src: url});
}
2013-02-21 11:56:56 +00:00
$message.hide();
2013-02-21 16:07:20 +00:00
$iframe.show();
2013-02-21 11:25:24 +00:00
} else {
$iframe.hide();
2013-02-21 11:56:56 +00:00
$message.show();
2013-02-21 11:25:24 +00:00
}
2013-02-21 10:49:37 +00:00
return that;
};
return that;
2013-02-21 14:09:23 +00:00
};