feat: make rating field's stars customizable with options field
This commit is contained in:
parent
bb1d3efc10
commit
c477398a2f
8 changed files with 45 additions and 5 deletions
|
|
@ -10,6 +10,7 @@ context('Control Rating', () => {
|
|||
fields: [{
|
||||
'fieldname': 'rate',
|
||||
'fieldtype': 'Rating',
|
||||
'options': 7
|
||||
}]
|
||||
});
|
||||
}
|
||||
|
|
@ -40,4 +41,14 @@ context('Control Rating', () => {
|
|||
.invoke('trigger', 'mouseleave')
|
||||
.should('not.have.class', 'star-hover');
|
||||
});
|
||||
|
||||
it('check number of stars in rating', () => {
|
||||
get_dialog_with_rating();
|
||||
|
||||
cy.get('div.rating')
|
||||
.first()
|
||||
.children('svg')
|
||||
.should('have.length', 7);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1074,6 +1074,11 @@ def validate_fields(meta):
|
|||
if getattr(docfield, 'max_height', None) and (docfield.max_height[-2:] not in ('px', 'em')):
|
||||
frappe.throw('Max for {} height must be in px, em, rem'.format(frappe.bold(docfield.fieldname)))
|
||||
|
||||
def check_no_of_ratings(docfield):
|
||||
if docfield.fieldtype == "Rating":
|
||||
if docfield.options and (int(docfield.options) > 10 or int(docfield.options) < 3):
|
||||
frappe.throw(_('Options for Rating field can range from 3 to 10'))
|
||||
|
||||
fields = meta.get("fields")
|
||||
fieldname_list = [d.fieldname for d in fields]
|
||||
|
||||
|
|
@ -1107,6 +1112,7 @@ def validate_fields(meta):
|
|||
scrub_fetch_from(d)
|
||||
validate_data_field_type(d)
|
||||
check_max_height(d)
|
||||
check_no_of_ratings(d)
|
||||
|
||||
check_fold(fields)
|
||||
check_search_fields(meta, fields)
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ class MariaDBDatabase(Database):
|
|||
'Dynamic Link': ('varchar', self.VARCHAR_LEN),
|
||||
'Password': ('text', ''),
|
||||
'Select': ('varchar', self.VARCHAR_LEN),
|
||||
'Rating': ('int', '1'),
|
||||
'Rating': ('decimal', '3,2'),
|
||||
'Read Only': ('varchar', self.VARCHAR_LEN),
|
||||
'Attach': ('text', ''),
|
||||
'Attach Image': ('text', ''),
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ class PostgresDatabase(Database):
|
|||
'Dynamic Link': ('varchar', self.VARCHAR_LEN),
|
||||
'Password': ('text', ''),
|
||||
'Select': ('varchar', self.VARCHAR_LEN),
|
||||
'Rating': ('smallint', None),
|
||||
'Rating': ('decimal', '3,2'),
|
||||
'Read Only': ('varchar', self.VARCHAR_LEN),
|
||||
'Attach': ('text', ''),
|
||||
'Attach Image': ('text', ''),
|
||||
|
|
|
|||
|
|
@ -187,3 +187,4 @@ frappe.patches.v14_0.copy_mail_data #08.03.21
|
|||
frappe.patches.v14_0.update_workspace2 # 20.09.2021
|
||||
frappe.patches.v14_0.update_github_endpoints #08-11-2021
|
||||
frappe.patches.v14_0.remove_db_aggregation
|
||||
frappe.patches.v14_0.save_ratings_in_fraction
|
||||
|
|
|
|||
12
frappe/patches/v14_0/save_ratings_in_fraction.py
Normal file
12
frappe/patches/v14_0/save_ratings_in_fraction.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import frappe
|
||||
|
||||
def execute():
|
||||
rating_fields = frappe.get_all("DocField", fields=["parent", "fieldname"], filters={"fieldtype": "Rating"})
|
||||
|
||||
custom_rating_fields = frappe.get_all("Custom Field", fields=["dt", "fieldname"], filters={"fieldtype": "Rating"})
|
||||
|
||||
for field in rating_fields + custom_rating_fields:
|
||||
doctype_name = field.get("parent") or field.get("dt")
|
||||
doctype = frappe.qb.DocType(doctype_name)
|
||||
field = field.fieldname
|
||||
(frappe.qb.update(doctype_name).set(doctype[field], doctype[field]/5)).run()
|
||||
|
|
@ -186,7 +186,6 @@ frappe.ui.form.Control = class BaseControl {
|
|||
}
|
||||
]);
|
||||
};
|
||||
|
||||
value = this.validate(value);
|
||||
if (value && value.then) {
|
||||
// got a promise
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@ frappe.ui.form.ControlRating = class ControlRating extends frappe.ui.form.Contro
|
|||
make_input() {
|
||||
super.make_input();
|
||||
let stars = '';
|
||||
[1, 2, 3, 4, 5].forEach(i => {
|
||||
let number_of_stars = this.df.options || 5;
|
||||
Array.from({length: cint(number_of_stars)}, (_, i) => i + 1).forEach(i => {
|
||||
stars += `<svg class="icon icon-md" data-rating=${i}>
|
||||
<use href="#icon-star"></use>
|
||||
</svg>`;
|
||||
});
|
||||
|
||||
const star_template = `
|
||||
<div class="rating">
|
||||
${stars}
|
||||
|
|
@ -42,6 +44,9 @@ frappe.ui.form.ControlRating = class ControlRating extends frappe.ui.form.Contro
|
|||
$(this).removeClass('star-click');
|
||||
}
|
||||
});
|
||||
let out_of_ratings = this.df.options || 5;
|
||||
|
||||
star_value = star_value/out_of_ratings;
|
||||
this.validate_and_set_in_model(star_value, ev);
|
||||
if (this.doctype && this.docname) {
|
||||
this.set_input(star_value);
|
||||
|
|
@ -49,9 +54,12 @@ frappe.ui.form.ControlRating = class ControlRating extends frappe.ui.form.Contro
|
|||
});
|
||||
}
|
||||
get_value() {
|
||||
return cint(this.value, null);
|
||||
let out_of_ratings = this.df.options || 5;
|
||||
return cint(this.value*out_of_ratings, null);
|
||||
}
|
||||
set_formatted_input(value) {
|
||||
let out_of_ratings = this.df.options || 5;
|
||||
value = value * out_of_ratings;
|
||||
let el = $(this.input_area).find('svg');
|
||||
el.children('svg').prevObject.each( function(e) {
|
||||
if (e < value) {
|
||||
|
|
@ -61,4 +69,7 @@ frappe.ui.form.ControlRating = class ControlRating extends frappe.ui.form.Contro
|
|||
}
|
||||
});
|
||||
}
|
||||
validate(fraction) {
|
||||
return parseFloat(fraction);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue