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 params = params || {};
12 _.extend(this, params);
13 this.dom = this.dom || $('<div>');
14 this.dom.addClass('canvas-container');
15 this.dom.toggleClass('reset-background', !!params.resetBackground);
16 this.element = element;
18 nodes.forEach(function(node) {
19 var el = this.element.createElement(node);
21 this.dom.append(el.dom);
26 _.extend(Container.prototype, {
28 this.element.removeContainer(this);
31 onNodeAdded: function(event) {
32 if(event.meta.node.isRoot()) {
33 this.element.canvas.reloadRoot();//
37 var ptr = event.meta.node.prev(),
38 referenceElement, referenceAction, actionArg;
40 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
44 if(referenceElement) {
45 referenceAction = 'after';
47 referenceElement = this;
48 referenceAction = '_prepend';
52 /* Let's check if this node had its own canvas element and it's accessible. */
53 actionArg = utils.getElementForElementRootNode(event.meta.node);
56 actionArg = event.meta.node;
59 referenceElement[referenceAction](actionArg);
61 onNodeDetached: function(event) {
63 this.dom.contents().each(function() {
64 var childElement = container.element.canvas.getDocumentElement(this);
65 if(childElement && childElement.wlxmlNode.sameNode(event.meta.node)) {
66 childElement.detach();
71 getVerticallyFirstTextElement: function(params) {
72 var documentElement = require('./documentElement'),
76 considerChildren: true
79 this.children().some(function(child) {
80 if(child instanceof documentElement.DocumentTextElement) {
83 } else if(params.considerChildren) {
84 toret = child.getVerticallyFirstTextElement();
92 getFirst: function(e1, e2) {
93 var idx1 = this._childIndex(e1),
94 idx2 = this._childIndex(e2);
95 if(e1 === null || e2 === null) {
98 return idx1 <= idx2 ? e1: e2;
101 _prepend: function(param) {
102 var documentElement = require('./documentElement'),
104 if(param instanceof documentElement.DocumentElement) {
107 element = this.element.createElement(param);//
110 this.dom.prepend(element.dom);
114 _childIndex: function(child) {
115 var children = this.children(),
117 children.forEach(function(c, idx) {
118 if(c.sameNode(child)) {
125 containsBlock: function() {
126 var documentElement = require('./documentElement');
127 return this.children()
128 .filter(function(child) {
129 return child instanceof documentElement.DocumentNodeElement;
131 .some(function(child) {
132 if(child.isBlock()) {
135 return child.containsBlock();
139 children: function() {
140 var element = this.element.canvas,
142 this.dom.contents().each(function() {
143 var childElement = element.getDocumentElement(this);
144 if(childElement === undefined) {
148 toret.push(childElement);
155 create: function(nodes, params, element) {
156 return new Container(nodes, params, element);