[REF] base_import: split import button code out of action

This commit is contained in:
Géry Debongnie
2017-04-11 19:44:38 +02:00
committed by Aaron Bohy
parent 1fadf4a793
commit 03fabb0c4a
4 changed files with 123 additions and 109 deletions
@@ -3,10 +3,6 @@ odoo.define('base_import.import', function (require) {
var ControlPanelMixin = require('web.ControlPanelMixin');
var core = require('web.core');
var KanbanController = require('web.KanbanController');
var KanbanView = require('web.KanbanView');
var ListController = require('web.ListController');
var ListView = require('web.ListView');
var session = require('web.session');
var time = require('web.time');
var Widget = require('web.Widget');
@@ -48,109 +44,6 @@ function jsonp(form, attributes, callback) {
$(form).ajaxSubmit(attributes);
}
// Mixins that enable the 'Import' feature
var ImportViewMixin = {
init: function (viewInfo, params) {
var importEnabled = 'import_enabled' in params ? params.import_enabled : true;
// if true, the 'Import' button will be visible
this.controllerParams.importEnabled = importEnabled;
},
};
var ImportControllerMixin = {
init: function (parent, model, renderer, params) {
this.importEnabled = params.importEnabled;
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Adds an event listener on the import button.
*
* @private
*/
_bindImport: function () {
if (!this.$buttons) {
return;
}
var self = this;
this.$buttons.on('click', '.o_button_import', function () {
var state = self.model.get(self.handle, {raw: true});
self.do_action({
type: 'ir.actions.client',
tag: 'import',
params: {
model: self.modelName,
context: state.getContext(),
}
}, {
on_reverse_breadcrumb: self.reload.bind(self),
});
});
}
};
// Activate 'Import' feature on List views
ListView.include({
init: function () {
this._super.apply(this, arguments);
ImportViewMixin.init.apply(this, arguments);
},
});
ListController.include({
init: function () {
this._super.apply(this, arguments);
ImportControllerMixin.init.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Extends the renderButtons function of ListView by adding an event listener
* on the import button.
*
* @override
*/
renderButtons: function () {
this._super.apply(this, arguments); // Sets this.$buttons
ImportControllerMixin._bindImport.call(this);
}
});
// Activate 'Import' feature on Kanban views
KanbanView.include({
init: function () {
this._super.apply(this, arguments);
ImportViewMixin.init.apply(this, arguments);
},
});
KanbanController.include({
init: function () {
this._super.apply(this, arguments);
ImportControllerMixin.init.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Extends the renderButtons function of ListView by adding an event listener
* on the import button.
*
* @override
*/
renderButtons: function () {
this._super.apply(this, arguments); // Sets this.$buttons
ImportControllerMixin._bindImport.call(this);
}
});
var DataImport = Widget.extend(ControlPanelMixin, {
template: 'ImportView',
opts: [
@@ -0,0 +1,120 @@
odoo.define('base_import.import_buttons', function (require) {
"use strict";
var KanbanController = require('web.KanbanController');
var KanbanView = require('web.KanbanView');
var ListController = require('web.ListController');
var ListView = require('web.ListView');
// Mixins that enable the 'Import' feature
var ImportViewMixin = {
/**
* @override
*/
init: function (viewInfo, params) {
var importEnabled = 'import_enabled' in params ? params.import_enabled : true;
// if true, the 'Import' button will be visible
this.controllerParams.importEnabled = importEnabled;
},
};
var ImportControllerMixin = {
/**
* @override
*/
init: function (parent, model, renderer, params) {
this.importEnabled = params.importEnabled;
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Adds an event listener on the import button.
*
* @private
*/
_bindImport: function () {
if (!this.$buttons) {
return;
}
var self = this;
this.$buttons.on('click', '.o_button_import', function () {
var state = self.model.get(self.handle, {raw: true});
self.do_action({
type: 'ir.actions.client',
tag: 'import',
params: {
model: self.modelName,
context: state.getContext(),
}
}, {
on_reverse_breadcrumb: self.reload.bind(self),
});
});
}
};
// Activate 'Import' feature on List views
ListView.include({
init: function () {
this._super.apply(this, arguments);
ImportViewMixin.init.apply(this, arguments);
},
});
ListController.include({
init: function () {
this._super.apply(this, arguments);
ImportControllerMixin.init.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Extends the renderButtons function of ListView by adding an event listener
* on the import button.
*
* @override
*/
renderButtons: function () {
this._super.apply(this, arguments); // Sets this.$buttons
ImportControllerMixin._bindImport.call(this);
}
});
// Activate 'Import' feature on Kanban views
KanbanView.include({
init: function () {
this._super.apply(this, arguments);
ImportViewMixin.init.apply(this, arguments);
},
});
KanbanController.include({
init: function () {
this._super.apply(this, arguments);
ImportControllerMixin.init.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Extends the renderButtons function of ListView by adding an event listener
* on the import button.
*
* @override
*/
renderButtons: function () {
this._super.apply(this, arguments); // Sets this.$buttons
ImportControllerMixin._bindImport.call(this);
}
});
});
@@ -5,14 +5,15 @@
<xpath expr="." position="inside">
<link rel="stylesheet" href="/base_import/static/src/less/base_import.less"/>
<script type="text/javascript" src="/base_import/static/lib/javascript-state-machine/state-machine.js"></script>
<script type="text/javascript" src="/base_import/static/src/js/base_import.js"></script>
<script type="text/javascript" src="/base_import/static/src/js/import_action.js"></script>
<script type="text/javascript" src="/base_import/static/src/js/import_buttons.js"></script>
</xpath>
</template>
<template id="qunit_suite" name="base_import_tests" inherit_id="web.qunit_suite">
<xpath expr="//t[@t-set='head']" position="inside">
<script type="text/javascript" src="/base_import/static/tests/base_import_tests.js"></script>
<script type="text/javascript" src="/base_import/static/tests/import_buttons_tests.js"></script>
</xpath>
</template>
</data>