-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
179 lines (139 loc) · 3.53 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
angular.module('schemaFormBuilder', ['schemaForm'])
// extend the decorator with builder controls
.config(function($provide){
// TODO: we need to extend ALL registered decorators somehow...
$provide.decorator('bootstrapDecoratorDirective', function ($delegate, $compile) {
var directive = $delegate[0];
var link = directive.link;
directive.compile = function(){
return function(scope, element, attrs){
// we've already injected the builder controls directive
if(typeof attrs.sfBuilderControls !== 'undefined')
return link.apply(this, arguments);
// inject the builder controls directive
element.attr('sf-builder-controls', '');
$compile(element)(scope);
};
};
return $delegate;
});
})
.directive('sfBuilder', [function(){
return {
templateUrl: 'template.html',
restrict: 'EA',
transclude: false,
scope: {
options: '=sfBuilderOptions',
schema: '=sfBuilderSchema',
form: '=sfBuilderForm'
},
link: function link(scope, element, attrs, controller, transclude) {
},
controller: ['$scope', function($scope){
// inspect a form item
this.inspectItem = function(item) {
$scope.$apply(function(){
$scope.inspected = item;
});
};
$scope.model = {};
$scope.types = {
text: {
title: 'Text',
schema: {
type: 'object',
properties: {
title: { 'type': 'string' },
description: { 'type': 'string' }
}
},
form: [
'title',
'description'
]
},
password: {
title: 'Password',
schema: {
type: 'object',
properties: {
title: { 'type': 'string' },
description: { 'type': 'string' }
}
},
form: [
'title',
'description'
]
}
}
}]
}
}])
.directive('sfBuilderDesigner', [function(){
return {
restrict: 'EA',
require: ['^^sfBuilder'],
scope: false,
controller: ['$scope', function($scope){}]
}
}])
.directive('sfBuilderPallette', [function(){
return {
restrict: 'EA',
require: ['^^sfBuilder'],
scope: false,
controller: ['$scope', function($scope){}]
}
}])
.directive('sfBuilderInspector', [function(){
return {
restrict: 'EA',
require: ['^^sfBuilder'],
scope: false,
controller: ['$scope', function($scope){}]
}
}])
.directive('sfBuilderControls', [function(){
return {
restrict: 'A',
require: ['?^^sfBuilder', '?^^sfBuilderDesigner'],
priority: -1000,
scope: false,
link: function link(scope, element, attrs, controllers) {
var sfBuilder = controllers[0];
var sfBuilderDesigner = controllers[1];
// only apply this if we're inside the designer
if(!sfBuilder || !sfBuilderDesigner) return;
// make draggable
element.attr('draggable', 'true');
// the parent
element.addClass('sf-builder-decorator');
element.on('click', function(event){
sfBuilder.inspectItem(scope.form)
});
// the controls DOM
var controls = angular.element('<div></div>');
controls.append('<div class="sf-builder-controls-handle"></div>');
// add the controls DOM
element.append(controls);
// if the controls DOM got removed, add it back
new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
for (var i = mutation.removedNodes.length - 1; i >= 0; i--) {
if(mutation.removedNodes[i] != controls[0]) continue;
element.append(controls);
};
});
}).observe(element[0], {
attributes: false,
childList: true,
characterData: false
});
},
controller: ['$scope', function($scope){
}]
}
}])