admin
3 years ago
chunks
3 years ago
editor
3 years ago
frontend
3 years ago
action-localize-helper.js
3 years ago
file-upload.js
3 years ago
import-form.js
3 years ago
map-field.js
3 years ago
action-localize-helper.js
106 lines
| 1 | const getLocalized = ( actionType, objectKey = '' ) => { |
| 2 | const preparedAction = window.jetFormActionTypes.find( action => actionType === action.id ); |
| 3 | if ( ! preparedAction ) { |
| 4 | return false; |
| 5 | } |
| 6 | const actionSelfLocalized = window[ preparedAction.self ]; |
| 7 | |
| 8 | return ( objectKey && actionSelfLocalized[ objectKey ] ) ? actionSelfLocalized[ objectKey ] : actionSelfLocalized; |
| 9 | }; |
| 10 | |
| 11 | const getSourceObjectName = actionType => { |
| 12 | const preparedAction = window.jetFormActionTypes.find( action => actionType === action.id ); |
| 13 | |
| 14 | return preparedAction ? preparedAction.self : false; |
| 15 | }; |
| 16 | |
| 17 | const getLocalizedWithFunc = ( { actionType = false, source = false }, |
| 18 | objectKey, |
| 19 | ifEmptyReturn = '' ) => { |
| 20 | |
| 21 | let action = false; |
| 22 | |
| 23 | if ( source && source[ objectKey ] ) { |
| 24 | action = source[ objectKey ]; |
| 25 | } else if ( actionType ) { |
| 26 | action = ( getLocalized( actionType )[ objectKey ] ); |
| 27 | } |
| 28 | |
| 29 | if ( ! action ) { |
| 30 | return () => ifEmptyReturn; |
| 31 | } |
| 32 | |
| 33 | return attr => { |
| 34 | if ( attr ) { |
| 35 | return ( action[ attr ] ? action[ attr ] : ifEmptyReturn ); |
| 36 | } else { |
| 37 | return action; |
| 38 | } |
| 39 | }; |
| 40 | }; |
| 41 | |
| 42 | const getActionLabel = ( type ) => { |
| 43 | const action = window.jetFormActionTypes.find( el => el.id === type ); |
| 44 | return action ? action.name : ''; |
| 45 | }; |
| 46 | |
| 47 | const localizedLabel = settings => { |
| 48 | return getLocalizedWithFunc( settings, '__labels', '[Empty Label]' ); |
| 49 | }; |
| 50 | |
| 51 | const localizedHelp = settings => { |
| 52 | return getLocalizedWithFunc( settings, '__help_messages' ); |
| 53 | }; |
| 54 | |
| 55 | const localizedGatewayAttrs = settings => { |
| 56 | return getLocalizedWithFunc( settings, '__gateway_attrs', [] ); |
| 57 | }; |
| 58 | |
| 59 | const localizedMessages = settings => { |
| 60 | return getLocalizedWithFunc( settings, '__messages', {} ); |
| 61 | }; |
| 62 | |
| 63 | const getLocalizedFullPack = ( actionType, source = false ) => { |
| 64 | if ( ! source ) { |
| 65 | source = getLocalized( actionType ); |
| 66 | } |
| 67 | |
| 68 | function setSource( props = {} ) { |
| 69 | const name = getSourceObjectName( actionType ); |
| 70 | |
| 71 | if ( ! name || ! window[ name ] ) { |
| 72 | return false; |
| 73 | } |
| 74 | window[ name ] = { |
| 75 | ...window[ name ], |
| 76 | ...props, |
| 77 | }; |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | const label = localizedLabel( { source } ); |
| 83 | const help = localizedHelp( { source } ); |
| 84 | const messages = localizedMessages( { source } ); |
| 85 | const gatewayAttrs = localizedGatewayAttrs( { source } ); |
| 86 | |
| 87 | return { source, label, help, messages, gatewayAttrs, setSource }; |
| 88 | }; |
| 89 | |
| 90 | const isInDefaultFlow = actionType => { |
| 91 | const flow = 'Jet_Form_Builder\\Actions\\Executors\\Action_Default_Executor'; |
| 92 | |
| 93 | return flow === getLocalized( actionType, '__flow' ); |
| 94 | }; |
| 95 | |
| 96 | window.JetFBLocalizeHelper = { |
| 97 | getActionLabel, |
| 98 | getLocalizedWithFunc, |
| 99 | localizedLabel, |
| 100 | localizedHelp, |
| 101 | localizedGatewayAttrs, |
| 102 | localizedMessages, |
| 103 | getLocalizedFullPack, |
| 104 | isInDefaultFlow |
| 105 | }; |
| 106 |