-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (106 loc) · 3.84 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var app = angular.module('app', [])
.controller('appCtrl', function ($scope, $filter, utils) {
$scope.data = {};
$scope.data.inputs = '';
$scope.data.multiple = [];
$scope.data.cols = 1;
$scope.data.base = '';
$scope.data.inline = false;
$scope.data.colClass = '';
$scope.updateCode = function () {
$scope.data.code = utils.getRaw($scope.data.multiple, $scope.data.cols);
};
$scope.setMulti = function () {
var r = $filter('splits')($scope.data.inputs);
var arr = [];
r.forEach(function (item) {
var tmp = {
model: $scope.data.base ? $scope.data.base + "." + item : item,
label: $filter('ucwords')(item),
id: $filter('ucwords')(item, true),
type: 'text'
};
arr.push(tmp);
});
$scope.data.multiple = arr;
$scope.data.code = utils.getRaw(arr, $scope.data.cols, $scope.data.inline);
};
})
.factory('utils', function ($timeout) {
return { getRaw: getRaw };
function getRaw(data, cols, inline) {
var colClass = ' col-md-12';
var code = '';
if (cols > 1) {
data = _.chunk(data, cols);
colClass = ' col-md-' + (12 / cols);
} else {
data = [data];
}
var iclass = '';
if (inline) {
iclass = 'class="form-inline"';
colClass = '';
}
code += '<form ' + iclass + '>' + "\n";
data.forEach(function (rows) {
if (!inline) code += "\t" + '<div class="row">' + "\n";
rows.forEach(function (cols) {
var input = '';
if (cols.type == 'text' || cols.type == 'password') {
input += "\t\t" + '<div class="form-group' + colClass + '">' + "\n";
input += "\t\t\t" + '<label for="' + cols.id + '">' + cols.label + '</label>' + "\n";
input += "\t\t\t" + '<input class="form-control" type="' + cols.type + '" id="' + cols.id + '" ng-model="' + cols.model + '"/>' + "\n";
input += "\t\t" + '</div>' + "\n";
}
if (cols.type == 'textarea') {
input += "\t\t" + '<div class="form-group' + colClass + '">' + "\n";
input += "\t\t\t" + '<label for="' + cols.id + '">' + cols.label + '</label>' + "\n";
input += "\t\t\t" + '<textarea class="form-control" id="' + cols.id + '" ng-model="' + cols.model + '"/></textarea>' + "\n";
input += "\t\t" + '</div>' + "\n";
}
if (cols.type == 'checkbox' || cols.type == 'radio') {
input += "\t\t" + '<div class="' + cols.type + ' form-group' + colClass + '">' + "\n";
input += "\t\t\t" + '<label>' + "\n";
input += "\t\t\t" + '<input type="' + cols.type + '" ng-model="' + cols.model + '"/>' + "\n";
input += "\t\t\t" + cols.label + "\n";
input += "\t\t\t" + '</label>' + "\n";
input += "\t\t" + '</div>' + "\n";
}
code += input;
});
if (!inline) code += "\t" + '</div>' + "\n";
});
code += '</form>' + "\n";
return code;
}
})
.filter('splits', function () {
return function (value) {
if (!value) return;
value = value.split(',');
value = value.map(function (i) {
i = i.trim();
if (i) return i;
});
value = value.filter(function (val) { return val !== undefined; })
return value;
};
})
.filter('trust', function ($sce) {
return function (value) {
return $sce.trustAsHtml(value);
};
})
.filter('ucwords', function () {
return function (value, prop) {
if (!prop) value = value.replace(/_/g, ' ');
value = value.split('.');
value = value[value.length - 1];
if (prop) return value;
return (value + '')
.replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function ($1) {
return $1.toUpperCase()
})
};
});