4 'modules/documentCanvas/canvas/utils'
5 ], function($, _, utils) {
8 /* global Node:false */
10 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
11 var DocumentElement = function(wlxmlNode, canvas) {
12 this.wlxmlNode = wlxmlNode;
19 this.dom = this.createDOM();
20 this.dom.data('canvas-element', this);
21 this.wlxmlNode.setData('canvasElement', this);
24 $.extend(DocumentElement.prototype, {
25 refreshPath: function() {
26 this.parents().forEach(function(parent) {
34 updateState: function(toUpdate) {
37 .filter(function(key) {
38 return this.state.hasOwnProperty(key);
40 .forEach(function(key) {
41 if(this.state !== toUpdate[key]) {
42 this.state[key] = changes[key] = toUpdate[key];
45 if(_.isFunction(this.onStateChange)) {
46 this.onStateChange(changes);
47 if(_.isBoolean(changes.active)) {
50 while(ptr && ptr.wlxmlNode.getTagName() === 'span') {
53 if(ptr && ptr.gutterGroup) {
54 ptr.gutterGroup.show();
61 var parents = this.dom.parents('[document-node-element]');
63 return this.canvas.getDocumentElement(parents[0]);
70 parent = this.parent();
73 parent = parent.parent();
78 sameNode: function(other) {
79 return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
83 this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
90 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
91 var DocumentNodeElement = function(wlxmlNode, canvas) {
92 DocumentElement.call(this, wlxmlNode, canvas);
97 var manipulate = function(e, params, action) {
99 if(params instanceof DocumentElement) {
102 element = e.canvas.createElement(params);
105 e.dom[action](element.dom);
111 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
114 $.extend(DocumentNodeElement.prototype, {
115 defaultDisplayStyle: 'block',
117 addWidget: function(widget) {
118 this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
120 clearWidgets: function() {
121 this.dom.children('.canvas-widgets').empty();
123 addToGutter: function(view) {
124 if(!this.gutterGroup) {
125 this.gutterGroup = this.canvas.gutter.createViewGroup({
126 offsetHint: function() {
127 return this.canvas.getElementOffset(this);
131 this.gutterGroup.addView(view);
133 handle: function(event) {
134 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
139 createDOM: function() {
140 var wrapper = $('<div>').attr('document-node-element', ''),
141 widgetsContainer = $('<div>')
142 .addClass('canvas-widgets'),
143 contentContainer = $('<div>')
144 .attr('document-element-content', '');
146 wrapper.append(contentContainer, widgetsContainer);
147 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
150 _container: function() {
151 return this.dom.children('[document-element-content]');
153 detach: function(isChild) {
156 if(this.gutterGroup) {
157 this.gutterGroup.remove();
159 if(_.isFunction(this.children)) {
160 this.children().forEach(function(child) {
166 parents = this.parents();
169 parents[0].refreshPath();
174 before: function(params) {
175 return manipulate(this, params, 'before');
178 after: function(params) {
179 return manipulate(this, params, 'after');
182 isBlock: function() {
183 return this.dom.css('display') === 'block';
186 displayAsBlock: function() {
187 this.dom.css('display', 'block');
188 this._container().css('display', 'block');
190 displayInline: function() {
191 this.dom.css('display', 'inline');
192 this._container().css('display', 'inline');
194 displayAs: function(what) {
195 // [this.dom(), this._container()].forEach(e) {
196 // var isBlock = window.getComputedStyle(e).display === 'block';
197 // if(!isBlock && what === 'block') {
198 // e.css('display', what);
199 // } else if(isBlock && what === 'inline') {
203 this.dom.css('display', what);
204 this._container().css('display', what);
209 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
210 var DocumentTextElement = function(wlxmlTextNode, canvas) {
211 DocumentElement.call(this, wlxmlTextNode, canvas);
214 $.extend(DocumentTextElement, {
215 isContentContainer: function(htmlElement) {
216 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
220 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
222 $.extend(DocumentTextElement.prototype, {
223 createDOM: function() {
225 .attr('document-text-element', '')
226 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
229 detach: function(isChild) {
235 setText: function(text) {
237 text = utils.unicode.ZWS;
239 if(text !== this.getText()) {
240 this.dom.contents()[0].data = text;
243 handle: function(event) {
244 this.setText(event.meta.node.getText());
246 getText: function(options) {
247 options = _.extend({raw: false}, options || {});
248 var toret = this.dom.text();
250 toret = toret.replace(utils.unicode.ZWS, '');
254 isEmpty: function() {
255 // Having at least Zero Width Space is guaranteed be Content Observer
256 return this.dom.contents()[0].data === utils.unicode.ZWS;
258 after: function(params) {
259 if(params instanceof DocumentTextElement || params.text) {
263 if(params instanceof DocumentNodeElement) {
266 element = this.canvas.createElement(params);
269 this.dom.wrap('<div>');
270 this.dom.parent().after(element.dom);
276 before: function(params) {
277 if(params instanceof DocumentTextElement || params.text) {
281 if(params instanceof DocumentNodeElement) {
284 element = this.canvas.createElement(params);
287 this.dom.wrap('<div>');
288 this.dom.parent().before(element.dom);
295 children: function() {
303 DocumentElement: DocumentElement,
304 DocumentNodeElement: DocumentNodeElement,
305 DocumentTextElement: DocumentTextElement