X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/14c5677515f8d609f9a57e6e08f15af1e32a0207..dab8e2daf3587e367922dd59c94999e565faeec0:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 6b71026..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'; @@ -9,6 +11,9 @@ var TEXT_NODE = Node.TEXT_NODE; var DocumentNode = function(nativeNode, document) { + if(!document) { + throw new Error('undefined document for a node'); + } this.document = document; this.nativeNode = nativeNode; this._$ = $(nativeNode); @@ -35,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) { @@ -44,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(); }, @@ -71,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() { @@ -130,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) { @@ -166,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, @@ -177,6 +204,10 @@ $.extend(Document.prototype, { createTextNode: function(nativeNode) { return new this.TextNodeFactory(nativeNode, this); + }, + + toXML: function() { + return this.root.toXML(); } });