Commit 71771180 authored by Baptiste Mesta's avatar Baptiste Mesta Committed by julienmege

feat(translate): add translate function in javascript expressions (#2507)

this function is now available and call the normal translate mechanism
the editor also completes this function (`uiTranslate()`)

Closes [BS-18892](https://bonitasoft.atlassian.net/browse/BS-18892)
parent 4089c36d
......@@ -2,7 +2,7 @@
'use strict';
angular.module('bonitasoft.ui.constants', []);
angular.module('bonitasoft.ui.services', []);
angular.module('bonitasoft.ui.services', ['gettext']);
angular.module('bonitasoft.ui.directives', ['gettext']);
angular.module('bonitasoft.ui.filters', ['gettext']);
angular.module('bonitasoft.ui.widgets', ['bonitasoft.ui.filters', 'bonitasoft.ui.services', 'ngSanitize']);
......
......@@ -5,7 +5,7 @@
.module('bonitasoft.ui.services')
.run(createExpressionResolver);
function createExpressionResolver(Resolver, ResolverService, $log, $rootScope) {
function createExpressionResolver(Resolver, ResolverService, $log, $rootScope, $filter) {
class ExpressionResolver extends Resolver {
constructor(model, name, content) {
......@@ -15,9 +15,15 @@
resolve() {
// use strict. Avoid pollution of the global object.
/* jshint evil: true */
var expression = new Function('$data', '"use strict";' + this.content);
var expression = new Function(
'$data',//inject all data
'uiTranslate',//inject translate function
'"use strict";' + this.content);
try {
this.model[this.name] = expression(this.model);
this.model[this.name] = expression(
this.model, // all data
(text) => $filter('translate')(text) // translate function
);
} catch (e) {
$log.warn('Error evaluating <', this.name, '> data: ', e.message);
}
......
describe('Service: expressionResolver', function () {
beforeEach(module('bonitasoft.ui.services'));
let model, $rootScope, aData;
beforeEach(inject((ResolverService, _$rootScope_, gettextCatalog) => {
$rootScope = _$rootScope_;
model = {
dep: 'depValue'
};
aData= (model, name, data) => {
let resolver = ResolverService.createResolver(model, name, data);
resolver.resolve();
resolver.watchDependencies();
};
gettextCatalog.setCurrentLanguage('nl');
gettextCatalog.setStrings("nl", {
"Hello": "Hallo"
});
}));
it('should resolve expression with no dependency', () => {
aData(model, 'myData', {
type: 'expression',
value: 'return "myValue";'
});
expect(model.myData).toBe('myValue');
});
it('should resolve expression with dependency', () => {
aData(model, 'myData', {
type: 'expression',
value: 'return $data.dep + " - myValue";'
});
expect(model.myData).toBe('depValue - myValue');
});
it('should translate text', () => {
aData(model, 'myData', {
type: 'expression',
value: 'return uiTranslate("Hello");'
});
expect(model.myData).toBe('Hallo');
});
});
......@@ -12,7 +12,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
angular.module('bonitasoft.designer.common.services').service('aceDataCompleter', function() {
angular.module('bonitasoft.designer.common.services').service('aceDataCompleter', function(gettext) {
return function(data) {
return {
getCompletions: function(editor, session, pos, prefix, callback) {
......@@ -32,10 +32,18 @@ angular.module('bonitasoft.designer.common.services').service('aceDataCompleter'
return {
name: data,
value: data,
score: 2, // increase score to show suggestion on top of the list
score: 10, // increase score to show suggestion on top of the list
meta: 'data' // the suggestion's category
};
});
// add translation function
completions.push({
caption: 'uiTranslate()',
snippet: 'uiTranslate($0)',
score: 9,
meta: 'function',
docHTML: gettext('Translate the given text')
});
callback(null, completions);
}
};
......
......@@ -12,14 +12,24 @@ describe('ace-data-completer', function() {
});
describe('getCompletions', function() {
it('should return an of completions ', function() {
it('should return each data in completions', function () {
var data = {
'users': {value: []}
};
var completer = service(data);
completer.getCompletions(null, null, 0, '', function (err, results) {
expect(results).toContain({name: '$data.users', value: '$data.users', score: 10, meta: 'data'});
});
});
it('should return translate function in completions', function () {
var data = {
'users': { value: [] }
};
var completer = service(data);
completer.getCompletions(null, null, 0, '' ,function(err, results) {
expect(results.length).toBe(1);
expect(results).toEqual([{ name: '$data.users', value: '$data.users', score: 2, meta: 'data' }]);
expect(results.find((e) => {
return e.caption && e.caption.includes('uiTranslate');
})).toBeDefined();
});
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment