@eform/ng-formio-builder
Version:
The Angular.js form builder component.
94 lines (86 loc) • 5.07 kB
JavaScript
'use strict';
var utils = require('/formiojs/lib/utils').default;
var _get = require('lodash/get');
var _reject = require('lodash/reject');
module.exports = [
function() {
return {
restrict: 'E',
scope: true,
template: '' +
'<uib-accordion>' +
'<div uib-accordion-group heading="Simple" class="panel panel-default" is-open="status.simple">' +
'This component should Display:' +
'<select class="form-control input-md" ng-model="component.conditional.show">' +
'<option ng-repeat="item in _booleans track by $index" value="{{item}}">{{item.toString()}}</option>' +
'</select>' +
'<br>When the form component:' +
'<select class="form-control input-md" ng-model="component.conditional.when">' +
'<option ng-repeat="item in _components track by $index" value="{{item.key}}">{{item !== "" ? item.label + " (" + item.key + ")" : ""}}</option>' +
'</select>' +
'<br>Has the value:' +
'<input type="text" class="form-control input-md" ng-model="component.conditional.eq">' +
'</div>' +
'<div uib-accordion-group heading="Advanced" class="panel panel-default" is-open="status.advanced">' +
'<formio-script-editor rows="5" id="custom" name="custom" ng-model="component.customConditional" placeholder="/*** Example Code ***/\nshow = (data[\'mykey\'] > 1);"></formio-script-editor>' +
'<small>' +
'<p>Enter custom conditional code.</p>' +
'<p>You must assign the <strong>show</strong> variable as either <strong>true</strong> or <strong>false</strong>.</p>' +
'<p>The global variable <strong>data</strong> is provided, and allows you to access the data of any form component, by using its API key.</p>' +
'<p>Also <strong>moment</strong> library is available, and allows you to manipulate dates in a convenient way.</p>' +
'<p><strong>Note: Advanced Conditional logic will override the results of the Simple Conditional logic.</strong></p>' +
'</small>' +
'</div>' +
'<div uib-accordion-group heading="JSON Conditional" class="panel panel-default" is-open="status.json">' +
'<small>' +
'<p>Execute custom validation logic with JSON and <a href="http://jsonlogic.com/">JsonLogic</a>.</p>' +
'<p>Submission data is available as JsonLogic variables, with the same api key as your components.</p>' +
'<p><a href="http://formio.github.io/formio.js/app/examples/conditions.html" target="_blank">Click here for an example</a></p>' +
'</small>' +
'<textarea class="form-control" rows="5" id="json" name="json" json-input ng-model="component.conditional.json" placeholder="{ ... }"></textarea>' +
'</div>' +
'</uib-accordion>',
controller: [
'$scope',
function(
$scope) {
// Default the current components conditional logic.
$scope.component = $scope.component || {};
$scope.component.conditional = $scope.component.conditional || {};
// The available logic functions.
$scope._booleans = ['', 'true', 'false'];
// Filter the list of available form components for conditional logic.
$scope._components = _get($scope, 'form.components') || [];
$scope._components = utils.flattenComponents($scope._components);
// Remove non-input/button fields because they don't make sense.
// FA-890 - Dont allow the current component to be a conditional trigger.
$scope._components = _reject($scope._components, function(c) {
return !c.input || (c.type === 'button') || (c.key === $scope.component.key) || (!c.label && !c.key);
});
// Add default item to the components list.
$scope._components.unshift('');
// Default and watch the show logic.
$scope.component.conditional.show = $scope.component.conditional.show || '';
// Coerce show var to supported value.
var _booleanMap = {
'': '',
'true': 'true',
'false': 'false'
};
$scope.component.conditional.show = _booleanMap.hasOwnProperty($scope.component.conditional.show)
? _booleanMap[$scope.component.conditional.show]
: '';
// Default and watch the when logic.
$scope.component.conditional.when = $scope.component.conditional.when || null;
// Default and watch the search logic.
$scope.component.conditional.eq = $scope.component.conditional.eq || '';
// Track the status of the accordion panels open state.
$scope.status = {
simple: !$scope.component.customConditional,
advanced: !!$scope.component.customConditional
};
}
]
};
}
];