restrictions
2 years ago
BrowserReporting.js
2 years ago
ReportingContext.js
2 years ago
ReportingInterface.js
2 years ago
RestrictionError.js
2 years ago
functions.js
2 years ago
functions.js
139 lines
| 1 | /** |
| 2 | * @param node {HTMLElement} |
| 3 | */ |
| 4 | import BrowserReporting from './BrowserReporting'; |
| 5 | import { allRejected } from '../functions'; |
| 6 | import InputData from '../inputs/InputData'; |
| 7 | import NativeRestriction from './restrictions/NativeRestriction'; |
| 8 | import RequiredRestriction from './restrictions/RequiredRestriction'; |
| 9 | |
| 10 | const { |
| 11 | applyFilters, |
| 12 | } = JetPlugins.hooks; |
| 13 | |
| 14 | const getReportTypes = () => applyFilters( |
| 15 | 'jet.fb.reporting', |
| 16 | [ |
| 17 | BrowserReporting, |
| 18 | ], |
| 19 | ); |
| 20 | |
| 21 | let reportTypes = []; |
| 22 | |
| 23 | const getDefaultRestrictions = () => applyFilters( |
| 24 | 'jet.fb.restrictions.default', |
| 25 | [ |
| 26 | NativeRestriction, |
| 27 | RequiredRestriction, |
| 28 | ], |
| 29 | ); |
| 30 | |
| 31 | let defaultRestrictions = []; |
| 32 | |
| 33 | /** |
| 34 | * @param reporting {BrowserReporting} |
| 35 | * @param node |
| 36 | * @returns {*} |
| 37 | */ |
| 38 | function createDefaultRestrictions( reporting, node ) { |
| 39 | if ( !defaultRestrictions.length ) { |
| 40 | defaultRestrictions = getDefaultRestrictions(); |
| 41 | } |
| 42 | |
| 43 | for ( const restriction of defaultRestrictions ) { |
| 44 | const current = new restriction(); |
| 45 | |
| 46 | if ( !current.isSupported( node, reporting ) ) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | reporting.restrictions.push( current ); |
| 51 | } |
| 52 | |
| 53 | reporting.restrictions.forEach( item => item.setReporting( reporting ) ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param input {InputData} |
| 58 | * @return {AdvancedReporting|BrowserReporting} |
| 59 | */ |
| 60 | function createReport( input ) { |
| 61 | if ( !reportTypes.length ) { |
| 62 | reportTypes = getReportTypes(); |
| 63 | } |
| 64 | |
| 65 | for ( const reportType of reportTypes ) { |
| 66 | const current = new reportType(); |
| 67 | |
| 68 | if ( !current.isSupported( input.nodes[ 0 ], input ) ) { |
| 69 | continue; |
| 70 | } |
| 71 | current.setInput( input ); |
| 72 | |
| 73 | return current; |
| 74 | } |
| 75 | |
| 76 | throw new Error( 'Something went wrong' ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param inputs {InputData[]} |
| 81 | * @param silence {Boolean} |
| 82 | * |
| 83 | * @return {Function[]} |
| 84 | */ |
| 85 | function getValidateCallbacks( inputs, silence = false ) { |
| 86 | const callbacks = []; |
| 87 | |
| 88 | inputs?.[ 0 ]?.getContext()?.reset( { silence } ); |
| 89 | |
| 90 | for ( const input of inputs ) { |
| 91 | if ( !( |
| 92 | input instanceof InputData |
| 93 | ) ) { |
| 94 | console.group( 'Input is not instance of InputData' ); |
| 95 | console.error( input ); |
| 96 | console.groupEnd(); |
| 97 | |
| 98 | continue; |
| 99 | } |
| 100 | callbacks.push( |
| 101 | ( resolve, reject ) => { |
| 102 | input.reporting.validateOnChangeState(). |
| 103 | then( resolve ). |
| 104 | catch( reject ); |
| 105 | }, |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | return callbacks; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param inputs {InputData[]} |
| 114 | * @param silence {Boolean} |
| 115 | * @return {Promise<unknown[]>} |
| 116 | */ |
| 117 | function validateInputs( inputs, silence = false ) { |
| 118 | return Promise.all( |
| 119 | getValidateCallbacks( inputs, silence ).map( |
| 120 | current => new Promise( current ), |
| 121 | ), |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param inputs {InputData[]} |
| 127 | * @param silence {Boolean} |
| 128 | */ |
| 129 | function validateInputsAll( inputs, silence = false ) { |
| 130 | return allRejected( getValidateCallbacks( inputs, silence ) ); |
| 131 | } |
| 132 | |
| 133 | export { |
| 134 | createReport, |
| 135 | validateInputs, |
| 136 | validateInputsAll, |
| 137 | createDefaultRestrictions, |
| 138 | getValidateCallbacks, |
| 139 | }; |