define([
'libs/jquery-1.9.1.min',
'libs/underscore-min',
+'libs/backbone-min',
'modules/documentCanvas/canvas/documentElement',
'modules/documentCanvas/canvas/utils'
-], function($, _, documentElement, utils) {
+], function($, _, Backbone, documentElement, utils) {
'use strict';
var Canvas = function(wlxml, publisher) {
+ this.eventBus = _.extend({}, Backbone.Events);
this.loadWlxml(wlxml);
this.publisher = publisher ? publisher : function() {};
};
if(d) {
this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
this.wrapper.append(d);
-
+ var canvas = this;
+
this.wrapper.find('*').replaceWith(function() {
var currentTag = $(this);
if(currentTag.attr('wlxml-tag'))
meta: meta,
others: others,
rawChildren: currentTag.contents()
- });
+ }, canvas);
['orig-before', 'orig-after', 'orig-begin', 'orig-end'].forEach(function(attr) {
element.data(attr, '');
this.d = this.wrapper.children(0);
- var canvas = this;
this.wrapper.on('keydown', function(e) {
if(e.which === 13) {
e.preventDefault();
el.toggleLabel(false);
});
+ this.eventBus.on('elementToggled', function(toggle, element) {
+ if(!toggle) {
+ element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', 'above', element.dom()[0]));
+ canvas.setCurrentElement(element);
+ }
+ })
+
} else {
this.d = null;
}
_setupDOMHandler: function(htmlElement) {
this.$element = $(htmlElement);
},
+ bound: function() {
+ return this.canvas !== undefined;
+ },
dom: function() {
return this.$element;
},
};
$.extend(DocumentNodeElement, {
- createDOM: function(params) {
+ createDOM: function(params, canvas) {
var dom = $('<div>')
.attr('document-node-element', ''),
widgetsContainer = $('<div>')
// Make sure widgets aren't navigable with arrow keys
widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
- var element = this.fromHTMLElement(dom[0]);
+ var element = this.fromHTMLElement(dom[0], canvas);
element.setWlxmlTag(params.tag);
if(params.klass)
},
create: function(params, canvas) {
- return this.fromHTMLElement(this.createDOM(params)[0]);
+ return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
},
fromHTMLElement: function(htmlElement, canvas) {
if(params instanceof DocumentNodeElement) {
element = params;
} else {
- element = DocumentNodeElement.create(params);
+ element = DocumentNodeElement.create(params, this.canvas);
}
this.dom().wrap('<div>');
this.dom().parent().after(element.dom());
if(params instanceof DocumentNodeElement) {
element = params;
} else {
- element = DocumentNodeElement.create(params);
+ element = DocumentNodeElement.create(params, this.canvas);
}
this.dom().wrap('<div>');
this.dom().parent().before(element.dom());
var DocumentElementWrapper = function(documentElement) {
+ this.documentElement = documentElement;
+
this.addWidget = function(widget) {
documentElement.dom().find('.canvas-widgets').append(widget);
};
this.toggle = function(toggle) {
documentElement._container().toggle(toggle);
}
+
+ var eventBus = documentElement.bound() ? documentElement.canvas.eventBus :
+ {trigger: function() {}};
+ this.trigger = function() {
+ eventBus.trigger.apply(eventBus, arguments);
+ }
+
}
var getDisplayStyle = function(tag, klass) {
this.hideButton = widgets.hideButton(closeHandler);
this.el.addWidget(this.hideButton);
- this.toggle(false);
+ this.toggle(false, {silent: true});
},
- toggle: function(toggle) {
+ toggle: function(toggle, options) {
+ options = options || {};
this.hideButton.toggle(toggle);
this.footnoteHandler.toggle(!toggle);
this.el.setDisplayStyle(toggle ? 'block' : 'inline');
this.el.toggle(toggle);
+ if(!options.silent)
+ this.el.trigger('elementToggled', toggle, this.el.documentElement);
}
})
managers.set('aside', 'footnote', FootnoteManager);