11$ ( ( ) => {
2+ /**
3+ * @type {Record<string, JQuery<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>> }
4+ */
25 const settingEditFields = {
6+ 'array' : $ ( `<select class="form-element js-setting-edit" multiple></select>` ) ,
37 'string' : $ ( `<input type="text" class="form-element js-setting-edit" />` ) ,
48 'integer' : $ ( '<input type="number" class="form-element js-setting-edit" />' ) ,
59 'float' : $ ( '<input type="number" step="0.0001" class="form-element js-setting-edit" />' ) ,
6- 'boolean' : $ ( `<select class="form-element js-setting-edit"><option value></option><option value="true">true</option><option value="false">false</option></select>` ) ,
10+ 'boolean' : $ ( `<select class="form-element js-setting-edit">
11+ <option value></option>
12+ <option value="true">true</option>
13+ <option value="false">false</option>
14+ </select>` ) ,
715 'json' : $ ( `<textarea rows="5" cols="100" class="form-element js-setting-edit"></textarea>` ) ,
816 'text' : $ ( `<textarea rows="5" cols="100" class="form-element js-setting-edit"></textarea>` )
917 } ;
@@ -26,9 +34,34 @@ $(() => {
2634 const data = await resp . json ( ) ;
2735 const value = data . typed ;
2836
29- const form = settingEditFields [ valueType ] . clone ( ) . val ( ! ! value ? value . toString ( ) : '' )
30- . attr ( 'data-name' , name ) . attr ( 'data-community-id' , communityId ) ;
31- $tgt . addClass ( 'editing' ) . html ( form ) . append ( `<button class="button is-primary is-filled js-setting-submit">Update</button>` ) ;
37+ const field = settingEditFields [ valueType ] . clone ( )
38+ . attr ( 'data-name' , name )
39+ . attr ( 'data-community-id' , communityId )
40+ . get ( 0 ) ;
41+
42+ if ( valueType === 'array' && field instanceof HTMLSelectElement ) {
43+ for ( const opt of data . options ?? [ ] ) {
44+ const option = document . createElement ( 'option' ) ;
45+ option . textContent = opt ;
46+ option . value = opt ;
47+ option . selected = value . includes ( opt ) ;
48+ field . add ( option ) ;
49+ }
50+ }
51+ else if ( valueType === 'boolean' ) {
52+ field . value = value . toString ( ) ;
53+ }
54+ else {
55+ field . value = ! ! value ? value . toString ( ) : '' ;
56+ }
57+
58+ $tgt . addClass ( 'editing' )
59+ . html ( field )
60+ . append ( `<button class="button is-primary is-filled js-setting-submit has-display-block">Update</button>` ) ;
61+
62+ if ( valueType === 'array' ) {
63+ $ ( field ) . select2 ( ) ;
64+ }
3265 } ) ;
3366
3467 $ ( document ) . on ( 'click' , '.js-setting-submit' , async ( evt ) => {
@@ -39,9 +72,14 @@ $(() => {
3972 const communityId = $input . data ( 'community-id' ) ;
4073 const value = $input . val ( ) ;
4174
42- let body = { site_setting : { value} } ;
75+ const body = {
76+ site_setting : {
77+ value : Array . isArray ( value ) ? value . join ( ' ' ) : value
78+ }
79+ } ;
80+
4381 if ( ! ! communityId ) {
44- body = Object . assign ( body , { community_id : communityId } ) ;
82+ body . community_id = communityId ;
4583 }
4684
4785 const resp = await QPixel . fetchJSON ( `/admin/settings/${ name } ` , body ) ;
0 commit comments