action-localize-helper.js
5 years ago
admin.js
5 years ago
editor.js
5 years ago
file-upload.js
5 years ago
form-block.js
5 years ago
frontend-forms.js
5 years ago
package.js
5 years ago
action-localize-helper.js
79 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 getLocalizedWithFunc = ( { actionType = false, source = false }, |
| 12 | objectKey, |
| 13 | ifEmptyReturn = '' ) => { |
| 14 | |
| 15 | let action = false; |
| 16 | |
| 17 | if ( source && source[ objectKey ] ) { |
| 18 | action = source[ objectKey ]; |
| 19 | } else if ( actionType ) { |
| 20 | action = ( getLocalized( actionType )[ objectKey ] ); |
| 21 | } |
| 22 | |
| 23 | if ( ! action ) { |
| 24 | return () => ifEmptyReturn; |
| 25 | } |
| 26 | |
| 27 | return attr => { |
| 28 | if ( attr ) { |
| 29 | return ( action[ attr ] ? action[ attr ] : ifEmptyReturn ); |
| 30 | } else { |
| 31 | return action; |
| 32 | } |
| 33 | }; |
| 34 | }; |
| 35 | |
| 36 | const getActionLabel = ( type ) => { |
| 37 | const action = window.jetFormActionTypes.find( el => el.id === type ); |
| 38 | return action ? action.name : ''; |
| 39 | }; |
| 40 | |
| 41 | const localizedLabel = settings => { |
| 42 | return getLocalizedWithFunc( settings, '__labels', '[Empty Label]' ); |
| 43 | }; |
| 44 | |
| 45 | const localizedHelp = settings => { |
| 46 | return getLocalizedWithFunc( settings, '__help_messages' ); |
| 47 | }; |
| 48 | |
| 49 | const localizedGatewayAttrs = settings => { |
| 50 | return getLocalizedWithFunc( settings, '__gateway_attrs', [] ); |
| 51 | }; |
| 52 | |
| 53 | const localizedMessages = settings => { |
| 54 | return getLocalizedWithFunc( settings, '__messages', {} ); |
| 55 | }; |
| 56 | |
| 57 | const getLocalizedFullPack = ( actionType, source = false ) => { |
| 58 | if ( ! source ) { |
| 59 | source = getLocalized( actionType ); |
| 60 | } |
| 61 | |
| 62 | const label = localizedLabel( { source } ); |
| 63 | const help = localizedHelp( { source } ); |
| 64 | const messages = localizedMessages( { source } ); |
| 65 | const gatewayAttrs = localizedGatewayAttrs( { source } ); |
| 66 | |
| 67 | return { source, label, help, messages, gatewayAttrs }; |
| 68 | }; |
| 69 | |
| 70 | window.JetFBLocalizeHelper = { |
| 71 | getActionLabel, |
| 72 | getLocalizedWithFunc, |
| 73 | localizedLabel, |
| 74 | localizedHelp, |
| 75 | localizedGatewayAttrs, |
| 76 | localizedMessages, |
| 77 | getLocalizedFullPack |
| 78 | }; |
| 79 |