'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
+ this.content = this.dom.find('#rng-module-documentCanvas-content');\r
+ this.setXML(xml);\r
+}\r
+\r
+Canvas.prototype.setXML = function(xml) {\r
+ this.xml = $.trim(xml);\r
+ this.content.html(transformations.fromXML.getHTMLTree(this.xml)); \r
}\r
\r
Canvas.prototype.toXML = function() {\r
return toret;\r
}\r
\r
+Canvas.prototype.getPreviousNode = function(options) {\r
+ var element = $(this.content.find('#' + options.node.id).get(0));\r
+ var prev = element.prev()\r
+ if(prev.length === 0)\r
+ prev = element.parent();\r
+ return new wlxmlNode.Node(prev);\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
\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
+ if(options.place == 'after') {\r
+ var node = this._createNode(options.tag, options.klass);\r
+ element[options.place](node);\r
+ return node;\r
+ }\r
else if(options.place == 'wrapText') {\r
var elementContents = element.contents();\r
if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3)\r
Canvas.prototype.splitNode = function(options) {\r
options = _.extend({textNodeIdx: 0}, options);\r
\r
- var element = $(this.content.find('#' + options.node.id).get(0));\r
+ var nodeToSplit = $(this.content.find('#' + options.node.id).get(0));\r
\r
- var elementContents = element.contents();\r
- if(elementContents.length === 0 || \r
- elementContents.length - 1 < options.textNodeIdx || \r
- elementContents.get(options.textNodeIdx).nodeType != 3)\r
+ var nodeContents = nodeToSplit.contents();\r
+ if(nodeContents.length === 0 || \r
+ nodeContents.length - 1 < options.textNodeIdx || \r
+ nodeContents.get(options.textNodeIdx).nodeType != 3)\r
return false;\r
\r
- var textElement = elementContents.get(options.textNodeIdx);\r
+ var textNode = $(nodeContents.get(options.textNodeIdx));\r
\r
var succeedingNodes = [];\r
var passed = false;\r
- elementContents.each(function() {\r
+ nodeContents.each(function() {\r
var node = this;\r
if(passed)\r
succeedingNodes.push(node);\r
- if(node.isSameNode(textElement))\r
+ if(node.isSameNode(textNode.get(0)))\r
passed = true;\r
});\r
\r
- var prefix = textElement.data.substr(0, options.offset);\r
- var suffix = textElement.data.substr(options.offset);\r
+ var prefix = textNode.text().substr(0, options.offset);\r
+ var suffix = textNode.text().substr(options.offset);\r
+ \r
+ textNode.before(prefix);\r
+ textNode.remove();\r
\r
- var $textElement = $(textElement);\r
- $textElement.before(prefix);\r
- $textElement.remove();\r
- var newNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class'));\r
+ var newNode = this._createNode(nodeToSplit.attr('wlxml-tag'), nodeToSplit.attr('wlxml-class'));\r
newNode.append(suffix);\r
succeedingNodes.forEach(function(node) {\r
newNode.append(node)\r
});\r
- element.after(newNode);\r
+ nodeToSplit.after(newNode);\r
+ return newNode;\r
+}\r
+\r
+Canvas.prototype.removeNode = function(options) {\r
+ var toRemove = $(this.content.find('#' + options.node.id).get(0));\r
+ toRemove.remove();\r
+\r
}\r
\r
Canvas.prototype.createList = function(options) {\r
else {\r
$node = $(node);\r
}\r
- $node.attr('wlxml-class', 'list.item');\r
+ $node.attr('wlxml-class', 'item');\r
nodesToWrap.push($node);\r
}\r
if(node.isSameNode(element2.get(0)))\r
- return;\r
+ return false;\r
});\r
\r
- var list = this._createNode('div', 'list');\r
+ var list = this._createNode('div', 'list-items');\r
element1.before(list);\r
\r
nodesToWrap.forEach(function(node) {\r
node.remove();\r
list.append(node);\r
});\r
+}\r
+\r
+Canvas.prototype.removeList = function(options) {\r
+ var pointerElement = $(this.content.find('#' + options.pointer.id));\r
+ var listElement = options.pointer.klass === 'list-items' ? pointerElement : \r
+ pointerElement.parent('[wlxml-class="list-items"][wlxml-tag]');\r
\r
+ listElement.find('[wlxml-class=item]').each(function() {\r
+ $(this).attr('wlxml-class', '');\r
+ });;\r
+ listElement.children().unwrap();\r
\r
- \r
+}\r
+\r
+Canvas.prototype.insideList = function(options) {\r
+ if(options.pointer) {\r
+ if(options.pointer.klass === 'list-items' || options.pointer.klass === 'item')\r
+ return true;\r
+ var pointerElement = $(this.content.find('#' + options.pointer.id));\r
+ return pointerElement.parents('list-items').length > 0;\r
+ }\r
+ return false;\r
}\r
\r
\r