广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
 
// Form service
angular.module('flowableModeler').service('FormBuilderService', ['$http', '$q', '$rootScope', '$translate',
    function ($http, $q, $rootScope, $translate) {
 
        // Removes all properties starting with '_', used before json serialization
        function removePrivateFields(fields) {
            fields.forEach(function(field, i){
                for (var attr in field) {
                    if (attr.indexOf('_') === 0) {
                        delete field[attr];
                    }
                }
                if (field.fields) {
                    for (var fieldsName in field.fields) {
                        removePrivateFields(field.fields[fieldsName]);
                    }
                }
            });
            return fields;
        }
 
        /**
         * Remove the given step from the given container.
         */
        this.saveForm = function (data, name, formKey, description, saveCallback, errorCallback) {
 
            var fieldIndex = 0;
            data.formRepresentation = $rootScope.currentForm;
            data.formRepresentation.name = name;
            data.formRepresentation.key = formKey;
 
            if (description && description.length > 0) {
                data.formRepresentation.description = description;
            }
            for (var i = 0; i < $rootScope.formItems.length; i++) {
                var field = $rootScope.formItems[i];
                this._cleanFormField(field, fieldIndex++);
            }
 
            var currentActiveTab = $rootScope.formBuilder.activeTab;
            if (currentActiveTab != 'design') {
                $rootScope.formBuilder.activeTab = 'design';
            }
 
            data.formRepresentation.formDefinition = {
                name: name,
                key: formKey,
                fields: removePrivateFields(angular.copy($rootScope.formItems)), 
                outcomes: $rootScope.currentOutcomes
            };
 
            html2canvas(jQuery('#canvasSection'), {
                onrendered: function (canvas) {
                    var scale = canvas.width / 300.0;
                    var extra_canvas = document.createElement("canvas");
                    extra_canvas.setAttribute('width', 300);
                    extra_canvas.setAttribute('height', canvas.height / scale);
                    var ctx = extra_canvas.getContext('2d');
                    ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, 300, canvas.height / scale);
                    data.formImageBase64 = extra_canvas.toDataURL("image/png");
 
                    $http({method: 'PUT', url: FLOWABLE.APP_URL.getFormModelUrl($rootScope.currentForm.id), data: data}).
                        success(function (response, status, headers, config) {
 
                            if (saveCallback) {
                                saveCallback();
                            }
                        }).
                        error(function (response, status, headers, config) {
                            if (errorCallback) {
                                errorCallback(response);
                            }
                        });
 
                    if (currentActiveTab != 'design') {
                        $rootScope.formBuilder.activeTab = currentActiveTab;
                    }
                }
            });
        };
        
        this._cleanFormField = function(field, fieldIndex) {
            if (!field.overrideId) {
                var fieldId;
                if (field.name && field.name.length > 0) {
                    fieldId = field.name.toLowerCase();
                    fieldId = fieldId.replace(new RegExp(' ', 'g'), '');
                    fieldId = fieldId.replace(/[&\/\\#,+~%.'":*?!<>{}()$@;]/g, '');
                } else {
                    fieldId = 'field' + fieldIndex;
                }
                field.id = fieldId;
            }
        };
 
        /**
         * Updates the form cache for a specific step
         * @param {String} stepId
         * @param {Object} formDefinition
         */
        this.updateFormsCache = function (stepId, formDefinition) {
            if (stepId) {
                if (formDefinition && formDefinition.id) {
                    _updateFormCache(stepId, formDefinition.id);
                } else {
                    _updateFormCache(stepId);
                }
            }
        };
 
        /**
         * Fetches forms from cache up to the given step (and not after)
         * @param {Array} steps all steps in sequential order
         * @param {String} stepId the current step
         * @return {Array} flattened list of all forms up to given step
         */
        this.getFormsForStep = function (steps, stepId) {
            var forms = [];
 
            if (stepId !== '__startform') {
                // check start for start form
                forms.push(_getFormForStep('__startform'));
 
                if (stepId && steps && steps.length > 0) {
                    for (var i = 0; i < steps.length; i++) {
                        if (steps[i].id != stepId) {
                            forms.push(_getFormForStep(steps[i].id));
                        } else {
                            break;
                        }
                    }
                }
            }
 
            // only unique forms
            var uniqueIds = [];
            var uniqueForms = [];
 
            forms.forEach(function (item) {
                if (item && item.id) {
                    if (uniqueIds.indexOf(item.id) < 0) {
                        uniqueIds.push(item.id);
                        uniqueForms.push(item);
                    }
                }
            });
 
            uniqueForms.sort(function(a,b){
                return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
            });
 
            return uniqueForms;
        };
 
        /**
         * Fetches form outcomes from cache for specific form
         * @param {String} formId
         * @return {Array} flattened list of all form outcomes
         */
        this.getFormOutcomesForForm = function (formId) {
            var outcomes = [];
            if (formId) {
                outcomes = _getFormOutcomesForForm(formId);
            }
 
            outcomes.sort(function(a,b){
                return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
            });
 
            return outcomes;
        };
 
        /**
         * Fetches form fields from cache for specific form
         * @param {String} formId
         * @return {Array} flattened list of all form form fields
         */
        this.getFormFieldsForForm = function (formId) {
            var formFields = [];
            if (formId) {
                formFields = _getFormFieldsForForm(formId);
            }
            return formFields;
        };
 
        var _updateFormCache = function (stepId, formId) {
            if (stepId && formId) {
                $http({method: 'GET', url: FLOWABLE.APP_URL.getFormModelUrl(formId)}).
                    success(function(response) {
                        if (response) {
                            var outcomes;
                            var formFields;
                            if (response.formDefinition) {
                                if (response.formDefinition.fields) {
                                    formFields = response.formDefinition.fields;
                                }
 
                                if (response.formDefinition.outcomes) {
                                    outcomes = response.formDefinition.outcomes;
                                }
                            }
                            var filteredFormFields = _filterFormFields(formFields);
                            var formFieldInfo = {id: response.id, name: response.name, description: response.description, outcomes: outcomes, fields: filteredFormFields};
                            _addFormFieldsForStepToCache(stepId, formFieldInfo);
                        }
                    });
            }
            if (stepId && !formId) {
                _deleteFormFieldsForStepFromCache(stepId);
            }
        };
 
        var _getFormDefinitions = function (formIds, callback) {
 
            if (formIds.length > 0) {
 
                var formIdParams = '';
                for (var i = 0; i < formIds.length; i++) {
                    if (formIdParams.length > 0) {
                        formIdParams += '&';
                    }
                    formIdParams += 'formId=' + formIds[i];
                }
                if (formIdParams.length > 0) {
                    formIdParams += '&';
                }
                formIdParams += 'version=' + Date.now();
 
                $http({method: 'GET', url: FLOWABLE.APP_URL.getFormModelValuesUrl(formIdParams)}).
                    success(function (data) {
                        if (callback) {
                            callback(data);
                        }
                    }).
 
                    error(function (data) {
                        console.log('Something went wrong when fetching form values:' + JSON.stringify(data));
                    });
            }
        };
    }
]);