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.contextMenuActions = [];
103 var manipulate = function(e, params, action) {
105 if(params instanceof DocumentElement) {
108 element = e.canvas.createElement(params);
111 e.dom[action](element.dom);
117 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
120 $.extend(DocumentNodeElement.prototype, {
121 defaultDisplayStyle: 'block',
123 addWidget: function(widget) {
124 this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
126 clearWidgets: function() {
127 this.dom.children('.canvas-widgets').empty();
129 addToGutter: function(view) {
130 if(!this.gutterGroup) {
131 this.gutterGroup = this.canvas.gutter.createViewGroup({
132 offsetHint: function() {
133 return this.canvas.getElementOffset(this);
137 this.gutterGroup.addView(view);
139 createContainer: function(nodes, params) {
140 var toret = container.create(nodes, params, this);
141 this.containers.push(toret);
144 removeContainer: function(container) {
146 if((idx = this.containers.indexOf(container)) !== -1) {
147 this.containers.splice(idx, 1);
150 addToContextMenu: function(actionFqName) {
151 this.contextMenuActions.push(this.canvas.createAction(actionFqName));
153 handle: function(event) {
154 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1),
156 if(event.type === 'nodeAdded' || event.type === 'nodeDetached') {
157 this.containers.some(function(container) {
158 if(container.manages(event.meta.node, event.meta.parent)) {
165 if(!target && this[method]) {
170 target[method](event);
173 createDOM: function() {
174 var wrapper = $('<div>').attr('document-node-element', ''),
175 widgetsContainer = $('<div>')
176 .addClass('canvas-widgets'),
177 contentContainer = $('<div>')
178 .attr('document-element-content', '');
180 wrapper.append(contentContainer, widgetsContainer);
181 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
184 _container: function() {
185 return this.dom.children('[document-element-content]');
187 detach: function(isChild) {
190 if(this.gutterGroup) {
191 this.gutterGroup.remove();
193 if(_.isFunction(this.children)) {
194 this.children().forEach(function(child) {
200 parents = this.parents();
203 parents[0].refreshPath();
208 before: function(params) {
209 return manipulate(this, params, 'before');
212 after: function(params) {
213 return manipulate(this, params, 'after');
216 isBlock: function() {
217 return this.dom.css('display') === 'block';
220 displayAsBlock: function() {
221 this.dom.css('display', 'block');
222 this._container().css('display', 'block');
224 displayInline: function() {
225 this.dom.css('display', 'inline');
226 this._container().css('display', 'inline');
228 displayAs: function(what) {
229 // [this.dom(), this._container()].forEach(e) {
230 // var isBlock = window.getComputedStyle(e).display === 'block';
231 // if(!isBlock && what === 'block') {
232 // e.css('display', what);
233 // } else if(isBlock && what === 'inline') {
237 this.dom.css('display', what);
238 this._container().css('display', what);
243 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
244 var DocumentTextElement = function(wlxmlTextNode, canvas) {
245 DocumentElement.call(this, wlxmlTextNode, canvas);
248 $.extend(DocumentTextElement, {
249 isContentContainer: function(htmlElement) {
250 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
254 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
256 $.extend(DocumentTextElement.prototype, {
257 createDOM: function() {
259 .attr('document-text-element', '')
260 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
263 detach: function(isChild) {
269 setText: function(text) {
271 text = utils.unicode.ZWS;
273 if(text !== this.getText()) {
274 this.dom.contents()[0].data = text;
277 handle: function(event) {
278 this.setText(event.meta.node.getText());
280 getText: function(options) {
281 options = _.extend({raw: false}, options || {});
282 var toret = this.dom.text();
284 toret = toret.replace(utils.unicode.ZWS, '');
288 isEmpty: function() {
289 // Having at least Zero Width Space is guaranteed be Content Observer
290 return this.dom.contents()[0].data === utils.unicode.ZWS;
292 after: function(params) {
293 if(params instanceof DocumentTextElement || params.text) {
297 if(params instanceof DocumentNodeElement) {
300 element = this.canvas.createElement(params);
303 this.dom.wrap('<div>');
304 this.dom.parent().after(element.dom);
310 before: function(params) {
311 if(params instanceof DocumentTextElement || params.text) {
315 if(params instanceof DocumentNodeElement) {
318 element = this.canvas.createElement(params);
321 this.dom.wrap('<div>');
322 this.dom.parent().before(element.dom);
329 children: function() {
337 DocumentElement: DocumentElement,
338 DocumentNodeElement: DocumentNodeElement,
339 DocumentTextElement: DocumentTextElement