+
+ asWLML: function(element, inner)
+ {
+ console.log("Source", element);
+ var doc = this.parser.parseFromString(this.serializer.serializeToString(element), 'text/xml');
+
+ var result = this.wlmlXSL.transformToDocument(doc);
+
+ if(!result) {
+ console.log("Failed", this.wlmlXSL, doc);
+ throw "Failed to transform fragment";
+ }
+
+ console.log("Transformed", doc, " to: ", result.documentElement);
+ if(inner) {
+ var children = result.documentElement.childNodes;
+ var buf = '';
+
+ for(var i=0; i < children.length; i++)
+ buf += this.serializer.serializeToString(children.item(i));
+
+ return buf;
+ }
+
+ return this.serializer.serializeToString(result.documentElement);
+ },
+
+ innerAsWLML: function(elem)
+ {
+ return this.asWLML(elem, true);
+ },
+
+ updateInnerWithWLML: function($element, innerML)
+ {
+ var e = $element.clone().html('<span x-node="out-of-flow-text" x-content="%"></span>')[0];
+ var s = this.asWLML(e);
+ // hurray for dirty hacks :P
+ s = s.replace(/>%<\//, '>'+innerML+'</');
+ return this.updateWithWLML($element, s);
+ },
+
+ updateWithWLML: function($element, text)
+ {
+ // filter the string
+ text = text.replace(/\/\s+/g, '<br />');
+ try {
+ var chunk = this.parser.parseFromString("<chunk>"+text+"</chunk>", "text/xml");
+ } catch(e) {
+ console.log('Caught parse exception.');
+ return "<p>Źle sformatowana zawartość:" + e.toString() + "</p>";
+ }
+
+ var parseError = chunk.getElementsByTagName('parsererror');
+ console.log("Errors:", parseError);
+
+ if(parseError.length > 0)
+ {
+ console.log("Parse errors.")
+ return this.serializer.serializeToString(parseError.item(0));
+ }
+
+ console.log("Transforming to HTML");
+ var result = this.htmlXSL.transformToFragment(chunk, $element[0].ownerDocument).firstChild;
+
+ if(!result) {
+ return "Błąd aplikacji - nie udało się wygenerować nowego widoku HTML.";
+ }
+
+ var errors = result.getElementsByTagName('error');
+ if(errors.length > 0)
+ {
+ var errorMessage = 'Wystąpiły błędy:<ul>';
+ for(var i=0; i < errors.length; i++)
+ {
+ var estr = this.serializer.serializeToString(errors.item(i));
+ console.log("XFRM error:", estr);
+ errorMessage += "<li>"+estr+"</li>";
+ }
+ errorMessage += "</ul>";
+ return errorMessage;
+ }
+
+ try {
+ $element.replaceWith(result);
+ this.set('state', 'dirty');
+ return false;
+ } catch(e) {
+ return "Błąd podczas wstawiania tekstu: '" + e.toString() + "'";
+ }
+ },
+
+ createXSLT: function(xslt_doc) {
+ var p = new XSLTProcessor();
+ p.importStylesheet(xslt_doc);
+ return p;
+ },
+
+ htmlXSLLoadSuccess: function(data)
+ {
+ try {
+ this.htmlXSL = this.createXSLT(data);
+
+ if(this.wlmlXSL && this.htmlXSL && this.rawText)
+ this.loadSuccess();
+ } catch(e) {
+ console.log(e);
+ this.set('error', e.toString() );
+ this.set('state', 'error');
+ }
+ },
+
+ wlmlXSLLoadSuccess: function(data)
+ {
+ try {
+ this.wlmlXSL = this.createXSLT(data);
+
+ if(this.wlmlXSL && this.htmlXSL && this.rawText)
+ this.loadSuccess();
+ } catch(e) {
+ console.log(e);
+ this.set('error', e.toString() );
+ this.set('state', 'error');
+ }
+ },
+
+ textLoadSuccess: function(data) {
+ this.rawText = data;
+
+ if(this.wlmlXSL && this.htmlXSL && this.rawText)
+ this.loadSuccess();
+ },
+
+ loadSuccess: function() {