X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/4607903456ce2c96d5c40d72c603093ef02f5d29..317112df0a3aa6652f867da0fd8b7de504156e2b:/platforma/static/js/models.js?ds=sidebyside diff --git a/platforma/static/js/models.js b/platforma/static/js/models.js index d0e7df13..dae64d8a 100755 --- a/platforma/static/js/models.js +++ b/platforma/static/js/models.js @@ -98,7 +98,7 @@ Editor.HTMLModel = Editor.Model.extend({ return false; }, - asWLML: function(element) + asWLML: function(element, inner) { console.log("Source", element); var doc = this.parser.parseFromString(this.serializer.serializeToString(element), 'text/xml'); @@ -111,30 +111,81 @@ Editor.HTMLModel = Editor.Model.extend({ } console.log("Transformed", doc, " to: ", result.documentElement); - return this.serializer.serializeToString(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 />'); - var chunk = this.parser.parseFromString("<chunk>"+text+"</chunk>", "text/xml"); - - var errors = $('parsererror', chunk); + 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>"; + } - // check if chunk is parsable - if(errors.length > 0) - throw {text: errors.text(), html: errors.html()}; + var parseError = chunk.getElementsByTagName('parsererror'); + console.log("Errors:", parseError); - var result = this.htmlXSL.transformToFragment(chunk, document); + if(parseError.length > 0) + { + console.log("Parse errors.") + return this.serializer.serializeToString(parseError.item(0)); + } - console.log("RESULT", this.serializer.serializeToString(result)); + console.log("Transforming to HTML"); + var result = this.htmlXSL.transformToFragment(chunk, $element[0].ownerDocument).firstChild; - if(!result) - throw "WLML->HTML transformation failed."; - - $element.replaceWith(result); - this.set('state', 'dirty'); + 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) { @@ -151,7 +202,9 @@ Editor.HTMLModel = Editor.Model.extend({ if(this.wlmlXSL && this.htmlXSL && this.rawText) this.loadSuccess(); } catch(e) { - this.loadingFailed(); + console.log(e); + this.set('error', e.toString() ); + this.set('state', 'error'); } }, @@ -163,7 +216,9 @@ Editor.HTMLModel = Editor.Model.extend({ if(this.wlmlXSL && this.htmlXSL && this.rawText) this.loadSuccess(); } catch(e) { - this.loadingFailed(); + console.log(e); + this.set('error', e.toString() ); + this.set('state', 'error'); } }, @@ -414,6 +469,22 @@ Editor.ImageGalleryModel = Editor.Model.extend({ this.pages = []; }, + setGallery: function(path) { + $.ajax({ + url: this.serverURL, + type: 'post', + data: { + path: path, + }, + success: this.settingGallerySucceeded.bind(this) + }); + }, + + settingGallerySucceeded: function(data) { + console.log('settingGallerySucceeded'); + this.load(true); + }, + load: function(force) { if (force || this.get('state') == 'empty') { console.log("setting state");