'modules/documentCanvas/canvas/genericElement',
'modules/documentCanvas/canvas/nullElement',
'modules/documentCanvas/canvas/gutter',
-], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter) {
+'libs/text!./canvas.html'
+], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter, canvasTemplate) {
'use strict';
/* global document:false, window:false, Node:false, gettext */
});
-var Canvas = function(wlxmlDocument, elements) {
+var Canvas = function(wlxmlDocument, elements, metadata) {
+ this.metadata = metadata || {};
this.elementsRegister = new ElementsRegister(documentElement.DocumentNodeElement, nullElement);
elements = [
}.bind(this));
this.eventBus = _.extend({}, Backbone.Events);
- this.wrapper = $('<div style="display: table; width: 96%"><div style="display: table-row" class="crow"></div></div>');
+ this.dom = $(canvasTemplate);
+ this.rootWrapper = this.dom.find('.root-wrapper');
this.gutter = gutter.create();
this.gutterView = new gutter.GutterView(this.gutter);
- this.rootWrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
- this.wrapper.find('.crow').append(this.rootWrapper, this.gutterView.dom);
+ this.dom.find('.view-row').append(this.gutterView.dom);
this.wlxmlListener = wlxmlListener.create(this);
this.loadWlxmlDocument(wlxmlDocument);
$.extend(Canvas.prototype, Backbone.Events, {
getElementOffset: function(element) {
- return element.dom.offset().top - this.wrapper.offset().top;
+ return element.dom.offset().top - this.dom.offset().top;
},
loadWlxmlDocument: function(wlxmlDocument) {
},
reloadRoot: function() {
+ if(this.rootElement) {
+ this.rootElement.detach();
+ }
this.rootElement = this.createElement(this.wlxmlDocument.root);
- this.rootWrapper.empty();
this.rootWrapper.append(this.rootElement.dom);
},
/* globals MutationObserver */
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
- if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
+ if(canvas.dom[0].contains(mutation.target) && documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
observer.disconnect();
if(mutation.target.data === '') {
mutation.target.data = utils.unicode.ZWS;
mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, '');
canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
}
- observer.observe(canvas.wrapper[0], config);
+ observer.observe(canvas.dom[0], config);
var textElement = canvas.getDocumentElement(mutation.target),
toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
},
view: function() {
- return this.wrapper;
+ return this.dom;
},
doc: function() {
if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) {
return byBrowser;
}
- return e.getVerticallyFirstTextElement();
+ return _.isFunction(e.getVerticallyFirstTextElement) ? e.getVerticallyFirstTextElement({considerChildren: false}) : null;
}.bind(this);
var _markAsCurrent = function(element) {
if(element instanceof documentElement.DocumentTextElement) {
if(position.element) {
this._moveCaretToTextElement(position.element, position.offset);
}
- },
-
- toggleGrid: function() {
- this.rootWrapper.toggleClass('grid-on');
- this.trigger('changed');
- },
- isGridToggled: function() {
- return this.rootWrapper.hasClass('grid-on');
}
});
return doc.createFragment(doc.CaretFragment, {node: anchorNode, offset: this.nativeSelection.anchorOffset});
}
if(this.type === 'textSelection') {
+ if(!anchorNode || !focusNode) {
+ return;
+ }
if(anchorNode.isSiblingOf(focusNode)) {
return doc.createFragment(doc.TextRangeFragment, {
node1: anchorNode,
});
return {
- fromXMLDocument: function(wlxmlDocument, elements) {
- return new Canvas(wlxmlDocument, elements);
+ fromXMLDocument: function(wlxmlDocument, elements, metadata) {
+ return new Canvas(wlxmlDocument, elements, metadata);
}
};