Skip to content

Commit

Permalink
v2-Beta17 release
Browse files Browse the repository at this point in the history
  • Loading branch information
kid1194 committed Jun 12, 2024
1 parent ac14036 commit 037d1af
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 89 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ It supports RTL layout and dark mode out of the box.

⚠️ **v2 is still in BETA stage** ⚠️

![v2 Beta16](https://img.shields.io/badge/v2_Beta16-2024/05/30-green?style=plastic)
![v2 Beta17](https://img.shields.io/badge/v2_Beta17-2024/06/13-green?style=plastic)

**Apologies in advance for any problem or bug you face with this module.**
**Please report any problem or bug you face so it can be fixed.**
Expand Down Expand Up @@ -35,9 +35,10 @@ It supports RTL layout and dark mode out of the box.
#### Version 2
- [![MohsinAli](https://img.shields.io/badge/MohsinAli-Debug_%7C_Test_%7C_Fix-red?style=plastic)](https://github.com/mohsinalimat)
- [![Robert C](https://img.shields.io/badge/Robert_C-Debug_%7C_Test-blue?style=plastic)](https://github.com/robert1112)
- [![NirajRegmi](https://img.shields.io/badge/NirajRegmi-Debug_%7C_Test-orange?style=plastic)](https://github.com/NirajRegmi)
- [![NirajRegmi](https://img.shields.io/badge/NirajRegmi-Debug_%7C_Test-blue?style=plastic)](https://github.com/NirajRegmi)
- [![galaxlabs](https://img.shields.io/badge/galaxlabs-Enhancement-a2eeef?style=plastic)](https://github.com/galaxlabs)
#### Version 1
- [![CA. B.C.Chechani](https://img.shields.io/badge/CA._B.C.Chechani-Debug_%7C_Test-green?style=plastic)](https://github.com/chechani)
- [![CA. B.C.Chechani](https://img.shields.io/badge/CA._B.C.Chechani-Debug_%7C_Test-blue?style=plastic)](https://github.com/chechani)

---

Expand Down Expand Up @@ -179,7 +180,7 @@ You can't modify the original fields of a doctype, so create a new field or clon
| :--- | :--- |
| **dialog_title** | Upload dialog title to be displayed ️(🔶Frappe >= v14.0.0).<br /><br />🔹Example: **"Upload Images"**<br />🔹Default: **"Upload"** |
| **upload_notes** | Upload text to be displayed.<br /><br />🔹Example: **"Only images and videos, with maximum size of 2MB, are allowed to be uploaded"**<br />🔹Default: **""** |
| **disable_auto_save** 🔴 | Disable form auto save after upload.<br /><br />🔹Default: **false** |
| **disable_auto_save** | Disable form auto save after upload.<br /><br />🔹Default: **false** |
| **disable_file_browser** | Disable file browser uploads.<br /><br />⚠️ *(File browser is always disabled in Web Form)*<br /><br />🔹Default: **false** |
| **allow_multiple** | Allow multiple uploads.<br /><br />⚠️ *(Field value is a JSON array of files url)*<br /><br />🔹Default: **false** |
| **max_file_size** | Maximum file size (in bytes) that is allowed to be uploaded.<br /><br />🔹Example: **2048** for **2KB**<br />🔹Default: **Value of maximum file size in Frappe's settings** |
Expand All @@ -190,17 +191,23 @@ You can't modify the original fields of a doctype, so create a new field or clon
| **allowed_filename** | Only allow files that match a specific file name to be uploaded.<br /><br />🔹Example: (String)**"picture.png"** or (RegExp String)**"/picture\-([0-9]+)\.png/"**<br />🔹Default: **null** |
| **allow_reload** | Allow reloading attachments (🔶Frappe >= v13.0.0).<br /><br />🔶 Affect the visibility of the reload button.🔶<br /><br />🔹Default: **true** |
| **allow_remove** | Allow removing and clearing attachments.<br /><br />🔶 Affect the visibility of the remove and clear buttons.🔶<br /><br />🔹Default: **true** |
| **users** 🔴 | Array of custom options for a specific user or group of users.<br /><br />🔹Example: **[{"for": "Guest", "disabled": true}, {"for": ["Administrator", "user"], "allow_multiple": true}]**<br />🔹Default: **null** |
| **roles** 🔴 | Array of custom options for a specific role or group of roles.<br />⚠️ *(Custom options for users is prioritized over roles.)*<br /><br />🔹Example: **[{"for": ["Administrator", "System"], "allow_multiple": true}]**<br />🔹Default: **null** |

🔴 New - 🔶 Changed

---

### Available JavaScript Methods
| Method | Description |
| :--- | :--- |
| **auto_save(enable: Boolean)** | Enable/Disable form auto save after upload. |
| **toggle_auto_save(enable: Boolean !Optional)** 🔶 | Enable/Disable form auto save after upload. |
| **toggle_reload(allow: Boolean !Optional)** | Allow/Deny reloading attachments and toggle the reload button (🔶Frappe >= v13.0.0). |
| **toggle_remove(allow: Boolean !Optional)** | Allow/Deny removing and clearing attachments and toggle the clear and remove buttons. |
| **set_options(options: JSON Object)** | Set or change the plugin options. |

🔴 New - 🔶 Changed

---

### Supported Fields
Expand Down
76 changes: 51 additions & 25 deletions frappe_better_attach_control/public/js/controls/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
super.refresh();
if (Helpers.isString(this.df.options))
this.df.options = Helpers.parseJson(this.df.options, {});
if (!Helpers.isPlainObject(this.df.options)) this.df.options = {};
else if (!Helpers.isPlainObject(this.df.options)) this.df.options = {};
if (!Helpers.isEqual(this.df.options, this._ls_options))
this.set_options(this.df.options);
this._update_options(true);
}
// Custom Methods
auto_save(enable) {
this._disable_auto_save = enable ? false : true;
toggle_auto_save(enable) {
if (enable != null) this._disable_auto_save = enable ? false : true;
else this._disable_auto_save = !this._disable_auto_save;
}
toggle_reload(allow) {
if (allow != null) this._allow_reload = !!allow;
Expand All @@ -187,8 +188,10 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
set_options(opts) {
if (Helpers.isString(opts) && opts.length) opts = Helpers.parseJson(opts, null);
if (Helpers.isEmpty(opts) || !Helpers.isPlainObject(opts)) return;
$.extend(true, this.df.options, opts);
this._update_options();
opts = Helpers.merge(this.df.options, opts);
if (Helpers.isEqual(this.df.options, opts)) return;
this.df.options = opts;
this._update_options(true);
}
// Private Methods
_setup_control() {
Expand Down Expand Up @@ -231,16 +234,37 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
this.df.options = Helpers.parseJson(this.df.options, {});
if (!Helpers.isPlainObject(this.df.options)) this.df.options = {};
}
_update_options() {
this._ls_options = Helpers.deepClone(this.df.options);
let opts;
if (Helpers.isEmpty(this._ls_options)) opts = {};
else opts = this._parse_options(this._ls_options);
this._options = opts.options || null;
_update_options(force) {
if (!force && this._ls_options) return;
this._ls_options = !Helpers.isEmpty(this.df.options) ? Helpers.deepClone(this.df.options) : {};
let opts = {};
if (!Helpers.isEmpty(this._ls_options)) {
opts = this._parse_options(this._ls_options);
if (!opts.disabled) {
if (Helpers.isArray(this._ls_options.users) && this._ls_options.users.length) {
let users = Helpers.filter(this._ls_options.users, function(v) {
return this.isPlainObject(v) && (
(this.isString(v.for) && v.for === frappe.session.user)
|| (this.isArray(v.for) && v.for.indexOf(frappe.session.user) >= 0)
);
});
if (users.length) opts = Helpers.merge(opts, this._parse_options(users[0]));
} else if (Helpers.isArray(this._ls_options.roles)) {
let roles = Helpers.filter(this._ls_options.roles, function(v) {
return this.isPlainObject(v)
&& (this.isString(v.for) || this.isArray(v.for))
&& frappe.user.has_role(v.for);
});
if (roles.length) opts = Helpers.merge(opts, this._parse_options(roles[0]));
}
}
}
this._options = !opts.disabled ? (opts.options || null) : null;
this._reload_control(opts);
}
_parse_options(opts) {
var tmp = {options: {restrictions: {}, extra: {}}};
tmp.disabled = Helpers.toBool(Helpers.ifNull(opts.disabled, false));
tmp.allow_reload = Helpers.toBool(Helpers.ifNull(opts.allow_reload, true));
tmp.allow_remove = Helpers.toBool(Helpers.ifNull(opts.allow_remove, true));
Helpers.each([
Expand Down Expand Up @@ -279,22 +303,24 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
}
_parse_allowed_file_types(opts) {
opts.extra.allowed_file_types = [];
if (Helpers.isEmpty(opts.restrictions.allowed_file_types)) return;
if (!opts.restrictions.allowed_file_types.length) return;
opts.restrictions.allowed_file_types = Helpers.filter(
opts.restrictions.allowed_file_types,
function(v) { return this.isRegExp(v) || (this.isString(v) && v.length); }
);
Helpers.each(opts.restrictions.allowed_file_types, function(t, i) {
if (this.isString(t)) {
if (t[0] === '$') t = new RegExp(t.substring(1));
else if (t.substring(t.length - 2) === '/*')
t = new RegExp(t.substring(0, t.length - 1) + '/(.*?)');
function(v) {
if (this.isString(v)) {
if (!v.length) return false;
if (v[0] === '$') {
opts.extra.allowed_file_types.push(new RegExp(v.substring(1)));
return false;
}
if (v.substring(v.length - 2) === '/*')
opts.extra.allowed_file_types.push(new RegExp(v.substring(0, v.length - 1) + '/(.*?)'));
return true;
} else if (this.isRegExp(v)) {
opts.extra.allowed_file_types.push(v);
}
return false;
}
opts.extra.allowed_file_types.push(t);
});
opts.restrictions.allowed_file_types = Helpers.filter(
opts.restrictions.allowed_file_types,
function(v) { return this.isString(v) && v[0] !== '$'; }
);
}
_toggle_remove_button() {
Expand Down
76 changes: 51 additions & 25 deletions frappe_better_attach_control/public/js/controls/v12/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
this._super();
if (Helpers.isString(this.df.options))
this.df.options = Helpers.parseJson(this.df.options, {});
if (!Helpers.isPlainObject(this.df.options)) this.df.options = {};
else if (!Helpers.isPlainObject(this.df.options)) this.df.options = {};
if (!Helpers.isEqual(this.df.options, this._ls_options))
this.set_options(this.df.options);
this._update_options(true);
this.set_input(Helpers.toArray(this.value));
},
// Custom Methods
auto_save: function(enable) {
this._disable_auto_save = enable ? false : true;
toggle_auto_save: function(enable) {
if (enable != null) this._disable_auto_save = enable ? false : true;
else this._disable_auto_save = !this._disable_auto_save;
},
toggle_remove: function(allow) {
if (allow != null) this._allow_remove = !!allow;
Expand All @@ -164,8 +165,10 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
set_options: function(opts) {
if (Helpers.isString(opts) && opts.length) opts = Helpers.parseJson(opts, null);
if (Helpers.isEmpty(opts) || !Helpers.isPlainObject(opts)) return;
$.extend(true, this.df.options, opts);
this._update_options();
opts = Helpers.merge(this.df.options, opts);
if (Helpers.isEqual(this.df.options, opts)) return;
this.df.options = opts;
this._update_options(true);
},
// Private Methods
_setup_control: function() {
Expand Down Expand Up @@ -207,16 +210,37 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
this.df.options = Helpers.parseJson(this.df.options, {});
if (!Helpers.isPlainObject(this.df.options)) this.df.options = {};
},
_update_options: function() {
this._ls_options = Helpers.deepClone(this.df.options);
let opts;
if (Helpers.isEmpty(this._ls_options)) opts = {};
else opts = this._parse_options(this._ls_options);
this._options = opts.options || null;
_update_options: function(force) {
if (!force && this._ls_options) return;
this._ls_options = !Helpers.isEmpty(this.df.options) ? Helpers.deepClone(this.df.options) : {};
let opts = {};
if (!Helpers.isEmpty(this._ls_options)) {
opts = this._parse_options(this._ls_options);
if (!opts.disabled) {
if (Helpers.isArray(this._ls_options.users) && this._ls_options.users.length) {
let users = Helpers.filter(this._ls_options.users, function(v) {
return this.isPlainObject(v) && (
(this.isString(v.for) && v.for === frappe.session.user)
|| (this.isArray(v.for) && v.for.indexOf(frappe.session.user) >= 0)
);
});
if (users.length) opts = Helpers.merge(opts, this._parse_options(users[0]));
} else if (Helpers.isArray(this._ls_options.roles)) {
let roles = Helpers.filter(this._ls_options.roles, function(v) {
return this.isPlainObject(v)
&& (this.isString(v.for) || this.isArray(v.for))
&& frappe.user.has_role(v.for);
});
if (roles.length) opts = Helpers.merge(opts, this._parse_options(roles[0]));
}
}
}
this._options = !opts.disabled ? (opts.options || null) : null;
this._reload_control(opts);
},
_parse_options: function(opts) {
var tmp = {options: {restrictions: {}, extra: {}}};
tmp.disabled = Helpers.toBool(Helpers.ifNull(opts.disabled, false));
tmp.allow_remove = Helpers.toBool(Helpers.ifNull(opts.allow_remove, true));
Helpers.each([
['upload_notes', 's'], ['disable_auto_save', 'b'],
Expand Down Expand Up @@ -251,22 +275,24 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
},
_parse_allowed_file_types: function(opts) {
opts.extra.allowed_file_types = [];
if (Helpers.isEmpty(opts.restrictions.allowed_file_types)) return;
if (!opts.restrictions.allowed_file_types.length) return;
opts.restrictions.allowed_file_types = Helpers.filter(
opts.restrictions.allowed_file_types,
function(v) { return this.isRegExp(v) || (this.isString(v) && v.length); }
);
Helpers.each(opts.restrictions.allowed_file_types, function(t, i) {
if (this.isString(t)) {
if (t[0] === '$') t = new RegExp(t.substring(1));
else if (t.substring(t.length - 2) === '/*')
t = new RegExp(t.substring(0, t.length - 1) + '/(.*?)');
function(v) {
if (this.isString(v)) {
if (!v.length) return false;
if (v[0] === '$') {
opts.extra.allowed_file_types.push(new RegExp(v.substring(1)));
return false;
}
if (v.substring(v.length - 2) === '/*')
opts.extra.allowed_file_types.push(new RegExp(v.substring(0, v.length - 1) + '/(.*?)'));
return true;
} else if (this.isRegExp(v)) {
opts.extra.allowed_file_types.push(v);
}
return false;
}
opts.extra.allowed_file_types.push(t);
});
opts.restrictions.allowed_file_types = Helpers.filter(
opts.restrictions.allowed_file_types,
function(v) { return this.isString(v) && v[0] !== '$'; }
);
},
_toggle_remove_button: function() {
Expand Down
Loading

0 comments on commit 037d1af

Please sign in to comment.