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]');
155 var parents = this.parents();
158 parents[0].refreshPath();
162 before: function(params) {
163 return manipulate(this, params, 'before');
166 after: function(params) {
167 return manipulate(this, params, 'after');
170 isBlock: function() {
171 return this.dom.css('display') === 'block';
174 displayAsBlock: function() {
175 this.dom.css('display', 'block');
176 this._container().css('display', 'block');
178 displayInline: function() {
179 this.dom.css('display', 'inline');
180 this._container().css('display', 'inline');
182 displayAs: function(what) {
183 // [this.dom(), this._container()].forEach(e) {
184 // var isBlock = window.getComputedStyle(e).display === 'block';
185 // if(!isBlock && what === 'block') {
186 // e.css('display', what);
187 // } else if(isBlock && what === 'inline') {
191 this.dom.css('display', what);
192 this._container().css('display', what);
197 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
198 var DocumentTextElement = function(wlxmlTextNode, canvas) {
199 DocumentElement.call(this, wlxmlTextNode, canvas);
202 $.extend(DocumentTextElement, {
203 isContentContainer: function(htmlElement) {
204 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
208 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
210 $.extend(DocumentTextElement.prototype, {
211 createDOM: function() {
213 .attr('document-text-element', '')
214 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
221 setText: function(text) {
223 text = utils.unicode.ZWS;
225 if(text !== this.getText()) {
226 this.dom.contents()[0].data = text;
229 handle: function(event) {
230 this.setText(event.meta.node.getText());
232 getText: function(options) {
233 options = _.extend({raw: false}, options || {});
234 var toret = this.dom.text();
236 toret = toret.replace(utils.unicode.ZWS, '');
240 isEmpty: function() {
241 // Having at least Zero Width Space is guaranteed be Content Observer
242 return this.dom.contents()[0].data === utils.unicode.ZWS;
244 after: function(params) {
245 if(params instanceof DocumentTextElement || params.text) {
249 if(params instanceof DocumentNodeElement) {
252 element = this.canvas.createElement(params);
255 this.dom.wrap('<div>');
256 this.dom.parent().after(element.dom);
262 before: function(params) {
263 if(params instanceof DocumentTextElement || params.text) {
267 if(params instanceof DocumentNodeElement) {
270 element = this.canvas.createElement(params);
273 this.dom.wrap('<div>');
274 this.dom.parent().before(element.dom);
281 children: function() {
289 DocumentElement: DocumentElement,
290 DocumentNodeElement: DocumentNodeElement,
291 DocumentTextElement: DocumentTextElement