mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-10-31 00:53:08 +00:00 
			
		
		
		
	Create select widget based on tomselect on django backend
Replace make_ajax in elections by the new widget
This commit is contained in:
		| @@ -13,12 +13,18 @@ import { type UserProfileSchema, userSearchUsers } from "#openapi"; | ||||
|  | ||||
| @registerComponent("autocomplete-select") | ||||
| class AutocompleteSelect extends inheritHtmlElement("select") { | ||||
|   static observedAttributes = ["delay", "placeholder", "max"]; | ||||
|   static observedAttributes = [ | ||||
|     "delay", | ||||
|     "placeholder", | ||||
|     "max", | ||||
|     "min-characters-for-search", | ||||
|   ]; | ||||
|   public widget: TomSelect; | ||||
|  | ||||
|   private delay: number | null = null; | ||||
|   private placeholder = ""; | ||||
|   private max: number | null = null; | ||||
|   protected minCharNumberForSearch = 0; | ||||
|   protected delay: number | null = null; | ||||
|   protected placeholder = ""; | ||||
|   protected max: number | null = null; | ||||
|  | ||||
|   protected attributeChangedCallback( | ||||
|     name: string, | ||||
| @@ -38,26 +44,59 @@ class AutocompleteSelect extends inheritHtmlElement("select") { | ||||
|         this.max = Number.parseInt(newValue) ?? null; | ||||
|         break; | ||||
|       } | ||||
|       case "min-characters-for-search": { | ||||
|         this.minCharNumberForSearch = Number.parseInt(newValue) ?? 0; | ||||
|         break; | ||||
|       } | ||||
|       default: { | ||||
|         return; | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   constructor() { | ||||
|     super(); | ||||
|  | ||||
|     window.addEventListener("DOMContentLoaded", () => { | ||||
|       this.widget = new TomSelect(this.node, this.tomSelectSettings()); | ||||
|       this.attachBehaviors(); | ||||
|     }); | ||||
|   connectedCallback() { | ||||
|     super.connectedCallback(); | ||||
|     // Collect all options nodes and put them into the select node | ||||
|     const options: Element[] = []; // We need to make a copy to delete while iterating | ||||
|     for (const child of this.children) { | ||||
|       if (child.tagName.toLowerCase() === "option") { | ||||
|         options.push(child); | ||||
|       } | ||||
|     } | ||||
|     for (const option of options) { | ||||
|       this.removeChild(option); | ||||
|       this.node.appendChild(option); | ||||
|     } | ||||
|     this.widget = new TomSelect(this.node, this.tomSelectSettings()); | ||||
|     this.attachBehaviors(); | ||||
|   } | ||||
|  | ||||
|   protected tomSelectSettings(): RecursivePartial<TomSettings> { | ||||
|     return { | ||||
|       maxItems: this.max, | ||||
|       maxItems: this.node.multiple ? this.max : 1, | ||||
|       loadThrottle: this.delay, | ||||
|       placeholder: this.placeholder, | ||||
|       shouldLoad: (query: string) => { | ||||
|         return query.length >= this.minCharNumberForSearch; // Avoid launching search with less than 2 characters | ||||
|       }, | ||||
|       render: { | ||||
|         option: (item: TomOption, sanitize: typeof escape_html) => { | ||||
|           return `<div class="select-item"> | ||||
|             <span class="select-item-text">${sanitize(item.text)}</span> | ||||
|           </div>`; | ||||
|         }, | ||||
|         item: (item: TomOption, sanitize: typeof escape_html) => { | ||||
|           return `<span><i class="fa fa-times"></i>${sanitize(item.text)}</span>`; | ||||
|         }, | ||||
|         // biome-ignore lint/style/useNamingConvention: that's how it's defined | ||||
|         not_loading: (data: TomOption, _sanitize: typeof escape_html) => { | ||||
|           return `<div class="no-results">${interpolate(gettext("You need to type %(number)s more characters"), { number: this.minCharNumberForSearch - data.input.length }, true)}</div>`; | ||||
|         }, | ||||
|         // biome-ignore lint/style/useNamingConvention: that's how it's defined | ||||
|         no_results: (_data: TomOption, _sanitize: typeof escape_html) => { | ||||
|           return `<div class="no-results">${gettext("No results found")}</div>`; | ||||
|         }, | ||||
|       }, | ||||
|     }; | ||||
|   } | ||||
|  | ||||
| @@ -76,23 +115,8 @@ class AutocompleteSelect extends inheritHtmlElement("select") { | ||||
| @registerComponent("user-ajax-select") | ||||
| export class UserAjaxSelect extends AutocompleteSelect { | ||||
|   public filter?: <T>(items: T[]) => T[]; | ||||
|   static observedAttributes = [ | ||||
|     "min-characters-for-search", | ||||
|     ...AutocompleteSelect.observedAttributes, | ||||
|   ]; | ||||
|  | ||||
|   private minCharNumberForSearch = 2; | ||||
|  | ||||
|   protected attributeChangedCallback( | ||||
|     name: string, | ||||
|     _oldValue?: string, | ||||
|     newValue?: string, | ||||
|   ) { | ||||
|     super.attributeChangedCallback(name, _oldValue, newValue); | ||||
|     if (name === "min-characters-for-search") { | ||||
|       this.minCharNumberForSearch = Number.parseInt(newValue) ?? 0; | ||||
|     } | ||||
|   } | ||||
|   protected minCharNumberForSearch = 2; | ||||
|  | ||||
|   protected tomSelectSettings(): RecursivePartial<TomSettings> { | ||||
|     return { | ||||
| @@ -103,9 +127,6 @@ export class UserAjaxSelect extends AutocompleteSelect { | ||||
|       valueField: "id", | ||||
|       labelField: "display_name", | ||||
|       searchField: [], // Disable local search filter and rely on tested backend | ||||
|       shouldLoad: (query: string) => { | ||||
|         return query.length >= this.minCharNumberForSearch; // Avoid launching search with less than 2 characters | ||||
|       }, | ||||
|       load: (query: string, callback: TomLoadCallback) => { | ||||
|         userSearchUsers({ | ||||
|           query: { | ||||
| @@ -124,6 +145,7 @@ export class UserAjaxSelect extends AutocompleteSelect { | ||||
|         }); | ||||
|       }, | ||||
|       render: { | ||||
|         ...super.tomSelectSettings().render, | ||||
|         option: (item: UserProfileSchema, sanitize: typeof escape_html) => { | ||||
|           return `<div class="select-item"> | ||||
|             <img | ||||
| @@ -137,14 +159,6 @@ export class UserAjaxSelect extends AutocompleteSelect { | ||||
|         item: (item: UserProfileSchema, sanitize: typeof escape_html) => { | ||||
|           return `<span><i class="fa fa-times"></i>${sanitize(item.display_name)}</span>`; | ||||
|         }, | ||||
|         // biome-ignore lint/style/useNamingConvention: that's how it's defined | ||||
|         not_loading: (data: TomOption, _sanitize: typeof escape_html) => { | ||||
|           return `<div class="no-results">${interpolate(gettext("You need to type %(number)s more characters"), { number: this.minCharNumberForSearch - data.input.length }, true)}</div>`; | ||||
|         }, | ||||
|         // biome-ignore lint/style/useNamingConvention: that's how it's defined | ||||
|         no_results: (_data: TomOption, _sanitize: typeof escape_html) => { | ||||
|           return `<div class="no-results">${gettext("No results found")}</div>`; | ||||
|         }, | ||||
|       }, | ||||
|     }; | ||||
|   } | ||||
|   | ||||
							
								
								
									
										192
									
								
								core/static/webpack/core/components/easymde-index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										192
									
								
								core/static/webpack/core/components/easymde-index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,192 @@ | ||||
| // biome-ignore lint/correctness/noUndeclaredDependencies: shipped by easymde | ||||
| import "codemirror/lib/codemirror.css"; | ||||
| import "easymde/src/css/easymde.css"; | ||||
| import { inheritHtmlElement, registerComponent } from "#core:utils/web-components"; | ||||
| // biome-ignore lint/correctness/noUndeclaredDependencies: Imported by EasyMDE | ||||
| import type CodeMirror from "codemirror"; | ||||
| // biome-ignore lint/style/useNamingConvention: This is how they called their namespace | ||||
| import EasyMDE from "easymde"; | ||||
| import { markdownRenderMarkdown } from "#openapi"; | ||||
|  | ||||
| const loadEasyMde = (textarea: HTMLTextAreaElement) => { | ||||
|   new EasyMDE({ | ||||
|     element: textarea, | ||||
|     spellChecker: false, | ||||
|     autoDownloadFontAwesome: false, | ||||
|     previewRender: Alpine.debounce((plainText: string, preview: MarkdownInput) => { | ||||
|       const func = async (plainText: string, preview: MarkdownInput): Promise<null> => { | ||||
|         preview.innerHTML = ( | ||||
|           await markdownRenderMarkdown({ body: { text: plainText } }) | ||||
|         ).data as string; | ||||
|         return null; | ||||
|       }; | ||||
|       func(plainText, preview); | ||||
|       return null; | ||||
|     }, 300), | ||||
|     forceSync: true, // Avoid validation error on generic create view | ||||
|     toolbar: [ | ||||
|       { | ||||
|         name: "heading-smaller", | ||||
|         action: EasyMDE.toggleHeadingSmaller, | ||||
|         className: "fa fa-header", | ||||
|         title: gettext("Heading"), | ||||
|       }, | ||||
|       { | ||||
|         name: "italic", | ||||
|         action: EasyMDE.toggleItalic, | ||||
|         className: "fa fa-italic", | ||||
|         title: gettext("Italic"), | ||||
|       }, | ||||
|       { | ||||
|         name: "bold", | ||||
|         action: EasyMDE.toggleBold, | ||||
|         className: "fa fa-bold", | ||||
|         title: gettext("Bold"), | ||||
|       }, | ||||
|       { | ||||
|         name: "strikethrough", | ||||
|         action: EasyMDE.toggleStrikethrough, | ||||
|         className: "fa fa-strikethrough", | ||||
|         title: gettext("Strikethrough"), | ||||
|       }, | ||||
|       { | ||||
|         name: "underline", | ||||
|         action: function customFunction(editor: { codemirror: CodeMirror.Editor }) { | ||||
|           const cm = editor.codemirror; | ||||
|           cm.replaceSelection(`__${cm.getSelection()}__`); | ||||
|         }, | ||||
|         className: "fa fa-underline", | ||||
|         title: gettext("Underline"), | ||||
|       }, | ||||
|       { | ||||
|         name: "superscript", | ||||
|         action: function customFunction(editor: { codemirror: CodeMirror.Editor }) { | ||||
|           const cm = editor.codemirror; | ||||
|           cm.replaceSelection(`^${cm.getSelection()}^`); | ||||
|         }, | ||||
|         className: "fa fa-superscript", | ||||
|         title: gettext("Superscript"), | ||||
|       }, | ||||
|       { | ||||
|         name: "subscript", | ||||
|         action: function customFunction(editor: { codemirror: CodeMirror.Editor }) { | ||||
|           const cm = editor.codemirror; | ||||
|           cm.replaceSelection(`~${cm.getSelection()}~`); | ||||
|         }, | ||||
|         className: "fa fa-subscript", | ||||
|         title: gettext("Subscript"), | ||||
|       }, | ||||
|       { | ||||
|         name: "code", | ||||
|         action: EasyMDE.toggleCodeBlock, | ||||
|         className: "fa fa-code", | ||||
|         title: gettext("Code"), | ||||
|       }, | ||||
|       "|", | ||||
|       { | ||||
|         name: "quote", | ||||
|         action: EasyMDE.toggleBlockquote, | ||||
|         className: "fa fa-quote-left", | ||||
|         title: gettext("Quote"), | ||||
|       }, | ||||
|       { | ||||
|         name: "unordered-list", | ||||
|         action: EasyMDE.toggleUnorderedList, | ||||
|         className: "fa fa-list-ul", | ||||
|         title: gettext("Unordered list"), | ||||
|       }, | ||||
|       { | ||||
|         name: "ordered-list", | ||||
|         action: EasyMDE.toggleOrderedList, | ||||
|         className: "fa fa-list-ol", | ||||
|         title: gettext("Ordered list"), | ||||
|       }, | ||||
|       "|", | ||||
|       { | ||||
|         name: "link", | ||||
|         action: EasyMDE.drawLink, | ||||
|         className: "fa fa-link", | ||||
|         title: gettext("Insert link"), | ||||
|       }, | ||||
|       { | ||||
|         name: "image", | ||||
|         action: EasyMDE.drawImage, | ||||
|         className: "fa-regular fa-image", | ||||
|         title: gettext("Insert image"), | ||||
|       }, | ||||
|       { | ||||
|         name: "table", | ||||
|         action: EasyMDE.drawTable, | ||||
|         className: "fa fa-table", | ||||
|         title: gettext("Insert table"), | ||||
|       }, | ||||
|       "|", | ||||
|       { | ||||
|         name: "clean-block", | ||||
|         action: EasyMDE.cleanBlock, | ||||
|         className: "fa fa-eraser fa-clean-block", | ||||
|         title: gettext("Clean block"), | ||||
|       }, | ||||
|       "|", | ||||
|       { | ||||
|         name: "preview", | ||||
|         action: EasyMDE.togglePreview, | ||||
|         className: "fa fa-eye no-disable", | ||||
|         title: gettext("Toggle preview"), | ||||
|       }, | ||||
|       { | ||||
|         name: "side-by-side", | ||||
|         action: EasyMDE.toggleSideBySide, | ||||
|         className: "fa fa-columns no-disable no-mobile", | ||||
|         title: gettext("Toggle side by side"), | ||||
|       }, | ||||
|       { | ||||
|         name: "fullscreen", | ||||
|         action: EasyMDE.toggleFullScreen, | ||||
|         className: "fa fa-expand no-mobile", | ||||
|         title: gettext("Toggle fullscreen"), | ||||
|       }, | ||||
|       "|", | ||||
|       { | ||||
|         name: "guide", | ||||
|         action: "/page/Aide_sur_la_syntaxe", | ||||
|         className: "fa fa-question-circle", | ||||
|         title: gettext("Markdown guide"), | ||||
|       }, | ||||
|     ], | ||||
|   }); | ||||
|  | ||||
|   const submits: HTMLInputElement[] = Array.from( | ||||
|     textarea.closest("form").querySelectorAll('input[type="submit"]'), | ||||
|   ); | ||||
|   const parentDiv = textarea.parentElement.parentElement; | ||||
|  | ||||
|   function checkMarkdownInput(event: Event) { | ||||
|     // an attribute is null if it does not exist, else a string | ||||
|     const required = textarea.getAttribute("required") != null; | ||||
|     const length = textarea.value.trim().length; | ||||
|  | ||||
|     if (required && length === 0) { | ||||
|       parentDiv.style.boxShadow = "red 0px 0px 1.5px 1px"; | ||||
|       event.preventDefault(); | ||||
|     } else { | ||||
|       parentDiv.style.boxShadow = ""; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   function onSubmitClick(e: Event) { | ||||
|     checkMarkdownInput(e); | ||||
|   } | ||||
|  | ||||
|   for (const submit of submits) { | ||||
|     submit.addEventListener("click", onSubmitClick); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| @registerComponent("markdown-input") | ||||
| class MarkdownInput extends inheritHtmlElement("textarea") { | ||||
|   connectedCallback() { | ||||
|     super.connectedCallback(); | ||||
|     loadEasyMde(this.node); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user