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);
23 $.extend(DocumentElement.prototype, {
24 refreshPath: function() {
25 this.parents().forEach(function(parent) {
33 updateState: function(toUpdate) {
36 .filter(function(key) {
37 return this.state.hasOwnProperty(key);
39 .forEach(function(key) {
40 if(this.state !== toUpdate[key]) {
41 this.state[key] = changes[key] = toUpdate[key];
44 if(_.isFunction(this.onStateChange)) {
45 this.onStateChange(changes);
46 if(_.isBoolean(changes.active)) {
49 while(ptr && ptr.wlxmlNode.getTagName() === 'span') {
52 if(ptr && ptr.gutterGroup) {
53 ptr.gutterGroup.show();
60 var parents = this.dom.parents('[document-node-element]');
62 return this.canvas.getDocumentElement(parents[0]);
69 parent = this.parent();
72 parent = parent.parent();
77 sameNode: function(other) {
78 return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
82 this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
89 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
90 var DocumentNodeElement = function(wlxmlNode, canvas) {
91 DocumentElement.call(this, wlxmlNode, canvas);
92 wlxmlNode.setData('canvasElement', this);
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 .attr('contenteditable', false),
144 contentContainer = $('<div>')
145 .attr('document-element-content', '');
147 wrapper.append(contentContainer, widgetsContainer);
148 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
151 _container: function() {
152 return this.dom.children('[document-element-content]');
154 detach: function(isChild) {
157 if(this.gutterGroup) {
158 this.gutterGroup.remove();
160 if(_.isFunction(this.children)) {
161 this.children().forEach(function(child) {
167 parents = this.parents();
170 parents[0].refreshPath();
175 before: function(params) {
176 return manipulate(this, params, 'before');
179 after: function(params) {
180 return manipulate(this, params, 'after');
183 isBlock: function() {
184 return this.dom.css('display') === 'block';
187 displayAsBlock: function() {
188 this.dom.css('display', 'block');
189 this._container().css('display', 'block');
191 displayInline: function() {
192 this.dom.css('display', 'inline');
193 this._container().css('display', 'inline');
195 displayAs: function(what) {
196 // [this.dom(), this._container()].forEach(e) {
197 // var isBlock = window.getComputedStyle(e).display === 'block';
198 // if(!isBlock && what === 'block') {
199 // e.css('display', what);
200 // } else if(isBlock && what === 'inline') {
204 this.dom.css('display', what);
205 this._container().css('display', what);
210 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
211 var DocumentTextElement = function(wlxmlTextNode, canvas) {
212 DocumentElement.call(this, wlxmlTextNode, canvas);
215 $.extend(DocumentTextElement, {
216 isContentContainer: function(htmlElement) {
217 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
221 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
223 $.extend(DocumentTextElement.prototype, {
224 createDOM: function() {
226 .attr('document-text-element', '')
227 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
230 detach: function(isChild) {
236 setText: function(text) {
238 text = utils.unicode.ZWS;
240 if(text !== this.getText()) {
241 this.dom.contents()[0].data = text;
244 handle: function(event) {
245 this.setText(event.meta.node.getText());
247 getText: function(options) {
248 options = _.extend({raw: false}, options || {});
249 var toret = this.dom.text();
251 toret = toret.replace(utils.unicode.ZWS, '');
255 isEmpty: function() {
256 // Having at least Zero Width Space is guaranteed be Content Observer
257 return this.dom.contents()[0].data === utils.unicode.ZWS;
259 after: function(params) {
260 if(params instanceof DocumentTextElement || params.text) {
264 if(params instanceof DocumentNodeElement) {
267 element = this.canvas.createElement(params);
270 this.dom.wrap('<div>');
271 this.dom.parent().after(element.dom);
277 before: function(params) {
278 if(params instanceof DocumentTextElement || params.text) {
282 if(params instanceof DocumentNodeElement) {
285 element = this.canvas.createElement(params);
288 this.dom.wrap('<div>');
289 this.dom.parent().before(element.dom);
296 children: function() {
304 DocumentElement: DocumentElement,
305 DocumentNodeElement: DocumentNodeElement,
306 DocumentTextElement: DocumentTextElement