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(_.isFunction(this.children)) {
158 this.children().forEach(function(child) {
164 parents = this.parents();
167 parents[0].refreshPath();
172 before: function(params) {
173 return manipulate(this, params, 'before');
176 after: function(params) {
177 return manipulate(this, params, 'after');
180 isBlock: function() {
181 return this.dom.css('display') === 'block';
184 displayAsBlock: function() {
185 this.dom.css('display', 'block');
186 this._container().css('display', 'block');
188 displayInline: function() {
189 this.dom.css('display', 'inline');
190 this._container().css('display', 'inline');
192 displayAs: function(what) {
193 // [this.dom(), this._container()].forEach(e) {
194 // var isBlock = window.getComputedStyle(e).display === 'block';
195 // if(!isBlock && what === 'block') {
196 // e.css('display', what);
197 // } else if(isBlock && what === 'inline') {
201 this.dom.css('display', what);
202 this._container().css('display', what);
207 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
208 var DocumentTextElement = function(wlxmlTextNode, canvas) {
209 DocumentElement.call(this, wlxmlTextNode, canvas);
212 $.extend(DocumentTextElement, {
213 isContentContainer: function(htmlElement) {
214 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
218 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
220 $.extend(DocumentTextElement.prototype, {
221 createDOM: function() {
223 .attr('document-text-element', '')
224 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
227 detach: function(isChild) {
233 setText: function(text) {
235 text = utils.unicode.ZWS;
237 if(text !== this.getText()) {
238 this.dom.contents()[0].data = text;
241 handle: function(event) {
242 this.setText(event.meta.node.getText());
244 getText: function(options) {
245 options = _.extend({raw: false}, options || {});
246 var toret = this.dom.text();
248 toret = toret.replace(utils.unicode.ZWS, '');
252 isEmpty: function() {
253 // Having at least Zero Width Space is guaranteed be Content Observer
254 return this.dom.contents()[0].data === utils.unicode.ZWS;
256 after: function(params) {
257 if(params instanceof DocumentTextElement || params.text) {
261 if(params instanceof DocumentNodeElement) {
264 element = this.canvas.createElement(params);
267 this.dom.wrap('<div>');
268 this.dom.parent().after(element.dom);
274 before: function(params) {
275 if(params instanceof DocumentTextElement || params.text) {
279 if(params instanceof DocumentNodeElement) {
282 element = this.canvas.createElement(params);
285 this.dom.wrap('<div>');
286 this.dom.parent().before(element.dom);
293 children: function() {
301 DocumentElement: DocumentElement,
302 DocumentNodeElement: DocumentNodeElement,
303 DocumentTextElement: DocumentTextElement