1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
odoo.define('web.AttachDocument', function (require) {
"use static";
var core = require('web.core');
var framework = require('web.framework');
var widgetRegistry = require('web.widget_registry');
var Widget = require('web.Widget');
var _t = core._t;
var AttachDocument = Widget.extend({
template: 'AttachDocument',
events: {
'click': '_onClickAttachDocument',
'change input.o_input_file': '_onFileChanged',
},
/**
* @constructor
* @param {Widget} parent
* @param {Object} record
* @param {Object} nodeInfo
*/
init: function (parent, record, nodeInfo) {
this._super.apply(this, arguments);
this.res_id = record.res_id;
this.res_model = record.model;
this.state = record;
this.node = nodeInfo;
this.fileuploadID = _.uniqueId('o_fileupload');
},
/**
* @override
*/
start: function () {
$(window).on(this.fileuploadID, this._onFileLoaded.bind(this));
return this._super.apply(this, arguments);
},
/**
* @override
*/
destroy: function () {
$(window).off(this.fileuploadID);
this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------
/**
* Helper function to display a warning that some fields have an invalid
* value. This is used when a save operation cannot be completed.
*
* @private
* @param {string[]} invalidFields - list of field names
*/
_notifyInvalidFields: function (invalidFields) {
var fields = this.state.fields;
var warnings = invalidFields.map(function (fieldName) {
var fieldStr = fields[fieldName].string;
return _.str.sprintf('<li>%s</li>', _.escape(fieldStr));
});
warnings.unshift('<ul>');
warnings.push('</ul>');
this.do_warn(_t("Invalid fields:"), warnings.join(''));
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* Opens File Explorer dialog if all fields are valid and record is saved
*
* @private
* @param {Event} ev
*/
_onClickAttachDocument: function (ev) {
if ($(ev.target).is('input.o_input_file')) {
return;
}
var fieldNames = this.getParent().canBeSaved(this.state.id);
if (fieldNames.length) {
return this._notifyInvalidFields(fieldNames);
}
// We want to save record on widget click and then open File Selection Explorer
// but due to this security restriction give warning to save record first.
// https://stackoverflow.com/questions/29728705/trigger-click-on-input-file-on-asynchronous-ajax-done/29873845#29873845
if (!this.res_id) {
return this.do_warn(false, _t('Please save before attaching a file'));
}
this.$('input.o_input_file').trigger('click');
},
/**
* Submits file
*
* @private
* @param {Event} ev
*/
_onFileChanged: function (ev) {
ev.stopPropagation();
this.$('form.o_form_binary_form').trigger('submit');
framework.blockUI();
},
/**
* Call action given as node attribute after file submission
*
* @private
*/
_onFileLoaded: function () {
var self = this;
// the first argument isn't a file but the jQuery.Event
var files = Array.prototype.slice.call(arguments, 1);
return new Promise(function (resolve) {
if (self.node.attrs.action) {
self._rpc({
model: self.res_model,
method: self.node.attrs.action,
args: [self.res_id],
kwargs: {
attachment_ids: _.map(files, function (file) {
return file.id;
}),
}
}).then(function () {
resolve();
});
} else {
resolve();
}
}).then(function () {
self.trigger_up('reload');
framework.unblockUI();
});
},
});
widgetRegistry.add('attach_document', AttachDocument);
});
|