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);
98 this.elementsRegister = canvas.createElementsRegister();
99 this.contextMenuActions = [];
104 var manipulate = function(e, params, action) {
106 if(params instanceof DocumentElement) {
109 element = e.createElement(params);
112 e.dom[action](element.dom);
118 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
121 $.extend(DocumentNodeElement.prototype, {
122 defaultDisplayStyle: 'block',
124 addWidget: function(widget) {
125 this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
127 clearWidgets: function() {
128 this.dom.children('.canvas-widgets').empty();
130 addToGutter: function(view) {
131 if(!this.gutterGroup) {
132 this.gutterGroup = this.canvas.gutter.createViewGroup({
133 offsetHint: function() {
134 return this.canvas.getElementOffset(this);
138 this.gutterGroup.addView(view);
140 createContainer: function(nodes, params) {
141 var toret = container.create(nodes, params, this);
142 this.containers.push(toret);
145 removeContainer: function(container) {
147 if((idx = this.containers.indexOf(container)) !== -1) {
148 this.containers.splice(idx, 1);
151 createElement: function(wlxmlNode) {
152 var parent = this.wlxmlNode.parent() ? utils.getElementForNode(this.wlxmlNode.parent()) : null;
153 return this.canvas.createElement(wlxmlNode, this.elementsRegister, !parent) || parent.createElement(wlxmlNode);
155 addToContextMenu: function(actionFqName) {
156 this.contextMenuActions.push(this.canvas.createAction(actionFqName));
158 handle: function(event) {
159 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1),
161 if(event.type === 'nodeAdded' || event.type === 'nodeDetached') {
162 this.containers.some(function(container) {
163 if(container.manages(event.meta.node, event.meta.parent)) {
170 if(!target && this[method]) {
175 target[method](event);
178 createDOM: function() {
179 var wrapper = $('<div>').attr('document-node-element', ''),
180 widgetsContainer = $('<div>')
181 .addClass('canvas-widgets'),
182 contentContainer = $('<div>')
183 .attr('document-element-content', '');
185 wrapper.append(contentContainer, widgetsContainer);
186 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
189 _container: function() {
190 return this.dom.children('[document-element-content]');
192 detach: function(isChild) {
195 if(this.gutterGroup) {
196 this.gutterGroup.remove();
198 if(_.isFunction(this.children)) {
199 this.children().forEach(function(child) {
205 parents = this.parents();
208 parents[0].refreshPath();
213 before: function(params) {
214 return manipulate(this, params, 'before');
217 after: function(params) {
218 return manipulate(this, params, 'after');
221 isBlock: function() {
222 return this.dom.css('display') === 'block';
225 displayAsBlock: function() {
226 this.dom.css('display', 'block');
227 this._container().css('display', 'block');
229 displayInline: function() {
230 this.dom.css('display', 'inline');
231 this._container().css('display', 'inline');
233 displayAs: function(what) {
234 // [this.dom(), this._container()].forEach(e) {
235 // var isBlock = window.getComputedStyle(e).display === 'block';
236 // if(!isBlock && what === 'block') {
237 // e.css('display', what);
238 // } else if(isBlock && what === 'inline') {
242 this.dom.css('display', what);
243 this._container().css('display', what);
248 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
249 var DocumentTextElement = function(wlxmlTextNode, canvas) {
250 DocumentElement.call(this, wlxmlTextNode, canvas);
253 $.extend(DocumentTextElement, {
254 isContentContainer: function(htmlElement) {
255 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
259 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
261 $.extend(DocumentTextElement.prototype, {
262 createDOM: function() {
264 .attr('document-text-element', '')
265 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
268 detach: function(isChild) {
274 setText: function(text) {
276 text = utils.unicode.ZWS;
278 if(text !== this.getText()) {
279 this.dom.contents()[0].data = text;
282 handle: function(event) {
283 this.setText(event.meta.node.getText());
285 getText: function(options) {
286 options = _.extend({raw: false}, options || {});
287 var toret = this.dom.text();
289 toret = toret.replace(utils.unicode.ZWS, '');
293 isEmpty: function() {
294 // Having at least Zero Width Space is guaranteed be Content Observer
295 return this.dom.contents()[0].data === utils.unicode.ZWS;
297 after: function(params) {
298 if(params instanceof DocumentTextElement || params.text) {
302 if(params instanceof DocumentNodeElement) {
305 element = this.parent().createElement(params);
308 this.dom.wrap('<div>');
309 this.dom.parent().after(element.dom);
315 before: function(params) {
316 if(params instanceof DocumentTextElement || params.text) {
320 if(params instanceof DocumentNodeElement) {
323 element = this.createElement(params);
326 this.dom.wrap('<div>');
327 this.dom.parent().before(element.dom);
334 children: function() {
342 DocumentElement: DocumentElement,
343 DocumentNodeElement: DocumentNodeElement,
344 DocumentTextElement: DocumentTextElement