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