diff --git a/frappe_better_attach_control/public/js/controls/attach.js b/frappe_better_attach_control/public/js/controls/attach.js index 5668d23..8e0aa16 100644 --- a/frappe_better_attach_control/public/js/controls/attach.js +++ b/frappe_better_attach_control/public/js/controls/attach.js @@ -247,28 +247,31 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro var tmp = {options: {restrictions: {}}}; tmp.allow_reload = toBool(ifNull(opts.allow_reload, true)); tmp.allow_remove = toBool(ifNull(opts.allow_remove, true)); + function parseVal(v, t) { + if (isEmpty(v)) v = null; + if (t === 's' && v) { + v = cstr(v); + v = v.length ? v : null; + } + else if (t === 'b') v = toBool(ifNull(v, false)); + else if (t === 'i' && v) { + v = cint(v); + if (isNaN(v) || v < 1) v = null; + } + else if (t === 'a') v = toArray(v); + return v; + } + each([['upload_notes', 's'], ['allow_multiple', 'b']], function(k) { + tmp.options[k[0]] = parseVal(opts[k[0]], k[1]); + }); each( [ - 'upload_notes', 'allow_multiple', - 'max_file_size', 'allowed_file_types', - 'max_number_of_files', 'crop_image_aspect_ratio', - 'as_public', + ['max_file_size', 'i'], ['allowed_file_types', 'a'], + ['max_number_of_files', 'i'], ['crop_image_aspect_ratio', 'i'], + ['as_public', 'b'], ], - function(k, i) { - let v = opts[k]; - if (isEmpty(v)) v = null; - if (v && i === 0) { - v = cstr(v); - v = v.length ? v : null; - } - else if (i === 1 || i === 6) v = toBool(ifNull(v, false)); - else if (v && [2, 4, 5].indexOf(i) >= 0) { - v = cint(v); - if (isNaN(v) || v < 1) v = null; - } - else if (i === 3) v = toArray(v); - if (i < 2) tmp.options[k] = v; - else tmp.options.restrictions[k] = v; + function(k) { + tmp.options.restrictions[k[0]] = parseVal(opts[k[0]], k[1]); } ); return tmp; @@ -283,12 +286,12 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro if (!this._allow_reload) this.enable_reload(); else this.disable_reload(); } - if (ifNull(opt.allow_remove, true) !== this._allow_remove) { + if (ifNull(opts.allow_remove, true) !== this._allow_remove) { if (!this._allow_remove) this.enable_remove(); else this.disable_remove(); } } - let allow_multiple = opt.options && !!opt.options.allow_multiple; + let allow_multiple = opts.options && !!opts.options.allow_multiple; if (allow_multiple !== this._allow_multiple) { this._allow_multiple = allow_multiple; this._set_max_attachments(); diff --git a/frappe_better_attach_control/public/js/controls/v12/attach.js b/frappe_better_attach_control/public/js/controls/v12/attach.js index 148f451..cd721b8 100644 --- a/frappe_better_attach_control/public/js/controls/v12/attach.js +++ b/frappe_better_attach_control/public/js/controls/v12/attach.js @@ -216,28 +216,30 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({ _parse_options: function(opts) { var tmp = {options: {restrictions: {}}}; tmp.allow_remove = toBool(ifNull(opts.allow_remove, true)); + function parseVal(v, t) { + if (isEmpty(v)) v = null; + if (t === 's' && v) { + v = cstr(v); + v = v.length ? v : null; + } + else if (t === 'b') v = toBool(ifNull(v, false)); + else if (t === 'i' && v) { + v = cint(v); + if (isNaN(v) || v < 1) v = null; + } + else if (t === 'a') v = toArray(v); + return v; + } + each([['upload_notes', 's'], ['allow_multiple', 'b']], function(k) { + tmp.options[k[0]] = parseVal(opts[k[0]], k[1]); + }); each( [ - 'upload_notes', 'allow_multiple', - 'max_file_size', 'allowed_file_types', - 'max_number_of_files', - 'as_public', + ['max_file_size', 'i'], ['allowed_file_types', 'a'], + ['max_number_of_files', 'i'], ['as_public', 'b'], ], - function(k, i) { - let v = opts[k]; - if (isEmpty(v)) v = null; - if (v && i === 0) { - v = cstr(v); - v = v.length ? v : null; - } - else if (i === 1 || i === 6) v = toBool(ifNull(v, false)); - else if (v && [2, 4, 5].indexOf(i) >= 0) { - v = cint(v); - if (isNaN(v) || v < 1) v = null; - } - else if (i === 3) v = toArray(v); - if (i < 2) tmp.options[k] = v; - else tmp.options.restrictions[k] = v; + function(k) { + tmp.options.restrictions[k[0]] = parseVal(opts[k[0]], k[1]); } ); return tmp; diff --git a/frappe_better_attach_control/public/js/controls/v13/attach.js b/frappe_better_attach_control/public/js/controls/v13/attach.js index 7e88d5a..6c29cca 100644 --- a/frappe_better_attach_control/public/js/controls/v13/attach.js +++ b/frappe_better_attach_control/public/js/controls/v13/attach.js @@ -237,28 +237,30 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({ var tmp = {options: {restrictions: {}}}; tmp.allow_reload = toBool(ifNull(opts.allow_reload, true)); tmp.allow_remove = toBool(ifNull(opts.allow_remove, true)); + function parseVal(v, t) { + if (isEmpty(v)) v = null; + if (t === 's' && v) { + v = cstr(v); + v = v.length ? v : null; + } + else if (t === 'b') v = toBool(ifNull(v, false)); + else if (t === 'i' && v) { + v = cint(v); + if (isNaN(v) || v < 1) v = null; + } + else if (t === 'a') v = toArray(v); + return v; + } + each([['upload_notes', 's'], ['allow_multiple', 'b']], function(k) { + tmp.options[k[0]] = parseVal(opts[k[0]], k[1]); + }); each( [ - 'upload_notes', 'allow_multiple', - 'max_file_size', 'allowed_file_types', - 'max_number_of_files', - 'as_public', + ['max_file_size', 'i'], ['allowed_file_types', 'a'], + ['max_number_of_files', 'i'], ['as_public', 'b'], ], - function(k, i) { - let v = opts[k]; - if (isEmpty(v)) v = null; - if (v && i === 0) { - v = cstr(v); - v = v.length ? v : null; - } - else if (i === 1 || i === 6) v = toBool(ifNull(v, false)); - else if (v && [2, 4, 5].indexOf(i) >= 0) { - v = cint(v); - if (isNaN(v) || v < 1) v = null; - } - else if (i === 3) v = toArray(v); - if (i < 2) tmp.options[k] = v; - else tmp.options.restrictions[k] = v; + function(k) { + tmp.options.restrictions[k[0]] = parseVal(opts[k[0]], k[1]); } ); return tmp; diff --git a/frappe_better_attach_control/public/js/uploader/index.js b/frappe_better_attach_control/public/js/uploader/index.js index 3883103..0910818 100644 --- a/frappe_better_attach_control/public/js/uploader/index.js +++ b/frappe_better_attach_control/public/js/uploader/index.js @@ -28,7 +28,7 @@ frappe.ui.FileUploader = class FileUploader extends frappe.ui.FileUploader { this.uploader.$watch('show_file_browser', function(show_file_browser) { if (show_file_browser && !me.uploader.$refs.file_browser._restrictions) { me._override_file_browser( - !isEmpty(opts.restrictions) + isPlainObject(opts) && !isEmpty(opts.restrictions) ? opts.restrictions : { max_file_size: null, @@ -42,7 +42,10 @@ frappe.ui.FileUploader = class FileUploader extends frappe.ui.FileUploader { } _override_uploader(opts) { var up = this.uploader; - up.restrictions.as_public = !!opts.restrictions.as_public; + if (opts) { + up.restrictions = opts.restrictions; + up.restrictions.as_public = !!opts.restrictions.as_public; + } up.dropfiles = function(e) { this.is_dragging = false; if (isObject(e) && isObject(e.dataTransfer)) diff --git a/frappe_better_attach_control/public/js/uploader/v12/index.js b/frappe_better_attach_control/public/js/uploader/v12/index.js index 83d726b..4a77752 100644 --- a/frappe_better_attach_control/public/js/uploader/v12/index.js +++ b/frappe_better_attach_control/public/js/uploader/v12/index.js @@ -28,7 +28,7 @@ frappe.ui.FileUploader = class FileUploader extends frappe.ui.FileUploader { this.uploader.$watch('show_file_browser', function(show_file_browser) { if (show_file_browser && !me.uploader.$refs.file_browser._restrictions) { me._override_file_browser( - !isEmpty(opts.restrictions) + isPlainObject(opts) && !isEmpty(opts.restrictions) ? opts.restrictions : { max_file_size: null, @@ -41,7 +41,10 @@ frappe.ui.FileUploader = class FileUploader extends frappe.ui.FileUploader { } _override_uploader(opts) { var up = this.uploader; - up.restrictions.as_public = !!opts.restrictions.as_public; + if (opts) { + up.restrictions = opts.restrictions; + up.restrictions.as_public = !!opts.restrictions.as_public; + } up.dropfiles = function(e) { this.is_dragging = false; if (isObject(e) && isObject(e.dataTransfer)) diff --git a/frappe_better_attach_control/public/js/uploader/v13/index.js b/frappe_better_attach_control/public/js/uploader/v13/index.js index e2483b0..8732fc4 100644 --- a/frappe_better_attach_control/public/js/uploader/v13/index.js +++ b/frappe_better_attach_control/public/js/uploader/v13/index.js @@ -28,7 +28,7 @@ frappe.ui.FileUploader = class FileUploader extends frappe.ui.FileUploader { this.uploader.$watch('show_file_browser', function(show_file_browser) { if (show_file_browser && !me.uploader.$refs.file_browser._restrictions) { me._override_file_browser( - !isEmpty(opts.restrictions) + isPlainObject(opts) && !isEmpty(opts.restrictions) ? opts.restrictions : { max_file_size: null, @@ -41,7 +41,10 @@ frappe.ui.FileUploader = class FileUploader extends frappe.ui.FileUploader { } _override_uploader(opts) { var up = this.uploader; - up.restrictions.as_public = !!opts.restrictions.as_public; + if (opts) { + up.restrictions = opts.restrictions; + up.restrictions.as_public = !!opts.restrictions.as_public; + } up.dropfiles = function(e) { this.is_dragging = false; if (isObject(e) && isObject(e.dataTransfer))