Skip to content
Snippets Groups Projects
Commit b782a5d9 authored by Géry Debongnie's avatar Géry Debongnie
Browse files

[FIX] web_editor: fix various issues with ace editor

Ace editor is not very smart, it does a rpc for each inherited view, it
tries to load workers before deciding not to use them, and it does way
too much work in general.  This commits tries to make sure that only the
necessary rpcs are done (lazily).

fix some issues with ace editor: lazy loading

fix various issues with ace editor
parent 75f321a1
No related branches found
No related tags found
No related merge requests found
......@@ -127,13 +127,14 @@ var ViewEditor = Widget.extend({
loadViews: function (views) {
var $viewList = this.$('#ace-view-list').empty();
_(this.buildViewGraph(views)).each(function (view) {
var viewGraph = this.buildViewGraph(views);
_(viewGraph).each(function (view) {
if (!view.id) { return; }
this.views[view.id] = view;
new ViewOption(this, view).appendTo($viewList);
this.loadView(view.id);
}.bind(this));
return this.loadView(viewGraph[0].id);
},
buildViewGraph: function (views) {
var activeViews = _.uniq(_.filter(views, function (view) {
......@@ -171,32 +172,36 @@ var ViewEditor = Widget.extend({
loadView: function (id) {
var viewId = parseInt(id, 10);
var self = this;
ajax.jsonRpc('/web/dataset/call', 'call', {
return ajax.jsonRpc('/web/dataset/call', 'call', {
model: 'ir.ui.view',
method: 'read',
args: [[viewId], ['arch'], _.extend(base.get_context(), {'lang': null})],
}).then(function (result) {
var editingSession = self.buffers[viewId] = new ace.EditSession(result[0].arch);
editingSession.setMode("ace/mode/xml");
editingSession.setUndoManager(new ace.UndoManager());
editingSession.setUseWorker(false);
editingSession.on("change", function () {
setTimeout(function () {
var $option = self.$('#ace-view-list').find('[value='+viewId+']');
var bufferName = $option.text();
var dirtyMarker = " (unsaved changes)";
var isDirty = editingSession.getUndoManager().hasUndo();
if (isDirty && bufferName.indexOf(dirtyMarker) < 0) {
$option.text(bufferName + dirtyMarker);
} else if (!isDirty && bufferName.indexOf(dirtyMarker) > 0) {
$option.text(bufferName.substring(0, bufferName.indexOf(dirtyMarker)));
}
}, 1);
});
if (viewId === self.selectedViewId()) {
self.displayView.call(self, viewId);
}
self._displayArch(result[0].arch, viewId);
});
},
_displayArch: function(arch, viewId) {
var self = this;
var editingSession = this.buffers[viewId] = new ace.EditSession(arch);
editingSession.setUseWorker(false);
editingSession.setMode("ace/mode/xml");
editingSession.setUndoManager(new ace.UndoManager());
editingSession.on("change", function () {
setTimeout(function () {
var $option = self.$('#ace-view-list').find('[value='+viewId+']');
var bufferName = $option.text();
var dirtyMarker = " (unsaved changes)";
var isDirty = editingSession.getUndoManager().hasUndo();
if (isDirty && bufferName.indexOf(dirtyMarker) < 0) {
$option.text(bufferName + dirtyMarker);
} else if (!isDirty && bufferName.indexOf(dirtyMarker) > 0) {
$option.text(bufferName.substring(0, bufferName.indexOf(dirtyMarker)));
}
}, 1);
});
if (viewId === self.selectedViewId()) {
self.displayView.call(self, viewId);
}
},
selectedViewId: function () {
return parseInt(this.$('#ace-view-list').val(), 10);
......@@ -212,8 +217,16 @@ var ViewEditor = Widget.extend({
}
},
displaySelectedView: function () {
this.displayView(this.selectedViewId());
this.updateHash();
var self = this;
var viewID = this.selectedViewId();
if (this.buffers[viewID]) {
this.displayView(viewID);
this.updateHash();
} else {
this.loadView(viewID).then(function() {
self.updateHash();
});
}
},
formatXml: function () {
var xml = new XmlDocument(this.aceEditor.getValue());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment