post JSON messages, relay theme change to same-domain iframe

This commit is contained in:
rolux 2013-02-22 09:46:58 +05:30
parent 699f0b64a5
commit ebdc2b7057
3 changed files with 28 additions and 13 deletions

View file

@ -315,10 +315,15 @@ appPanel
if (isEmbed) { if (isEmbed) {
pandora.$ui.embedPanel = pandora.ui.embedPanel().display(); pandora.$ui.embedPanel = pandora.ui.embedPanel().display();
window.addEventListener('message', function(e) { window.addEventListener('message', function(e) {
var url = e.data; var data;
if (pandora.isEmbedURL(url)) { try {
pandora.URL.push(url); data = JSON.parse(e.data);
if (Ox.contains(pandora.site.themes, data.theme)) {
Ox.Theme(data.theme);
} else if (pandora.isEmbedURL(data.url)) {
pandora.URL.push(data.url);
} }
} catch() {}
}); });
} else { } else {
pandora.$ui.appPanel = pandora.ui.appPanel().display(); pandora.$ui.appPanel = pandora.ui.appPanel().display();

View file

@ -227,8 +227,18 @@ pandora.ui.mainMenu = function() {
filters[index].sort[0].operator = operator; filters[index].sort[0].operator = operator;
pandora.UI.set({filters: filters}); pandora.UI.set({filters: filters});
} else if (data.id == 'settheme') { } else if (data.id == 'settheme') {
var iframe, src;
Ox.Theme(value); Ox.Theme(value);
pandora.UI.set('theme', value); pandora.UI.set('theme', value);
iframe = $('#embed')[0];
if (iframe) {
src = $(iframe).attr('src');
if (src && Ox.parseURL(src).hostname == document.location.hostname) {
iframe.contentWindow.postMessage(JSON.stringify({
{theme: value}
}), '*');
}
}
} else if (data.id == 'showsiteposters') { } else if (data.id == 'showsiteposters') {
pandora.UI.set('showSitePosters', data.checked) pandora.UI.set('showSitePosters', data.checked)
} else if (Ox.startsWith(data.id, 'sortfilter')) { } else if (Ox.startsWith(data.id, 'sortfilter')) {

View file

@ -21,7 +21,7 @@ pandora.ui.textPanel = function() {
embedURLs = text.type == 'html' embedURLs = text.type == 'html'
? getEmbedURLs(text.text) ? getEmbedURLs(text.text)
: []; : [];
selected = embedURLs.length ? 0 : -1; selected = -1;
var $toolbar = Ox.Bar({size: 24}), var $toolbar = Ox.Bar({size: 24}),
@ -104,7 +104,7 @@ pandora.ui.textPanel = function() {
: pandora.ui.textPDF(text) : pandora.ui.textPDF(text)
}, },
{ {
element: pandora.$ui.textEmbed = pandora.ui.textEmbed(embedURLs[selected]), element: pandora.$ui.textEmbed = pandora.ui.textEmbed(),
size: pandora.user.ui.embedSize, size: pandora.user.ui.embedSize,
resizable: true, resizable: true,
resize: [192, 256, 320, 384, 448, 512] resize: [192, 256, 320, 384, 448, 512]
@ -157,6 +157,8 @@ pandora.ui.textPanel = function() {
that.replaceElement(1, $panel); that.replaceElement(1, $panel);
that.replaceElement(2, $statusbar); that.replaceElement(2, $statusbar);
embedURLs.length && that.selectEmbed(0);
}); });
function getEmbedURLs(text) { function getEmbedURLs(text) {
@ -317,7 +319,7 @@ pandora.ui.textPDF = function(text) {
}; };
pandora.ui.textEmbed = function(url) { pandora.ui.textEmbed = function() {
var that = Ox.Element() var that = Ox.Element()
.bindEvent({ .bindEvent({
@ -342,6 +344,7 @@ pandora.ui.textEmbed = function(url) {
$iframe = $('<iframe>') $iframe = $('<iframe>')
.attr({ .attr({
height: '100%', height: '100%',
id: 'embed',
frameborder: 0, frameborder: 0,
src: '', src: '',
width: '100%', width: '100%',
@ -374,10 +377,9 @@ pandora.ui.textEmbed = function(url) {
&& parsed.url.protocol == parsed.src.protocol && parsed.url.protocol == parsed.src.protocol
&& parsed.url.hostname == parsed.src.hostname && parsed.url.hostname == parsed.src.hostname
) { ) {
$iframe[0].contentWindow.postMessage( $iframe[0].contentWindow.postMessage(JSON.stringify({
parsed.url.pathname + parsed.url.search + parsed.url.hash, url: parsed.url.pathname + parsed.url.search + parsed.url.hash
'*' }), '*');
);
} else { } else {
$iframe.attr({src: url}); $iframe.attr({src: url});
} }
@ -390,8 +392,6 @@ pandora.ui.textEmbed = function(url) {
return that; return that;
}; };
that.update(url);
return that; return that;
}; };