| 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 => { |
| 58 |
const source = getLocalized( actionType ); |
| 59 |
|
| 60 |
const label = localizedLabel( { source } ); |
| 61 |
const help = localizedHelp( { source } ); |
| 62 |
const messages = localizedMessages( { source } ); |
| 63 |
const gatewayAttrs = localizedGatewayAttrs( { source } ); |
| 64 |
|
| 65 |
return { source, label, help, messages, gatewayAttrs }; |
| 66 |
}; |
| 67 |
|
| 68 |
window.JetFBLocalizeHelper = { |
| 69 |
getActionLabel, |
| 70 |
getLocalizedWithFunc, |
| 71 |
localizedLabel, |
| 72 |
localizedHelp, |
| 73 |
localizedGatewayAttrs, |
| 74 |
localizedMessages, |
| 75 |
getLocalizedFullPack |
| 76 |
}; |
| 77 |
|