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;
15 this.dom = this.createDOM();
16 this.dom.data('canvas-element', this);
19 $.extend(DocumentElement.prototype, {
20 refreshPath: function() {
21 this.parents().forEach(function(parent) {
30 var parents = this.dom.parents('[document-node-element]');
32 return this.canvas.getDocumentElement(parents[0]);
39 parent = this.parent();
42 parent = parent.parent();
47 sameNode: function(other) {
48 return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
52 this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
59 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
60 var DocumentNodeElement = function(wlxmlNode, canvas) {
61 DocumentElement.call(this, wlxmlNode, canvas);
62 wlxmlNode.setData('canvasElement', this);
67 var manipulate = function(e, params, action) {
69 if(params instanceof DocumentElement) {
72 element = e.canvas.createElement(params);
74 e.dom[action](element.dom);
79 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
82 $.extend(DocumentNodeElement.prototype, {
83 defaultDisplayStyle: 'block',
85 addWidget: function(widget) {
86 this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
88 clearWidgets: function() {
89 this.dom.children('.canvas-widgets').empty();
91 handle: function(event) {
92 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
97 createDOM: function() {
98 var wrapper = $('<div>').attr('document-node-element', ''),
99 widgetsContainer = $('<div>')
100 .addClass('canvas-widgets')
101 .attr('contenteditable', false),
102 contentContainer = $('<div>')
103 .attr('document-element-content', '');
105 wrapper.append(contentContainer, widgetsContainer);
106 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
109 _container: function() {
110 return this.dom.children('[document-element-content]');
113 var parents = this.parents();
116 parents[0].refreshPath();
120 before: function(params) {
121 return manipulate(this, params, 'before');
124 after: function(params) {
125 return manipulate(this, params, 'after');
128 toggleLabel: function(toggle) {
129 var displayCss = toggle ? 'inline-block' : 'none';
130 var label = this.dom.children('.canvas-widgets').find('.canvas-widget-label');
131 label.css('display', displayCss);
132 this.toggleHighlight(toggle);
135 markAsCurrent: function() {},
137 toggleHighlight: function(toggle) {
138 this._container().toggleClass('highlighted-element', toggle);
141 isBlock: function() {
142 return this.dom.css('display') === 'block';
145 displayAsBlock: function() {
146 this.dom.css('display', 'block');
147 this._container().css('display', 'block');
149 displayInline: function() {
150 this.dom.css('display', 'inline');
151 this._container().css('display', 'inline');
153 displayAs: function(what) {
154 // [this.dom(), this._container()].forEach(e) {
155 // var isBlock = window.getComputedStyle(e).display === 'block';
156 // if(!isBlock && what === 'block') {
157 // e.css('display', what);
158 // } else if(isBlock && what === 'inline') {
162 this.dom.css('display', what);
163 this._container().css('display', what);
168 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
169 var DocumentTextElement = function(wlxmlTextNode, canvas) {
170 DocumentElement.call(this, wlxmlTextNode, canvas);
173 $.extend(DocumentTextElement, {
174 isContentContainer: function(htmlElement) {
175 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
179 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
181 $.extend(DocumentTextElement.prototype, {
182 createDOM: function() {
184 .attr('document-text-element', '')
185 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
192 setText: function(text) {
194 text = utils.unicode.ZWS;
196 if(text !== this.getText()) {
197 this.dom.contents()[0].data = text;
200 getText: function(options) {
201 options = _.extend({raw: false}, options || {});
202 var toret = this.dom.text();
204 toret = toret.replace(utils.unicode.ZWS, '');
208 isEmpty: function() {
209 // Having at least Zero Width Space is guaranteed be Content Observer
210 return this.dom.contents()[0].data === utils.unicode.ZWS;
212 after: function(params) {
213 if(params instanceof DocumentTextElement || params.text) {
217 if(params instanceof DocumentNodeElement) {
220 element = this.canvas.createElement(params);
222 this.dom.wrap('<div>');
223 this.dom.parent().after(element.dom);
228 before: function(params) {
229 if(params instanceof DocumentTextElement || params.text) {
233 if(params instanceof DocumentNodeElement) {
236 element = this.canvas.createElement(params);
238 this.dom.wrap('<div>');
239 this.dom.parent().before(element.dom);
245 toggleHighlight: function() {
246 // do nothing for now
248 children: function() {
256 DocumentElement: DocumentElement,
257 DocumentNodeElement: DocumentNodeElement,
258 DocumentTextElement: DocumentTextElement