').text(def));
});
});
new instance.web.Dialog(self, {
title: _.str.sprintf(_t("Model %s fields"),
self.dataset.model),
width: '95%'}, $root).open();
});
break;
case 'edit_workflow':
return this.do_action({
res_model : 'workflow',
domain : [['osv', '=', this.dataset.model]],
views: [[false, 'list'], [false, 'form'], [false, 'diagram']],
type : 'ir.actions.act_window',
view_type : 'list',
view_mode : 'list'
});
break;
case 'edit':
this.do_edit_resource($option.data('model'), $option.data('id'), { name : $option.text() });
break;
case 'manage_filters':
this.do_action({
res_model: 'ir.filters',
views: [[false, 'list'], [false, 'form']],
type: 'ir.actions.act_window',
context: {
search_default_my_filters: true,
search_default_model_id: this.dataset.model
}
});
break;
case 'print_workflow':
if (current_view.get_selected_ids && current_view.get_selected_ids().length == 1) {
instance.web.blockUI();
var action = {
context: { active_ids: current_view.get_selected_ids() },
report_name: "workflow.instance.graph",
datas: {
model: this.dataset.model,
id: current_view.get_selected_ids()[0],
nested: true,
}
};
this.session.get_file({ url: '/web/report', data: {action: JSON.stringify(action)}, complete: instance.web.unblockUI });
}
break;
default:
if (val) {
console.log("No debug handler for ", val);
}
}
evt.currentTarget.selectedIndex = 0;
},
do_edit_resource: function(model, id, action) {
var action = _.extend({
res_model : model,
res_id : id,
type : 'ir.actions.act_window',
view_type : 'form',
view_mode : 'form',
views : [[false, 'form']],
target : 'new',
flags : {
action_buttons : true,
form : {
resize_textareas : true
}
}
}, action || {});
this.do_action(action);
},
switch_mode: function (view_type, no_store, options) {
var self = this;
return $.when(this._super.apply(this, arguments)).done(function () {
var controller = self.views[self.active_view].controller;
self.$el.find('.oe_debug_view').html(QWeb.render('ViewManagerDebug', {
view: controller,
view_manager: self
}));
self.set_title();
});
},
do_create_view: function(view_type) {
var r = this._super.apply(this, arguments);
var view = this.views[view_type].controller;
view.set({ 'title': this.action.name });
return r;
},
get_action_manager: function() {
var cur = this;
while (cur = cur.getParent()) {
if (cur instanceof instance.web.ActionManager) {
return cur;
}
}
return undefined;
},
set_title: function(title) {
this.$el.find('.oe_breadcrumb_title:first').html(this.get_action_manager().get_title());
},
do_push_state: function(state) {
if (this.getParent() && this.getParent().do_push_state) {
state["view_type"] = this.active_view;
this.url_states[this.active_view] = state;
this.getParent().do_push_state(state);
}
},
do_load_state: function(state, warm) {
var self = this,
defs = [];
if (state.view_type && state.view_type !== this.active_view) {
defs.push(
this.views[this.active_view].deferred.then(function() {
return self.switch_mode(state.view_type, true);
})
);
}
$.when(defs).done(function() {
self.views[self.active_view].controller.do_load_state(state, warm);
});
},
});
instance.web.Sidebar = instance.web.Widget.extend({
init: function(parent) {
var self = this;
this._super(parent);
var view = this.getParent();
this.sections = [
{ 'name' : 'print', 'label' : _t('Print'), },
{ 'name' : 'other', 'label' : _t('More'), }
];
this.items = {
'print' : [],
'other' : []
};
this.fileupload_id = _.uniqueId('oe_fileupload');
$(window).on(this.fileupload_id, function() {
var args = [].slice.call(arguments).slice(1);
self.do_attachement_update(self.dataset, self.model_id,args);
instance.web.unblockUI();
});
},
start: function() {
var self = this;
this._super(this);
this.redraw();
this.$el.on('click','.oe_dropdown_menu li a', function(event) {
var section = $(this).data('section');
var index = $(this).data('index');
var item = self.items[section][index];
if (item.callback) {
item.callback.apply(self, [item]);
} else if (item.action) {
self.on_item_action_clicked(item);
} else if (item.url) {
return true;
}
event.preventDefault();
});
},
redraw: function() {
var self = this;
self.$el.html(QWeb.render('Sidebar', {widget: self}));
// Hides Sidebar sections when item list is empty
this.$('.oe_form_dropdown_section').each(function() {
$(this).toggle(!!$(this).find('li').length);
});
},
/**
* For each item added to the section:
*
* ``label``
* will be used as the item's name in the sidebar, can be html
*
* ``action``
* descriptor for the action which will be executed, ``action`` and
* ``callback`` should be exclusive
*
* ``callback``
* function to call when the item is clicked in the sidebar, called
* with the item descriptor as its first argument (so information
* can be stored as additional keys on the object passed to
* ``add_items``)
*
* ``classname`` (optional)
* ``@class`` set on the sidebar serialization of the item
*
* ``title`` (optional)
* will be set as the item's ``@title`` (tooltip)
*
* @param {String} section_code
* @param {Array<{label, action | callback[, classname][, title]}>} items
*/
add_items: function(section_code, items) {
var self = this;
if (items) {
this.items[section_code].push.apply(this.items[section_code],items);
this.redraw();
}
},
add_toolbar: function(toolbar) {
var self = this;
_.each(['print','action','relate'], function(type) {
var items = toolbar[type];
if (items) {
for (var i = 0; i < items.length; i++) {
items[i] = {
label: items[i]['name'],
action: items[i],
classname: 'oe_sidebar_' + type
}
}
self.add_items(type=='print' ? 'print' : 'other', items);
}
});
},
on_item_action_clicked: function(item) {
var self = this;
self.getParent().sidebar_eval_context().done(function (sidebar_eval_context) {
var ids = self.getParent().get_selected_ids();
if (ids.length == 0) {
instance.web.dialog($("").text(_t("You must choose at least one record.")), { title: _t("Warning"), modal: true });
return false;
}
var active_ids_context = {
active_id: ids[0],
active_ids: ids,
active_model: self.getParent().dataset.model
};
self.rpc("/web/action/load", {
action_id: item.action.id,
context: active_ids_context,
eval_context: new instance.web.CompoundContext(sidebar_eval_context, active_ids_context),
}).done(function(result) {
console.log(result.context);
result.context = new instance.web.CompoundContext(result.context || {}, active_ids_context);
result.flags = result.flags || {};
result.flags.new_window = true;
self.do_action(result, {
on_close: function() {
// reload view
self.getParent().reload();
},
});
});
});
},
do_attachement_update: function(dataset, model_id,args) {
var self = this;
this.dataset = dataset;
this.model_id = model_id;
if (args && args[0]["erorr"]) {
instance.web.dialog($(''),{
modal: true,
title: "OpenERP " + _.str.capitalize(args[0]["title"]),
buttons: [{
text: _t("Ok"),
click: function(){
$(this).dialog("close");
}}]
}).html(args[0]["erorr"]);
}
if (!model_id) {
this.on_attachments_loaded([]);
} else {
var dom = [ ['res_model', '=', dataset.model], ['res_id', '=', model_id], ['type', 'in', ['binary', 'url']] ];
var ds = new instance.web.DataSetSearch(this, 'ir.attachment', dataset.get_context(), dom);
ds.read_slice(['name', 'url', 'type'], {}).done(this.on_attachments_loaded);
}
},
on_attachments_loaded: function(attachments) {
var self = this;
var items = [];
var prefix = this.session.url('/web/binary/saveas', {model: 'ir.attachment', field: 'datas', filename_field: 'name'});
_.each(attachments,function(a) {
a.label = a.name;
if(a.type === "binary") {
a.url = prefix + '&id=' + a.id + '&t=' + (new Date().getTime());
}
});
self.items['files'] = attachments;
self.redraw();
this.$('.oe_sidebar_add_attachment .oe_form_binary_file').change(this.on_attachment_changed);
this.$el.find('.oe_sidebar_delete_item').click(this.on_attachment_delete);
},
on_attachment_changed: function(e) {
var $e = $(e.target);
if ($e.val() !== '') {
this.$el.find('form.oe_form_binary_form').submit();
$e.parent().find('input[type=file]').prop('disabled', true);
$e.parent().find('button').prop('disabled', true).find('img, span').toggle();
this.$('.oe_sidebar_add_attachment span').text(_t('Uploading...'));
instance.web.blockUI();
}
},
on_attachment_delete: function(e) {
var self = this;
e.preventDefault();
e.stopPropagation();
var self = this;
var $e = $(e.currentTarget);
if (confirm(_t("Do you really want to delete this attachment ?"))) {
(new instance.web.DataSet(this, 'ir.attachment')).unlink([parseInt($e.attr('data-id'), 10)]).done(function() {
self.do_attachement_update(self.dataset, self.model_id);
});
}
}
});
instance.web.View = instance.web.Widget.extend({
// name displayed in view switchers
display_name: '',
/**
* Define a view type for each view to allow automatic call to fields_view_get.
*/
view_type: undefined,
init: function(parent, dataset, view_id, options) {
this._super(parent);
this.dataset = dataset;
this.view_id = view_id;
this.set_default_options(options);
},
start: function () {
return this.load_view();
},
load_view: function(context) {
var self = this;
var view_loaded;
if (this.embedded_view) {
view_loaded = $.Deferred();
$.async_when().done(function() {
view_loaded.resolve(self.embedded_view);
});
} else {
if (! this.view_type)
console.warn("view_type is not defined", this);
view_loaded = this.rpc("/web/view/load", {
"model": this.dataset.model,
"view_id": this.view_id,
"view_type": this.view_type,
toolbar: !!this.options.$sidebar,
context: this.dataset.get_context(context)
});
}
return view_loaded.then(function(r) {
self.trigger('view_loaded', r);
// add css classes that reflect the (absence of) access rights
self.$el.addClass('oe_view')
.toggleClass('oe_cannot_create', !self.is_action_enabled('create'))
.toggleClass('oe_cannot_edit', !self.is_action_enabled('edit'))
.toggleClass('oe_cannot_delete', !self.is_action_enabled('delete'));
});
},
set_default_options: function(options) {
this.options = options || {};
_.defaults(this.options, {
// All possible views options should be defaulted here
$sidebar: null,
sidebar_id: null,
action: null,
action_views_ids: {}
});
},
/**
* Fetches and executes the action identified by ``action_data``.
*
* @param {Object} action_data the action descriptor data
* @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
* @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
* @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
* @param {Object} [action_data.context=null] additional action context, to add to the current context
* @param {instance.web.DataSet} dataset a dataset object used to communicate with the server
* @param {Object} [record_id] the identifier of the object on which the action is to be applied
* @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
*/
do_execute_action: function (action_data, dataset, record_id, on_closed) {
var self = this;
var result_handler = function () {
if (on_closed) { on_closed.apply(null, arguments); }
if (self.getParent() && self.getParent().on_action_executed) {
return self.getParent().on_action_executed.apply(null, arguments);
}
};
var context = new instance.web.CompoundContext(dataset.get_context(), action_data.context || {});
var handler = function (r) {
var action = r;
if (action && action.constructor == Object) {
var ncontext = new instance.web.CompoundContext(context);
if (record_id) {
ncontext.add({
active_id: record_id,
active_ids: [record_id],
active_model: dataset.model
});
}
ncontext.add(action.context || {});
return self.rpc('/web/session/eval_domain_and_context', {
contexts: [ncontext],
domains: []
}).then(function (results) {
action.context = results.context;
/* niv: previously we were overriding once more with action_data.context,
* I assumed this was not a correct behavior and removed it
*/
return self.do_action(action, {
on_close: result_handler,
});
}, null);
} else {
self.do_action({"type":"ir.actions.act_window_close"});
return result_handler();
}
};
if (action_data.special === 'cancel') {
return handler({"type":"ir.actions.act_window_close"});
} else if (action_data.type=="object") {
var args = [[record_id]], additional_args = [];
if (action_data.args) {
try {
// Warning: quotes and double quotes problem due to json and xml clash
// Maybe we should force escaping in xml or do a better parse of the args array
additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
args = args.concat(additional_args);
} catch(e) {
console.error("Could not JSON.parse arguments", action_data.args);
}
}
args.push(context);
return dataset.call_button(action_data.name, args).done(handler);
} else if (action_data.type=="action") {
return this.rpc('/web/action/load', { action_id: action_data.name, context: context, do_not_eval: true}).done(handler);
} else {
return dataset.exec_workflow(record_id, action_data.name).done(handler);
}
},
/**
* Directly set a view to use instead of calling fields_view_get. This method must
* be called before start(). When an embedded view is set, underlying implementations
* of instance.web.View must use the provided view instead of any other one.
*
* @param embedded_view A view.
*/
set_embedded_view: function(embedded_view) {
this.embedded_view = embedded_view;
},
do_show: function () {
this.$el.show();
},
do_hide: function () {
this.$el.hide();
},
is_active: function () {
var manager = this.getParent();
return !manager || !manager.active_view
|| manager.views[manager.active_view].controller === this;
}, /**
* Wraps fn to only call it if the current view is the active one. If the
* current view is not active, doesn't call fn.
*
* fn can not return anything, as a non-call to fn can't return anything
* either
*
* @param {Function} fn function to wrap in the active guard
*/
guard_active: function (fn) {
var self = this;
return function () {
if (self.is_active()) {
fn.apply(self, arguments);
}
}
},
do_push_state: function(state) {
if (this.getParent() && this.getParent().do_push_state) {
this.getParent().do_push_state(state);
}
},
do_load_state: function(state, warm) {
},
/**
* Switches to a specific view type
*/
do_switch_view: function() {
this.trigger.apply(this, ['switch_mode'].concat(_.toArray(arguments)));
},
/**
* Cancels the switch to the current view, switches to the previous one
*
* @param {Object} [options]
* @param {Boolean} [options.created=false] resource was created
* @param {String} [options.default=null] view to switch to if no previous view
*/
do_search: function(view) {
},
on_sidebar_export: function() {
new instance.web.DataExport(this, this.dataset).open();
},
sidebar_eval_context: function () {
return $.when({});
},
/**
* Asks the view to reload itself, if the reloading is asynchronous should
* return a {$.Deferred} indicating when the reloading is done.
*/
reload: function () {
return $.when();
},
/**
* Return whether the user can perform the action ('create', 'edit', 'delete') in this view.
* An action is disabled by setting the corresponding attribute in the view's main element,
* like: