4 'modules/documentCanvas/canvas/utils'
 
   5 ], function($, _, utils) {
 
   8 /* global Node:false */
 
  10 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
 
  11 var DocumentElement = function(wlxmlNode, canvas) {
 
  12     this.wlxmlNode = wlxmlNode;
 
  19     this.dom = this.createDOM();
 
  20     this.dom.data('canvas-element', this);
 
  21     this.wlxmlNode.setData('canvasElement', this);
 
  24 $.extend(DocumentElement.prototype, {
 
  25     refreshPath: function() {
 
  26         this.parents().forEach(function(parent) {
 
  34     updateState: function(toUpdate) {
 
  37             .filter(function(key) {
 
  38                 return this.state.hasOwnProperty(key);
 
  40             .forEach(function(key) {
 
  41                 if(this.state !== toUpdate[key]) {
 
  42                     this.state[key] = changes[key] = toUpdate[key];
 
  45         if(_.isFunction(this.onStateChange)) {
 
  46             this.onStateChange(changes);
 
  47             if(_.isBoolean(changes.active)) {
 
  50                     while(ptr && ptr.wlxmlNode.getTagName() === 'span') {
 
  53                     if(ptr && ptr.gutterGroup) {
 
  54                         ptr.gutterGroup.show();
 
  61         var parents = this.dom.parents('[document-node-element]');
 
  63             return this.canvas.getDocumentElement(parents[0]);
 
  70             parent = this.parent();
 
  73             parent = parent.parent();
 
  78     sameNode: function(other) {
 
  79         return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
 
  81     isRootElement: function() {
 
  82         return this.sameNode(this.canvas.rootElement);
 
  86         this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
 
  93 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
 
  94 var DocumentNodeElement = function(wlxmlNode, canvas) {
 
  95     DocumentElement.call(this, wlxmlNode, canvas);
 
 100 var manipulate = function(e, params, action) {
 
 102     if(params instanceof DocumentElement) {
 
 105         element = e.canvas.createElement(params);
 
 108         e.dom[action](element.dom);
 
 114 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
 
 117 $.extend(DocumentNodeElement.prototype, {
 
 118     defaultDisplayStyle: 'block',
 
 120     addWidget: function(widget) {
 
 121         this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
 
 123     clearWidgets: function() {
 
 124         this.dom.children('.canvas-widgets').empty();
 
 126     addToGutter: function(view) {
 
 127         if(!this.gutterGroup) {
 
 128             this.gutterGroup = this.canvas.gutter.createViewGroup({
 
 129                 offsetHint: function() {
 
 130                     return this.canvas.getElementOffset(this);
 
 134         this.gutterGroup.addView(view);
 
 136     handle: function(event) {
 
 137         var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
 
 142     createDOM: function() {
 
 143         var wrapper = $('<div>').attr('document-node-element', ''),
 
 144             widgetsContainer = $('<div>')
 
 145                 .addClass('canvas-widgets'),
 
 146             contentContainer = $('<div>')
 
 147                 .attr('document-element-content', '');
 
 149         wrapper.append(contentContainer, widgetsContainer);
 
 150         widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
 
 153     _container: function() {
 
 154         return this.dom.children('[document-element-content]');
 
 156     detach: function(isChild) {
 
 159         if(this.gutterGroup) {
 
 160             this.gutterGroup.remove();
 
 162         if(_.isFunction(this.children)) {
 
 163             this.children().forEach(function(child) {
 
 169             parents = this.parents();
 
 172                 parents[0].refreshPath();
 
 177     before: function(params) {
 
 178         return manipulate(this, params, 'before');
 
 181     after: function(params) {
 
 182         return manipulate(this, params, 'after');
 
 185     isBlock: function() {
 
 186         return this.dom.css('display') === 'block';
 
 189     displayAsBlock: function() {
 
 190         this.dom.css('display', 'block');
 
 191         this._container().css('display', 'block');
 
 193     displayInline: function() {
 
 194         this.dom.css('display', 'inline');
 
 195         this._container().css('display', 'inline');
 
 197     displayAs: function(what) {
 
 198         // [this.dom(), this._container()].forEach(e) {
 
 199         //     var isBlock = window.getComputedStyle(e).display === 'block';
 
 200         //     if(!isBlock && what === 'block') {
 
 201         //         e.css('display', what);
 
 202         //     } else if(isBlock && what === 'inline') {
 
 206         this.dom.css('display', what);
 
 207         this._container().css('display', what);
 
 212 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
 
 213 var DocumentTextElement = function(wlxmlTextNode, canvas) {
 
 214     DocumentElement.call(this, wlxmlTextNode, canvas);
 
 217 $.extend(DocumentTextElement, {
 
 218     isContentContainer: function(htmlElement) {
 
 219         return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
 
 223 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
 
 225 $.extend(DocumentTextElement.prototype, {
 
 226     createDOM: function() {
 
 228             .attr('document-text-element', '')
 
 229             .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
 
 232     detach: function(isChild) {
 
 238     setText: function(text) {
 
 240             text = utils.unicode.ZWS;
 
 242         if(text !== this.getText()) {
 
 243             this.dom.contents()[0].data = text;
 
 246     handle: function(event) {
 
 247         this.setText(event.meta.node.getText());
 
 249     getText: function(options) {
 
 250         options = _.extend({raw: false}, options || {});
 
 251         var toret = this.dom.text();
 
 253             toret = toret.replace(utils.unicode.ZWS, '');
 
 257     isEmpty: function() {
 
 258         // Having at least Zero Width Space is guaranteed be Content Observer
 
 259         return this.dom.contents()[0].data === utils.unicode.ZWS;
 
 261     after: function(params) {
 
 262         if(params instanceof DocumentTextElement || params.text) {
 
 266         if(params instanceof DocumentNodeElement) {
 
 269             element = this.canvas.createElement(params);
 
 272             this.dom.wrap('<div>');
 
 273             this.dom.parent().after(element.dom);
 
 279     before: function(params) {
 
 280         if(params instanceof DocumentTextElement || params.text) {
 
 284         if(params instanceof DocumentNodeElement) {
 
 287             element = this.canvas.createElement(params);
 
 290             this.dom.wrap('<div>');
 
 291             this.dom.parent().before(element.dom);
 
 298     children: function() {
 
 306     DocumentElement: DocumentElement,
 
 307     DocumentNodeElement: DocumentNodeElement,
 
 308     DocumentTextElement: DocumentTextElement