@@ -6,55 +6,57 @@ import {POST} from '../modules/fetch.js';
6
6
const { appSubUrl} = window . config ;
7
7
8
8
export 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 ) ;
16
17
const $topicDropdownSearch = $topicDropdown . find ( 'input.search' ) ;
17
18
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 ,
20
21
} ;
21
22
22
- $ mgrBtn. on ( 'click' , ( ) => {
23
- hideElem ( $ viewDiv) ;
24
- showElem ( $ editDiv) ;
23
+ mgrBtn . addEventListener ( 'click' , ( ) => {
24
+ hideElem ( viewDiv ) ;
25
+ showElem ( editDiv ) ;
25
26
$topicDropdownSearch . trigger ( 'focus' ) ;
26
27
} ) ;
27
28
28
29
$ ( '#cancel_topic_edit' ) . on ( 'click' , ( ) => {
29
- hideElem ( $ editDiv) ;
30
- showElem ( $ viewDiv) ;
31
- $ mgrBtn. trigger ( ' focus' ) ;
30
+ hideElem ( editDiv ) ;
31
+ showElem ( viewDiv ) ;
32
+ mgrBtn . focus ( ) ;
32
33
} ) ;
33
34
34
- $ saveBtn. on ( 'click' , async ( ) => {
35
+ saveBtn . addEventListener ( 'click' , async ( ) => {
35
36
const topics = $ ( 'input[name=topics]' ) . val ( ) ;
36
37
37
38
const data = new FormData ( ) ;
38
39
data . append ( 'topics' , topics ) ;
39
40
40
- const response = await POST ( $ saveBtn. attr ( 'data-link' ) , { data} ) ;
41
+ const response = await POST ( saveBtn . getAttribute ( 'data-link' ) , { data} ) ;
41
42
42
43
if ( response . ok ) {
43
44
const responseData = await response . json ( ) ;
44
45
if ( responseData . status === 'ok' ) {
45
- $viewDiv . children ( '.topic' ) . remove ( ) ;
46
+ $ ( viewDiv ) . children ( '.topic' ) . remove ( ) ;
46
47
if ( topics . length ) {
47
48
const topicArray = topics . split ( ',' ) ;
48
49
topicArray . sort ( ) ;
49
50
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
54
56
}
55
57
}
56
- hideElem ( $ editDiv) ;
57
- showElem ( $ viewDiv) ;
58
+ hideElem ( editDiv ) ;
59
+ showElem ( viewDiv ) ;
58
60
}
59
61
} else if ( response . status === 422 ) {
60
62
const responseData = await response . json ( ) ;
@@ -144,14 +146,14 @@ export function initRepoTopicBar() {
144
146
} ,
145
147
onAdd ( addedValue , _addedText , $addedChoice ) {
146
148
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 ) ;
149
151
} ,
150
152
} ) ;
151
153
152
154
$ . fn . form . settings . rules . validateTopic = function ( _values , regExp ) {
153
155
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 ) ;
155
157
if ( ! status ) {
156
158
$topics . last ( ) . removeClass ( 'green' ) . addClass ( 'red' ) ;
157
159
}
0 commit comments