diff options
Diffstat (limited to 'language-web/src/main/js/xtext/ServiceBuilder.js')
-rw-r--r-- | language-web/src/main/js/xtext/ServiceBuilder.js | 274 |
1 files changed, 274 insertions, 0 deletions
diff --git a/language-web/src/main/js/xtext/ServiceBuilder.js b/language-web/src/main/js/xtext/ServiceBuilder.js new file mode 100644 index 00000000..38b08ecc --- /dev/null +++ b/language-web/src/main/js/xtext/ServiceBuilder.js | |||
@@ -0,0 +1,274 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-2.0. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | ******************************************************************************/ | ||
9 | |||
10 | define([ | ||
11 | 'jquery', | ||
12 | 'xtext/services/XtextService', | ||
13 | 'xtext/services/LoadResourceService', | ||
14 | 'xtext/services/SaveResourceService', | ||
15 | 'xtext/services/HighlightingService', | ||
16 | 'xtext/services/ValidationService', | ||
17 | 'xtext/services/UpdateService', | ||
18 | 'xtext/services/ContentAssistService', | ||
19 | 'xtext/services/HoverService', | ||
20 | 'xtext/services/OccurrencesService', | ||
21 | 'xtext/services/FormattingService' | ||
22 | ], function(jQuery, XtextService, LoadResourceService, SaveResourceService, HighlightingService, | ||
23 | ValidationService, UpdateService, ContentAssistService, HoverService, OccurrencesService, | ||
24 | FormattingService) { | ||
25 | |||
26 | /** | ||
27 | * Builder class for the Xtext services. | ||
28 | */ | ||
29 | function ServiceBuilder(xtextServices) { | ||
30 | this.services = xtextServices; | ||
31 | }; | ||
32 | |||
33 | /** | ||
34 | * Create all the available Xtext services depending on the configuration. | ||
35 | */ | ||
36 | ServiceBuilder.prototype.createServices = function() { | ||
37 | var services = this.services; | ||
38 | var options = services.options; | ||
39 | var editorContext = services.editorContext; | ||
40 | editorContext.xtextServices = services; | ||
41 | var self = this; | ||
42 | if (!options.serviceUrl) { | ||
43 | if (!options.baseUrl) | ||
44 | options.baseUrl = '/'; | ||
45 | else if (options.baseUrl.charAt(0) != '/') | ||
46 | options.baseUrl = '/' + options.baseUrl; | ||
47 | options.serviceUrl = window.location.protocol + '//' + window.location.host + options.baseUrl + 'xtext-service'; | ||
48 | } | ||
49 | if (options.resourceId) { | ||
50 | if (!options.xtextLang) | ||
51 | options.xtextLang = options.resourceId.split(/[?#]/)[0].split('.').pop(); | ||
52 | if (options.loadFromServer === undefined) | ||
53 | options.loadFromServer = true; | ||
54 | if (options.loadFromServer && this.setupPersistenceServices) { | ||
55 | services.loadResourceService = new LoadResourceService(options.serviceUrl, options.resourceId, false); | ||
56 | services.loadResource = function(addParams) { | ||
57 | return services.loadResourceService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
58 | } | ||
59 | services.saveResourceService = new SaveResourceService(options.serviceUrl, options.resourceId); | ||
60 | services.saveResource = function(addParams) { | ||
61 | return services.saveResourceService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
62 | } | ||
63 | services.revertResourceService = new LoadResourceService(options.serviceUrl, options.resourceId, true); | ||
64 | services.revertResource = function(addParams) { | ||
65 | return services.revertResourceService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
66 | } | ||
67 | this.setupPersistenceServices(); | ||
68 | services.loadResource(); | ||
69 | } | ||
70 | } else { | ||
71 | if (options.loadFromServer === undefined) | ||
72 | options.loadFromServer = false; | ||
73 | if (options.xtextLang) { | ||
74 | var randomId = Math.floor(Math.random() * 2147483648).toString(16); | ||
75 | options.resourceId = randomId + '.' + options.xtextLang; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | if (this.setupSyntaxHighlighting) { | ||
80 | this.setupSyntaxHighlighting(); | ||
81 | } | ||
82 | if (options.enableHighlightingService || options.enableHighlightingService === undefined) { | ||
83 | services.highlightingService = new HighlightingService(options.serviceUrl, options.resourceId); | ||
84 | services.computeHighlighting = function(addParams) { | ||
85 | return services.highlightingService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
86 | } | ||
87 | } | ||
88 | if (options.enableValidationService || options.enableValidationService === undefined) { | ||
89 | services.validationService = new ValidationService(options.serviceUrl, options.resourceId); | ||
90 | services.validate = function(addParams) { | ||
91 | return services.validationService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
92 | } | ||
93 | } | ||
94 | if (this.setupUpdateService) { | ||
95 | function refreshDocument() { | ||
96 | if (services.highlightingService && self.doHighlighting) { | ||
97 | services.highlightingService.setState(undefined); | ||
98 | self.doHighlighting(); | ||
99 | } | ||
100 | if (services.validationService && self.doValidation) { | ||
101 | services.validationService.setState(undefined); | ||
102 | self.doValidation(); | ||
103 | } | ||
104 | } | ||
105 | if (!options.sendFullText) { | ||
106 | services.updateService = new UpdateService(options.serviceUrl, options.resourceId); | ||
107 | services.update = function(addParams) { | ||
108 | return services.updateService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
109 | } | ||
110 | if (services.saveResourceService) | ||
111 | services.saveResourceService._updateService = services.updateService; | ||
112 | editorContext.addServerStateListener(refreshDocument); | ||
113 | } | ||
114 | this.setupUpdateService(refreshDocument); | ||
115 | } | ||
116 | if ((options.enableContentAssistService || options.enableContentAssistService === undefined) | ||
117 | && this.setupContentAssistService) { | ||
118 | services.contentAssistService = new ContentAssistService(options.serviceUrl, options.resourceId, services.updateService); | ||
119 | services.getContentAssist = function(addParams) { | ||
120 | return services.contentAssistService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
121 | } | ||
122 | this.setupContentAssistService(); | ||
123 | } | ||
124 | if ((options.enableHoverService || options.enableHoverService === undefined) | ||
125 | && this.setupHoverService) { | ||
126 | services.hoverService = new HoverService(options.serviceUrl, options.resourceId, services.updateService); | ||
127 | services.getHoverInfo = function(addParams) { | ||
128 | return services.hoverService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
129 | } | ||
130 | this.setupHoverService(); | ||
131 | } | ||
132 | if ((options.enableOccurrencesService || options.enableOccurrencesService === undefined) | ||
133 | && this.setupOccurrencesService) { | ||
134 | services.occurrencesService = new OccurrencesService(options.serviceUrl, options.resourceId, services.updateService); | ||
135 | services.getOccurrences = function(addParams) { | ||
136 | return services.occurrencesService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
137 | } | ||
138 | this.setupOccurrencesService(); | ||
139 | } | ||
140 | if ((options.enableFormattingService || options.enableFormattingService === undefined) | ||
141 | && this.setupFormattingService) { | ||
142 | services.formattingService = new FormattingService(options.serviceUrl, options.resourceId, services.updateService); | ||
143 | services.format = function(addParams) { | ||
144 | return services.formattingService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
145 | } | ||
146 | this.setupFormattingService(); | ||
147 | } | ||
148 | if (options.enableGeneratorService || options.enableGeneratorService === undefined) { | ||
149 | services.generatorService = new XtextService(); | ||
150 | services.generatorService.initialize(services, 'generate'); | ||
151 | services.generatorService._initServerData = function(serverData, editorContext, params) { | ||
152 | if (params.allArtifacts) | ||
153 | serverData.allArtifacts = params.allArtifacts; | ||
154 | else if (params.artifactId) | ||
155 | serverData.artifact = params.artifactId; | ||
156 | if (params.includeContent !== undefined) | ||
157 | serverData.includeContent = params.includeContent; | ||
158 | } | ||
159 | services.generate = function(addParams) { | ||
160 | return services.generatorService.invoke(editorContext, ServiceBuilder.mergeOptions(addParams, options)); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | if (options.dirtyElement) { | ||
165 | var doc = options.document || document; | ||
166 | var dirtyElement; | ||
167 | if (typeof(options.dirtyElement) === 'string') | ||
168 | dirtyElement = jQuery('#' + options.dirtyElement, doc); | ||
169 | else | ||
170 | dirtyElement = jQuery(options.dirtyElement); | ||
171 | var dirtyStatusClass = options.dirtyStatusClass; | ||
172 | if (!dirtyStatusClass) | ||
173 | dirtyStatusClass = 'dirty'; | ||
174 | editorContext.addDirtyStateListener(function(dirty) { | ||
175 | if (dirty) | ||
176 | dirtyElement.addClass(dirtyStatusClass); | ||
177 | else | ||
178 | dirtyElement.removeClass(dirtyStatusClass); | ||
179 | }); | ||
180 | } | ||
181 | |||
182 | services.successListeners = []; | ||
183 | services.errorListeners = [function(serviceType, severity, message, requestData) { | ||
184 | if (options.showErrorDialogs) | ||
185 | window.alert('Xtext service \'' + serviceType + '\' failed: ' + message); | ||
186 | else | ||
187 | console.log('Xtext service \'' + serviceType + '\' failed: ' + message); | ||
188 | }]; | ||
189 | } | ||
190 | |||
191 | /** | ||
192 | * Change the resource associated with this service builder. | ||
193 | */ | ||
194 | ServiceBuilder.prototype.changeResource = function(resourceId) { | ||
195 | var services = this.services; | ||
196 | var options = services.options; | ||
197 | options.resourceId = resourceId; | ||
198 | for (var p in services) { | ||
199 | if (services.hasOwnProperty(p)) { | ||
200 | var service = services[p]; | ||
201 | if (service._serviceType && jQuery.isFunction(service.initialize)) | ||
202 | services[p].initialize(options.serviceUrl, service._serviceType, resourceId, services.updateService); | ||
203 | } | ||
204 | } | ||
205 | var knownServerState = services.editorContext.getServerState(); | ||
206 | delete knownServerState.stateId; | ||
207 | delete knownServerState.text; | ||
208 | if (options.loadFromServer && jQuery.isFunction(services.loadResource)) { | ||
209 | services.loadResource(); | ||
210 | } | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * Create a copy of the given object. | ||
215 | */ | ||
216 | ServiceBuilder.copy = function(obj) { | ||
217 | var copy = {}; | ||
218 | for (var p in obj) { | ||
219 | if (obj.hasOwnProperty(p)) | ||
220 | copy[p] = obj[p]; | ||
221 | } | ||
222 | return copy; | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * Translate an HTML attribute name to a JS option name. | ||
227 | */ | ||
228 | ServiceBuilder.optionName = function(name) { | ||
229 | var prefix = 'data-editor-'; | ||
230 | if (name.substring(0, prefix.length) === prefix) { | ||
231 | var key = name.substring(prefix.length); | ||
232 | key = key.replace(/-([a-z])/ig, function(all, character) { | ||
233 | return character.toUpperCase(); | ||
234 | }); | ||
235 | return key; | ||
236 | } | ||
237 | return undefined; | ||
238 | } | ||
239 | |||
240 | /** | ||
241 | * Copy all default options into the given set of additional options. | ||
242 | */ | ||
243 | ServiceBuilder.mergeOptions = function(options, defaultOptions) { | ||
244 | if (options) { | ||
245 | for (var p in defaultOptions) { | ||
246 | if (defaultOptions.hasOwnProperty(p)) | ||
247 | options[p] = defaultOptions[p]; | ||
248 | } | ||
249 | return options; | ||
250 | } else { | ||
251 | return ServiceBuilder.copy(defaultOptions); | ||
252 | } | ||
253 | } | ||
254 | |||
255 | /** | ||
256 | * Merge all properties of the given parent element with the given default options. | ||
257 | */ | ||
258 | ServiceBuilder.mergeParentOptions = function(parent, defaultOptions) { | ||
259 | var options = ServiceBuilder.copy(defaultOptions); | ||
260 | for (var attr, j = 0, attrs = parent.attributes, l = attrs.length; j < l; j++) { | ||
261 | attr = attrs.item(j); | ||
262 | var key = ServiceBuilder.optionName(attr.nodeName); | ||
263 | if (key) { | ||
264 | var value = attr.nodeValue; | ||
265 | if (value === 'true' || value === 'false') | ||
266 | value = value === 'true'; | ||
267 | options[key] = value; | ||
268 | } | ||
269 | } | ||
270 | return options; | ||
271 | } | ||
272 | |||
273 | return ServiceBuilder; | ||
274 | }); \ No newline at end of file | ||