mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 11:28:24 +00:00 
			
		
		
		
	Add a button editing action secret (#34348)
Add a button editing action secret Closes #34190 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		
							
								
								
									
										14
									
								
								web_src/js/features/common-button.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								web_src/js/features/common-button.test.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| import {assignElementProperty} from './common-button.ts'; | ||||
|  | ||||
| test('assignElementProperty', () => { | ||||
|   const elForm = document.createElement('form'); | ||||
|   assignElementProperty(elForm, 'action', '/test-link'); | ||||
|   expect(elForm.action).contains('/test-link'); // the DOM always returns absolute URL | ||||
|   assignElementProperty(elForm, 'text-content', 'dummy'); | ||||
|   expect(elForm.textContent).toBe('dummy'); | ||||
|  | ||||
|   const elInput = document.createElement('input'); | ||||
|   expect(elInput.readOnly).toBe(false); | ||||
|   assignElementProperty(elInput, 'read-only', 'true'); | ||||
|   expect(elInput.readOnly).toBe(true); | ||||
| }); | ||||
| @@ -102,6 +102,21 @@ function onHidePanelClick(el: HTMLElement, e: MouseEvent) { | ||||
|   throw new Error('no panel to hide'); // should never happen, otherwise there is a bug in code | ||||
| } | ||||
|  | ||||
| export function assignElementProperty(el: any, name: string, val: string) { | ||||
|   name = camelize(name); | ||||
|   const old = el[name]; | ||||
|   if (typeof old === 'boolean') { | ||||
|     el[name] = val === 'true'; | ||||
|   } else if (typeof old === 'number') { | ||||
|     el[name] = parseFloat(val); | ||||
|   } else if (typeof old === 'string') { | ||||
|     el[name] = val; | ||||
|   } else { | ||||
|     // in the future, we could introduce a better typing system like `data-modal-form.action:string="..."` | ||||
|     throw new Error(`cannot assign element property ${name} by value ${val}`); | ||||
|   } | ||||
| } | ||||
|  | ||||
| function onShowModalClick(el: HTMLElement, e: MouseEvent) { | ||||
|   // A ".show-modal" button will show a modal dialog defined by its "data-modal" attribute. | ||||
|   // Each "data-modal-{target}" attribute will be filled to target element's value or text-content. | ||||
| @@ -109,7 +124,7 @@ function onShowModalClick(el: HTMLElement, e: MouseEvent) { | ||||
|   // * Then, try to query '[name=target]' | ||||
|   // * Then, try to query '.target' | ||||
|   // * Then, try to query 'target' as HTML tag | ||||
|   // If there is a ".{attr}" part like "data-modal-form.action", then the form's "action" attribute will be set. | ||||
|   // If there is a ".{prop-name}" part like "data-modal-form.action", the "form" element's "action" property will be set, the "prop-name" will be camel-cased to "propName". | ||||
|   e.preventDefault(); | ||||
|   const modalSelector = el.getAttribute('data-modal'); | ||||
|   const elModal = document.querySelector(modalSelector); | ||||
| @@ -122,7 +137,7 @@ function onShowModalClick(el: HTMLElement, e: MouseEvent) { | ||||
|     } | ||||
|  | ||||
|     const attrTargetCombo = attrib.name.substring(modalAttrPrefix.length); | ||||
|     const [attrTargetName, attrTargetAttr] = attrTargetCombo.split('.'); | ||||
|     const [attrTargetName, attrTargetProp] = attrTargetCombo.split('.'); | ||||
|     // try to find target by: "#target" -> "[name=target]" -> ".target" -> "<target> tag" | ||||
|     const attrTarget = elModal.querySelector(`#${attrTargetName}`) || | ||||
|       elModal.querySelector(`[name=${attrTargetName}]`) || | ||||
| @@ -133,8 +148,8 @@ function onShowModalClick(el: HTMLElement, e: MouseEvent) { | ||||
|       continue; | ||||
|     } | ||||
|  | ||||
|     if (attrTargetAttr) { | ||||
|       (attrTarget as any)[camelize(attrTargetAttr)] = attrib.value; | ||||
|     if (attrTargetProp) { | ||||
|       assignElementProperty(attrTarget, attrTargetProp, attrib.value); | ||||
|     } else if (attrTarget.matches('input, textarea')) { | ||||
|       (attrTarget as HTMLInputElement | HTMLTextAreaElement).value = attrib.value; // FIXME: add more supports like checkbox | ||||
|     } else { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user