-
Notifications
You must be signed in to change notification settings - Fork 6
/
ngDynamicForms.js
77 lines (73 loc) · 1.89 KB
/
ngDynamicForms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var app = angular.module('ngDynamicForms',[]);
app.controller('FormBuilderCtrl',function FormBuilderCtrl($scope)
{
$scope.newField = {};
$scope.fields = [ {
type : 'text',
name : 'Name',
placeholder : 'Please enter your name',
order : 1
} ];
$scope.editing = false;
$scope.tokenize = function(slug1, slug2) {
var result = slug1;
result = result.replace(/[^-a-zA-Z0-9,&\s]+/ig, '');
result = result.replace(/-/gi, "_");
result = result.replace(/\s/gi, "-");
if (slug2) {
result += '-' + $scope.token(slug2);
}
return result;
};
$scope.saveField = function() {
console.log("entered save");
if ($scope.newField.type == 'checkboxes') {
$scope.newField.value = {};
}
if ($scope.editing !== false) {
$scope.fields[$scope.editing] = $scope.newField;
$scope.editing = false;
} else {
$scope.fields.push($scope.newField);
}
$scope.newField = {
order : 0
};
};
$scope.editField = function(field) {
$scope.editing = $scope.fields.indexOf(field);
$scope.newField = field;
};
$scope.splice = function(field, fields) {
fields.splice(fields.indexOf(field), 1);
};
$scope.addOption = function() {
if ($scope.newField.options === undefined) {
$scope.newField.options = [];
}
$scope.newField.options.push({
order : 0
});
};
$scope.typeSwitch = function(type) {
/*if (angular.Array.indexOf(['checkboxes','select','radio'], type) === -1)
return type;*/
if (type == 'checkboxes')
return 'multiple';
if (type == 'select')
return 'multiple';
if (type == 'radio')
return 'multiple';
return type;
}
});
app.directive('ngDynamicForm', function () {
return {
// We limit this directive to attributes only.
restrict : 'A',
// We will not replace the original element code
replace : false,
// We must supply at least one element in the code
templateUrl : 'dynamicForms.html'
}
});