Skip to content

Commit

Permalink
fix: depends_on in fields layout
Browse files Browse the repository at this point in the history
  • Loading branch information
shariquerik committed Jan 1, 2025
1 parent c4cebe6 commit e647056
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
8 changes: 6 additions & 2 deletions crm/fcrm/doctype/crm_fields_layout/crm_fields_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CRMFieldsLayout(Document):


@frappe.whitelist()
def get_fields_layout(doctype: str, type: str):
def get_fields_layout(doctype: str, type: str, no_reactivity=False):
tabs = []
layout = None

Expand All @@ -30,7 +30,7 @@ def get_fields_layout(doctype: str, type: str):
has_tabs = tabs[0].get("sections") if tabs and tabs[0] else False

if not has_tabs:
tabs = [{"sections": tabs}]
tabs = [{"name": "first_tab", "sections": tabs}]

allowed_fields = []
for tab in tabs:
Expand Down Expand Up @@ -60,6 +60,10 @@ def get_fields_layout(doctype: str, type: str):
"read_only": field.read_only,
"placeholder": field.get("placeholder"),
"filters": field.get("link_filters"),
"depends_on": "" if no_reactivity else field.get("depends_on"),
"mandatory_depends_on": ""
if no_reactivity
else field.get("mandatory_depends_on"),
}
column["fields"][column.get("fields").index(field["name"])] = field

Expand Down
14 changes: 12 additions & 2 deletions frontend/src/components/FieldLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ import Link from '@/components/Controls/Link.vue'
import Grid from '@/components/Controls/Grid.vue'
import { getMeta } from '@/stores/meta'
import { usersStore } from '@/stores/users'
import { getFormat } from '@/utils'
import { getFormat, evaluateDependsOnValue } from '@/utils'
import { flt } from '@/utils/numberFormat.js'
import { Tabs, Tooltip, DatePicker, DateTimePicker } from 'frappe-ui'
import { ref, computed } from 'vue'
Expand Down Expand Up @@ -300,7 +300,17 @@ const _tabs = computed(() => {
if (field.type == 'Link' && field.options == 'User') {
field.type = 'User'
}
return field
return {
...field,
display_via_depends_on: evaluateDependsOnValue(
field.depends_on,
props.data,
),
mandatory_via_depends_on: evaluateDependsOnValue(
field.mandatory_depends_on,
props.data,
),
}
})
.filter((field) => {
return (
Expand Down
27 changes: 24 additions & 3 deletions frontend/src/components/FieldLayoutEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@
@click="
tabs[tabIndex].sections.push({
label: __('New Section'),
name: 'section_' + getRandom(),
opened: true,
columns: [{ fields: [] }],
columns: [{ name: 'column_' + getRandom(), fields: [] }],
})
"
>
Expand All @@ -218,6 +219,7 @@
import Autocomplete from '@/components/frappe-ui/Autocomplete.vue'
import DragVerticalIcon from '@/components/Icons/DragVerticalIcon.vue'
import Draggable from 'vuedraggable'
import { getRandom } from '@/utils'
import { Dropdown, createResource } from 'frappe-ui'
import { ref, computed, watch } from 'vue'
Expand Down Expand Up @@ -293,16 +295,31 @@ function addTab() {
return
}
props.tabs.push({ label: __('New Tab'), sections: [] })
props.tabs.push({
label: __('New Tab'),
name: 'tab_' + getRandom(),
sections: [],
})
tabIndex.value = props.tabs.length ? props.tabs.length - 1 : 0
}
function addField(column, field) {
if (!field) return
if (field.fieldtype === 'Select') {
field.options = field.options.split('\n')
field.options = field.options.map((option) => {
return { label: option, value: option }
})
if (field.options[0].value !== '') {
field.options.unshift({ label: '', value: '' })
}
}
let newFieldObj = {
...field,
name: field.fieldname,
type: field.fieldtype,
depends_on: '',
mandatory_depends_on: '',
}
column.fields.push(newFieldObj)
}
Expand Down Expand Up @@ -420,7 +437,11 @@ function getSectionOptions(i, section, tab) {
label: __('Add column'),
icon: 'columns',
onClick: () => {
section.columns.push({ label: '', fields: [] })
section.columns.push({
label: '',
name: 'column_' + getRandom(),
fields: [],
})
},
condition: () => section.columns.length < 4,
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Modals/DataFieldsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const dirty = ref(false)
const preview = ref(false)
function getParams() {
return { doctype: _doctype.value, type: 'Data Fields' }
return { doctype: _doctype.value, type: 'Data Fields', no_reactivity: 1 }
}
const tabs = createResource({
Expand Down
32 changes: 4 additions & 28 deletions frontend/src/components/SidePanelLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ import Link from '@/components/Controls/Link.vue'
import UserAvatar from '@/components/UserAvatar.vue'
import { getMeta } from '@/stores/meta'
import { usersStore } from '@/stores/users'
import { getFormat } from '@/utils'
import { getFormat, evaluateDependsOnValue } from '@/utils'
import { flt } from '@/utils/numberFormat.js'
import { Tooltip, DateTimePicker, DatePicker } from 'frappe-ui'
import { computed } from 'vue'
Expand Down Expand Up @@ -263,7 +263,9 @@ const _fields = computed(() => {
let all_fields = []
props.fields?.forEach((field) => {
let df = field?.all_properties
if (df?.depends_on) evaluate_depends_on(df.depends_on, field)
if (df?.depends_on && !evaluateDependsOnValue(df.depends_on, data.value)) {
field.hidden = true
}
all_fields.push({
...field,
filters: df?.link_filters && JSON.parse(df.link_filters),
Expand All @@ -273,32 +275,6 @@ const _fields = computed(() => {
return all_fields
})
function evaluate_depends_on(expression, field) {
if (expression.substr(0, 5) == 'eval:') {
try {
let out = evaluate(expression.substr(5), { doc: data.value })
if (!out) {
field.hidden = true
}
} catch (e) {
console.error(e)
}
}
}
function evaluate(code, context = {}) {
let variable_names = Object.keys(context)
let variables = Object.values(context)
code = `let out = ${code}; return out`
try {
let expression_function = new Function(...variable_names, code)
return expression_function(...variables)
} catch (error) {
console.log('Error evaluating the following expression:')
console.error(code)
throw error
}
}
</script>
<style scoped>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export function isImage(extention) {
)
}

export function getRandom(len) {
export function getRandom(len=4) {
let text = ''
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

Expand Down

0 comments on commit e647056

Please sign in to comment.