I have this custom widget that is used in an Odoo 12 project. I am new to Odoo still, and I need to convert it to Odoo 16. I am familiar with Odoo 12 because that's the version I followed a course from, but not with 16, so I can't really understand where the problem could be, but I am pretty sure it's in the widget.js file, and maybe it's something related to the libraries that are required in the code (maybe they are outdated)? The manifest file should be correct (unless there are some major changes that you are aware), and the view is a very simple xml file that shouldn't have any problems.
What this widget does btw, it takes a pdf file and it creates an url for it so you can preview it.
Thanks in advance, let me know if there's something I've been missing, but here's the js file:
odoo.define("pdf_preview_widget", function (require) {
"use strict";
var core = require('web.core');
var fieldRegistry = require("web.field_registry");
var basicFields = require("web.basic_fields");
var PdfPreviewWidget = basicFields.FieldBinaryFile.extend({
template: "PdfPreviewWidget",
_renderModal: function (data) {
const self = this;
if($('.pdf-preview-modal').length) return;
let modalTemplate = core.qweb.render('PdfPreviewModal', {
iframe_data: data
});
$('body').append(modalTemplate);
$('.modal-window').addClass('visible');
$(".modal-close").on('click', function(ev) {
ev.preventDefault();
$(this).parent().parent().parent().remove();
});
},
_b64toBlob: function(b64Data, contentType='', sliceSize=512) {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType, title: 'Download.pdf'});
return blob;
},
_render: function () {
const self = this;
this.$(".oe_field_pdf_preview").on('click', function(ev) {
ev.preventDefault();
const resBlob = self._b64toBlob(self.value, 'application/pdf');
const resFile = new File([resBlob], 'Download.pdf', {type: 'application/pdf'});
const resUrl = URL.createObjectURL(resFile, {type: 'application/pdf'});
self._renderModal(resUrl);
});
}
});
fieldRegistry.add("pdf_preview", PdfPreviewWidget);
});