1 define(function(require) {
5 var $ = require('libs/jquery'),
6 _ = require('libs/underscore'),
7 utils = require('./utils');
10 var Container = function(nodes, params, element) {
11 _.extend(this, params);
12 this.dom = this.dom || $('<div>');
13 this.dom.addClass('canvas-container');
14 this.element = element;
16 nodes.forEach(function(node) {
17 var el = this.element.createElement(node);
19 this.dom.append(el.dom);
24 _.extend(Container.prototype, {
26 this.element.removeContainer(this);
29 onNodeAdded: function(event) {
30 if(event.meta.node.isRoot()) {
31 this.element.canvas.reloadRoot();//
35 var ptr = event.meta.node.prev(),
36 referenceElement, referenceAction, actionArg;
38 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
42 if(referenceElement) {
43 referenceAction = 'after';
45 referenceElement = this;
46 referenceAction = '_prepend';
50 /* Let's check if this node had its own canvas element and it's accessible. */
51 actionArg = utils.getElementForElementRootNode(event.meta.node);
54 actionArg = event.meta.node;
57 referenceElement[referenceAction](actionArg);
59 onNodeDetached: function(event) {
61 this.dom.contents().each(function() {
62 var childElement = container.element.canvas.getDocumentElement(this);
63 if(childElement && childElement.wlxmlNode.sameNode(event.meta.node)) {
64 childElement.detach();
69 getVerticallyFirstTextElement: function(params) {
70 var documentElement = require('./documentElement'),
74 considerChildren: true
77 this.children().some(function(child) {
78 if(child instanceof documentElement.DocumentTextElement) {
81 } else if(params.considerChildren) {
82 toret = child.getVerticallyFirstTextElement();
90 getFirst: function(e1, e2) {
91 var idx1 = this._childIndex(e1),
92 idx2 = this._childIndex(e2);
93 if(e1 === null || e2 === null) {
96 return idx1 <= idx2 ? e1: e2;
99 _prepend: function(param) {
100 var documentElement = require('./documentElement'),
102 if(param instanceof documentElement.DocumentElement) {
105 element = this.element.createElement(param);//
108 this.dom.prepend(element.dom);
112 _childIndex: function(child) {
113 var children = this.children(),
115 children.forEach(function(c, idx) {
116 if(c.sameNode(child)) {
123 containsBlock: function() {
124 var documentElement = require('./documentElement');
125 return this.children()
126 .filter(function(child) {
127 return child instanceof documentElement.DocumentNodeElement;
129 .some(function(child) {
130 if(child.isBlock()) {
133 return child.containsBlock();
137 children: function() {
138 var element = this.element.canvas,
140 this.dom.contents().each(function() {
141 var childElement = element.getDocumentElement(this);
142 if(childElement === undefined) {
146 toret.push(childElement);
153 create: function(nodes, params, element) {
154 return new Container(nodes, params, element);