Switching to explicit inheritance for canvas elements
[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(widgetsContainer, contentContainer);
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         this.canvas = null;
116         if(parents[0]) {
117             parents[0].refreshPath();
118         }
119          return this;
120     },
121     before: function(params) {
122         return manipulate(this, params, 'before');
123
124     },
125     after: function(params) {
126         return manipulate(this, params, 'after');
127     },
128
129     toggleLabel: function(toggle) {
130         var displayCss = toggle ? 'inline-block' : 'none';
131         var label = this.dom.children('.canvas-widgets').find('.canvas-widget-label');
132         label.css('display', displayCss);
133         this.toggleHighlight(toggle);
134     },
135
136     toggleHighlight: function(toggle) {
137         this._container().toggleClass('highlighted-element', toggle);
138     },
139
140     isBlock: function() {
141         return this.dom.css('display') === 'block';
142     },
143
144     displayAsBlock: function() {
145         this.dom.css('display', 'block');
146         this._container().css('display', 'block');
147     },
148     displayInline: function() {
149         this.dom.css('display', 'inline');
150         this._container().css('display', 'inline');
151     },
152     displayAs: function(what) {
153         // [this.dom(), this._container()].forEach(e) {
154         //     var isBlock = window.getComputedStyle(e).display === 'block';
155         //     if(!isBlock && what === 'block') {
156         //         e.css('display', what);
157         //     } else if(isBlock && what === 'inline') {
158         //         e.css('display')
159         //     }
160         // })
161         this.dom.css('display', what);
162         this._container().css('display', what);
163     }
164 });
165
166
167 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
168 var DocumentTextElement = function(wlxmlTextNode, canvas) {
169     DocumentElement.call(this, wlxmlTextNode, canvas);
170 };
171
172 $.extend(DocumentTextElement, {
173     isContentContainer: function(htmlElement) {
174         return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
175     }
176 });
177
178 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
179
180 $.extend(DocumentTextElement.prototype, {
181     createDOM: function() {
182         var dom = $('<div>')
183             .attr('document-text-element', '')
184             .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
185         return dom;
186     },
187     detach: function() {
188         this.dom.detach();
189         this.canvas = null;
190         return this;
191     },
192     setText: function(text) {
193         this.dom.contents()[0].data = text;
194     },
195     getText: function(options) {
196         options = _.extend({raw: false}, options || {});
197         var toret = this.dom.text();
198         if(!options.raw) {
199             toret = toret.replace(utils.unicode.ZWS, '');
200         }
201         return toret;
202     },
203     isEmpty: function() {
204         // Having at least Zero Width Space is guaranteed be Content Observer
205         return this.dom.contents()[0].data === utils.unicode.ZWS;
206     },
207     after: function(params) {
208         if(params instanceof DocumentTextElement || params.text) {
209             return false;
210         }
211         var element;
212         if(params instanceof DocumentNodeElement) {
213             element = params;
214         } else {
215             element = this.canvas.createElement(params);
216         }
217         this.dom.wrap('<div>');
218         this.dom.parent().after(element.dom);
219         this.dom.unwrap();
220         this.refreshPath();
221         return element;
222     },
223     before: function(params) {
224         if(params instanceof DocumentTextElement || params.text) {
225             return false;
226         }
227         var element;
228         if(params instanceof DocumentNodeElement) {
229             element = params;
230         } else {
231             element = this.canvas.createElement(params);
232         }
233         this.dom.wrap('<div>');
234         this.dom.parent().before(element.dom);
235         this.dom.unwrap();
236         this.refreshPath();
237         return element;
238     },
239
240     toggleHighlight: function() {
241         // do nothing for now
242     },
243     children: function() {
244         return [];
245     }
246
247 });
248
249
250 return {
251     DocumentElement: DocumentElement,
252     DocumentNodeElement: DocumentNodeElement,
253     DocumentTextElement: DocumentTextElement
254 };
255
256 });