-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangular-schema-form-complex-ui.js
More file actions
138 lines (138 loc) · 7.25 KB
/
angular-schema-form-complex-ui.js
File metadata and controls
138 lines (138 loc) · 7.25 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
angular.module("schemaForm").run(["$templateCache", function($templateCache) {$templateCache.put("directives/decorators/bootstrap/complex-ui/angular-schema-form-complex-ui.html","<div ng-class=\"{\'has-error\': hasError()}\"><div ng-init=\"parentController.model=$$value$$\" complex-ui-directive=\"\"><div ng-if=\"form.options.modal == true\"><label>{{form.title}}</label> <button ng-click=\"parentController.toggleModal()\">{{form.options.buttonCaption}}</button><modal title=\"{{form.title}}\" visible=\"showModal\"><div ng-if=\"form.options.includeURI != \'\'\"><ng-include src=\"form.options.includeURI\"></ng-include></div><div ng-if=\"form.options.includeURI == null || form.options.includeURI == \'\'\"><div name=\"\" sf-schema=\"parentController.schema\" sf-form=\"parentController.form\" sf-model=\"parentController.model\"></div></div></modal></div><div ng-if=\"form.options.modal != true\"><label ng-show=\"showTitle()\">{{form.title}}</label><div ng-if=\"form.options.includeURI != \'\'\"><ng-include src=\"form.options.includeURI\"></ng-include></div><div ng-if=\"form.options.includeURI == null || form.options.includeURI == \'\'\"><div name=\"\" sf-schema=\"parentController.schema\" sf-form=\"parentController.form\" sf-model=\"parentController.model\"></div></div></div></div><span class=\"help-block\">{{ (hasError() && errorMessage(schemaError())) || form.description}}</span><br><span ng-show=\"form.some_setting\">The some setting-setting is true for the model at $$value$$!</span></div>");}]);
/// <reference path="../typings/angularjs/angular.d.ts" />
/// <reference path="../typings/jquery/jquery.d.ts" />
angular.module('schemaForm').config(['schemaFormProvider',
'schemaFormDecoratorsProvider', 'sfPathProvider',
function (schemaFormProvider, schemaFormDecoratorsProvider, sfPathProvider) {
// Second, we want it to show if someone have explicitly set the form type
schemaFormDecoratorsProvider.addMapping('bootstrapDecorator', 'complex-ui', 'directives/decorators/bootstrap/complex-ui/angular-schema-form-complex-ui.html');
}]);
// Declare a controller, this is used in the typescriptDirective below
var ComplexUIController = (function () {
function ComplexUIController($scope, element) {
var _this = this;
this.$scope = $scope;
this.toggleModal = function () {
this.directiveScope.showModal = !this.directiveScope.showModal;
};
this.getCallback = function (callback) {
if (typeof (callback) == "string") {
var _result = _this.directiveScope.$parent.evalExpr(callback);
if (typeof (_result) == "function") {
return _result;
}
else {
throw ("A callback string must match name of a function in the parent scope");
}
}
else if (typeof (callback) == "function") {
return callback;
}
else {
throw ("A callback must either be a string matching the name of a function in the parent scope or a " +
"direct function reference");
}
};
this.getDefinitions = function () {
if (_this.directiveScope.form["options"]) {
var schemaRef = void 0;
if ("schemaRef" in _this.directiveScope.form["options"]) {
schemaRef = _this.directiveScope.form["options"]["schemaRef"];
}
else {
schemaRef = null;
}
if ("definitionsCallback" in _this.directiveScope.form["options"]) {
var callback = _this.getCallback(_this.directiveScope.form["options"]["definitionsCallback"]);
var _defs = callback(schemaRef);
// TODO: This is probably in the wrong order, it should be possible to read form and schema the usual way.
// How can some get a form and some not.
if ("form" in _defs) {
_this.form = _defs["form"];
}
else if ("complexForm" in _this.directiveScope.form["options"]) {
_this.form = _this.directiveScope.form["options"]["complexForm"];
}
else {
_this.form = ["*"];
}
_this.form.onChange = _this.directiveScope.form.onChange;
_this.schema = _defs["schema"];
}
}
};
this.innerSubmit = function (form) {
_this.directiveScope.$broadcast("schemaFormValidate");
console.log(_this.model);
};
console.log("Initiating the ComplexUI controller" + $scope.toString());
$scope.parentController = this;
this.directiveScope = $scope;
}
return ComplexUIController;
}());
;
angular.module('schemaForm').directive('modal', function () {
// TODO: Add setting for class
return {
template: '<div class="modal fade">' +
'<div class="{{ form.htmlClass ? form.htmlClass: \'modal-dialog\'}}">' +
'<div class="modal-content" style="overflow:auto;">' +
'<div class="modal-header">' +
'<button type="button" class="close" ng-click="parentController.toggleModal()" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ form.title }}</h4>' +
'</div>' +
'<div class="{{ form.fieldHtmlClass ? form.fieldHtmlClass: \'modal-body\'}} " ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace: true,
scope: false,
link: function postLink(scope, element, attrs) {
scope.$watch((attrs).visible, function (value) {
if (value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
}
};
});
// Create a directive to properly access the ngModel set in the view (src/angular-schema-form-typescript.html)
angular.module('schemaForm').directive('complexUiDirective', function () {
return {
require: [],
restrict: 'A',
// Do not create a isolate scope, pass on
scope: false,
// Define a controller, use the function from above, inject the scope
controller: ['$scope', ComplexUIController],
link: function (scope, iElement, iAttrs, ngModelCtrl) {
scope.parentController.getDefinitions();
}
};
});
angular.module('schemaForm').directive('script', function () {
return {
restrict: 'E',
scope: false,
link: function (scope, elem, attr) {
if (attr["type"] == 'text/javascript-lazy') {
var s = document.createElement("script");
s.type = "text/javascript";
var src = elem.attr('src');
if (src !== undefined) {
s.src = src;
}
else {
var code = elem.text();
s.text = code;
}
document.head.appendChild(s);
elem.remove();
}
}
};
});