( options: Options = {}, args: InitArgs = {} )
| 33 | * @return A created Splide instance. |
| 34 | */ |
| 35 | export function init( options: Options = {}, args: InitArgs = {} ): Splide { |
| 36 | const { width = SLIDER_WIDTH, height = 0 } = options; |
| 37 | const { |
| 38 | id, |
| 39 | length = 10, |
| 40 | arrows, |
| 41 | autoplay, |
| 42 | progress, |
| 43 | mount = true, |
| 44 | html, |
| 45 | src, |
| 46 | dataSrc, |
| 47 | dataSrcset, |
| 48 | json, |
| 49 | insertHtml, |
| 50 | dataInterval, |
| 51 | } = args; |
| 52 | |
| 53 | const slideWidth = +width / ( options.perPage || 1 ); |
| 54 | const slideHeight = +height / ( options.perPage || 1 ); |
| 55 | const innerHtml = html |
| 56 | || buildHtml( { length, arrows, autoplay, progress, src, dataSrc, dataSrcset, json, id, dataInterval } ); |
| 57 | |
| 58 | if ( insertHtml ) { |
| 59 | if ( ! document.body.innerHTML ) { |
| 60 | throw new Error( 'Invalid usage.' ); |
| 61 | } |
| 62 | |
| 63 | document.body.insertAdjacentHTML( 'beforeend', innerHtml ); |
| 64 | } else { |
| 65 | document.head.innerHTML = ''; |
| 66 | document.body.innerHTML = innerHtml; |
| 67 | } |
| 68 | |
| 69 | const root = id ? document.getElementById( id ) : document.querySelector( `.${ CLASS_ROOT }` ); |
| 70 | const track = root.querySelector( `.${ CLASS_TRACK }` ); |
| 71 | const list = root.querySelector<HTMLElement>( `.${ CLASS_LIST }` ); |
| 72 | const slides = root.querySelectorAll<HTMLElement>( `.${ CLASS_SLIDE }` ); |
| 73 | |
| 74 | root.getBoundingClientRect = (): DOMRect => { |
| 75 | return assign( {}, DOM_RECT, { width: +width } ); |
| 76 | }; |
| 77 | |
| 78 | track.getBoundingClientRect = (): DOMRect => { |
| 79 | return assign( {}, DOM_RECT, { width: +width, right: +width } ); |
| 80 | }; |
| 81 | |
| 82 | list.getBoundingClientRect = (): DOMRect => { |
| 83 | return assign( {}, DOM_RECT, { |
| 84 | width: +width, |
| 85 | ...parseTransform( list ), |
| 86 | } ); |
| 87 | }; |
| 88 | |
| 89 | setSlidesRect( Array.from( slides ), list, slideWidth, slideHeight ); |
| 90 | |
| 91 | const splide = new Splide( root as HTMLElement, options ); |
| 92 |
no test coverage detected
searching dependent graphs…