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);
245 children: function() {
251 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
252 var DocumentTextElement = function(wlxmlTextNode, canvas) {
253 DocumentElement.call(this, wlxmlTextNode, canvas);
256 $.extend(DocumentTextElement, {
257 isContentContainer: function(htmlElement) {
258 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
262 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
264 $.extend(DocumentTextElement.prototype, {
265 createDOM: function() {
267 .attr('document-text-element', '')
268 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
271 detach: function(isChild) {
277 setText: function(text) {
279 text = utils.unicode.ZWS;
281 if(text !== this.getText()) {
282 this.dom.contents()[0].data = text;
285 handle: function(event) {
286 this.setText(event.meta.node.getText());
288 getText: function(options) {
289 options = _.extend({raw: false}, options || {});
290 var toret = this.dom.text();
292 toret = toret.replace(utils.unicode.ZWS, '');
296 isEmpty: function() {
297 // Having at least Zero Width Space is guaranteed be Content Observer
298 return this.dom.contents()[0].data === utils.unicode.ZWS;
300 after: function(params) {
301 if(params instanceof DocumentTextElement || params.text) {
305 if(params instanceof DocumentNodeElement) {
308 element = this.parent().createElement(params);
311 this.dom.wrap('<div>');
312 this.dom.parent().after(element.dom);
318 before: function(params) {
319 if(params instanceof DocumentTextElement || params.text) {
323 if(params instanceof DocumentNodeElement) {
326 element = this.createElement(params);
329 this.dom.wrap('<div>');
330 this.dom.parent().before(element.dom);
337 children: function() {
345 DocumentElement: DocumentElement,
346 DocumentNodeElement: DocumentNodeElement,
347 DocumentTextElement: DocumentTextElement