| 196 | } |
| 197 | |
| 198 | export const create = function(elem, slides, opts) { |
| 199 | |
| 200 | // Validate options |
| 201 | opts = validate(opts) |
| 202 | |
| 203 | // Slide index counter |
| 204 | let c = null |
| 205 | |
| 206 | // Object containing references to all rendered elements |
| 207 | let refs = null |
| 208 | |
| 209 | // Returns the slider element |
| 210 | const _element = () => elem |
| 211 | |
| 212 | // Return the total number of slides |
| 213 | const _length = () => slides.length |
| 214 | |
| 215 | // Returns the current slide index |
| 216 | const _current = () => c() |
| 217 | |
| 218 | // Navigate to a given slide |
| 219 | // Use c() as the default oldIndex as the counter hasn't been recreated yet, |
| 220 | // when called through the API. Internal functions can set a custom oldIndex. |
| 221 | const _goto = (newIndex, oldIndex = c()) => { |
| 222 | |
| 223 | // Run beforePrev event |
| 224 | // Stop execution when function returns false |
| 225 | if (opts.beforeChange(instance, newIndex, oldIndex) === false) return false |
| 226 | |
| 227 | // Recreate counter with new initial value |
| 228 | c = counter(0, _length() - 1, newIndex) |
| 229 | |
| 230 | // Switch to new slide |
| 231 | setSlide(refs.dotElems, refs.slidesElem, c(), _length()) |
| 232 | |
| 233 | // Run afterShow event |
| 234 | opts.afterChange(instance, newIndex, oldIndex) |
| 235 | |
| 236 | } |
| 237 | |
| 238 | // Navigate to the previous slide |
| 239 | const _prev = () => { |
| 240 | |
| 241 | // Store old index before modifying the counter |
| 242 | const oldIndex = c() |
| 243 | const newIndex = c(-1) |
| 244 | |
| 245 | _goto(newIndex, oldIndex) |
| 246 | |
| 247 | } |
| 248 | |
| 249 | // Navigate to the next slide |
| 250 | const _next = () => { |
| 251 | |
| 252 | // Store old index before modifying the counter |
| 253 | const oldIndex = c() |
| 254 | const newIndex = c(1) |
| 255 | |