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.$element = this.createDOM();
16 this.$element.data('canvas-element', this);
19 $.extend(DocumentElement.prototype, {
20 refreshPath: function() {
21 this.parents().forEach(function(parent) {
33 var parents = this.$element.parents('[document-node-element]');
35 return this.canvas.getDocumentElement(parents[0]);
42 parent = this.parent();
45 parent = parent.parent();
50 sameNode: function(other) {
51 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
54 getPreviousTextElement: function(includeInvisible) {
55 return this.getNearestTextElement('above', includeInvisible);
58 getNextTextElement: function(includeInvisible) {
59 return this.getNearestTextElement('below', includeInvisible);
62 getNearestTextElement: function(direction, includeInvisible) {
63 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
64 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
65 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
69 //this.canvas.bus.trigger()
76 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
77 var DocumentNodeElement = function(wlxmlNode, canvas) {
78 DocumentElement.call(this, wlxmlNode, canvas);
79 wlxmlNode.setData('canvasElement', this);
80 this.init(this.$element);
84 var manipulate = function(e, params, action) {
86 if(params instanceof DocumentElement) {
89 element = e.canvas.createElement(params);
91 e.dom()[action](element.dom());
96 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
99 $.extend(DocumentNodeElement.prototype, {
100 defaultDisplayStyle: 'block',
101 addWidget: function(widget) {
102 this.$element.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
104 clearWidgets: function() {
105 this.$element.children('.canvas-widgets').empty();
107 handle: function(event) {
108 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
113 createDOM: function() {
114 var wrapper = $('<div>').attr('document-node-element', ''),
115 widgetsContainer = $('<div>')
116 .addClass('canvas-widgets')
117 .attr('contenteditable', false),
118 contentContainer = $('<div>')
119 .attr('document-element-content', '');
121 wrapper.append(widgetsContainer, contentContainer);
122 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
125 _container: function() {
126 return this.dom().children('[document-element-content]');
129 var parents = this.parents();
133 parents[0].refreshPath();
137 before: function(params) {
138 return manipulate(this, params, 'before');
141 after: function(params) {
142 return manipulate(this, params, 'after');
145 toggleLabel: function(toggle) {
146 var displayCss = toggle ? 'inline-block' : 'none';
147 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
148 label.css('display', displayCss);
149 this.toggleHighlight(toggle);
152 toggleHighlight: function(toggle) {
153 this._container().toggleClass('highlighted-element', toggle);
156 isBlock: function() {
157 return this.dom().css('display') === 'block';
160 displayAsBlock: function() {
161 this.dom().css('display', 'block');
162 this._container().css('display', 'block');
164 displayInline: function() {
165 this.dom().css('display', 'inline');
166 this._container().css('display', 'inline');
168 displayAs: function(what) {
169 // [this.dom(), this._container()].forEach(e) {
170 // var isBlock = window.getComputedStyle(e).display === 'block';
171 // if(!isBlock && what === 'block') {
172 // e.css('display', what);
173 // } else if(isBlock && what === 'inline') {
177 this.dom().css('display', what);
178 this._container().css('display', what);
183 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
184 var DocumentTextElement = function(wlxmlTextNode, canvas) {
185 DocumentElement.call(this, wlxmlTextNode, canvas);
188 $.extend(DocumentTextElement, {
189 isContentContainer: function(htmlElement) {
190 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
194 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
196 $.extend(DocumentTextElement.prototype, {
197 createDOM: function() {
199 .attr('document-text-element', '')
200 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
208 setText: function(text) {
209 this.dom().contents()[0].data = text;
211 getText: function(options) {
212 options = _.extend({raw: false}, options || {});
213 var toret = this.dom().text();
215 toret = toret.replace(utils.unicode.ZWS, '');
219 isEmpty: function() {
220 // Having at least Zero Width Space is guaranteed be Content Observer
221 return this.dom().contents()[0].data === utils.unicode.ZWS;
223 after: function(params) {
224 if(params instanceof DocumentTextElement || params.text) {
228 if(params instanceof DocumentNodeElement) {
231 element = this.canvas.createElement(params);
233 this.dom().wrap('<div>');
234 this.dom().parent().after(element.dom());
239 before: function(params) {
240 if(params instanceof DocumentTextElement || params.text) {
244 if(params instanceof DocumentNodeElement) {
247 element = this.canvas.createElement(params);
249 this.dom().wrap('<div>');
250 this.dom().parent().before(element.dom());
256 toggleHighlight: function() {
257 // do nothing for now
259 children: function() {
267 DocumentElement: DocumentElement,
268 DocumentNodeElement: DocumentNodeElement,
269 DocumentTextElement: DocumentTextElement