4 'modules/documentCanvas/canvas/utils'
5 ], function($, _, utils) {
8 /* global Node:false, document: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;
16 this.$element.data('canvas-element', this);
19 $.extend(DocumentElement.prototype, {
20 refreshPath: function() {
21 this.parents().forEach(function(parent) {
30 return $.contains(document.documentElement, this.dom()[0]);
36 var parents = this.$element.parents('[document-node-element]');
38 return this.canvas.getDocumentElement(parents[0]);
45 parent = this.parent();
48 parent = parent.parent();
53 sameNode: function(other) {
54 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
57 getPreviousTextElement: function(includeInvisible) {
58 return this.getNearestTextElement('above', includeInvisible);
61 getNextTextElement: function(includeInvisible) {
62 return this.getNearestTextElement('below', includeInvisible);
65 getNearestTextElement: function(direction, includeInvisible) {
66 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
67 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
68 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
72 //this.canvas.bus.trigger()
79 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
80 var DocumentNodeElement = function(wlxmlNode, canvas) {
81 DocumentElement.call(this, wlxmlNode, canvas);
82 wlxmlNode.setData('canvasElement', this);
90 var manipulate = function(e, params, action) {
92 if(params instanceof DocumentElement) {
95 element = e.canvas.createElement(params);
97 e.dom()[action](element.dom());
102 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
105 $.extend(DocumentNodeElement.prototype, {
106 defaultDisplayStyle: 'block',
107 addWidget: function(widget) {
108 this.$element.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
110 clearWidgets: function() {
111 this.$element.children('.canvas-widgets').empty();
113 handle: function(event) {
114 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
119 createDOM: function() {
120 var wrapper = $('<div>').attr('document-node-element', ''),
121 widgetsContainer = $('<div>')
122 .addClass('canvas-widgets')
123 .attr('contenteditable', false),
124 contentContainer = $('<div>')
125 .attr('document-element-content', '');
127 wrapper.append(widgetsContainer, contentContainer);
128 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
129 this.$element = wrapper;
130 this.displayAs(this.defaultDisplayStyle);
132 _container: function() {
133 return this.dom().children('[document-element-content]');
136 var parents = this.parents();
140 parents[0].refreshPath();
144 before: function(params) {
145 return manipulate(this, params, 'before');
148 after: function(params) {
149 return manipulate(this, params, 'after');
152 toggleLabel: function(toggle) {
153 var displayCss = toggle ? 'inline-block' : 'none';
154 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
155 label.css('display', displayCss);
156 this.toggleHighlight(toggle);
159 toggleHighlight: function(toggle) {
160 this._container().toggleClass('highlighted-element', toggle);
163 isBlock: function() {
164 return this.dom().css('display') === 'block';
167 displayAsBlock: function() {
168 this.dom().css('display', 'block');
169 this._container().css('display', 'block');
171 displayInline: function() {
172 this.dom().css('display', 'inline');
173 this._container().css('display', 'inline');
175 displayAs: function(what) {
176 // [this.dom(), this._container()].forEach(e) {
177 // var isBlock = window.getComputedStyle(e).display === 'block';
178 // if(!isBlock && what === 'block') {
179 // e.css('display', what);
180 // } else if(isBlock && what === 'inline') {
184 this.dom().css('display', what);
185 this._container().css('display', what);
190 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
191 var DocumentTextElement = function(wlxmlTextNode, canvas) {
192 DocumentElement.call(this, wlxmlTextNode, canvas);
195 $.extend(DocumentTextElement, {
196 isContentContainer: function(htmlElement) {
197 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
201 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
203 $.extend(DocumentTextElement.prototype, {
204 createDOM: function() {
205 this.$element = $('<div>')
206 .attr('document-text-element', '')
207 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
214 setText: function(text) {
215 this.dom().contents()[0].data = text;
217 getText: function(options) {
218 options = _.extend({raw: false}, options || {});
219 var toret = this.dom().text();
221 toret = toret.replace(utils.unicode.ZWS, '');
225 isEmpty: function() {
226 // Having at least Zero Width Space is guaranteed be Content Observer
227 return this.dom().contents()[0].data === utils.unicode.ZWS;
229 after: function(params) {
230 if(params instanceof DocumentTextElement || params.text) {
234 if(params instanceof DocumentNodeElement) {
237 element = this.canvas.createElement(params);
239 this.dom().wrap('<div>');
240 this.dom().parent().after(element.dom());
245 before: function(params) {
246 if(params instanceof DocumentTextElement || params.text) {
250 if(params instanceof DocumentNodeElement) {
253 element = this.canvas.createElement(params);
255 this.dom().wrap('<div>');
256 this.dom().parent().before(element.dom());
262 toggleHighlight: function() {
263 // do nothing for now
265 children: function() {
271 var SpanElement = function() {
272 DocumentNodeElement.apply(this, Array.prototype.slice.call(arguments, 0));
274 SpanElement.prototype = $.extend(Object.create(DocumentNodeElement.prototype), {
275 defaultDisplayStyle: 'inline',
277 if(this.containsBlock()) {
278 this.displayAsBlock();
280 this.displayInline();
283 refresh: function() {
294 DocumentElement: DocumentElement,
295 DocumentNodeElement: DocumentNodeElement,
296 DocumentTextElement: DocumentTextElement, //,
297 factoryForTag: function(tagName) {
298 return elements[tagName] || DocumentNodeElement;