Refactor multiple JavaScript classes to use ES6 syntax, including Download, Pdf, Printer, PrinterPos, PoleDisplay, and Simulator. Update method names for consistency and improve error handling. Modify AJAX requests to enhance functionality and user experience.
This commit is contained in:
+77
-73
@@ -4,82 +4,86 @@
|
||||
Desc. : Fungsi-fungsi download data & laporan
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : April 1st, 2011.
|
||||
Last Modified : October 2nd, 2026.
|
||||
Last Modified : November 2nd, 2026.
|
||||
|
||||
(c) 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
*******************************************************************
|
||||
*
|
||||
* Class Download
|
||||
*
|
||||
* Download atau Share file.
|
||||
*
|
||||
* @license MIT, https://www.3fonia.com
|
||||
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {string} options.url - Endpoint URL File
|
||||
* @param {string} [options.filename='file.bin'] - Default file name for download is header is missing
|
||||
* @param {string} [options.title] - Title for share file
|
||||
* @param {string} [options.text] - Text for share file
|
||||
* @param {Array} [options.params] - Request payload
|
||||
*/
|
||||
|
||||
var Download = function(options) {
|
||||
var _options = {
|
||||
'url': '',
|
||||
'filename': 'file.bin',
|
||||
'title': '',
|
||||
'text': 'share dari aplikasi'
|
||||
};
|
||||
var _params = [];
|
||||
class Download {
|
||||
constructor(options = {}) {
|
||||
this.url = typeof options === 'string' ? options : options.url || '';
|
||||
this.filename = options.filename || 'file.bin';
|
||||
this.title = options.title || '';
|
||||
this.text = options.text || '';
|
||||
this.params = options.params || [];
|
||||
}
|
||||
|
||||
this.construct = function(options){
|
||||
if (typeof options == 'object') {
|
||||
$.extend(_options, options);
|
||||
} else _options.url = options;
|
||||
};
|
||||
this.set_url = function (url){
|
||||
_options.url = url;
|
||||
};
|
||||
this.add_param = function(par_id, par_val) {
|
||||
var _getvalue = (par_val) ? par_id + '=' + encodeURIComponent(par_val) : par_id;
|
||||
_params.push(_getvalue);
|
||||
};
|
||||
this._exec = async function(is_share) {
|
||||
if (!_options.url) {
|
||||
console.warn('cek url !');
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const blob = await $.ajax({
|
||||
type: 'POST',
|
||||
url: _options.url,
|
||||
data: FoniaJS.RequestParam(_params),
|
||||
xhrFields: {
|
||||
responseType: 'blob'
|
||||
},
|
||||
success: async function (data, status, xhr) {
|
||||
const disposition = xhr.getResponseHeader('Content-Disposition');
|
||||
if (disposition && disposition.indexOf('attachment') !== -1) {
|
||||
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
|
||||
const matches = filenameRegex.exec(disposition);
|
||||
if (matches != null && matches[1]) {
|
||||
_options.filename = matches[1].replace(/['"]/g, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const file = new File([blob], _options.filename, { type: blob.type });
|
||||
|
||||
if (is_share === true && navigator.canShare && navigator.canShare({ files: [file] })) {
|
||||
await navigator.share({
|
||||
files: [file],
|
||||
title: _options.title ?? _options.filename,
|
||||
text: _options.text
|
||||
});
|
||||
} else {
|
||||
const link = document.createElement('a');
|
||||
link.href = URL.createObjectURL(blob);
|
||||
link.download = _options.filename;
|
||||
link.click();
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Download / sharing gagal :', error);
|
||||
};
|
||||
addParam(id, val) {
|
||||
this.params.push(val !== undefined ? `${id}=${encodeURIComponent(val)}` : id);
|
||||
return this;
|
||||
};
|
||||
this.share = async function() {
|
||||
await this._exec(true);
|
||||
};
|
||||
this.download = async function() {
|
||||
await this._exec(false);
|
||||
};
|
||||
this.construct(options);
|
||||
};
|
||||
}
|
||||
|
||||
async _exec(isShare = false) {
|
||||
if (!this.url) {
|
||||
console.warn('Url File Download dibutuhkan!');
|
||||
return;
|
||||
}
|
||||
await $.ajax({
|
||||
type: 'POST',
|
||||
url: this.url,
|
||||
data: FoniaJS.RequestParam(this.params),
|
||||
xhrFields: { responseType: 'blob' }
|
||||
})
|
||||
.done((blob, status, xhr) => {
|
||||
let filename = this.filename;
|
||||
const disposition = xhr.getResponseHeader('Content-Disposition');
|
||||
const match = disposition?.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
|
||||
if (match?.[1]) filename = match[1].replace(/['"]/g, '');
|
||||
|
||||
if (!isShare) {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = Object.assign(document.createElement('a'), {
|
||||
href: url,
|
||||
download: filename
|
||||
});
|
||||
link.click();
|
||||
URL.revokeObjectURL(url);
|
||||
} else {
|
||||
const file = new File([blob], filename, { type: blob.type });
|
||||
if (isShare && navigator.canShare?.({ files: [file] })) {
|
||||
navigator.share({
|
||||
files: [file],
|
||||
title: this.title || filename,
|
||||
text: this.text
|
||||
});
|
||||
}
|
||||
file = null;
|
||||
}
|
||||
})
|
||||
.fail((xhr, status, err) => console.error('Gagal download File karena : ', err));
|
||||
}
|
||||
|
||||
share() {
|
||||
return this._exec(true);
|
||||
}
|
||||
|
||||
download() {
|
||||
return this._exec(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user