[forms] Refactor

This commit is contained in:
Ilya Kirillov
2025-06-11 16:04:22 +03:00
parent f31bb12081
commit 930f507eaf

View File

@ -185,19 +185,19 @@
* Creates a text field with the specified text field properties. * Creates a text field with the specified text field properties.
* @memberof Api * @memberof Api
* @typeofeditors ["CDE", "CFE"] * @typeofeditors ["CDE", "CFE"]
* @param {TextFormPr} oFormPr - Text field properties. * @param {TextFormPr} formPr - Text field properties.
* @returns {ApiTextForm} * @returns {ApiTextForm}
* @see office-js-api/Examples/Forms/Api/Methods/CreateTextForm.js * @see office-js-api/Examples/Forms/Api/Methods/CreateTextForm.js
*/ */
Api.prototype.CreateTextForm = function(oFormPr) Api.prototype.CreateTextForm = function(formPr)
{ {
return executeNoFormLockCheck(function() return executeNoFormLockCheck(function()
{ {
if (!oFormPr) if (!formPr)
oFormPr = {}; formPr = {};
let form = CreateCommonForm(oFormPr); let form = CreateCommonForm(formPr);
ApplyTextFormPr(form, oFormPr); ApplyTextFormPr(form, formPr);
CheckForm(form); CheckForm(form);
return new AscBuilder.ApiTextForm(form); return new AscBuilder.ApiTextForm(form);
}, this); }, this);
@ -206,26 +206,26 @@
* Creates a checkbox / radio button with the specified checkbox / radio button properties. * Creates a checkbox / radio button with the specified checkbox / radio button properties.
* @memberof Api * @memberof Api
* @typeofeditors ["CDE", "CFE"] * @typeofeditors ["CDE", "CFE"]
* @param {CheckBoxFormPr} oFormPr - Checkbox / radio button properties. * @param {CheckBoxFormPr} formPr - Checkbox / radio button properties.
* @returns {ApiCheckBoxForm} * @returns {ApiCheckBoxForm}
* @see office-js-api/Examples/Forms/Api/Methods/CreateCheckBoxForm.js * @see office-js-api/Examples/Forms/Api/Methods/CreateCheckBoxForm.js
*/ */
Api.prototype.CreateCheckBoxForm = function(oFormPr) Api.prototype.CreateCheckBoxForm = function(formPr)
{ {
return executeNoFormLockCheck(function() return executeNoFormLockCheck(function()
{ {
if (!oFormPr) if (!formPr)
oFormPr = {}; formPr = {};
oFormPr["placeholder"] = undefined; formPr["placeholder"] = undefined;
var oCC; var oCC;
var oCheckboxPr = new AscCommon.CSdtCheckBoxPr(); var oCheckboxPr = new AscCommon.CSdtCheckBoxPr();
if (GetBoolParameter(oFormPr["radio"], false)) if (GetBoolParameter(formPr["radio"], false))
{ {
oCheckboxPr.CheckedSymbol = 0x25C9; oCheckboxPr.CheckedSymbol = 0x25C9;
oCheckboxPr.UncheckedSymbol = 0x25CB; oCheckboxPr.UncheckedSymbol = 0x25CB;
oCheckboxPr.GroupKey = GetStringParameter(oFormPr["key"], "Group1"); oCheckboxPr.GroupKey = GetStringParameter(formPr["key"], "Group1");
} }
else else
{ {
@ -256,7 +256,7 @@
function private_PerformAddCheckBox() function private_PerformAddCheckBox()
{ {
oCC = CreateCommonForm(oFormPr); oCC = CreateCommonForm(formPr);
oCC.ApplyCheckBoxPr(oCheckboxPr); oCC.ApplyCheckBoxPr(oCheckboxPr);
} }
@ -281,25 +281,25 @@
* Creates a combo box / dropdown list with the specified combo box / dropdown list properties. * Creates a combo box / dropdown list with the specified combo box / dropdown list properties.
* @memberof Api * @memberof Api
* @typeofeditors ["CDE", "CFE"] * @typeofeditors ["CDE", "CFE"]
* @param {ComboBoxFormPr} oFormPr - Combo box / dropdown list properties. * @param {ComboBoxFormPr} formPr - Combo box / dropdown list properties.
* @returns {ApiComboBoxForm} * @returns {ApiComboBoxForm}
* @see office-js-api/Examples/Forms/Api/Methods/CreateComboBoxForm.js * @see office-js-api/Examples/Forms/Api/Methods/CreateComboBoxForm.js
*/ */
Api.prototype.CreateComboBoxForm = function(oFormPr) Api.prototype.CreateComboBoxForm = function(formPr)
{ {
return executeNoFormLockCheck(function() return executeNoFormLockCheck(function()
{ {
if (!oFormPr) if (!formPr)
oFormPr = {}; formPr = {};
var oPr = new AscCommon.CSdtComboBoxPr(); var oPr = new AscCommon.CSdtComboBoxPr();
oPr.AddItem(AscCommon.translateManager.getValue("Choose an item"), ""); oPr.AddItem(AscCommon.translateManager.getValue("Choose an item"), "");
var oCC = CreateCommonForm(oFormPr); var oCC = CreateCommonForm(formPr);
let sPlaceholder = GetStringParameter(oFormPr["placeholder"], undefined); let sPlaceholder = GetStringParameter(formPr["placeholder"], undefined);
let arrList = GetArrayParameter(oFormPr["items"], []); let arrList = GetArrayParameter(formPr["items"], []);
for (let nIndex = 0, nCount = arrList.length; nIndex < nCount; ++nIndex) for (let nIndex = 0, nCount = arrList.length; nIndex < nCount; ++nIndex)
{ {
let oItem = arrList[nIndex]; let oItem = arrList[nIndex];
@ -316,9 +316,9 @@
oPr.AddItem(sDisplay, sValue); oPr.AddItem(sDisplay, sValue);
} }
} }
oPr.SetAutoFit(GetBoolParameter(oFormPr["autoFit"], false)); oPr.SetAutoFit(GetBoolParameter(formPr["autoFit"], false));
if (!GetBoolParameter(oFormPr["editable"], false)) if (!GetBoolParameter(formPr["editable"], false))
{ {
if (sPlaceholder) if (sPlaceholder)
{ {
@ -351,27 +351,27 @@
* Creates a picture form with the specified picture form properties. * Creates a picture form with the specified picture form properties.
* @memberof Api * @memberof Api
* @typeofeditors ["CDE", "CFE"] * @typeofeditors ["CDE", "CFE"]
* @param {PictureFormPr} oFormPr - Picture form properties. * @param {PictureFormPr} formPr - Picture form properties.
* @returns {ApiPictureForm} * @returns {ApiPictureForm}
* @see office-js-api/Examples/Forms/Api/Methods/CreatePictureForm.js * @see office-js-api/Examples/Forms/Api/Methods/CreatePictureForm.js
*/ */
Api.prototype.CreatePictureForm = function(oFormPr) Api.prototype.CreatePictureForm = function(formPr)
{ {
return executeNoFormLockCheck(function() return executeNoFormLockCheck(function()
{ {
if (!oFormPr) if (!formPr)
oFormPr = {}; formPr = {};
if (GetStringParameter("placeholder", null)) if (GetStringParameter("placeholder", null))
oFormPr["placeholder"] = AscCommon.translateManager.getValue("Click to load image"); formPr["placeholder"] = AscCommon.translateManager.getValue("Click to load image");
var oCC = CreateCommonForm(oFormPr); var oCC = CreateCommonForm(formPr);
oCC.ApplyPicturePr(true); oCC.ApplyPicturePr(true);
oCC.ConvertFormToFixed(); oCC.ConvertFormToFixed();
let oPr = new AscCommon.CSdtPictureFormPr(); let oPr = new AscCommon.CSdtPictureFormPr();
let sScale = GetStringParameter(oFormPr["scaleFlag"], undefined); let sScale = GetStringParameter(formPr["scaleFlag"], undefined);
switch (sScale) switch (sScale)
{ {
case "always": case "always":
@ -388,10 +388,10 @@
break; break;
} }
oPr.SetConstantProportions(GetBoolParameter(oFormPr["lockAspectRatio"], true)); oPr.SetConstantProportions(GetBoolParameter(formPr["lockAspectRatio"], true));
oPr.SetRespectBorders(GetBoolParameter(oFormPr["respectBorders"], false)); oPr.SetRespectBorders(GetBoolParameter(formPr["respectBorders"], false));
oPr.SetShiftX(Math.max(0, Math.min(100, GetNumberParameter(oFormPr["shiftX"], 50))) / 100); oPr.SetShiftX(Math.max(0, Math.min(100, GetNumberParameter(formPr["shiftX"], 50))) / 100);
oPr.SetShiftY(Math.max(0, Math.min(100, GetNumberParameter(oFormPr["shiftY"], 50))) / 100); oPr.SetShiftY(Math.max(0, Math.min(100, GetNumberParameter(formPr["shiftY"], 50))) / 100);
oCC.SetPictureFormPr(oPr); oCC.SetPictureFormPr(oPr);
@ -403,19 +403,19 @@
* Creates a date form with the specified date form properties. * Creates a date form with the specified date form properties.
* @memberof Api * @memberof Api
* @typeofeditors ["CDE", "CFE"] * @typeofeditors ["CDE", "CFE"]
* @param {DateFormPr} oFormPr - Date form properties. * @param {DateFormPr} formPr - Date form properties.
* @returns {ApiDateForm} * @returns {ApiDateForm}
* @see office-js-api/Examples/Forms/Api/Methods/CreateDateForm.js * @see office-js-api/Examples/Forms/Api/Methods/CreateDateForm.js
*/ */
Api.prototype.CreateDateForm = function(oFormPr) Api.prototype.CreateDateForm = function(formPr)
{ {
return executeNoFormLockCheck(function() return executeNoFormLockCheck(function()
{ {
if (!oFormPr) if (!formPr)
oFormPr = {}; formPr = {};
let form = CreateCommonForm(oFormPr); let form = CreateCommonForm(formPr);
ApplyDateFormPr(form, oFormPr); ApplyDateFormPr(form, formPr);
CheckForm(form); CheckForm(form);
return new AscBuilder.ApiDateForm(form); return new AscBuilder.ApiDateForm(form);
}, this); }, this);
@ -424,18 +424,18 @@
* Creates a complex form with the specified complex form properties. * Creates a complex form with the specified complex form properties.
* @memberof Api * @memberof Api
* @typeofeditors ["CDE", "CFE"] * @typeofeditors ["CDE", "CFE"]
* @param {FormPrBase} oFormPr - Complex form properties. * @param {FormPrBase} formPr - Complex form properties.
* @returns {ApiComplexForm} * @returns {ApiComplexForm}
* @see office-js-api/Examples/Forms/Api/Methods/CreateComplexForm.js * @see office-js-api/Examples/Forms/Api/Methods/CreateComplexForm.js
*/ */
Api.prototype.CreateComplexForm = function(oFormPr) Api.prototype.CreateComplexForm = function(formPr)
{ {
return executeNoFormLockCheck(function() return executeNoFormLockCheck(function()
{ {
if (!oFormPr) if (!formPr)
oFormPr = {}; formPr = {};
let form = CreateCommonForm(oFormPr); let form = CreateCommonForm(formPr);
ApplyComplexFormPr(form); ApplyComplexFormPr(form);
CheckForm(form); CheckForm(form);
return new AscBuilder.ApiComplexForm(form); return new AscBuilder.ApiComplexForm(form);
@ -445,32 +445,32 @@
* Inserts a text box with the specified text box properties over the selected text. * Inserts a text box with the specified text box properties over the selected text.
* @memberof ApiDocument * @memberof ApiDocument
* @typeofeditors ["CDE", "CFE"] * @typeofeditors ["CDE", "CFE"]
* @param {TextFormInsertPr} oFormPr - Properties for inserting a text field. * @param {TextFormInsertPr} formPr - Properties for inserting a text field.
* @returns {ApiTextForm} * @returns {ApiTextForm}
* @see office-js-api/Examples/Forms/ApiDocument/Methods/InsertTextForm.js * @see office-js-api/Examples/Forms/ApiDocument/Methods/InsertTextForm.js
*/ */
ApiDocument.prototype.InsertTextForm = function(oFormPr) ApiDocument.prototype.InsertTextForm = function(formPr)
{ {
return executeNoFormLockCheck(function() return executeNoFormLockCheck(function()
{ {
if (!oFormPr) if (!formPr)
oFormPr = {}; formPr = {};
let logicDocument = this.Document; let logicDocument = this.Document;
let placeholder = GetStringParameter(oFormPr["placeholder"], undefined); let placeholder = GetStringParameter(formPr["placeholder"], undefined);
if (GetBoolParameter(oFormPr["placeholderFromSelection"], false)) if (GetBoolParameter(formPr["placeholderFromSelection"], false))
placeholder = logicDocument.GetSelectedText(); placeholder = logicDocument.GetSelectedText();
if (!GetBoolParameter(oFormPr["keepSelectedTextInForm"], true)) if (!GetBoolParameter(formPr["keepSelectedTextInForm"], true))
logicDocument.RemoveBeforePaste(); logicDocument.RemoveBeforePaste();
let contentControl = logicDocument.AddContentControl(c_oAscSdtLevelType.Inline); let contentControl = logicDocument.AddContentControl(c_oAscSdtLevelType.Inline);
if (!contentControl) if (!contentControl)
return null; return null;
ApplyCommonFormPr(contentControl, oFormPr); ApplyCommonFormPr(contentControl, formPr);
SetFormPlaceholder(contentControl, placeholder); SetFormPlaceholder(contentControl, placeholder);
ApplyTextFormPr(contentControl, oFormPr, true); ApplyTextFormPr(contentControl, formPr, true);
CheckForm(contentControl); CheckForm(contentControl);
return new AscBuilder.ApiTextForm(contentControl); return new AscBuilder.ApiTextForm(contentControl);
}, this); }, this);
@ -684,16 +684,16 @@
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private area // Private area
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CreateCommonForm(oFormPr) function CreateCommonForm(formPr)
{ {
let contentControl = new AscCommonWord.CInlineLevelSdt(); let contentControl = new AscCommonWord.CInlineLevelSdt();
ApplyCommonFormPr(contentControl, oFormPr); ApplyCommonFormPr(contentControl, formPr);
let placeholder = oFormPr ? GetStringParameter(oFormPr["placeholder"], undefined) : undefined; let placeholder = formPr ? GetStringParameter(formPr["placeholder"], undefined) : undefined;
SetFormPlaceholder(contentControl, placeholder); SetFormPlaceholder(contentControl, placeholder);
let tag = oFormPr ? GetStringParameter(oFormPr["tag"], undefined) : undefined; let tag = formPr ? GetStringParameter(formPr["tag"], undefined) : undefined;
if (tag) if (tag)
contentControl.SetTag(tag); contentControl.SetTag(tag);
@ -747,7 +747,7 @@
{ {
let complexFormPr = new AscWord.CSdtComplexFormPr(); let complexFormPr = new AscWord.CSdtComplexFormPr();
form.SetComplexFormPr(complexFormPr); form.SetComplexFormPr(complexFormPr);
}; }
function CheckForm(form) function CheckForm(form)
{ {
CheckFormKey(form); CheckFormKey(form);