X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/ba1e24e73cbeef44c1a08c8d225cc12923f3dfd6..dab8e2daf3587e367922dd59c94999e565faeec0:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 1686abd..034b907 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -1,6 +1,8 @@ define([ - 'libs/jquery' -], function($) { + 'libs/jquery', + 'libs/backbone', + 'smartxml/events' +], function($, Backbone, events) { 'use strict'; @@ -38,6 +40,11 @@ $.extend(DocumentNode.prototype, { } node.append(this); }, + + triggerChangeEvent: function(type, metaData) { + var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {})); + this.document.trigger('change', event); + }, }); var ElementNode = function(nativeNode, document) { @@ -47,6 +54,13 @@ var ElementNode = function(nativeNode, document) { $.extend(ElementNode.prototype, DocumentNode.prototype, { nodeType: Node.ELEMENT_NODE, + setData: function(key, value) { + this._$.data(key, value); + }, + getData: function(key) { + return this._$.data(key); + }, + getTagName: function() { return this.nativeNode.tagName.toLowerCase(); }, @@ -74,7 +88,9 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { }, setAttr: function(name, value) { + var oldVal = this.getAttr(name); this._$.attr(name, value); + this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value}); }, getAttrs: function() { @@ -133,8 +149,13 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)], element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)] }; - } + }, + toXML: function() { + var wrapper = $('
'); + wrapper.append(this._$); + return wrapper.html(); + } }); var TextNode = function(nativeNode, document) { @@ -169,8 +190,11 @@ var Document = function(xml) { Object.defineProperty(this, 'root', {get: function() { return doc.createElementNode($document[0]); }}); + Object.defineProperty(this, 'dom', {get: function() { + return $document[0]; + }}); }; -$.extend(Document.prototype, { +$.extend(Document.prototype, Backbone.Events, { ElementNodeFactory: ElementNode, TextNodeFactory: TextNode, @@ -180,6 +204,10 @@ $.extend(Document.prototype, { createTextNode: function(nativeNode) { return new this.TextNodeFactory(nativeNode, this); + }, + + toXML: function() { + return this.root.toXML(); } });