@@ -9,25 +9,26 @@ let $ = { // Convenience for internals.
9
9
// Table of contents and convenient call chaining sugar. For a familiar "jQuery like" syntax. 🙂
10
10
// Check before adding new: https://youmightnotneedjquery.com/
11
11
sugar ( e ) {
12
- if ( e == null ) { console . warn ( `Surreal: Cannot use "${ e } ". Missing a character?` ) }
12
+ if ( $ . isNodeList ( e ) ) { e . forEach ( _ => { $ . sugar ( _ ) } ) } // Apply Surreal to nodes in array as well as the array.
13
+ if ( ! $ . isNode ( e ) && ! $ . isNodeList ( e ) ) { console . warn ( `Surreal: Not a supported element / node / array of nodes "${ e } "` ) ; return e }
13
14
if ( e . hasOwnProperty ( 'hasSurreal' ) ) return e // Surreal already applied
14
15
15
16
// General
16
- e . run = ( value ) => { return $ . run ( e , value ) }
17
+ e . run = ( f ) => { return $ . run ( e , f ) }
17
18
e . remove = ( ) => { return $ . remove ( e ) }
18
19
19
20
// Classes and CSS.
20
21
e . classAdd = ( name ) => { return $ . classAdd ( e , name ) }
21
- e . class_add = e . add_class = e . addClass = e . classAdd // Alias
22
+ e . class_add = e . add_class = e . addClass = e . classAdd // Alias
22
23
e . classRemove = ( name ) => { return $ . classRemove ( e , name ) }
23
24
e . class_remove = e . remove_class = e . removeClass = e . classRemove // Alias
24
25
e . classToggle = ( name , force ) => { return $ . classToggle ( e , name , force ) }
25
26
e . class_toggle = e . toggle_class = e . toggleClass = e . classToggle // Alias
26
27
e . styles = ( value ) => { return $ . styles ( e , value ) }
27
28
28
29
// Events.
29
- e . on = ( name , fn ) => { return $ . on ( e , name , fn ) }
30
- e . off = ( name , fn ) => { return $ . off ( e , name , fn ) }
30
+ e . on = ( name , f ) => { return $ . on ( e , name , f ) }
31
+ e . off = ( name , f ) => { return $ . off ( e , name , f ) }
31
32
e . offAll = ( name ) => { return $ . offAll ( e , name ) }
32
33
e . off_all = e . offAll // Alias
33
34
e . disable = ( ) => { return $ . disable ( e ) }
@@ -66,16 +67,17 @@ let $ = { // Convenience for internals.
66
67
// Returns an Array of elements (so you can use methods like forEach/filter/map/reduce if you want).
67
68
// Example: any('button')
68
69
any ( selector , start = document , warning = true ) {
69
- if ( selector == null ) return $ . sugar ( [ start . currentScript . parentElement ] ) //Just local me() in <script>
70
+ if ( selector == null ) return $ . sugar ( [ start . currentScript . parentElement ] ) //Similar to me()
70
71
if ( selector instanceof Event ) return selector . currentTarget ?$ . any ( selector . currentTarget ) :( console . warn ( `Surreal: Event currentTarget is null. Please save your element because async will lose it` ) , null ) // Events try currentTarget
71
72
if ( selector === '-' || selector === 'prev' || selector === 'previous' ) return $ . sugar ( [ start . currentScript . previousElementSibling ] ) // Element directly before <script>
72
73
if ( $ . isSelector ( selector , start , warning ) ) return $ . sugar ( Array . from ( start . querySelectorAll ( selector ) ) ) // String selector.
73
74
if ( $ . isNode ( selector ) ) return $ . sugar ( [ selector ] ) // Single element. Convert to Array.
74
75
if ( $ . isNodeList ( selector ) ) return $ . sugar ( Array . from ( selector ) ) // Valid NodeList or Array.
75
- return null // Invalid.
76
+ return $ . sugar ( [ ] ) // Invalid.
76
77
} ,
77
78
// Run any function on element(s)
78
79
run ( e , f ) {
80
+ if ( typeof f !== 'function' ) { console . warn ( `Surreal: run(f) f must be a function` ) ; return e }
79
81
if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . run ( _ , f ) } )
80
82
if ( $ . isNode ( e ) ) { f ( e ) ; }
81
83
return e
@@ -88,24 +90,23 @@ let $ = { // Convenience for internals.
88
90
} ,
89
91
// Add class to element(s).
90
92
classAdd ( e , name ) {
91
- if ( e === null || ( Array . isArray ( e ) && e . length === 0 ) ) return null
92
- if ( typeof name !== 'string' ) return null
93
+ if ( typeof name !== 'string' ) return e
93
94
if ( name . charAt ( 0 ) === '.' ) name = name . substring ( 1 )
94
95
if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . classAdd ( _ , name ) } )
95
96
if ( $ . isNode ( e ) ) e . classList . add ( name )
96
97
return e
97
98
} ,
98
99
// Remove class from element(s).
99
100
classRemove ( e , name ) {
100
- if ( typeof name !== 'string' ) return null
101
+ if ( typeof name !== 'string' ) return e
101
102
if ( name . charAt ( 0 ) === '.' ) name = name . substring ( 1 )
102
103
if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . classRemove ( _ , name ) } )
103
104
if ( $ . isNode ( e ) ) e . classList . remove ( name )
104
105
return e
105
106
} ,
106
107
// Toggle class in element(s).
107
108
classToggle ( e , name , force ) {
108
- if ( typeof name !== 'string' ) return null
109
+ if ( typeof name !== 'string' ) return e
109
110
if ( name . charAt ( 0 ) === '.' ) name = name . substring ( 1 )
110
111
if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . classToggle ( _ , name , force ) } )
111
112
if ( $ . isNode ( e ) ) e . classList . toggle ( name , force )
@@ -126,21 +127,20 @@ let $ = { // Convenience for internals.
126
127
if ( $ . isNode ( e ) ) { Object . assign ( e . style , value ) }
127
128
return e
128
129
}
130
+ return e
129
131
} ,
130
132
// Add event listener to element(s).
131
- // Match a sender: if(!event.target.matches(".selector")) return;
133
+ // Match a sender: if (!event.target.matches(".selector")) return;
132
134
//📚️ https://developer.mozilla.org/en-US/docs/Web/API/Event
133
135
//✂️ Vanilla: document.querySelector(".thing").addEventListener("click", (e) => { alert("clicked") }
134
- on ( e , name , fn ) {
135
- if ( typeof name !== 'string' ) return null
136
- if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . on ( _ , name , fn ) } )
137
- if ( $ . isNode ( e ) ) e . addEventListener ( name , fn )
136
+ on ( e , name , f ) {
137
+ if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . on ( _ , name , f ) } )
138
+ if ( $ . isNode ( e ) ) e . addEventListener ( name , f )
138
139
return e
139
140
} ,
140
- off ( e , name , fn ) {
141
- if ( typeof name !== 'string' ) return null
142
- if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . off ( _ , name , fn ) } )
143
- if ( $ . isNode ( e ) ) e . removeEventListener ( name , fn )
141
+ off ( e , name , f ) {
142
+ if ( $ . isNodeList ( e ) ) e . forEach ( _ => { $ . off ( _ , name , f ) } )
143
+ if ( $ . isNode ( e ) ) e . removeEventListener ( name , f )
144
144
return e
145
145
} ,
146
146
offAll ( e ) {
@@ -173,8 +173,8 @@ let $ = { // Convenience for internals.
173
173
// Halt event. Default: Stops normal event actions and event propagation.
174
174
halt ( ev , keepBubbling = false , keepDefault = false ) {
175
175
if ( ev instanceof Event ) {
176
- if ( ! keepDefault ) ev . preventDefault ( )
177
- if ( ! keepBubbling ) ev . stopPropagation ( )
176
+ if ( ! keepDefault ) ev . preventDefault ( )
177
+ if ( ! keepBubbling ) ev . stopPropagation ( )
178
178
}
179
179
return ev
180
180
} ,
@@ -225,9 +225,9 @@ let $ = { // Convenience for internals.
225
225
} ,
226
226
// ⚙️ Used internally by DOM functions. Warning when selector is invalid. Likely missing a "#" or "."
227
227
isSelector ( selector = "" , start = document , warning = true ) {
228
- if ( typeof selector !== 'string' ) return false
228
+ if ( typeof selector !== 'string' ) return false
229
229
if ( start . querySelector ( selector ) == null ) {
230
- if ( warning ) console . warn ( `Surreal: "${ selector } "was not found. Missing a character? (. #) ` )
230
+ if ( warning ) console . log ( `Surreal: "${ selector } " not found, ignoring. ` )
231
231
return false
232
232
}
233
233
return true // Valid.
@@ -243,25 +243,25 @@ return $
243
243
function pluginEffects ( e ) {
244
244
// Fade out and remove element.
245
245
// Equivalent to jQuery fadeOut(), but actually removes the element!
246
- function fadeOut ( e , fn = false , ms = 1000 , remove = true ) {
246
+ function fadeOut ( e , f = undefined , ms = 1000 , remove = true ) {
247
247
let thing = e
248
- if ( surreal . isNodeList ( e ) ) e . forEach ( _ => { fadeOut ( _ , fn , ms ) } )
248
+ if ( surreal . isNodeList ( e ) ) e . forEach ( _ => { fadeOut ( _ , f , ms ) } )
249
249
if ( surreal . isNode ( e ) ) {
250
250
( async ( ) => {
251
251
surreal . styles ( e , { transform :'scale(1)' , transition :`all${ ms } ms ease-out` , overflow :'hidden' } )
252
252
await tick ( )
253
253
surreal . styles ( e , { transform :'scale(0.9)' , opacity :'0' } )
254
254
await sleep ( ms , e )
255
- if ( typeof fn === 'function' ) fn ( thing ) // Run custom callback?
255
+ if ( typeof f === 'function' ) f ( thing ) // Run custom callback?
256
256
if ( remove ) surreal . remove ( thing ) // Remove element after animation is completed?
257
257
} ) ( )
258
258
}
259
259
}
260
260
// Fade in an element that has opacity under 1
261
- function fadeIn ( e , fn = false , ms = 1000 ) {
261
+ function fadeIn ( e , f = undefined , ms = 1000 ) {
262
262
let thing = e
263
- if ( surreal . isNodeList ( e ) ) e . forEach ( _ => { fadeIn ( _ , fn , ms ) } )
264
- if ( surreal . isNode ( e ) ) {
263
+ if ( surreal . isNodeList ( e ) ) e . forEach ( _ => { fadeIn ( _ , f , ms ) } )
264
+ if ( surreal . isNode ( e ) ) {
265
265
( async ( ) => {
266
266
let save = e . style // Store original style.
267
267
surreal . styles ( e , { transition :`all${ ms } ms ease-in` , overflow :'hidden' } )
@@ -270,14 +270,14 @@ function pluginEffects(e) {
270
270
await sleep ( ms , e )
271
271
e . style = save // Revert back to original style.
272
272
surreal . styles ( e , { opacity :'1' } ) // Ensure we're visible after reverting to original style.
273
- if ( typeof fn === 'function' ) fn ( thing ) // Run custom callback?
273
+ if ( typeof f === 'function' ) f ( thing ) // Run custom callback?
274
274
} ) ( )
275
275
}
276
276
}
277
277
// Add sugar
278
- e . fadeOut = ( fn , ms , remove ) => { return fadeOut ( e , fn , ms , remove ) }
278
+ e . fadeOut = ( f , ms , remove ) => { return fadeOut ( e , f , ms , remove ) }
279
279
e . fade_out = e . fadeOut
280
- e . fadeIn = ( fn , ms ) => { return fadeIn ( e , fn , ms ) }
280
+ e . fadeIn = ( f , ms ) => { return fadeIn ( e , f , ms ) }
281
281
e . fade_in = e . fadeIn
282
282
}
283
283