X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/4c81ad4075b0e8e67f984cce3494f9853179a9d6..7b4405539ac7c11a11b5259b4f968e1289f0b2a7:/modules/documentCanvas/canvas.js diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 44f88a9..80d3915 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -9,13 +9,14 @@ define([ 'use strict'; var Canvas = function(xml) { - this.xml = xml; this.dom = $(template); - - this.content = this.dom.find('#rng-module-documentCanvas-content') - - - this.content.html(transformations.fromXML.getHTMLTree(xml)); + this.content = this.dom.find('#rng-module-documentCanvas-content'); + this.setXML(xml); +} + +Canvas.prototype.setXML = function(xml) { + this.xml = $.trim(xml); + this.content.html(transformations.fromXML.getHTMLTree(this.xml)); } Canvas.prototype.toXML = function() { @@ -35,6 +36,14 @@ Canvas.prototype.getNode = function(desc) { return toret; } +Canvas.prototype.getPreviousNode = function(options) { + var element = $(this.content.find('#' + options.node.id).get(0)); + var prev = element.prev() + if(prev.length === 0) + prev = element.parent(); + return new wlxmlNode.Node(prev); +} + Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) { var toBlock = ['div', 'document', 'section', 'header']; var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span'; @@ -48,8 +57,11 @@ Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) { Canvas.prototype.insertNode = function(options) { var element = $(this.content.find('#' + options.context.id).get(0)); - if(options.place == 'after') - element[options.place](this._createNode(options.tag, options.klass)); + if(options.place == 'after') { + var node = this._createNode(options.tag, options.klass); + element[options.place](node); + return node; + } else if(options.place == 'wrapText') { var elementContents = element.contents(); if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3) @@ -70,38 +82,45 @@ Canvas.prototype.insertNode = function(options) { Canvas.prototype.splitNode = function(options) { options = _.extend({textNodeIdx: 0}, options); - var element = $(this.content.find('#' + options.node.id).get(0)); + var nodeToSplit = $(this.content.find('#' + options.node.id).get(0)); - var elementContents = element.contents(); - if(elementContents.length === 0 || - elementContents.length - 1 < options.textNodeIdx || - elementContents.get(options.textNodeIdx).nodeType != 3) + var nodeContents = nodeToSplit.contents(); + if(nodeContents.length === 0 || + nodeContents.length - 1 < options.textNodeIdx || + nodeContents.get(options.textNodeIdx).nodeType != 3) return false; - var textElement = elementContents.get(options.textNodeIdx); + var textNode = $(nodeContents.get(options.textNodeIdx)); var succeedingNodes = []; var passed = false; - elementContents.each(function() { + nodeContents.each(function() { var node = this; if(passed) succeedingNodes.push(node); - if(node.isSameNode(textElement)) + if(node.isSameNode(textNode.get(0))) passed = true; }); - var prefix = textElement.data.substr(0, options.offset); - var suffix = textElement.data.substr(options.offset); + var prefix = textNode.text().substr(0, options.offset); + var suffix = textNode.text().substr(options.offset); + + textNode.before(prefix); + textNode.remove(); - var $textElement = $(textElement); - $textElement.before(prefix); - $textElement.remove(); - var newNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class')); + var newNode = this._createNode(nodeToSplit.attr('wlxml-tag'), nodeToSplit.attr('wlxml-class')); newNode.append(suffix); succeedingNodes.forEach(function(node) { newNode.append(node) }); - element.after(newNode); + nodeToSplit.after(newNode); + return newNode; +} + +Canvas.prototype.removeNode = function(options) { + var toRemove = $(this.content.find('#' + options.node.id).get(0)); + toRemove.remove(); + } Canvas.prototype.createList = function(options) { @@ -128,23 +147,42 @@ Canvas.prototype.createList = function(options) { else { $node = $(node); } - $node.attr('wlxml-class', 'list.item'); + $node.attr('wlxml-class', 'item'); nodesToWrap.push($node); } if(node.isSameNode(element2.get(0))) - return; + return false; }); - var list = this._createNode('div', 'list'); + var list = this._createNode('div', 'list-items'); element1.before(list); nodesToWrap.forEach(function(node) { node.remove(); list.append(node); }); +} + +Canvas.prototype.removeList = function(options) { + var pointerElement = $(this.content.find('#' + options.pointer.id)); + var listElement = options.pointer.klass === 'list-items' ? pointerElement : + pointerElement.parent('[wlxml-class="list-items"][wlxml-tag]'); + listElement.find('[wlxml-class=item]').each(function() { + $(this).attr('wlxml-class', ''); + });; + listElement.children().unwrap(); - +} + +Canvas.prototype.insideList = function(options) { + if(options.pointer) { + if(options.pointer.klass === 'list-items' || options.pointer.klass === 'item') + return true; + var pointerElement = $(this.content.find('#' + options.pointer.id)); + return pointerElement.parents('list-items').length > 0; + } + return false; }