4 'modules/documentCanvas/canvas/utils'
5 ], function($, _, utils) {
8 /* global Node: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;
15 this.$element = this.createDOM();
16 this.$element.data('canvas-element', this);
19 $.extend(DocumentElement.prototype, {
20 refreshPath: function() {
21 this.parents().forEach(function(parent) {
33 var parents = this.$element.parents('[document-node-element]');
35 return this.canvas.getDocumentElement(parents[0]);
42 parent = this.parent();
45 parent = parent.parent();
50 sameNode: function(other) {
51 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
55 //this.canvas.bus.trigger()
62 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
63 var DocumentNodeElement = function(wlxmlNode, canvas) {
64 DocumentElement.call(this, wlxmlNode, canvas);
65 wlxmlNode.setData('canvasElement', this);
66 this.init(this.$element);
70 var manipulate = function(e, params, action) {
72 if(params instanceof DocumentElement) {
75 element = e.canvas.createElement(params);
77 e.dom()[action](element.dom());
82 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
85 $.extend(DocumentNodeElement.prototype, {
86 defaultDisplayStyle: 'block',
87 addWidget: function(widget) {
88 this.$element.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
90 clearWidgets: function() {
91 this.$element.children('.canvas-widgets').empty();
93 handle: function(event) {
94 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
99 createDOM: function() {
100 var wrapper = $('<div>').attr('document-node-element', ''),
101 widgetsContainer = $('<div>')
102 .addClass('canvas-widgets')
103 .attr('contenteditable', false),
104 contentContainer = $('<div>')
105 .attr('document-element-content', '');
107 wrapper.append(widgetsContainer, contentContainer);
108 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
111 _container: function() {
112 return this.dom().children('[document-element-content]');
115 var parents = this.parents();
119 parents[0].refreshPath();
123 before: function(params) {
124 return manipulate(this, params, 'before');
127 after: function(params) {
128 return manipulate(this, params, 'after');
131 toggleLabel: function(toggle) {
132 var displayCss = toggle ? 'inline-block' : 'none';
133 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
134 label.css('display', displayCss);
135 this.toggleHighlight(toggle);
138 toggleHighlight: function(toggle) {
139 this._container().toggleClass('highlighted-element', toggle);
142 isBlock: function() {
143 return this.dom().css('display') === 'block';
146 displayAsBlock: function() {
147 this.dom().css('display', 'block');
148 this._container().css('display', 'block');
150 displayInline: function() {
151 this.dom().css('display', 'inline');
152 this._container().css('display', 'inline');
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') {
163 this.dom().css('display', what);
164 this._container().css('display', what);
169 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
170 var DocumentTextElement = function(wlxmlTextNode, canvas) {
171 DocumentElement.call(this, wlxmlTextNode, canvas);
174 $.extend(DocumentTextElement, {
175 isContentContainer: function(htmlElement) {
176 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
180 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
182 $.extend(DocumentTextElement.prototype, {
183 createDOM: function() {
185 .attr('document-text-element', '')
186 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
194 setText: function(text) {
195 this.dom().contents()[0].data = text;
197 getText: function(options) {
198 options = _.extend({raw: false}, options || {});
199 var toret = this.dom().text();
201 toret = toret.replace(utils.unicode.ZWS, '');
205 isEmpty: function() {
206 // Having at least Zero Width Space is guaranteed be Content Observer
207 return this.dom().contents()[0].data === utils.unicode.ZWS;
209 after: function(params) {
210 if(params instanceof DocumentTextElement || params.text) {
214 if(params instanceof DocumentNodeElement) {
217 element = this.canvas.createElement(params);
219 this.dom().wrap('<div>');
220 this.dom().parent().after(element.dom());
225 before: function(params) {
226 if(params instanceof DocumentTextElement || params.text) {
230 if(params instanceof DocumentNodeElement) {
233 element = this.canvas.createElement(params);
235 this.dom().wrap('<div>');
236 this.dom().parent().before(element.dom());
242 toggleHighlight: function() {
243 // do nothing for now
245 children: function() {
253 DocumentElement: DocumentElement,
254 DocumentNodeElement: DocumentNodeElement,
255 DocumentTextElement: DocumentTextElement