@eform/ng-formio-builder
Version:
The Angular.js form builder component.
80 lines (74 loc) • 3.38 kB
JavaScript
/**
* A directive that provides a UI to add {value, label} objects to an array.
*/
var _map = require('lodash/map');
var _camelCase = require('lodash/camelCase');
module.exports = function() {
return {
scope: {
data: '=',
label: '@',
tooltipText: '@',
valueLabel: '@',
labelLabel: '@',
valueProperty: '@',
labelProperty: '@'
},
restrict: 'E',
template: '<div class="form-group">' +
'<label form-builder-tooltip="{{ tooltipText | formioTranslate }}">{{ label | formioTranslate }}</label>' +
'<table class="table table-condensed">' +
'<thead>' +
'<tr>' +
'<th class="col-xs-6">{{ labelLabel | formioTranslate }}</th>' +
'<th class="col-xs-4">{{ valueLabel | formioTranslate }}</th>' +
'<th class="col-xs-2"></th>' +
'</tr>' +
'</thead>' +
'<tbody>' +
'<tr ng-repeat="v in data track by $index">' +
'<td class="col-xs-6"><input type="text" class="form-control" ng-model="v[labelProperty]" placeholder="{{ labelLabel | formioTranslate }}"/></td>' +
'<td class="col-xs-4"><input type="text" class="form-control" ng-model="v[valueProperty]" placeholder="{{ valueLabel | formioTranslate }}"/></td>' +
'<td class="col-xs-2"><button type="button" class="btn btn-danger btn-xs" ng-click="removeValue($index)" tabindex="-1"><span class="glyphicon glyphicon-remove-circle"></span></button></td>' +
'</tr>' +
'</tbody>' +
'</table>' +
'<button type="button" class="btn btn-primary" ng-click="addValue()"><span class="glyphicon glyphicon-plus"></span> {{ \'Add Value\' | formioTranslate }}</button>' +
'</div>',
replace: true,
link: function($scope, el, attrs) {
$scope.valueProperty = $scope.valueProperty || 'value';
$scope.labelProperty = $scope.labelProperty || 'label';
$scope.valueLabel = $scope.valueLabel || 'Value';
$scope.labelLabel = $scope.labelLabel || 'Label';
$scope.data = $scope.data || [];
$scope.addValue = function() {
var obj = {};
obj[$scope.valueProperty] = '';
obj[$scope.labelProperty] = '';
$scope.data.push(obj);
};
$scope.removeValue = function(index) {
$scope.data.splice(index, 1);
};
if ($scope.data.length === 0) {
$scope.addValue();
}
if (!attrs.noAutocompleteValue) {
$scope.$watch('data', function(newValue, oldValue) {
// Ignore array addition/deletion changes
if (newValue.length !== oldValue.length) {
return;
}
_map(newValue, function(entry, i) {
if (entry[$scope.labelProperty] !== oldValue[i][$scope.labelProperty]) {// label changed
if (entry[$scope.valueProperty] === '' || entry[$scope.valueProperty] === _camelCase(oldValue[i][$scope.labelProperty])) {
entry[$scope.valueProperty] = _camelCase(entry[$scope.labelProperty]);
}
}
});
}, true);
}
}
};
};