-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from philomena-dev/ts-tagsmisc
Convert tagsmisc and sources scripts to TypeScript
- Loading branch information
Showing
4 changed files
with
87 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Tags Misc | ||
*/ | ||
|
||
import { assertType, assertNotNull } from './utils/assert'; | ||
import { $, $$ } from './utils/dom'; | ||
import store from './utils/store'; | ||
import { initTagDropdown } from './tags'; | ||
import { setupTagsInput, reloadTagsInput } from './tagsinput'; | ||
import '../types/ujs'; | ||
|
||
type TagInputActionFunction = (tagInput: HTMLTextAreaElement | null) => void; | ||
type TagInputActionList = Record<string, TagInputActionFunction>; | ||
|
||
function tagInputButtons(event: MouseEvent) { | ||
const target = assertType(event.target, HTMLElement); | ||
|
||
const actions: TagInputActionList = { | ||
save(tagInput: HTMLTextAreaElement | null) { | ||
tagInput && store.set('tag_input', tagInput.value); | ||
}, | ||
load(tagInput: HTMLTextAreaElement | null) { | ||
if (!tagInput) { return; } | ||
|
||
// If entry 'tag_input' does not exist, try to use the current list | ||
tagInput.value = store.get('tag_input') || tagInput.value; | ||
reloadTagsInput(tagInput); | ||
}, | ||
clear(tagInput: HTMLTextAreaElement | null) { | ||
if (!tagInput) { return; } | ||
|
||
tagInput.value = ''; | ||
reloadTagsInput(tagInput); | ||
}, | ||
}; | ||
|
||
for (const [ name, action ] of Object.entries(actions)) { | ||
if (target && target.matches(`#tagsinput-${name}`)) { | ||
action($<HTMLTextAreaElement>('image_tag_input')); | ||
} | ||
} | ||
} | ||
|
||
function setupTags() { | ||
$$<HTMLDivElement>('.js-tag-block').forEach(el => { | ||
setupTagsInput(el); | ||
el.classList.remove('js-tag-block'); | ||
}); | ||
} | ||
|
||
function updateTagSauce({ target, detail }: FetchcompleteEvent) { | ||
if (target.matches('#tags-form')) { | ||
const tagSauce = assertNotNull($<HTMLDivElement>('.js-tagsauce')); | ||
|
||
detail.text().then(text => { | ||
tagSauce.outerHTML = text; | ||
setupTags(); | ||
initTagDropdown(); | ||
}); | ||
} | ||
} | ||
|
||
function setupTagEvents() { | ||
setupTags(); | ||
document.addEventListener('fetchcomplete', updateTagSauce); | ||
document.addEventListener('click', tagInputButtons); | ||
} | ||
|
||
export { setupTagEvents }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export {}; | ||
|
||
declare global { | ||
interface FetchcompleteEvent extends CustomEvent<Response> { | ||
target: HTMLElement, | ||
} | ||
|
||
interface GlobalEventHandlersEventMap { | ||
fetchcomplete: FetchcompleteEvent; | ||
} | ||
} |