Files
tryton/src/wizard.js
T

300 lines
11 KiB
JavaScript
Raw Normal View History

2013-04-18 17:25:23 +02:00
/* This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. */
(function() {
'use strict';
Sao.Wizard = Sao.class_(Object, {
init: function(name) {
this.widget = jQuery('<div/>', {
'class': 'wizard'
});
2016-07-05 09:54:16 +02:00
this.name = name || '';
this.action_id = null;
2013-04-18 17:25:23 +02:00
this.id = null;
this.ids = null;
this.action = null;
this.context = null;
this.states = {};
this.session_id = null;
this.start_state = null;
this.end_state = null;
this.screen = null;
this.screen_state = null;
this.state = null;
this.session = Sao.Session.current_session;
this.__processing = false;
this.__waiting_response = false;
2015-07-03 19:35:28 +02:00
this.info_bar = new Sao.Window.InfoBar();
2013-04-18 17:25:23 +02:00
},
run: function(attributes) {
this.action = attributes.action;
this.action_id = attributes.data.action_id;
2013-04-18 17:25:23 +02:00
this.id = attributes.data.id;
this.ids = attributes.data.ids;
this.model = attributes.data.model;
this.context = attributes.context;
Sao.rpc({
'method': 'wizard.' + this.action + '.create',
'params': [this.session.context]
}, this.session).then(function(result) {
this.session_id = result[0];
this.start_state = this.state = result[1];
this.end_state = result[2];
this.process();
}.bind(this));
},
process: function() {
if (this.__processing || this.__waiting_response) {
return;
}
var process = function() {
if (this.state == this.end_state) {
this.end();
return;
}
var ctx = jQuery.extend({}, this.context);
ctx.active_id = this.id;
ctx.active_ids = this.ids;
ctx.active_model = this.model;
ctx.action_id = this.action_id;
2013-04-18 17:25:23 +02:00
var data = {};
if (this.screen) {
data[this.screen_state] = this.screen.get_on_change_value();
}
Sao.rpc({
'method': 'wizard.' + this.action + '.execute',
'params': [this.session_id, data, this.state, ctx]
}, this.session).then(function(result) {
if (result.view) {
this.clean();
var view = result.view;
this.update(view.fields_view, view.defaults,
view.buttons);
this.screen_state = view.state;
this.__waiting_response = true;
} else {
this.state = this.end_state;
}
2014-04-01 12:19:19 +02:00
var execute_actions = function execute_actions() {
if (result.actions) {
result.actions.forEach(function(action) {
Sao.Action.exec_action(action[0], action[1],
jQuery.extend({}, this.context));
}.bind(this));
2013-04-18 17:25:23 +02:00
}
}.bind(this);
2013-04-18 17:25:23 +02:00
2014-04-01 12:19:19 +02:00
if (this.state == this.end_state) {
this.end().then(execute_actions);
} else {
execute_actions();
2013-04-18 17:25:23 +02:00
}
this.__processing = false;
}.bind(this), function(result) {
// TODO end for server error.
this.__processing = false;
}.bind(this));
2013-05-10 18:24:26 +02:00
};
process.call(this);
2013-04-18 17:25:23 +02:00
},
destroy: function() {
// TODO
},
end: function() {
return Sao.rpc({
2013-04-18 17:25:23 +02:00
'method': 'wizard.' + this.action + '.delete',
'params': [this.session_id, this.session.context]
}, this.session);
},
clean: function() {
this.widget.children().remove();
this.states = {};
},
response: function(state) {
this.__waiting_response = false;
this.screen.current_view.set_value();
2015-03-16 19:14:57 +01:00
return this.screen.current_record.validate().then(function(validate) {
2013-07-23 10:44:54 +02:00
if ((!validate) && state != this.end_state) {
2015-10-27 10:55:57 +01:00
this.screen.display(true);
2015-07-03 19:35:28 +02:00
this.info_bar.message(
2015-07-16 22:36:11 +02:00
this.screen.invalid_message(), 'danger');
2013-07-23 10:44:54 +02:00
return;
}
2015-07-03 19:35:28 +02:00
this.info_bar.message();
2013-07-23 10:44:54 +02:00
this.state = state;
this.process();
}.bind(this));
2013-04-18 17:25:23 +02:00
},
_get_button: function(definition) {
2013-05-02 17:23:37 +02:00
var button = new Sao.common.Button(definition);
2013-04-18 17:25:23 +02:00
this.states[definition.state] = button;
2015-02-06 00:32:51 +01:00
if (definition.default) {
button.el.addClass('btn-primary');
} else if (definition.state == this.end_state) {
button.el.addClass('btn-link');
}
2013-04-18 17:25:23 +02:00
return button;
},
update: function(view, defaults, buttons) {
buttons.forEach(function(button) {
this._get_button(button);
}.bind(this));
this.screen = new Sao.Screen(view.model,
{mode: [], context: this.context});
this.screen.add_view(view);
this.screen.switch_view();
// TODO record-modified
// TODO title
// TODO toolbar
this.widget.append(this.screen.screen_container.el);
this.screen.new_(false);
this.screen.current_record.set_default(defaults);
2015-10-27 10:55:57 +01:00
this.screen.set_cursor();
2013-04-18 17:25:23 +02:00
}
});
Sao.Wizard.create = function(attributes) {
var win;
if (attributes.window) {
win = new Sao.Wizard.Form(attributes.name);
2015-03-16 19:14:57 +01:00
var tab = new Sao.Tab.Wizard(win);
Sao.Tab.add(tab);
2013-04-18 17:25:23 +02:00
} else {
win = new Sao.Wizard.Dialog(attributes.name);
}
win.run(attributes);
};
2015-03-16 19:14:57 +01:00
Sao.Wizard.Form = Sao.class_(Sao.Wizard, {
init: function(name) {
Sao.Wizard.Form._super.init.call(this);
this.tab = null; // Filled by Sao.Tab.Wizard
2016-07-05 09:54:16 +02:00
this.name = name || '';
2015-03-16 19:14:57 +01:00
this.form = jQuery('<div/>', {
'class': 'wizard-form',
}).append(this.widget);
this.footer = jQuery('<div/>', {
'class': 'modal-footer'
}).appendTo(this.form);
},
clean: function() {
Sao.Wizard.Form._super.clean.call(this);
this.footer.children().remove();
},
_get_button: function(definition) {
var button = Sao.Wizard.Form._super._get_button.call(this,
definition);
this.footer.append(button.el);
button.el.click(function() {
this.response(definition.state);
}.bind(this));
return button;
},
end: function() {
return Sao.Wizard.Form._super.end.call(this).always(function() {
return this.tab.close();
}.bind(this));
}
});
2013-04-18 17:25:23 +02:00
Sao.Wizard.Dialog = Sao.class_(Sao.Wizard, { // TODO nomodal
init: function(name) {
if (!name) {
2015-05-29 10:44:10 +02:00
name = Sao.i18n.gettext('Wizard');
2013-04-18 17:25:23 +02:00
}
Sao.Wizard.Dialog._super.init.call(this);
2015-03-27 11:49:22 +01:00
var dialog = new Sao.Dialog(name, 'wizard-dialog', 'lg');
this.dialog = dialog.modal;
2015-03-27 13:10:28 +01:00
this.content = dialog.content;
2015-03-27 11:49:22 +01:00
this.footer = dialog.footer;
2015-07-03 19:35:28 +02:00
dialog.body.append(this.info_bar.el).append(this.widget);
2013-04-18 17:25:23 +02:00
},
clean: function() {
Sao.Wizard.Dialog._super.clean.call(this);
2015-02-06 00:32:51 +01:00
this.footer.children().remove();
2013-04-18 17:25:23 +02:00
},
_get_button: function(definition) {
var button = Sao.Wizard.Dialog._super._get_button.call(this,
definition);
2015-02-06 00:32:51 +01:00
this.footer.append(button.el);
2015-02-27 10:25:02 +01:00
if (definition['default']) {
this.content.unbind('submit');
this.content.submit(function(e) {
this.response(definition.state);
e.preventDefault();
}.bind(this));
button.el.attr('type', 'submit');
} else {
button.el.click(function() {
this.response(definition.state);
}.bind(this));
}
2013-04-18 17:25:23 +02:00
return button;
},
update: function(view, defaults, buttons) {
2015-02-27 10:25:02 +01:00
this.content.unbind('submit');
2013-04-18 17:25:23 +02:00
Sao.Wizard.Dialog._super.update.call(this, view, defaults,
buttons);
2015-02-06 00:32:51 +01:00
this.dialog.modal('show');
2013-04-18 17:25:23 +02:00
},
destroy: function(action) {
2013-04-18 17:25:23 +02:00
Sao.Wizard.Dialog._super.destroy.call(this);
2015-03-24 21:13:50 +01:00
this.dialog.on('hidden.bsd.modal', function(event) {
jQuery(this).remove();
});
this.dialog.modal('hide');
var dialog = jQuery('.wizard-dialog').filter(':visible')[0];
var is_menu = false;
var screen;
if (!dialog) {
dialog = Sao.Tab.tabs.get_current();
if (dialog) {
if (dialog.screen &&
dialog.screen.model_name != this.model) {
is_menu = true;
screen = Sao.main_menu_screen;
}
} else {
is_menu = true;
screen = Sao.main_menu_screen;
}
}
if (dialog && dialog.screen) {
screen = dialog.screen;
}
if (screen) {
if (screen.current_record && !is_menu) {
var ids;
if (screen.model_name == this.model) {
ids = this.ids;
} else {
// Wizard run form a children record so reload parent
// record
ids = [screen.current_record.id];
}
screen.reload(ids, true);
}
if (action) {
screen.client_action(action);
}
}
2013-04-18 17:25:23 +02:00
},
end: function() {
2014-04-01 12:19:19 +02:00
return Sao.Wizard.Dialog._super.end.call(this).then(
this.destroy.bind(this));
2013-04-18 17:25:23 +02:00
},
show: function() {
2015-02-06 00:32:51 +01:00
this.dialog.modal('show');
2013-04-18 17:25:23 +02:00
},
hide: function() {
2015-02-06 00:32:51 +01:00
this.dialog.modal('hide');
2013-04-18 17:25:23 +02:00
},
state_changed: function() {
this.process();
}
});
}());