1
- import $ from 'jquery/dist/jquery.slim' ;
2
1
import clone from 'lodash/clone' ;
3
2
import template from 'lodash/template' ;
4
3
import filter from 'lodash/filter' ;
5
4
import findIndex from 'lodash/findIndex' ;
5
+
6
6
import DeepDiff from 'deep-diff' ;
7
+
7
8
import { Subject } from 'rxjs/Subject' ;
8
9
import { Observable } from 'rxjs/Observable' ;
9
10
import 'rxjs/add/observable/fromEvent' ;
@@ -19,10 +20,10 @@ import 'rxjs/add/operator/scan';
19
20
import 'rxjs/add/operator/mergeMap' ;
20
21
21
22
function readyElements ( el , templates ) {
22
- const $select = $ ( " .subnav-spacer-right .select-menu-list" , el ) ;
23
+ const selectElement = el . querySelector ( ' .subnav-spacer-right .select-menu-list' ) ;
23
24
return {
24
- $input : injectInputform ( $select , templates ) ,
25
- $select
25
+ inputElement : injectInputform ( selectElement , templates ) ,
26
+ selectElement
26
27
}
27
28
}
28
29
@@ -59,11 +60,11 @@ function createProps(el) {
59
60
}
60
61
61
62
function intent ( { element } ) {
62
- const { $input , $select } = element ;
63
+ const { inputElement , selectElement } = element ;
63
64
64
65
const error$ = new Subject ( ) ;
65
66
const enterInput$ = Observable
66
- . fromEvent ( $input , 'keypress' )
67
+ . fromEvent ( inputElement , 'keypress' )
67
68
. filter ( e => e . key === 'Enter' )
68
69
. filter ( ( ) => {
69
70
const isFilterInput = ! ! location . search ;
@@ -74,7 +75,7 @@ function intent({ element }) {
74
75
75
76
return isFilterInput ;
76
77
} )
77
- . filter ( e => $ . trim ( e . target . value ) !== '' )
78
+ . filter ( e => e . target . value . trim ( ) !== '' )
78
79
. do ( e => {
79
80
e . preventDefault ( ) ;
80
81
e . stopPropagation ( ) ;
@@ -85,7 +86,7 @@ function intent({ element }) {
85
86
return value ;
86
87
} ) ;
87
88
const clickDelBtn$ = Observable
88
- . fromEvent ( $select , 'click' )
89
+ . fromEvent ( selectElement , 'click' )
89
90
. filter ( ( { target } ) => {
90
91
return target . classList . contains ( 'gfe--deleteBtn' )
91
92
|| target . parentNode . classList . contains ( 'gfe--deleteBtn' ) ;
@@ -151,28 +152,29 @@ function view(state$, { element, template }) {
151
152
152
153
function updateItem ( path , rhs ) {
153
154
const [ idx , key ] = path ;
154
- const $target = $ ( '. gfe--filterItem') . eq ( idx ) ;
155
+ const targetElement = document . getElementsByClassName ( ' gfe--filterItem') [ idx ] ;
155
156
156
157
if ( key === 'name' ) {
157
- $target . find ( '.select-menu-item-text' ) . text ( rhs ) ;
158
- $target . find ( 'svg' ) . attr ( 'data-name' , rhs ) ;
159
- $target . attr ( 'id' , rhs ) ;
158
+ targetElement . querySelector ( '.select-menu-item-text' ) . textContent = rhs ;
159
+ targetElement . querySelector ( 'svg' ) . setAttribute ( 'data-name' , rhs ) ;
160
+ targetElement . id = rhs ;
160
161
} else if ( key === 'path' ) {
161
- $target . attr ( ' href' , rhs ) ;
162
+ targetElement . href = rhs ;
162
163
}
163
164
}
164
165
165
166
function removeItem ( name ) {
166
- $ ( `#${ name } ` ) . remove ( ) ;
167
+ const targetNode = document . getElementById ( name ) ;
168
+ targetNode . parentNode . removeChild ( targetNode ) ;
167
169
}
168
170
169
171
function patch ( diff , element , template ) {
170
172
const { kind, item, rhs, lhs, path } = diff ;
171
- const { $input } = element ;
173
+ const { inputElement } = element ;
172
174
const { tplItem } = template ;
173
175
174
176
if ( kind === 'N' ) {
175
- prependItem ( tplItem , $input , rhs ) ;
177
+ prependItem ( tplItem , inputElement , rhs ) ;
176
178
} else if ( kind === 'A' ) {
177
179
patch ( item , element , template ) ;
178
180
} else if ( kind === 'E' ) {
@@ -182,9 +184,9 @@ function patch(diff, element, template) {
182
184
}
183
185
}
184
186
185
- function prependItem ( tplItem , $target , data ) {
187
+ function prependItem ( tplItem , targetElement , data ) {
186
188
const itemHtml = template ( tplItem ) ( data ) ;
187
- $target . before ( itemHtml ) ;
189
+ targetElement . insertAdjacentHTML ( 'beforebegin' , itemHtml ) ;
188
190
}
189
191
190
192
function main ( props ) {
@@ -214,10 +216,13 @@ function run(main, el) {
214
216
DOM . subscribe ( ) ;
215
217
}
216
218
217
- function injectInputform ( $select , { tplInput } ) {
218
- const $input = $ ( tplInput ) ;
219
- $select . children ( ) . last ( ) . before ( $input ) ;
220
- return $input ;
219
+ function injectInputform ( selectNode , { tplInput } ) {
220
+ const childNods = selectNode . children ;
221
+ const lastIdx = childNods . length - 1 ;
222
+
223
+ childNods [ lastIdx ] . insertAdjacentHTML ( 'beforebegin' , tplInput ) ;
224
+
225
+ return selectNode . children [ lastIdx ] ;
221
226
}
222
227
223
228
function isOnInvalidPath ( ) {
@@ -230,7 +235,13 @@ function isOnInvalidPath() {
230
235
return isNotGithubPage || hasNoFilterOnPage ;
231
236
}
232
237
233
- $ ( document ) . ready ( ( ) => {
238
+ ; ( function ready ( fn ) {
239
+ if ( document . readyState != 'loading' ) {
240
+ fn ( ) ;
241
+ } else {
242
+ document . addEventListener ( 'DOMContentLoaded' , fn ) ;
243
+ }
244
+ } ) ( ( ) => {
234
245
const runner = run . bind ( null , main , document ) ;
235
246
chrome . extension . onMessage . addListener ( runner ) ;
236
247
runner . call ( this ) ;
0 commit comments