@@ -6,55 +6,57 @@ import {POST} from '../modules/fetch.js';
66const { appSubUrl} = window . config ;
77
88export function initRepoTopicBar ( ) {
9- const $mgrBtn = $ ( '#manage_topic' ) ;
10- if ( ! $mgrBtn . length ) return ;
11- const $editDiv = $ ( '#topic_edit' ) ;
12- const $viewDiv = $ ( '#repo-topics' ) ;
13- const $saveBtn = $ ( '#save_topic' ) ;
14- const $topicDropdown = $ ( '#topic_edit .dropdown' ) ;
15- const $topicForm = $editDiv ; // the old logic, $editDiv is topicForm
9+ const mgrBtn = document . getElementById ( 'manage_topic' ) ;
10+ if ( ! mgrBtn ) return ;
11+ const editDiv = document . getElementById ( 'topic_edit' ) ;
12+ const viewDiv = document . getElementById ( 'repo-topics' ) ;
13+ const saveBtn = document . getElementById ( 'save_topic' ) ;
14+ const topicDropdown = editDiv . querySelector ( '.dropdown' ) ;
15+ const $topicDropdown = $ ( topicDropdown ) ;
16+ const $topicForm = $ ( editDiv ) ;
1617 const $topicDropdownSearch = $topicDropdown . find ( 'input.search' ) ;
1718 const topicPrompts = {
18- countPrompt : $ topicDropdown. attr ( 'data-text-count-prompt' ) ,
19- formatPrompt : $ topicDropdown. attr ( 'data-text-format-prompt' ) ,
19+ countPrompt : topicDropdown . getAttribute ( 'data-text-count-prompt' ) ?? undefined ,
20+ formatPrompt : topicDropdown . getAttribute ( 'data-text-format-prompt' ) ?? undefined ,
2021 } ;
2122
22- $ mgrBtn. on ( 'click' , ( ) => {
23- hideElem ( $ viewDiv) ;
24- showElem ( $ editDiv) ;
23+ mgrBtn . addEventListener ( 'click' , ( ) => {
24+ hideElem ( viewDiv ) ;
25+ showElem ( editDiv ) ;
2526 $topicDropdownSearch . trigger ( 'focus' ) ;
2627 } ) ;
2728
2829 $ ( '#cancel_topic_edit' ) . on ( 'click' , ( ) => {
29- hideElem ( $ editDiv) ;
30- showElem ( $ viewDiv) ;
31- $ mgrBtn. trigger ( ' focus' ) ;
30+ hideElem ( editDiv ) ;
31+ showElem ( viewDiv ) ;
32+ mgrBtn . focus ( ) ;
3233 } ) ;
3334
34- $ saveBtn. on ( 'click' , async ( ) => {
35+ saveBtn . addEventListener ( 'click' , async ( ) => {
3536 const topics = $ ( 'input[name=topics]' ) . val ( ) ;
3637
3738 const data = new FormData ( ) ;
3839 data . append ( 'topics' , topics ) ;
3940
40- const response = await POST ( $ saveBtn. attr ( 'data-link' ) , { data} ) ;
41+ const response = await POST ( saveBtn . getAttribute ( 'data-link' ) , { data} ) ;
4142
4243 if ( response . ok ) {
4344 const responseData = await response . json ( ) ;
4445 if ( responseData . status === 'ok' ) {
45- $viewDiv . children ( '.topic' ) . remove ( ) ;
46+ $ ( viewDiv ) . children ( '.topic' ) . remove ( ) ;
4647 if ( topics . length ) {
4748 const topicArray = topics . split ( ',' ) ;
4849 topicArray . sort ( ) ;
4950 for ( const topic of topicArray ) {
50- const $link = $ ( '<a class="ui repo-topic large label topic tw-m-0"></a>' ) ;
51- $link . attr ( 'href' , `${ appSubUrl } /explore/repos?q=${ encodeURIComponent ( topic ) } &topic=1` ) ;
52- $link . text ( topic ) ;
53- $link . insertBefore ( $mgrBtn ) ; // insert all new topics before manage button
51+ const link = document . createElement ( 'a' ) ;
52+ link . classList . add ( 'ui' , 'repo-topic' , 'large' , 'label' , 'topic' , 'tw-m-0' ) ;
53+ link . href = `${ appSubUrl } /explore/repos?q=${ encodeURIComponent ( topic ) } &topic=1` ;
54+ link . textContent = topic ;
55+ mgrBtn . parentNode . insertBefore ( link , mgrBtn ) ; // insert all new topics before manage button
5456 }
5557 }
56- hideElem ( $ editDiv) ;
57- showElem ( $ viewDiv) ;
58+ hideElem ( editDiv ) ;
59+ showElem ( viewDiv ) ;
5860 }
5961 } else if ( response . status === 422 ) {
6062 const responseData = await response . json ( ) ;
@@ -144,14 +146,14 @@ export function initRepoTopicBar() {
144146 } ,
145147 onAdd ( addedValue , _addedText , $addedChoice ) {
146148 addedValue = addedValue . toLowerCase ( ) . trim ( ) ;
147- $ ( $addedChoice ) . attr ( 'data-value' , addedValue ) ;
148- $ ( $addedChoice ) . attr ( 'data-text' , addedValue ) ;
149+ $ ( $addedChoice ) [ 0 ] . setAttribute ( 'data-value' , addedValue ) ;
150+ $ ( $addedChoice ) [ 0 ] . setAttribute ( 'data-text' , addedValue ) ;
149151 } ,
150152 } ) ;
151153
152154 $ . fn . form . settings . rules . validateTopic = function ( _values , regExp ) {
153155 const $topics = $topicDropdown . children ( 'a.ui.label' ) ;
154- const status = $topics . length === 0 || $topics . last ( ) . attr ( 'data-value' ) . match ( regExp ) ;
156+ const status = $topics . length === 0 || $topics . last ( ) [ 0 ] . getAttribute ( 'data-value' ) . match ( regExp ) ;
155157 if ( ! status ) {
156158 $topics . last ( ) . removeClass ( 'green' ) . addClass ( 'red' ) ;
157159 }
0 commit comments