-
Notifications
You must be signed in to change notification settings - Fork 4
Migrate text plugin #689
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
benthongtiang
wants to merge
4
commits into
master
Choose a base branch
from
ben/TextPlugin
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
Migrate text plugin #689
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d5457d3
migate text plugin
wirachot f7621b9
Potential fix for code scanning alert no. 633: Incomplete string esca…
benthongtiang d670642
Potential fix for code scanning alert no. 634: Incomplete string esca…
benthongtiang c781f35
Potential fix for code scanning alert no. 635: Double escaping or une…
benthongtiang 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
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
258 changes: 258 additions & 0 deletions
258
AppBuilder/platform/plugins/included/view_text/FNAbviewtext.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,258 @@ | ||
| import FNAbviewtextComponent from "./FNAbviewtextComponent.js"; | ||
|
|
||
|
|
||
| // FNAbviewtext Web | ||
| // A web side import for an ABView. | ||
| // | ||
| export default function FNAbviewtext({ | ||
| /*AB,*/ | ||
| ABViewWidgetPlugin, | ||
| ABViewComponentPlugin, | ||
| ABViewContainer | ||
| }) { | ||
| const ABAbviewtextComponent = FNAbviewtextComponent({ ABViewComponentPlugin }); | ||
|
|
||
| const ABViewTextPropertyComponentDefaults = { | ||
| text: "", | ||
| // {string} | ||
| // A multilingual text template that is used to display a given set of | ||
| // values. | ||
|
|
||
| height: 0, | ||
| // {integer} | ||
| // The default height of this widget. | ||
|
|
||
| dataviewID: null, | ||
| // {uuid} | ||
| // The {ABDataCollection.id} of the datacollection this ABViewText is | ||
| // pulling data from. | ||
| // In most usage situations this ABView is tied to the data in an | ||
| // ABDataCollection. However, it is possible for an ABObject to be | ||
| // directly assigned to the ABView, and that will be used instead. | ||
| }; | ||
|
|
||
| const ABViewDefaults = { | ||
| key: "text", | ||
| // {string} | ||
| // unique key for this view | ||
|
|
||
| icon: "font", | ||
| // {string} | ||
| // fa-[icon] reference for this view | ||
|
|
||
| labelKey: "Text", | ||
| // {string} | ||
| // the multilingual label key for the class label | ||
| }; | ||
|
|
||
| class ABViewTextCore extends ABViewWidgetPlugin { | ||
| constructor(values, application, parent, defaultValues) { | ||
| super(values, application, parent, defaultValues || ABViewDefaults); | ||
|
|
||
| this._object = null; | ||
| } | ||
|
|
||
| static common() { | ||
| return ABViewDefaults; | ||
| } | ||
|
|
||
| static defaultValues() { | ||
| return ABViewTextPropertyComponentDefaults; | ||
| } | ||
|
|
||
| /// | ||
| /// Instance Methods | ||
| /// | ||
|
|
||
| /** | ||
| * @method toObj() | ||
| * | ||
| * properly compile the current state of this ABViewLabel instance | ||
| * into the values needed for saving. | ||
| * | ||
| * @return {json} | ||
| */ | ||
| toObj() { | ||
| // NOTE: ABView auto translates/untranslates "label" | ||
| // add in any additional fields here: | ||
| this.unTranslate(this, this, ["text"]); | ||
|
|
||
| var obj = super.toObj(); | ||
| obj.views = []; | ||
| return obj; | ||
| } | ||
|
|
||
| /** | ||
| * @method fromValues() | ||
| * | ||
| * initialze this object with the given set of values. | ||
| * @param {obj} values | ||
| */ | ||
| fromValues(values) { | ||
| super.fromValues(values); | ||
|
|
||
| this.settings = this.settings || {}; | ||
|
|
||
| // convert from "0" => 0 | ||
| this.settings.height = parseInt( | ||
| this.settings.height || ABViewTextPropertyComponentDefaults.height | ||
| ); | ||
|
|
||
| // if this is being instantiated on a read from the Property UI, | ||
| this.text = values.text || ABViewTextPropertyComponentDefaults.text; | ||
|
|
||
| // NOTE: ABView auto translates/untranslates "label" | ||
| // add in any additional fields here: | ||
| this.translate(this, this, ["text"]); | ||
| } | ||
|
|
||
| /** | ||
| * @method componentList | ||
| * return the list of components available on this view to display in the editor. | ||
| */ | ||
| componentList() { | ||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @property datacollection | ||
| * return ABDatacollection of this form | ||
| * | ||
| * @return {ABDatacollection} | ||
| */ | ||
| get datacollection() { | ||
| if (this.parent?.key == "dataview") { | ||
| return this.AB.datacollectionByID(this.parent.settings.dataviewID); | ||
| } else { | ||
| return this.AB.datacollectionByID(this.settings.dataviewID); | ||
| } | ||
| } | ||
|
|
||
| fieldKey(field) { | ||
| let label = field.label || ""; | ||
| // First escape backslashes to avoid leaving metacharacters unescaped | ||
| label = label.replace(/\\/g, "\\\\"); | ||
| // Then escape parentheses | ||
| label = label.replace(/\(/g, "\\("); | ||
| label = label.replace(/\)/g, "\\)"); | ||
|
||
| return label; | ||
| } | ||
|
|
||
| displayText(val, componentID) { | ||
| var result = this.text; | ||
|
|
||
| let clearTemplateValue = (result) => { | ||
| return result.replace(/{(.*?)}/g, ""); | ||
| }; | ||
|
|
||
| var dv = this.datacollection; | ||
| // if (!dv) return clearTemplateValue(result); | ||
|
|
||
| var object = dv?.datasource ?? this._object; | ||
| if (!object) return clearTemplateValue(result); | ||
|
|
||
| const rowData = val || dv.getCursor() || {}; | ||
|
|
||
| object.fields().forEach((f) => { | ||
| // add \\ in front of the regular expression special charactors | ||
| // let label = f.label || ""; | ||
| // label = label.replace(/\(/g, "\\("); | ||
| // label = label.replace(/\)/g, "\\)"); | ||
| let label = this.fieldKey(f); | ||
|
|
||
| var template = new RegExp("{" + label + "}", "g"); | ||
|
|
||
| // IDEA: I'd like to keep all the image url logic INSIDE the ABFieldImage | ||
| // object. Is there some way we can simply call: f.imageTemplate(rowData) | ||
| // and parse the results for the url to display here? | ||
|
|
||
| var data = f.format(rowData); | ||
| if (f.key == "image") { | ||
| var fData = data; | ||
| data = f.urlImage(fData); | ||
|
|
||
| // Question: should we change f.urlImage() to return the defaultImageUrl | ||
| // if fData is "" and .useDefaultImage = true? | ||
|
|
||
| if ( | ||
| !fData && | ||
| f.settings.defaultImageUrl && | ||
| f.settings.useDefaultImage | ||
| ) { | ||
| data = f.urlImage(f.settings.defaultImageUrl); | ||
|
|
||
| //// | ||
| //// James: Revisit this and make sure we are handling things ok now. | ||
| // result = result.replace( | ||
| // "img", | ||
| // 'img onload=\'AD.comm.hub.publish("component.adjust", {"containerID": "' + | ||
| // componentID + | ||
| // "\"});' " | ||
| // ); | ||
| // } else if ( | ||
| // fData != "" && | ||
| // result.indexOf("onload") == -1 && | ||
| // componentID | ||
| // ) { | ||
| // result = result.replace( | ||
| // "img", | ||
| // 'img onload=\'AD.comm.hub.publish("component.adjust", {"containerID": "' + | ||
| // componentID + | ||
| // "\"});' " | ||
| // ); | ||
| } else { | ||
| //// | ||
| //// James: It looks like this routine assumes the this.text template will | ||
| //// only have 1 <img> tag in it. Is that necessarilly true? | ||
| //// | ||
| //// If NOT, then we need to rethink this next line: | ||
|
|
||
| result = result.replace( | ||
| "img", | ||
| "img onerror='this.parentNode.removeChild(this);' " | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| result = result.replace(template, data); | ||
| }); | ||
|
|
||
| // Support {uuid} tag in tempalte | ||
| result = result.replace(/{PK}/g, rowData[object.PK()]); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| objectLoad(object) { | ||
| this._object = object; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| return class ABViewText extends ABViewTextCore { | ||
|
|
||
| /** | ||
| * @method getPluginKey | ||
| * return the plugin key for this view. | ||
| * @return {string} plugin key | ||
| */ | ||
| static getPluginKey() { | ||
| return this.common().key; | ||
| } | ||
|
|
||
| /** | ||
| * @method component() | ||
| * return a UI component based upon this view. | ||
| * @return {obj} UI component | ||
| */ | ||
| component(parentId) { | ||
| return new ABAbviewtextComponent(this, parentId); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| }; | ||
|
|
||
| } | ||
|
|
||
78 changes: 78 additions & 0 deletions
78
AppBuilder/platform/plugins/included/view_text/FNAbviewtextComponent.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,78 @@ | ||
| export default function FNAbviewtextComponent({ | ||
| /*AB,*/ | ||
| ABViewComponentPlugin, | ||
| }) { | ||
| return class ABAbviewtextComponent extends ABViewComponentPlugin { | ||
|
|
||
|
|
||
| constructor(baseView, idBase, ids) { | ||
| super( | ||
| baseView, | ||
| idBase || `ABViewText_${baseView.id}`, | ||
| Object.assign( | ||
| { | ||
| text: "", | ||
| }, | ||
| ids | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| ui() { | ||
| const ids = this.ids; | ||
| const settings = this.settings; | ||
|
|
||
| const _uiText = { | ||
| id: ids.text, | ||
| view: "template", | ||
| minHeight: 10, | ||
| css: "ab-custom-template", | ||
| borderless: true, | ||
| }; | ||
|
|
||
| if (settings.height) _uiText.height = settings.height; | ||
| else _uiText.autoheight = true; | ||
|
|
||
| const _ui = super.ui([_uiText]); | ||
|
|
||
| delete _ui.type; | ||
|
|
||
| return _ui; | ||
| } | ||
|
|
||
| displayText(value) { | ||
| const ids = this.ids; | ||
| const result = this.view.displayText(value, ids.text); | ||
|
|
||
| const $text = $$(ids.text); | ||
|
|
||
| if (!$text) return; | ||
|
|
||
| $text.define("template", result); | ||
| $text.refresh(); | ||
| } | ||
|
|
||
| onShow() { | ||
| super.onShow(); | ||
|
|
||
| // listen DC events | ||
| const dataview = this.datacollection; | ||
| const baseView = this.view; | ||
|
|
||
| if (dataview && baseView.parent.key !== "dataview") { | ||
| ["changeCursor", "cursorStale"].forEach((key) => { | ||
| baseView.eventAdd({ | ||
| emitter: dataview, | ||
| eventName: key, | ||
| listener: (...p) => this.displayText(...p), | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| this.displayText(); | ||
| } | ||
|
|
||
|
|
||
| }; | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.