editor: canvas fix
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / documentElement.js
1 define([
2 'libs/jquery',
3 'libs/underscore',
4 'modules/documentCanvas/canvas/utils'
5 ], function($, _, utils) {
6     
7 'use strict';
8 /* global Node:false */
9
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;
13     this.canvas = canvas;
14
15     this.dom = this.createDOM();
16     this.dom.data('canvas-element', this);
17 };
18
19 $.extend(DocumentElement.prototype, {
20     refreshPath: function() {
21         this.parents().forEach(function(parent) {
22             parent.refresh();
23         });
24         this.refresh();
25     },
26     refresh: function() {
27         // noop
28     },
29     parent: function() {
30         var parents = this.dom.parents('[document-node-element]');
31         if(parents.length) {
32             return this.canvas.getDocumentElement(parents[0]);
33         }
34         return null;
35     },
36
37     parents: function() {
38         var parents = [],
39             parent = this.parent();
40         while(parent) {
41             parents.push(parent);
42             parent = parent.parent();
43         }
44         return parents;
45     },
46
47     sameNode: function(other) {
48         return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
49     },
50
51     trigger: function() {
52         this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
53     }
54
55
56 });
57
58
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);
63     this.init(this.dom);
64 };
65
66
67 var manipulate = function(e, params, action) {
68     var element;
69     if(params instanceof DocumentElement) {
70         element = params;
71     } else {
72         element = e.canvas.createElement(params);
73     }
74     e.dom[action](element.dom);
75     e.refreshPath();
76     return element;
77 };
78
79 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
80
81
82 $.extend(DocumentNodeElement.prototype, {
83     defaultDisplayStyle: 'block',
84     init: function() {},
85     addWidget: function(widget) {
86         this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
87     },
88     clearWidgets: function() {
89         this.dom.children('.canvas-widgets').empty();
90     },
91     handle: function(event) {
92         var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
93         if(this[method]) {
94             this[method](event);
95         }
96     },
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', '');
104         
105         wrapper.append(contentContainer, widgetsContainer);
106         widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
107         return wrapper;
108     },
109     _container: function() {
110         return this.dom.children('[document-element-content]');
111     },
112     detach: function() {
113         var parents = this.parents();
114         this.dom.detach();
115         if(parents[0]) {
116             parents[0].refreshPath();
117         }
118          return this;
119     },
120     before: function(params) {
121         return manipulate(this, params, 'before');
122
123     },
124     after: function(params) {
125         return manipulate(this, params, 'after');
126     },
127
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);
133     },
134
135     markAsCurrent: function() {},
136
137     toggleHighlight: function(toggle) {
138         this._container().toggleClass('highlighted-element', toggle);
139     },
140
141     isBlock: function() {
142         return this.dom.css('display') === 'block';
143     },
144
145     displayAsBlock: function() {
146         this.dom.css('display', 'block');
147         this._container().css('display', 'block');
148     },
149     displayInline: function() {
150         this.dom.css('display', 'inline');
151         this._container().css('display', 'inline');
152     },
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') {
159         //         e.css('display')
160         //     }
161         // })
162         this.dom.css('display', what);
163         this._container().css('display', what);
164     }
165 });
166
167
168 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
169 var DocumentTextElement = function(wlxmlTextNode, canvas) {
170     DocumentElement.call(this, wlxmlTextNode, canvas);
171 };
172
173 $.extend(DocumentTextElement, {
174     isContentContainer: function(htmlElement) {
175         return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
176     }
177 });
178
179 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
180
181 $.extend(DocumentTextElement.prototype, {
182     createDOM: function() {
183         var dom = $('<div>')
184             .attr('document-text-element', '')
185             .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
186         return dom;
187     },
188     detach: function() {
189         this.dom.detach();
190         return this;
191     },
192     setText: function(text) {
193         if(text === '') {
194             text = utils.unicode.ZWS;
195         }
196         if(text !== this.getText()) {
197             this.dom.contents()[0].data = text;
198         }
199     },
200     getText: function(options) {
201         options = _.extend({raw: false}, options || {});
202         var toret = this.dom.text();
203         if(!options.raw) {
204             toret = toret.replace(utils.unicode.ZWS, '');
205         }
206         return toret;
207     },
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;
211     },
212     after: function(params) {
213         if(params instanceof DocumentTextElement || params.text) {
214             return false;
215         }
216         var element;
217         if(params instanceof DocumentNodeElement) {
218             element = params;
219         } else {
220             element = this.canvas.createElement(params);
221         }
222         this.dom.wrap('<div>');
223         this.dom.parent().after(element.dom);
224         this.dom.unwrap();
225         this.refreshPath();
226         return element;
227     },
228     before: function(params) {
229         if(params instanceof DocumentTextElement || params.text) {
230             return false;
231         }
232         var element;
233         if(params instanceof DocumentNodeElement) {
234             element = params;
235         } else {
236             element = this.canvas.createElement(params);
237         }
238         this.dom.wrap('<div>');
239         this.dom.parent().before(element.dom);
240         this.dom.unwrap();
241         this.refreshPath();
242         return element;
243     },
244
245     toggleHighlight: function() {
246         // do nothing for now
247     },
248     children: function() {
249         return [];
250     }
251
252 });
253
254
255 return {
256     DocumentElement: DocumentElement,
257     DocumentNodeElement: DocumentNodeElement,
258     DocumentTextElement: DocumentTextElement
259 };
260
261 });