Files
WebODF/webodf/odf.html
T
2010-06-25 22:05:00 +02:00

223 lines
7.9 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<style type="text/css" media="screen">
body, html, div {
padding: 0px;
margin: 0px;
border: 0px;
background: #888888;
}
</style>
<link rel="stylesheet" type="text/css" href="defaultodfstyle.css"/>
<style id='stylesxmlcss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace fo url(urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0);
@namespace office url(urn:oasis:names:tc:opendocument:xmlns:office:1.0);
@namespace style url(urn:oasis:names:tc:opendocument:xmlns:style:1.0);
@namespace table url(urn:oasis:names:tc:opendocument:xmlns:table:1.0);
@namespace text url(urn:oasis:names:tc:opendocument:xmlns:text:1.0);
</style>
<style id='autostylesxmlcss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace fo url(urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0);
@namespace office url(urn:oasis:names:tc:opendocument:xmlns:office:1.0);
@namespace style url(urn:oasis:names:tc:opendocument:xmlns:style:1.0);
@namespace table url(urn:oasis:names:tc:opendocument:xmlns:table:1.0);
@namespace text url(urn:oasis:names:tc:opendocument:xmlns:text:1.0);
</style>
<style id='contentxmlcss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace fo url(urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0);
@namespace office url(urn:oasis:names:tc:opendocument:xmlns:office:1.0);
@namespace style url(urn:oasis:names:tc:opendocument:xmlns:style:1.0);
@namespace table url(urn:oasis:names:tc:opendocument:xmlns:table:1.0);
@namespace text url(urn:oasis:names:tc:opendocument:xmlns:text:1.0);
</style>
<style id='positioncss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace svg url(urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0);
</style>
<script src="style2css.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var xhtmlns = "http://www.w3.org/1999/xhtml";
var dynamicOdfJavascript = null;
function loadJavascript(jsfiles) {
if (dynamicOdfJavascript) return;
var req = new XMLHttpRequest();
var js = "";
for (var i in jsfiles) {
req.open('GET', jsfiles[i], null);
req.send(null);
js += req.responseText;
}
if (js.length ) {
dynamicOdfJavascript = js;
}
}
function init() {
// create a native ODF object
if (false) { // TODO check if there is a global ODF object
window.odf = new ODF();
// window.odf.OdfContainer = function() {};
// window.odf.OdfContainer.prototype.getXmlNode() = function() {};
} else {
// try to load the javascript required when no native ODF support is
// available
// if there is no native window.odf, there still might be a window.qtodf
var jsfiles;
if (window.qtodf) {
jsfiles = ['qtodf.js'];
} else {
jsfiles = ['base64.js','bytearray.js','rawinflate.js','zip.js','odf.js'];
}
loadJavascript(jsfiles);
window.odf = eval(dynamicOdfJavascript);
}
// if the url has a fragment (#...), try to load the file it represents
var location = new String(document.location);
var pos = location.indexOf('#');
if (pos == -1) {
return;
}
location = location.substr(pos+1);
// open the odf container
window.odfcontainer = window.odf.getContainer(location);
refreshOdf();
}
function clear(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
}
function refreshOdf() {
// synchronize the object a window.odfcontainer with the view
var container = window.odfcontainer;
clear(document.body);
var stylesxmlnode = container.getXmlNode('styles.xml');
var contentxmlnode = container.getXmlNode('content.xml');
if (!stylesxmlnode || !stylesxmlnode.documentElement) {
throw "No styles element found.";
}
if (!contentxmlnode || !contentxmlnode.documentElement) {
throw "No content element found.";
}
stylesxmlnode = document.importNode(stylesxmlnode.documentElement, true);
contentxmlnode = document.importNode(contentxmlnode.documentElement, true);
handleStyles(stylesxmlnode, contentxmlnode);
// do content last, because otherwise the document is constantly updated
// whenever the css changes
handleContentXmlLoaded(contentxmlnode);
}
var officens = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
/**
* A new content.xml has been loaded. Update the live document with it.
**/
function handleContentXmlLoaded(contentxml, stylesxml) {
// replace content.xml in the live document
var contentcontainer = document.getElementById('contentxml');
if (!contentcontainer) {
contentcontainer = document.createElementNS(xhtmlns, "div");
document.body.appendChild(contentcontainer);
}
// experiment
var positioncss = document.getElementById('positioncss');
modifyImages(contentxml, positioncss.sheet);
// only append the content at the end
clear(contentcontainer);
contentcontainer.appendChild(contentxml);
}
function getDirectChild(ns, name, node) {
node = (node) ?node.firstChild :null;
while (node) {
if (node.localName == name && node.namespaceURI == ns) {
return node;
}
node = node.nextSibling;
}
}
/**
* A new styles.xml has been loaded. Update the live document with it.
**/
function handleStyles(stylesxml, contentxml) {
// replace styles.xml in the live document
var stylescontainer = document.getElementById('stylesxml');
if (!stylescontainer) {
stylescontainer = document.createElementNS(xhtmlns, "div");
document.body.appendChild(stylescontainer);
}
clear(stylescontainer);
//alert(stylesxml);
stylescontainer.appendChild(stylesxml);
// update the css translation of the styles
var stylestyles = getDirectChild(officens, 'styles', stylesxml);
styleautostyles = getDirectChild(officens, 'automatic-styles', stylesxml);
contentautostyles = getDirectChild(officens, 'automatic-styles', contentxml);
var stylesxmlcss = document.getElementById('stylesxmlcss');
try {
style2css(stylesxmlcss.sheet, stylestyles, styleautostyles, contentautostyles);
} catch (e) {
alert(e);
throw e;
}
}
var drawns="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
var svgns="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0";
function modifyImages(contentxml, stylesheet) {
var namespaces = {
draw: drawns,
svg: svgns
};
var namespaceResolver = function(prefix) {
return namespaces[prefix];
}
var doc = contentxml.ownerDocument;
var frames = contentxml.getElementsByTagNameNS(drawns, 'frame');
for (var i=0; i<frames.length; ++i) {
setFramePosition(i, frames.item(i), stylesheet);
}
}
function setFramePosition(id, frame, stylesheet) {
frame.setAttribute('styleid', id);
var rule = 'position: relative;';
var x = frame.getAttributeNS(svgns, 'x');
if (x) { rule += 'top: ' + x + ';'; }
var y = frame.getAttributeNS(svgns, 'x');
if (y) { rule += 'left: ' + y + ';'; }
var width = frame.getAttributeNS(svgns, 'width');
if (width) { rule += 'width: ' + width + ';'; }
var height = frame.getAttributeNS(svgns, 'height');
if (height) { rule += 'height: ' + height + ';' }
rule = 'draw|frame[styleid="' + id + '"] {' + rule + '}';
stylesheet.insertRule(rule, stylesheet.cssRules.length);
}
</script>
<title></title>
</head>
<body onload='init()'>
</body>
</html>