99 class CrudField {
1010 constructor (name ) {
1111 this .name = name;
12- this .wrapper = $ (` [bp-field-name*="${ name} "][bp-field-wrapper]` ).first ();
1312
13+ // get the input/textarea/select that has that field name
14+ this .$input = $ (' input[name="' + this .name + ' "], textarea[name="' + this .name + ' "], select[name="' + this .name + ' "], select[name="' + this .name + ' []"]' ).first ();
15+
16+ // get the field wraper
17+ this .wrapper = this .inputWrapper ;
18+
19+ // in case `bp-field-main-input` is specified on a field input, use that one as input
20+ this .$input = this .mainInput ;
21+
22+ // Validate that the wrapper has been found
1423 if (this .wrapper .length === 0 ) {
1524 console .error (` CrudField error! Could not select WRAPPER for "${ this .name } "` );
1625 }
1726
18- this .type = this .wrapper .attr (' bp-field-type' );
19- this .$input = this .mainInput ;
20- this .input = this .$input [0 ];
21-
2227 // Validate that the field has been found
23- if (! this .input ) {
28+ if (this .$ input. length === 0 ) {
2429 console .error (` CrudField error! Could not select INPUT for "${ this .name } "` );
2530 }
26- }
2731
28- get value () {
29- return this .$input .val ();
32+ this .input = this .$input [0 ];
33+ this .type = this .wrapper .attr (' bp-field-type' );
34+
35+ return this ;
36+
3037 }
3138
3239 get mainInput () {
33- let input;
34-
35- // search input in ancestors
36- input = this .wrapper .closest (' [bp-field-main-input]' );
40+ let input = this .wrapper .find (' [bp-field-main-input]' ).first ();
3741
38- // search input in children
39- if (input .length = == 0 ) {
40- input = this . wrapper . find ( ' [bp-field-main- input] ' ) ;
42+ // if a bp-field-main- input has been specified by developer, that's it, use that one
43+ if (input .length ! == 0 ) {
44+ return input;
4145 }
4246
43- // if no bp-field-main-input has been declared in the field itself , try to find an input with that name inside wraper
44- if (input .length === 0 ) {
45- input = this . wrapper . find ( ` input[bp-field-name=" ${ this . name } "], textarea[bp- field-name=" ${ this . name } "], select[ bp-field-name=" ${ this . name } "] ` ). first ();
46- }
47+ // otherwise , try to find the input using other selectors
48+ if (this . $ input .length === 0 ) {
49+ // try searching for the field with the corresponding bp-field-name
50+ input = this . wrapper . find ( ' input[bp-field-name=" ' + this . name + ' "], textarea[bp-field-name=" ' + this . name + ' "], select[bp-field-name=" ' + this . name + ' "], select[bp-field-name=" ' + this . name + ' []"] ' ). first ();
4751
48- // if nothing works, use the first input found in field wrapper.
49- if (input .length === 0 ) {
50- input = this .wrapper .find (' input, textarea, select' ).first ();
52+ // if not input found yet, just get the first input in that wrapper
53+ if (input .length === 0 ) {
54+ input = this .wrapper .find (' input, textarea, select' ).first ();
55+ }
56+
57+ return input;
5158 }
5259
53- return input;
60+ return this .$input ;
61+
62+ }
63+
64+ get value () {
65+ return this .$input .val ();
66+ }
67+
68+ get inputWrapper () {
69+ let wrapper = this .$input .closest (' [bp-field-wrapper]' );
70+ if (wrapper .length === 0 ) {
71+ wrapper = $ (` [bp-field-name="${ this .name } "][bp-field-wrapper]` ).first ();
72+ }
73+ return wrapper;
5474 }
5575
5676 onChange (closure ) {
@@ -61,7 +81,7 @@ class CrudField {
6181 window .crud .subfieldsCallbacks = window .crud .subfieldsCallbacks ?? [];
6282 window .crud .subfieldsCallbacks [this .subfieldHolder ] = window .crud .subfieldsCallbacks [this .subfieldHolder ] ?? [];
6383 if (! window .crud .subfieldsCallbacks [this .subfieldHolder ].some ( callbacks => callbacks[' fieldName' ] === this .name )) {
64- window .crud .subfieldsCallbacks [this .subfieldHolder ].push ({fieldName : this . name , closure: closure, field: this });
84+ window .crud .subfieldsCallbacks [this .subfieldHolder ].push ({closure: closure, field: this });
6585 }
6686 return this ;
6787 }
@@ -83,7 +103,7 @@ class CrudField {
83103 }
84104 return this ;
85105 }
86-
106+
87107 this .$input .trigger (` change` );
88108 }
89109
@@ -127,7 +147,8 @@ class CrudField {
127147 }
128148
129149 subfield (name , rowNumber = false ) {
130- let subfield = new CrudField (name);
150+ let subfield = new CrudField (this .name );
151+ subfield .name = name;
131152 if (! rowNumber) {
132153 subfield .isSubfield = true ;
133154 subfield .subfieldHolder = this .name ;
0 commit comments