summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/fields/basic_fields.js
diff options
context:
space:
mode:
authorMartin Geubelle <mge@odoo.com>2018-02-05 13:42:17 +0100
committerMartin Geubelle <mge@odoo.com>2018-02-12 11:16:21 +0100
commita7b521fe4401e921dc294ed3019f59c9027b345a (patch)
tree00d805df0f764d27d81f0cadec8d5da6eb6f4201 /addons/web/static/src/js/fields/basic_fields.js
parentc265e47a6335e10467b748ff4a37808ed53263ca (diff)
[MOV] web, mrp: move `pdf_viewer` widget
This widget is generic enough to be used in other modules than mrp so we move it in the web module. Closes #22569
Diffstat (limited to 'addons/web/static/src/js/fields/basic_fields.js')
-rw-r--r--addons/web/static/src/js/fields/basic_fields.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js
index 022fca36470..061345683b6 100644
--- a/addons/web/static/src/js/fields/basic_fields.js
+++ b/addons/web/static/src/js/fields/basic_fields.js
@@ -1313,6 +1313,112 @@ var FieldBinaryFile = AbstractFieldBinary.extend({
},
});
+var FieldPdfViewer = FieldBinaryFile.extend({
+ supportedFieldTypes: ['binary'],
+ template: 'FieldPdfViewer',
+ /**
+ * @override
+ */
+ init: function () {
+ this._super.apply(this, arguments);
+ this.PDFViewerApplication = false;
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * @private
+ * @param {DOMElement} iframe
+ */
+ _disableButtons: function (iframe) {
+ if (this.mode === 'readonly') {
+ $(iframe).contents().find('button#download').hide();
+ }
+ $(iframe).contents().find('button#openFile').hide();
+ },
+ /**
+ * @private
+ * @returns {string} the pdf viewer URI
+ */
+ _getURI: function () {
+ var queryObj = {
+ model: this.model,
+ field: this.name,
+ id: this.res_id,
+ };
+ var page = this.recordData[this.name + '_page'] || 1;
+ var queryString = $.param(queryObj);
+ var url = encodeURIComponent('/web/image?' + queryString);
+ var viewerURL = '/web/static/lib/pdfjs/web/viewer.html?file=';
+ return viewerURL + url + '#page=' + page;
+ },
+ /**
+ * @private
+ * @override
+ */
+ _render: function () {
+ var self = this;
+ var $pdfViewer = this.$('.o_form_pdf_controls').children().add(this.$('.o_pdfview_iframe'));
+ var $selectUpload = this.$('.o_select_file_button').first();
+ var $iFrame = this.$('.o_pdfview_iframe');
+
+ $iFrame.on('load', function () {
+ self.PDFViewerApplication = this.contentWindow.window.PDFViewerApplication;
+ self._disableButtons(this);
+ });
+ if (this.mode === "readonly" && this.value) {
+ $iFrame.attr('src', this._getURI());
+ } else {
+ if (this.value) {
+ var binSize = utils.is_bin_size(this.value);
+ $pdfViewer.removeClass('o_hidden');
+ $selectUpload.addClass('o_hidden');
+ if (binSize) {
+ $iFrame.attr('src', this._getURI());
+ }
+ } else {
+ $pdfViewer.addClass('o_hidden');
+ $selectUpload.removeClass('o_hidden');
+ }
+ }
+ },
+
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * @override
+ * @private
+ * @param {Event} ev
+ */
+ on_file_change: function (ev) {
+ this._super.apply(this, arguments);
+ if (this.PDFViewerApplication) {
+ var files = ev.target.files;
+ if (!files || files.length === 0) {
+ return;
+ }
+ var file = files[0];
+ // TOCheck: is there requirement to fallback on FileReader if browser don't support URL
+ this.PDFViewerApplication.open(URL.createObjectURL(file), 0);
+ }
+ },
+ /**
+ * Remove the behaviour of on_save_as in FieldBinaryFile.
+ *
+ * @override
+ * @private
+ * @param {MouseEvent} ev
+ */
+ on_save_as: function (ev) {
+ ev.stopPropagation();
+ },
+
+});
+
var PriorityWidget = AbstractField.extend({
// the current implementation of this widget makes it
// only usable for fields of type selection
@@ -2544,6 +2650,7 @@ return {
DebouncedField: DebouncedField,
FieldEmail: FieldEmail,
FieldBinaryFile: FieldBinaryFile,
+ FieldPdfViewer: FieldPdfViewer,
FieldBinaryImage: FieldBinaryImage,
FieldBoolean: FieldBoolean,
FieldBooleanButton: FieldBooleanButton,