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);
49 var parents = this.dom.parents('[document-node-element]');
51 return this.canvas.getDocumentElement(parents[0]);
58 parent = this.parent();
61 parent = parent.parent();
66 sameNode: function(other) {
67 return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
71 this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
78 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
79 var DocumentNodeElement = function(wlxmlNode, canvas) {
80 DocumentElement.call(this, wlxmlNode, canvas);
81 wlxmlNode.setData('canvasElement', this);
86 var manipulate = function(e, params, action) {
88 if(params instanceof DocumentElement) {
91 element = e.canvas.createElement(params);
93 e.dom[action](element.dom);
98 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
101 $.extend(DocumentNodeElement.prototype, {
102 defaultDisplayStyle: 'block',
104 addWidget: function(widget) {
105 this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
107 clearWidgets: function() {
108 this.dom.children('.canvas-widgets').empty();
110 handle: function(event) {
111 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
116 createDOM: function() {
117 var wrapper = $('<div>').attr('document-node-element', ''),
118 widgetsContainer = $('<div>')
119 .addClass('canvas-widgets')
120 .attr('contenteditable', false),
121 contentContainer = $('<div>')
122 .attr('document-element-content', '');
124 wrapper.append(contentContainer, widgetsContainer);
125 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
128 _container: function() {
129 return this.dom.children('[document-element-content]');
132 var parents = this.parents();
135 parents[0].refreshPath();
139 before: function(params) {
140 return manipulate(this, params, 'before');
143 after: function(params) {
144 return manipulate(this, params, 'after');
147 isBlock: function() {
148 return this.dom.css('display') === 'block';
151 displayAsBlock: function() {
152 this.dom.css('display', 'block');
153 this._container().css('display', 'block');
155 displayInline: function() {
156 this.dom.css('display', 'inline');
157 this._container().css('display', 'inline');
159 displayAs: function(what) {
160 // [this.dom(), this._container()].forEach(e) {
161 // var isBlock = window.getComputedStyle(e).display === 'block';
162 // if(!isBlock && what === 'block') {
163 // e.css('display', what);
164 // } else if(isBlock && what === 'inline') {
168 this.dom.css('display', what);
169 this._container().css('display', what);
174 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
175 var DocumentTextElement = function(wlxmlTextNode, canvas) {
176 DocumentElement.call(this, wlxmlTextNode, canvas);
179 $.extend(DocumentTextElement, {
180 isContentContainer: function(htmlElement) {
181 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
185 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
187 $.extend(DocumentTextElement.prototype, {
188 createDOM: function() {
190 .attr('document-text-element', '')
191 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
198 setText: function(text) {
200 text = utils.unicode.ZWS;
202 if(text !== this.getText()) {
203 this.dom.contents()[0].data = text;
206 getText: function(options) {
207 options = _.extend({raw: false}, options || {});
208 var toret = this.dom.text();
210 toret = toret.replace(utils.unicode.ZWS, '');
214 isEmpty: function() {
215 // Having at least Zero Width Space is guaranteed be Content Observer
216 return this.dom.contents()[0].data === utils.unicode.ZWS;
218 after: function(params) {
219 if(params instanceof DocumentTextElement || params.text) {
223 if(params instanceof DocumentNodeElement) {
226 element = this.canvas.createElement(params);
228 this.dom.wrap('<div>');
229 this.dom.parent().after(element.dom);
234 before: function(params) {
235 if(params instanceof DocumentTextElement || params.text) {
239 if(params instanceof DocumentNodeElement) {
242 element = this.canvas.createElement(params);
244 this.dom.wrap('<div>');
245 this.dom.parent().before(element.dom);
251 children: function() {
259 DocumentElement: DocumentElement,
260 DocumentNodeElement: DocumentNodeElement,
261 DocumentTextElement: DocumentTextElement