ember-frost-bunsen
Version:
Create UI's from JSON configurations.
121 lines (98 loc) • 3.21 kB
JavaScript
import {utils} from 'bunsen-core'
const {parseVariables} = utils
import Ember from 'ember'
const {get} = Ember
import computed, {readOnly} from 'ember-computed-decorators'
import AbstractInput from './abstract-input'
import layout from 'ember-frost-bunsen/templates/components/frost-bunsen-input-link'
export default AbstractInput.extend({
// == Component Properties ===================================================
classNames: [
'frost-bunsen-input-link',
'frost-field'
],
layout,
// == Computed Properties ====================================================
defaultLabel (cellConfig) {
return (
get(cellConfig, 'renderer.defaultLabel') ||
get(cellConfig, 'renderer.options.defaultLabel')
)
},
dereferencedLinkLabel (bunsenId, defaultLabel, formValue, linkLabel, value) {
if (!linkLabel) return value
return (
parseVariables(formValue, linkLabel, bunsenId, true) ||
defaultLabel ||
'Link'
)
},
dereferencedUrl (bunsenId, formValue, url, value) {
if (!url) return value
return parseVariables(formValue, url, bunsenId, true) || ''
},
linkLabel (cellConfig) {
return (
get(cellConfig, 'renderer.label') ||
get(cellConfig, 'renderer.options.label')
)
},
route (cellConfig) {
return get(cellConfig, 'renderer.route')
},
url (cellConfig) {
return (
get(cellConfig, 'renderer.url') ||
get(cellConfig, 'renderer.options.url')
)
},
/**
* Get class name for input element
* @param {String} errorMessage - error message for input
* @returns {String} input class name
*/
valueClassName (errorMessage) {
const classNames = ['frost-link', 'inline']
if (errorMessage) {
classNames.push('error')
}
return classNames.join(' ')
},
// == Functions ==============================================================
formValueChanged (newValue) {
const cellConfig = this.get('cellConfig')
const rendererLabel = get(cellConfig, 'renderer.label')
const rendererUrl = get(cellConfig, 'renderer.url')
const labelContainsReferences = rendererLabel && rendererLabel.indexOf('${') !== -1
const urlContainsReferences = rendererUrl && rendererUrl.indexOf('${') !== -1
if (!labelContainsReferences && !urlContainsReferences) return
this.set('formValue', newValue)
},
// == Events ================================================================
init () {
this._super(...arguments)
this.registerForFormValueChanges(this)
},
// == Actions ===============================================================
actions: {
handleClick (e) {
// Prevent link click event from bubbling which causes problems when it is
// rendered in certain parent components such as a frost-list
e.stopPropagation()
}
}
})