+define([\r
+'libs/jquery-1.9.1.min',\r
+'libs/underscore-min',\r
+'modules/documentCanvas/transformations',\r
+'modules/documentCanvas/wlxmlNode',\r
+'libs/text!./template.html'\r
+], function($, _, transformations, wlxmlNode, template) {\r
+\r
+'use strict';\r
+\r
+var Canvas = function(xml) {\r
+ this.xml = xml;\r
+ this.dom = $(template);\r
+ \r
+ this.content = this.dom.find('#rng-module-documentCanvas-content')\r
+ \r
+ \r
+ this.content.html(transformations.fromXML.getHTMLTree(xml));\r
+}\r
+\r
+Canvas.prototype.toXML = function() {\r
+ return transformations.toXML.getXML(this.content.html());\r
+}\r
+\r
+Canvas.prototype.getNode = function(desc) {\r
+ var selector = '';\r
+ if(desc.klass)\r
+ selector += '[wlxml-class=' + desc.klass + ']';\r
+ if(desc.tag)\r
+ selector += '[wlxml-tag=' + desc.tag + ']';\r
+ var toret = [];\r
+ this.content.find(selector).each(function() {\r
+ toret.push(new wlxmlNode.Node($(this)));\r
+ });\r
+ return toret;\r
+}\r
+\r
+Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) {\r
+ var toBlock = ['div', 'document', 'section', 'header'];\r
+ var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';\r
+ var toret = $('<' + htmlTag + '>');\r
+ toret.attr('wlxml-tag', wlxmlTag);\r
+ if(wlxmlClass)\r
+ toret.attr('wlxml-class', wlxmlClass);\r
+ toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));\r
+ return toret;\r
+ };\r
+\r
+Canvas.prototype.insertNode = function(options) {\r
+ var element = $(this.content.find('#' + options.context.id).get(0));\r
+ if(options.place == 'after')\r
+ element[options.place](this._createNode(options.tag, options.klass));\r
+ else if(options.place == 'wrapText') {\r
+ var elementContents = element.contents();\r
+ if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3)\r
+ return false;\r
+ var textElement = elementContents.get(0);\r
+\r
+ var prefix = textElement.data.substr(0, options.offsetStart);\r
+ var suffix = textElement.data.substr(options.offsetEnd);\r
+ var core = textElement.data.substr(options.offsetStart, options.offsetEnd - options.offsetStart);\r
+ var newNode = this._createNode(options.tag, options.klass);\r
+ newNode.text(core);\r
+ $(textElement).replaceWith(newNode);\r
+ newNode.before(prefix);\r
+ newNode.after(suffix);\r
+ }\r
+}\r
+\r
+Canvas.prototype.splitNode = function(options) {\r
+ var element = $(this.content.find('#' + options.node.id).get(0));\r
+ \r
+ var elementContents = element.contents();\r
+ if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3)\r
+ return false;\r
+ var textElement = elementContents.get(0);\r
+ \r
+ var prefix = textElement.data.substr(0, options.offset);\r
+ var suffix = textElement.data.substr(options.offset);\r
+ var prefixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class'));\r
+ var suffixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class'));\r
+ prefixNode.text(prefix);\r
+ suffixNode.text(suffix);\r
+ element.before(prefixNode);\r
+ element.after(suffixNode);\r
+ element.remove();\r
+}\r
+\r
+Canvas.prototype.createList = function(options) {\r
+ var element1 = $(this.content.find('#' + options.start.id).get(0));\r
+ var element2 = $(this.content.find('#' + options.end.id).get(0));\r
+ if(!element1.parent().get(0).isSameNode(element2.parent().get(0)))\r
+ return false;\r
+ \r
+ var parent = element1.parent();\r
+ var nodesToWrap = [];\r
+ \r
+ var place = 'before';\r
+ var canvas = this;\r
+ parent.contents().each(function() {\r
+ var node = this;\r
+ if(node.isSameNode(element1.get(0)))\r
+ place = 'inside';\r
+ if(place === 'inside') {\r
+ var $node;\r
+ if(node.nodeType === 3) {\r
+ $node = canvas._createNode('div').text(node.data);\r
+ $(node).remove();\r
+ }\r
+ else {\r
+ $node = $(node);\r
+ }\r
+ $node.attr('wlxml-class', 'list.item');\r
+ nodesToWrap.push($node);\r
+ }\r
+ if(node.isSameNode(element2.get(0)))\r
+ return;\r
+ });\r
+ \r
+ var list = this._createNode('div', 'list');\r
+ element1.before(list);\r
+ \r
+ nodesToWrap.forEach(function(node) {\r
+ node.remove();\r
+ list.append(node);\r
+ });\r
+ \r
+ \r
+ \r
+}\r
+\r
+\r
+return {Canvas: Canvas, Node: Node};\r
+\r
+});
\ No newline at end of file