X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/c988cd5d12cb6203ca8216e05bcbc202e14a04f8..a9efc708bce6a636b314c58b3c9769488e8d5999:/platforma/static/js/models.js diff --git a/platforma/static/js/models.js b/platforma/static/js/models.js index d2869ede..dae64d8a 100755 --- a/platforma/static/js/models.js +++ b/platforma/static/js/models.js @@ -98,39 +98,94 @@ Editor.HTMLModel = Editor.Model.extend({ return false; }, - asWLML: function(element) - { - var result = this.wlmlXSL.transformToFragment(element, document); + 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(this.wlmlXSL.transformToDocument(element)); + console.log("Failed", this.wlmlXSL, doc); throw "Failed to transform fragment"; } - console.log("Transform result:", result); - return this.serializer.serializeToString(result); + 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('')[0]; + var s = this.asWLML(e); + // hurray for dirty hacks :P + s = s.replace(/>%<\//, '>'+innerML+''); - var chunk = this.parser.parseFromString(""+text+"", "text/xml"); - - var errors = $('parsererror', chunk); + try { + var chunk = this.parser.parseFromString(""+text+"", "text/xml"); + } catch(e) { + console.log('Caught parse exception.'); + return "

Źle sformatowana zawartość:" + e.toString() + "

"; + } - // 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); + 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:"; + 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) { @@ -147,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'); } }, @@ -159,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'); } }, @@ -201,20 +260,23 @@ Editor.HTMLModel = Editor.Model.extend({ save: function(message) { if (this.get('state') == 'dirty') { - this.set('state', 'updating'); - messageCenter.addMessage('info', 'xmlsave', 'Zapisuję XML...'); + this.set('state', 'saving'); + + messageCenter.addMessage('info', 'htmlsave', 'Zapisuję HTML...'); + var wlml = this.asWLML(this.get('data')); var payload = { - contents: this.get('data'), + contents: wlml, revision: this.get('revision'), user: this.document.get('user') }; + if (message) { payload.message = message; } $.ajax({ - url: this.serverURL, + url: this.textURL, type: 'post', dataType: 'json', data: payload, @@ -227,19 +289,19 @@ Editor.HTMLModel = Editor.Model.extend({ }, saveSucceeded: function(data) { - if (this.get('state') != 'updating') { + if (this.get('state') != 'saving') { alert('erroneous state:', this.get('state')); } this.set('revision', data.revision); this.set('state', 'updated'); - messageCenter.addMessage('success', 'xmlsave', 'Zapisałem XML :-)'); + messageCenter.addMessage('success', 'htmlsave', 'Zapisałem :-)'); }, saveFailed: function() { - if (this.get('state') != 'updating') { + if (this.get('state') != 'saving') { alert('erroneous state:', this.get('state')); } - messageCenter.addMessage('error', 'xmlsave', 'Nie udało mi się zapisać XML. Spróbuj ponownie :-('); + messageCenter.addMessage('error', 'htmlsave', 'Nie udało mi się zapisać.'); this.set('state', 'dirty'); }, @@ -407,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");