refactoring: DocumentElement.dom
[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     addWidget: function(widget) {
85         this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
86     },
87     clearWidgets: function() {
88         this.dom.children('.canvas-widgets').empty();
89     },
90     handle: function(event) {
91         var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
92         if(this[method]) {
93             this[method](event);
94         }
95     },
96     createDOM: function() {
97         var wrapper = $('<div>').attr('document-node-element', ''),
98             widgetsContainer = $('<div>')
99                 .addClass('canvas-widgets')
100                 .attr('contenteditable', false),
101             contentContainer = $('<div>')
102                 .attr('document-element-content', '');
103         
104         wrapper.append(widgetsContainer, contentContainer);
105         widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
106         return wrapper;
107     },
108     _container: function() {
109         return this.dom.children('[document-element-content]');
110     },
111     detach: function() {
112         var parents = this.parents();
113         this.dom.detach();
114         this.canvas = null;
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     toggleHighlight: function(toggle) {
136         this._container().toggleClass('highlighted-element', toggle);
137     },
138
139     isBlock: function() {
140         return this.dom.css('display') === 'block';
141     },
142
143     displayAsBlock: function() {
144         this.dom.css('display', 'block');
145         this._container().css('display', 'block');
146     },
147     displayInline: function() {
148         this.dom.css('display', 'inline');
149         this._container().css('display', 'inline');
150     },
151     displayAs: function(what) {
152         // [this.dom(), this._container()].forEach(e) {
153         //     var isBlock = window.getComputedStyle(e).display === 'block';
154         //     if(!isBlock && what === 'block') {
155         //         e.css('display', what);
156         //     } else if(isBlock && what === 'inline') {
157         //         e.css('display')
158         //     }
159         // })
160         this.dom.css('display', what);
161         this._container().css('display', what);
162     }
163 });
164
165
166 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
167 var DocumentTextElement = function(wlxmlTextNode, canvas) {
168     DocumentElement.call(this, wlxmlTextNode, canvas);
169 };
170
171 $.extend(DocumentTextElement, {
172     isContentContainer: function(htmlElement) {
173         return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
174     }
175 });
176
177 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
178
179 $.extend(DocumentTextElement.prototype, {
180     createDOM: function() {
181         var dom = $('<div>')
182             .attr('document-text-element', '')
183             .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
184         return dom;
185     },
186     detach: function() {
187         this.dom.detach();
188         this.canvas = null;
189         return this;
190     },
191     setText: function(text) {
192         this.dom.contents()[0].data = text;
193     },
194     getText: function(options) {
195         options = _.extend({raw: false}, options || {});
196         var toret = this.dom.text();
197         if(!options.raw) {
198             toret = toret.replace(utils.unicode.ZWS, '');
199         }
200         return toret;
201     },
202     isEmpty: function() {
203         // Having at least Zero Width Space is guaranteed be Content Observer
204         return this.dom.contents()[0].data === utils.unicode.ZWS;
205     },
206     after: function(params) {
207         if(params instanceof DocumentTextElement || params.text) {
208             return false;
209         }
210         var element;
211         if(params instanceof DocumentNodeElement) {
212             element = params;
213         } else {
214             element = this.canvas.createElement(params);
215         }
216         this.dom.wrap('<div>');
217         this.dom.parent().after(element.dom);
218         this.dom.unwrap();
219         this.refreshPath();
220         return element;
221     },
222     before: function(params) {
223         if(params instanceof DocumentTextElement || params.text) {
224             return false;
225         }
226         var element;
227         if(params instanceof DocumentNodeElement) {
228             element = params;
229         } else {
230             element = this.canvas.createElement(params);
231         }
232         this.dom.wrap('<div>');
233         this.dom.parent().before(element.dom);
234         this.dom.unwrap();
235         this.refreshPath();
236         return element;
237     },
238
239     toggleHighlight: function() {
240         // do nothing for now
241     },
242     children: function() {
243         return [];
244     }
245
246 });
247
248
249 return {
250     DocumentElement: DocumentElement,
251     DocumentNodeElement: DocumentNodeElement,
252     DocumentTextElement: DocumentTextElement
253 };
254
255 });