4 'modules/documentCanvas/canvas/utils',
5 'modules/documentCanvas/canvas/container'
6 ], function($, _, utils, container) {
9 /* global Node:false */
11 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
12 var DocumentElement = function(wlxmlNode, canvas) {
13 this.wlxmlNode = wlxmlNode;
20 this.dom = this.createDOM();
21 this.dom.data('canvas-element', this);
22 this.wlxmlNode.setData('canvasElement', this);
25 $.extend(DocumentElement.prototype, {
26 refreshPath: function() {
27 this.parents().forEach(function(parent) {
35 updateState: function(toUpdate) {
38 .filter(function(key) {
39 return this.state.hasOwnProperty(key);
41 .forEach(function(key) {
42 if(this.state !== toUpdate[key]) {
43 this.state[key] = changes[key] = toUpdate[key];
46 if(_.isFunction(this.onStateChange)) {
47 this.onStateChange(changes);
48 if(_.isBoolean(changes.active)) {
51 while(ptr && ptr.wlxmlNode.getTagName() === 'span') {
54 if(ptr && ptr.gutterGroup) {
55 ptr.gutterGroup.show();
62 var parents = this.dom.parents('[document-node-element]');
64 return this.canvas.getDocumentElement(parents[0]);
71 parent = this.parent();
74 parent = parent.parent();
79 sameNode: function(other) {
80 return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
82 isRootElement: function() {
83 return this.sameNode(this.canvas.rootElement);
87 this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
94 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
95 var DocumentNodeElement = function(wlxmlNode, canvas) {
96 DocumentElement.call(this, wlxmlNode, canvas);
102 var manipulate = function(e, params, action) {
104 if(params instanceof DocumentElement) {
107 element = e.canvas.createElement(params);
110 e.dom[action](element.dom);
116 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
119 $.extend(DocumentNodeElement.prototype, {
120 defaultDisplayStyle: 'block',
122 addWidget: function(widget) {
123 this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
125 clearWidgets: function() {
126 this.dom.children('.canvas-widgets').empty();
128 addToGutter: function(view) {
129 if(!this.gutterGroup) {
130 this.gutterGroup = this.canvas.gutter.createViewGroup({
131 offsetHint: function() {
132 return this.canvas.getElementOffset(this);
136 this.gutterGroup.addView(view);
138 createContainer: function(nodes, params) {
139 var toret = container.create(nodes, params, this);
140 this.containers.push(toret);
143 removeContainer: function(container) {
145 if((idx = this.containers.indexOf(container)) !== -1) {
146 this.containers.splice(idx, 1);
149 handle: function(event) {
150 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1),
152 if(event.type === 'nodeAdded' || event.type === 'nodeDetached') {
153 this.containers.some(function(container) {
154 if(container.manages(event.meta.node, event.meta.parent)) {
161 if(!target && this[method]) {
166 target[method](event);
169 createDOM: function() {
170 var wrapper = $('<div>').attr('document-node-element', ''),
171 widgetsContainer = $('<div>')
172 .addClass('canvas-widgets'),
173 contentContainer = $('<div>')
174 .attr('document-element-content', '');
176 wrapper.append(contentContainer, widgetsContainer);
177 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
180 _container: function() {
181 return this.dom.children('[document-element-content]');
183 detach: function(isChild) {
186 if(this.gutterGroup) {
187 this.gutterGroup.remove();
189 if(_.isFunction(this.children)) {
190 this.children().forEach(function(child) {
196 parents = this.parents();
199 parents[0].refreshPath();
204 before: function(params) {
205 return manipulate(this, params, 'before');
208 after: function(params) {
209 return manipulate(this, params, 'after');
212 isBlock: function() {
213 return this.dom.css('display') === 'block';
216 displayAsBlock: function() {
217 this.dom.css('display', 'block');
218 this._container().css('display', 'block');
220 displayInline: function() {
221 this.dom.css('display', 'inline');
222 this._container().css('display', 'inline');
224 displayAs: function(what) {
225 // [this.dom(), this._container()].forEach(e) {
226 // var isBlock = window.getComputedStyle(e).display === 'block';
227 // if(!isBlock && what === 'block') {
228 // e.css('display', what);
229 // } else if(isBlock && what === 'inline') {
233 this.dom.css('display', what);
234 this._container().css('display', what);
239 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
240 var DocumentTextElement = function(wlxmlTextNode, canvas) {
241 DocumentElement.call(this, wlxmlTextNode, canvas);
244 $.extend(DocumentTextElement, {
245 isContentContainer: function(htmlElement) {
246 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
250 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
252 $.extend(DocumentTextElement.prototype, {
253 createDOM: function() {
255 .attr('document-text-element', '')
256 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
259 detach: function(isChild) {
265 setText: function(text) {
267 text = utils.unicode.ZWS;
269 if(text !== this.getText()) {
270 this.dom.contents()[0].data = text;
273 handle: function(event) {
274 this.setText(event.meta.node.getText());
276 getText: function(options) {
277 options = _.extend({raw: false}, options || {});
278 var toret = this.dom.text();
280 toret = toret.replace(utils.unicode.ZWS, '');
284 isEmpty: function() {
285 // Having at least Zero Width Space is guaranteed be Content Observer
286 return this.dom.contents()[0].data === utils.unicode.ZWS;
288 after: function(params) {
289 if(params instanceof DocumentTextElement || params.text) {
293 if(params instanceof DocumentNodeElement) {
296 element = this.canvas.createElement(params);
299 this.dom.wrap('<div>');
300 this.dom.parent().after(element.dom);
306 before: function(params) {
307 if(params instanceof DocumentTextElement || params.text) {
311 if(params instanceof DocumentNodeElement) {
314 element = this.canvas.createElement(params);
317 this.dom.wrap('<div>');
318 this.dom.parent().before(element.dom);
325 children: function() {
333 DocumentElement: DocumentElement,
334 DocumentNodeElement: DocumentNodeElement,
335 DocumentTextElement: DocumentTextElement