ProtoViewLogic: createUiComponents()
与えられた一連の記述子に基づいて Element
を生成します。
構文
ProtoViewLogic.createUiComponents(componentDescriptors) => object
引数
-
componentDescriptors
:object
各
Element
またはArray<Element>
の記述子をプロパティに持つオブジェクトです。各プロパティには
obejct
またはobject
の配列を指定します。 それぞれのobject
はプロパティ名で識別されるElement
またはArray<Element>
の記述子です。記述子は以下のプロパティを持ちます:
-
tagName
:string
定義する
Element
のタグ名を指定します -
value
:string
|undefined
(省略可)定義する
Element
のvalue
属性の値を指定しますinfovalue
の指定はattributes
内のvalue
属性の指定に優先します(後者の値は前者の値で上書きされます)
-
text
:string
|undefined
(省略可)定義する
Element
のtextContent
を指定しますinfochildren
プロパティと併用した際、text
は定義されるElement
の最初のノード (firstNode
) として登録されます(結果としてchildren
のインデックスは 1 つずれることに注意してください)
-
children
:Array<Element | string | object>
|undefined
(省略可)定義する
Element
の子のノードとなる要素、テキスト、または要素の記述子を指定しますinfotext
プロパティと併用した際、定義されるElement
の最初のノード (firstNode
) としてtext
が先に登録され、それに続いてchildren
の成分が追加されます(結果としてchildren
のインデックスは 1 つずれることに注意してください)
-
primary
:string
|undefined
(省略可)定義する
Element
のdata-primary
カスタムデータ属性の値を指定しますinfoprimary
の指定はattributes
内のdata-primary
属性の指定に優先します(後者の値は前者の値で上書きされます)
-
activeEvents
:string
|Array<string>
|undefined
(省略可)定義する
Element
のdata-active-events
カスタムデータ属性を指定しますinfoactiveEvents
の指定はattributes
内のdata-active-events
属性の指定に優先します(後者の値は前者の値で上書きされます)string
として指定した場合、カンマ区切りされたイベント種別の文字列になっていなければなりません
-
attributes
:object
|undefined
(省略可)定義する
Element
の属性を指定します。定義する属性と同じ名前を持つプロパティに、設定する値を持たせたオブジェクトを指定します。
infovalue
,primary
,activeEvents
プロパティの指定はattributes
内のvalue
,data-primary
,data-active-events
属性の指定に優先します(後者の値は前者の値で上書きされます)attributes
内のid
属性の指定は常に無視され、記述子を持つプロパティ名 またはそれに配列の添え字を付けたものが使用されます(前者はcomponentDescriptors
のプロパティがobject
の場合、後者はobject
の配列だった場合に使用されます)
-
返値: object
引数 componentDescriptors
と共通のプロパティ名を持ち、その値として生成された Element
または Array<Element>
を持つオブジェクト。
返値のオブジェクトは Object
を継承していません(null-prototype object です)。
例外
TypeError
- 引数
componentDescriptors
が 非null
のオブジェクトでなかった場合
- 引数
AggregateError
- 以下のいずれかの例外が発生した場合、それらを集約した
AggregateError
が発生します:TypeError
: 値componentDescriptors[component_name]
が非null
のオブジェクトでなかった場合TypeError
: 値componentDescriptors[component_name].tagName
が文字列でなかった場合TypeError
: 値componentDescriptors[component_name].value
がnull
でも文字列でもなかった場合TypeError
: 値componentDescriptors[component_name].text
がnull
でも文字列でもなかった場合TypeError
: 値componentDescriptors[component_name].children
がnull
でも反復可能オブジェクト (iterable) でもなかった場合TypeError
: 値componentDescriptors[component_name].primary
がnull
でも文字列でもなかった場合TypeError
: 値componentDescriptors[component_name].activeEvents
がnull
でも文字列でも反復可能オブジェクト (iterable) でもなかった場合TypeError
: 値componentDescriptors[component_name].attributes
がnull
でも非null
オブジェクトでもなかった場合TypeError
: 記述子のプロパティtagName
がタグ名として正しくなかった場合TypeError
: 記述子のプロパティactiveEvents
が文字列でない成分を含んでいた場合TypeError
: 記述子のプロパティactiveEvents
が空文字列または空白のみからなる文字列を含んでいた場合TypeError
: 記述子のプロパティattributes
が空文字列または空白のみからなる文字列をキーとするプロパティを持っていた場合TypeError
: 記述子のプロパティattributes
がNaN
を値とするプロパティを持っていた場合TypeError
: 記述子のプロパティattributes
がSymbol
型の値のプロパティを持っていた場合TypeError
: 記述子のプロパティattributes
がfunction
型の値のプロパティを持っていた場合TypeError
: 記述子のプロパティchildren
がElement
でも文字列でも非null
のオブジェクトでもなかった場合
- 以下のいずれかの例外が発生した場合、それらを集約した
例
// SP
const { ProtoViewLogic } = await Alier.import("/alier_sys/ProtoViewLogic.js");
// Web
// import { ProtoViewLogic } from "/path/to/alier/src/ProtoViewLogic.js";
// paragraph, textarea, buttons を vl に関連付けられた要素として定義する。
// paragraph は <p> 要素、textarea は <textarea> 要素を表し、buttons は <button> 要素の配列を表す。
const components = ProtoViewLogic.createUiComponents({
paragraph: {
tagName: "p",
text : "this is a paragraph"
},
textarea: {
tagName : "textarea",
value : "default value of this textarea",
activeEvents: "input"
},
buttons: [
{
tagName : "button",
value : "ok",
text : "OK",
activeEvents: "click",
attributes : { type: "button" }
},
{
tagName : "button",
value : "cancel",
text : "Cancel",
activeEvents: "click",
attributes : { type: "button" }
}
]
});
// 生成した components の内容を確認する
console.log(components.paragraph, components.textarea, components.buttons[0], components.buttons[1]);
// ==> <p>, <textarea>, <button>, <button> の詳細がログに出力される
解説
引数として与えられた componentDescriptors
が持つ記述子に従って、Element
を生成し、対応するプロパティ名を持つ null-prototype なオブジェクトを返します。
- この関数は
defineUiComponents()
の中で、relateElements()
とともに呼び出されます - この関数は与えられた記述子に対してそれぞれ
ProtoViewLogic.createElement()
を呼び出します