attrs
2 years ago
calc.module
2 years ago
html.macro
2 years ago
init
2 years ago
inputs
2 years ago
reactive
2 years ago
reporting
2 years ago
signals
2 years ago
submit
2 years ago
Observable.js
2 years ago
functions.js
2 years ago
main.js
2 years ago
supports.js
2 years ago
functions.js
162 lines
| 1 | import BaseHtmlAttr from './attrs/BaseHtmlAttr'; |
| 2 | import MaxFilesHtmlAttr from './attrs/MaxFilesHtmlAttr'; |
| 3 | import MaxFileSizeHtmlAttr from './attrs/MaxFileSizeHtmlAttr'; |
| 4 | import RemainingCalcAttr from './attrs/RemainingCalcAttr'; |
| 5 | import FileExtensionHtmlAttr from './attrs/FileExtensionHtmlAttr'; |
| 6 | |
| 7 | const { applyFilters } = JetPlugins.hooks; |
| 8 | |
| 9 | /** |
| 10 | * @param callbacks {Function[]} |
| 11 | * @return {Promise<*>} |
| 12 | */ |
| 13 | async function allRejected( callbacks ) { |
| 14 | const results = await Promise.allSettled( |
| 15 | callbacks.map( current => new Promise( current ) ), |
| 16 | ); |
| 17 | |
| 18 | if ( window?.JetFormBuilderSettings?.devmode ) { |
| 19 | console.group( 'allRejected' ); |
| 20 | console.info( ...results ); |
| 21 | console.groupEnd(); |
| 22 | } |
| 23 | |
| 24 | const invalid = results.filter( |
| 25 | ( error ) => 'rejected' === error.status, |
| 26 | ); |
| 27 | |
| 28 | return invalid.map( ( { reason, value } ) => { |
| 29 | return reason?.length ? reason[ 0 ] : ( |
| 30 | reason ?? value |
| 31 | ); |
| 32 | } ); |
| 33 | } |
| 34 | |
| 35 | function getLanguage() { |
| 36 | const lang = window?.navigator?.languages?.length |
| 37 | ? window.navigator.languages[ 0 ] |
| 38 | : window?.navigator?.language; |
| 39 | |
| 40 | return lang ?? 'en-US'; |
| 41 | } |
| 42 | |
| 43 | const getInputHtmlAttr = () => applyFilters( |
| 44 | 'jet.fb.input.html.attrs', |
| 45 | [ |
| 46 | 'min', |
| 47 | 'max', |
| 48 | 'minLength', |
| 49 | 'maxLength', |
| 50 | MaxFilesHtmlAttr, |
| 51 | MaxFileSizeHtmlAttr, |
| 52 | RemainingCalcAttr, |
| 53 | FileExtensionHtmlAttr, |
| 54 | ], |
| 55 | ); |
| 56 | |
| 57 | /** |
| 58 | * @type {BaseHtmlAttr[]} |
| 59 | */ |
| 60 | let inputHtmlAttrs = []; |
| 61 | |
| 62 | /** |
| 63 | * @param name {String} |
| 64 | */ |
| 65 | function getDefaultAttrByName( name ) { |
| 66 | const attr = new BaseHtmlAttr(); |
| 67 | attr.attrName = name; |
| 68 | |
| 69 | return attr; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param input {InputData} |
| 74 | */ |
| 75 | function setAttrs( input ) { |
| 76 | if ( !inputHtmlAttrs.length ) { |
| 77 | inputHtmlAttrs = getInputHtmlAttr(); |
| 78 | } |
| 79 | |
| 80 | for ( let inputHtmlAttr of inputHtmlAttrs ) { |
| 81 | let current; |
| 82 | if ( 'string' === typeof inputHtmlAttr ) { |
| 83 | current = getDefaultAttrByName( inputHtmlAttr ); |
| 84 | } |
| 85 | else { |
| 86 | current = new inputHtmlAttr(); |
| 87 | } |
| 88 | |
| 89 | if ( !current.isSupported( input ) ) { |
| 90 | continue; |
| 91 | } |
| 92 | input.attrs[ current.attrName ] = current; |
| 93 | |
| 94 | current.setInput( input ); |
| 95 | current.observe(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | function toHTML( text ) { |
| 100 | const template = document.createElement( 'template' ); |
| 101 | template.innerHTML = text.trim(); |
| 102 | |
| 103 | return template.content; |
| 104 | } |
| 105 | |
| 106 | function isEmpty( value ) { |
| 107 | if ( null === value || undefined === value ) { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | if ( 'object' === typeof value && !Array.isArray( value ) ) { |
| 112 | return !Object.keys( value )?.length; |
| 113 | } |
| 114 | |
| 115 | if ( 'number' === typeof value ) { |
| 116 | return 0 === value; |
| 117 | } |
| 118 | |
| 119 | return !value?.length; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param node {HTMLElement} |
| 124 | */ |
| 125 | function isVisible( node ) { |
| 126 | return ( |
| 127 | node?.isConnected && null !== node?.offsetParent |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param node {Element} |
| 133 | */ |
| 134 | function getOffsetTop( node ) { |
| 135 | const rect = node.getBoundingClientRect(); |
| 136 | |
| 137 | return rect?.top + window.scrollY; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param inputs {InputData[]} |
| 142 | */ |
| 143 | function focusOnInvalidInput( inputs ) { |
| 144 | for ( const input of inputs ) { |
| 145 | if ( input.reporting.validityState.current ) { |
| 146 | continue; |
| 147 | } |
| 148 | !input.reporting.hasAutoScroll() && input.onFocus(); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | export { |
| 154 | allRejected, |
| 155 | getLanguage, |
| 156 | setAttrs, |
| 157 | toHTML, |
| 158 | isEmpty, |
| 159 | getOffsetTop, |
| 160 | focusOnInvalidInput, |
| 161 | isVisible, |
| 162 | }; |