-
Notifications
You must be signed in to change notification settings - Fork 4
Add Detail Widget as plugin #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Joshua-ChiangMai
wants to merge
5
commits into
master
Choose a base branch
from
jb/testPluginDetail
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfbeaff
Add Detail Widget as plugin
Joshua-ChiangMai 752e403
resolve merge conflicts with master for Detail plugin
Joshua-ChiangMai ab7df7b
temp: remove ABViewDetail.test.js from PR (keep local copy)
Joshua-ChiangMai bc13ad1
fix: detail widget render template instead of raw JSON
Joshua-ChiangMai ed0d7e2
merge master into detail branch and resolve index.js conflict
Joshua-ChiangMai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule core
updated
from 9c645d to c66267
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
AppBuilder/platform/plugins/included/view_detail/FNAbviewdetail.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import FNAbviewdetailComponent from "./FNAbviewdetailComponent.js"; | ||
|
|
||
| // Detail view plugin: replaces the original ABViewDetail / ABViewDetailCore. | ||
| // All logic from both Core and platform is contained in this file. | ||
| export default function FNAbviewdetail({ | ||
| ABViewContainer, | ||
| ABViewContainerComponent, | ||
| ABViewComponentPlugin, | ||
| }) { | ||
| const ABViewDetailComponent = FNAbviewdetailComponent({ | ||
| ABViewContainerComponent, | ||
| ABViewComponentPlugin, | ||
| }); | ||
|
|
||
| const ABViewDetailDefaults = { | ||
| key: "detail", | ||
| icon: "file-text-o", | ||
| labelKey: "Detail(plugin)", | ||
| }; | ||
|
|
||
| const ABViewDetailPropertyComponentDefaults = { | ||
| dataviewID: null, | ||
| showLabel: true, | ||
| labelPosition: "left", | ||
| labelWidth: 120, | ||
| height: 0, | ||
| }; | ||
|
|
||
| return class ABViewDetailPlugin extends ABViewContainer { | ||
| /** | ||
| * @param {obj} values key=>value hash of ABView values | ||
| * @param {ABApplication} application the application object this view is under | ||
| * @param {ABView} parent the ABView this view is a child of. (can be null) | ||
| */ | ||
| constructor(values, application, parent, defaultValues) { | ||
| super( | ||
| values, | ||
| application, | ||
| parent, | ||
| defaultValues ?? ABViewDetailDefaults | ||
| ); | ||
| } | ||
|
|
||
| static getPluginType() { | ||
| return "view"; | ||
| } | ||
|
|
||
| static getPluginKey() { | ||
| return this.common().key; | ||
| } | ||
|
|
||
| static common() { | ||
| return ABViewDetailDefaults; | ||
| } | ||
|
|
||
| static defaultValues() { | ||
| return ABViewDetailPropertyComponentDefaults; | ||
| } | ||
|
|
||
| /** | ||
| * @method fromValues() | ||
| * Initialize this object with the given set of values. | ||
| * @param {obj} values | ||
| */ | ||
| fromValues(values) { | ||
| super.fromValues(values); | ||
|
|
||
| this.settings.labelPosition = | ||
| this.settings.labelPosition || | ||
| ABViewDetailPropertyComponentDefaults.labelPosition; | ||
|
|
||
| this.settings.showLabel = JSON.parse( | ||
| this.settings.showLabel != null | ||
| ? this.settings.showLabel | ||
| : ABViewDetailPropertyComponentDefaults.showLabel | ||
| ); | ||
|
|
||
| this.settings.labelWidth = parseInt( | ||
| this.settings.labelWidth || | ||
| ABViewDetailPropertyComponentDefaults.labelWidth | ||
| ); | ||
| this.settings.height = parseInt( | ||
| this.settings.height ?? | ||
| ABViewDetailPropertyComponentDefaults.height | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @method componentList | ||
| * Return the list of components available on this view to display in the editor. | ||
| */ | ||
| componentList() { | ||
| const viewsToAllow = ["label", "text"]; | ||
| const allComponents = this.application.viewAll(); | ||
| return allComponents.filter((c) => | ||
| viewsToAllow.includes(c.common().key) | ||
| ); | ||
| } | ||
|
|
||
| addFieldToDetail(field, yPosition) { | ||
| if (field == null) return; | ||
|
|
||
| const newView = field | ||
| .detailComponent() | ||
| .newInstance(this.application, this); | ||
| if (newView == null) return; | ||
|
|
||
| newView.settings = newView.settings ?? {}; | ||
| newView.settings.fieldId = field.id; | ||
| newView.settings.labelWidth = | ||
| this.settings.labelWidth || | ||
| ABViewDetailPropertyComponentDefaults.labelWidth; | ||
| newView.settings.alias = field.alias; | ||
| newView.position.y = yPosition; | ||
|
|
||
| this._views.push(newView); | ||
| return newView; | ||
| } | ||
|
|
||
| /** | ||
| * @method component() | ||
| * Return a UI component based upon this view. | ||
| * @return {obj} UI component | ||
| */ | ||
| component() { | ||
| return new ABViewDetailComponent(this); | ||
| } | ||
|
|
||
| warningsEval() { | ||
| super.warningsEval(); | ||
|
|
||
| const DC = this.datacollection; | ||
| if (!DC) { | ||
| this.warningsMessage( | ||
| `can't resolve it's datacollection[${this.settings.dataviewID}]` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
| } |
179 changes: 179 additions & 0 deletions
179
AppBuilder/platform/plugins/included/view_detail/FNAbviewdetailComponent.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| export default function FNAbviewdetailComponent({ | ||
| ABViewContainerComponent, | ||
| ABViewComponentPlugin, | ||
| }) { | ||
| const ContainerComponent = | ||
| ABViewContainerComponent?.default ?? ABViewContainerComponent; | ||
| const Base = ContainerComponent ?? ABViewComponentPlugin; | ||
| if (!Base) { | ||
| return class ABAbviewdetailComponent {}; | ||
| } | ||
|
|
||
| return class ABAbviewdetailComponent extends Base { | ||
| constructor(baseView, idBase, ids) { | ||
| super( | ||
| baseView, | ||
| idBase || `ABViewDetail_${baseView.id}`, | ||
| Object.assign({ detail: "" }, ids) | ||
| ); | ||
| this.idBase = idBase || `ABViewDetail_${baseView.id}`; | ||
| } | ||
|
|
||
| ui() { | ||
| if (!ContainerComponent) { | ||
| return this._uiDataviewFallback(); | ||
| } | ||
| const _ui = super.ui(); | ||
| return { | ||
| type: "form", | ||
| id: this.ids.component, | ||
| borderless: true, | ||
| rows: [{ body: _ui }], | ||
| }; | ||
| } | ||
|
|
||
| _uiDataviewFallback() { | ||
| const settings = this.settings; | ||
| const _uiDetail = { | ||
| id: this.ids.detail, | ||
| view: "dataview", | ||
| type: { width: 1000, height: 30 }, | ||
| template: (item) => (item ? JSON.stringify(item) : ""), | ||
| }; | ||
| if (settings.height !== 0) _uiDetail.height = settings.height; | ||
| else _uiDetail.autoHeight = true; | ||
| const _ui = super.ui([_uiDetail]); | ||
| delete _ui.type; | ||
| return _ui; | ||
| } | ||
|
|
||
| onShow() { | ||
| const baseView = this.view; | ||
| try { | ||
| const dataCy = `Detail ${baseView.name?.split(".")[0]} ${baseView.id}`; | ||
| $$(this.ids.component)?.$view?.setAttribute("data-cy", dataCy); | ||
| } catch (e) { | ||
| console.warn("Problem setting data-cy", e); | ||
| } | ||
|
|
||
| const dv = this.datacollection; | ||
| if (dv) { | ||
| const currData = dv.getCursor(); | ||
| if (currData) this.displayData(currData); | ||
|
|
||
| ["changeCursor", "cursorStale", "collectionEmpty"].forEach((key) => { | ||
| this.eventAdd({ | ||
| emitter: dv, | ||
| eventName: key, | ||
| listener: (...p) => this.displayData(...p), | ||
| }); | ||
| }); | ||
| this.eventAdd({ | ||
| emitter: dv, | ||
| eventName: "create", | ||
| listener: (createdRow) => { | ||
| if (dv.getCursor()?.id === createdRow.id) | ||
| this.displayData(createdRow); | ||
| }, | ||
| }); | ||
| this.eventAdd({ | ||
| emitter: dv, | ||
| eventName: "update", | ||
| listener: (updatedRow) => { | ||
| if (dv.getCursor()?.id === updatedRow.id) | ||
| this.displayData(updatedRow); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| super.onShow?.(); | ||
| } | ||
|
|
||
| displayData(rowData = {}) { | ||
| if (!ContainerComponent) return; | ||
| if (rowData == null && this.datacollection) | ||
| rowData = this.datacollection.getCursor() ?? {}; | ||
|
|
||
| const views = (this.view.views() || []).sort((a, b) => { | ||
| if (!a?.field?.() || !b?.field?.()) return 0; | ||
| if (a.field().key === "formula" && b.field().key === "calculate") | ||
| return -1; | ||
| if (a.field().key === "calculate" && b.field().key === "formula") | ||
| return 1; | ||
| return 0; | ||
| }); | ||
|
|
||
| views.forEach((f) => { | ||
| let val; | ||
| if (f.field) { | ||
| const field = f.field(); | ||
| if (!field) return; | ||
|
|
||
| switch (field.key) { | ||
| case "connectObject": | ||
| val = field.pullRelationValues(rowData); | ||
| break; | ||
| case "list": | ||
| val = rowData?.[field.columnName]; | ||
| if (!val || (Array.isArray(val) && val.length === 0)) { | ||
| val = ""; | ||
| break; | ||
| } | ||
| if (field.settings.isMultiple === 0) { | ||
| let myVal = ""; | ||
| (field.settings.options || []).forEach((opt) => { | ||
| if (opt.id === val) myVal = opt.text; | ||
| }); | ||
| if (field.settings.hasColors) { | ||
| let hasCustomColor = ""; | ||
| (field.settings.options || []).forEach((h) => { | ||
| if (h.text === myVal) { | ||
| hasCustomColor = "hascustomcolor"; | ||
| } | ||
| }); | ||
| const hex = (field.settings.options || []).find( | ||
| (o) => o.text === myVal | ||
| )?.hex ?? "#66666"; | ||
| myVal = `<span class="webix_multicombo_value ${hasCustomColor}" style="background-color: ${hex} !important;"><span>${myVal}</span></span>`; | ||
| } | ||
| val = myVal; | ||
| } else { | ||
| const items = (val || []).map((value) => { | ||
| let myVal = ""; | ||
| (field.settings.options || []).forEach((opt) => { | ||
| if (opt.id === value.id) myVal = opt.text; | ||
| }); | ||
| const optionHex = | ||
| field.settings.hasColors && value.hex | ||
| ? `background: ${value.hex};` | ||
| : ""; | ||
| const hasCustomColor = | ||
| field.settings.hasColors && value.hex | ||
| ? "hascustomcolor" | ||
| : ""; | ||
| return `<span class="webix_multicombo_value ${hasCustomColor}" style="${optionHex}" optvalue="${value.id}"><span>${myVal}</span></span>`; | ||
| }); | ||
| val = items.join(""); | ||
| } | ||
| break; | ||
| case "user": | ||
| val = field.pullRelationValues(rowData); | ||
| break; | ||
| case "file": | ||
| val = rowData?.[field.columnName] ?? ""; | ||
| break; | ||
| case "formula": | ||
| val = rowData ? field.format(rowData, false) : ""; | ||
| break; | ||
| default: | ||
| val = field.format(rowData); | ||
| } | ||
| } | ||
|
|
||
| const vComponent = f.component(this.idBase); | ||
| vComponent?.setValue?.(val); | ||
| vComponent?.displayText?.(rowData); | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Useless conditional Warning
Copilot Autofix
AI 4 days ago
In general, to fix useless conditionals where a variable is always truthy/falsey at a given point, remove the redundant logical operation or rewrite the logic to reflect the true set of possible values. Here,
valis already ensured to be a non-empty array when theelsebranch is executed, because falsy and empty-array cases are caught earlier and converted to"". Thus(val || [])will always evaluate toval, making the|| []part useless. The best fix is to remove the fallback and iterate directly overval.Concretely, in
AppBuilder/platform/plugins/included/view_detail/FNAbviewdetailComponent.js, locate thecase "list":handling, in theelseblock forfield.settings.isMultiple !== 0. On line 141, change:to:
No additional imports, methods, or definitions are needed; this is a localized change that preserves existing functionality while removing the useless conditional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed this line of code to "if (!val || (Array.isArray(val) && val.length === 0))".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you update line 141? or did you update it back at line 118?