Commit e188fdd1 authored by Vincent's avatar Vincent Committed by julien-reboul

Refactor bindings creation to a more flexible approche

parent e1b30ea1
(function () {
'use strict';
angular.module('bonitasoft.ui.services').service('Binding', () => (
class Binding {
constructor(property, context) {
this.property = property;
this.context = context;
}
}
));
})();
(function () {
'use strict';
function capitalize(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
}
angular.module('bonitasoft.ui.services')
.service('bindingService', ($injector) => ({
create(property, context) {
let Binding = $injector.get(capitalize(property.type) + 'Binding');
return new Binding(property, context);
}
}));
})();
(function () {
'use strict';
angular.module('bonitasoft.ui.services').service('ConstantBinding', (Binding) => (
class ConstantBinding extends Binding {
getValue() {
return this.property.value;
}
}
));
})();
(function () {
'use strict';
angular.module('bonitasoft.ui.services').service('ExpressionBinding', (Binding, $parse) => (
class ExpressionBinding extends Binding {
constructor(property, context) {
super(property, context);
this.getter = $parse(property.value);
}
getValue() {
return this.getter(this.context);
}
}
));
})();
(function () {
'use strict';
angular.module('bonitasoft.ui.services').service('InterpolationBinding', (Binding, $interpolate) => (
class InterpolationBinding extends Binding {
getValue() {
return $interpolate(this.property.value || '')(this.context);
}
}
));
})();
(function () {
'use strict';
angular.module('bonitasoft.ui.services').service('VariableBinding', (Binding, $parse) => (
class VariableBinding extends Binding {
constructor(property, context) {
super(property, context);
this.getter = $parse(property.value);
}
getValue() {
return this.getter(this.context);
}
setValue(value) {
return this.getter.assign && this.getter.assign(this.context, value);
}
}
));
})();
(function() {
(function () {
'use strict';
/**
* Factory to create bindings associating properties (also known as properties) to the model.
*
* @param $interpolate
* @param $parse
* @returns {{create: createBindings}}
*/
function bindingsFactory($interpolate, $parse) {
angular.module('bonitasoft.ui.services').factory('bindingsFactory', (bindingService) => ({
/**
* Define destination properties allowing to
......@@ -18,61 +11,20 @@
* @param context - against which property.value expression will be executed.
* @param destination - object where to bind the resulting properties.
*/
function createBindings(properties, context, destination) {
create(properties, context, destination) {
function createVariable(propertyValue) {
var getter = $parse(propertyValue);
return {
get: () => getter(context),
set: function(value) {
if (getter.assign) {
getter.assign(context, value);
}
Object.keys(properties).forEach(function (name) {
var binding = bindingService.create(properties[name], context);
Object.defineProperty(destination, name, {
get: function () {
return binding.getValue();
},
enumerable: true
};
}
function createExpression(propertyValue) {
var getter = $parse(propertyValue);
return {
get: () => getter(context),
enumerable: true
};
}
function createInterpolation(propertyValue) {
return {
get: () => $interpolate(propertyValue || '')(context),
enumerable: true
};
}
function createConstant(propertyValue) {
return {
get: () => propertyValue,
enumerable: true
};
}
var bonds = {
'variable': createVariable,
'expression': createExpression,
'interpolation': createInterpolation,
'constant': createConstant
};
Object.keys(properties).forEach(function(name) {
var property = properties[name];
Object.defineProperty(destination, name, bonds[property.type](property.value, name));
set: function (value) {
return binding.setValue && binding.setValue(value);
},
enumarable: true
});
});
}
return {
create: createBindings
};
}
angular.module('bonitasoft.ui.services')
.factory('bindingsFactory', bindingsFactory);
}));
})();
describe('Service: bindingService', function () {
beforeEach(module('bonitasoft.ui.services'));
let bindingService, property, context;
beforeEach(inject(function (_bindingService_) {
bindingService = _bindingService_;
property = {};
context = {};
}));
it('should create a constant binding for a constant property', function () {
property.type = 'constant';
property.value = 'hello';
let binding = bindingService.create(property, context);
expect(binding.constructor.name).toBe('ConstantBinding');
expect(binding.getValue()).toBe('hello');
});
it('should create an interpolation binding for an interpolated property', function () {
property.type = 'interpolation';
property.value = '{{ variable }}';
context.variable = 'hello';
let binding = bindingService.create(property, context);
expect(binding.constructor.name).toBe('InterpolationBinding');
expect(binding.getValue()).toBe('hello');
});
it('should create an expression binding for an expression property', function () {
property.type = 'expression';
property.value = 'a + b';
context.a = 3;
context.b = 4;
let binding = bindingService.create(property, context);
expect(binding.constructor.name).toBe('ExpressionBinding');
expect(binding.getValue()).toBe(7);
});
it('should create a readable variable binding for a variable property', function () {
property.type = 'variable';
property.value = 'foobar';
context.foobar = 'hello';
let binding = bindingService.create(property, context);
expect(binding.constructor.name).toBe('VariableBinding');
expect(binding.getValue()).toBe('hello');
});
it('should create a writable variable binding for a variable property', function () {
property.type = 'variable';
property.value = 'foobar';
context.foobar = 'hello';
let binding = bindingService.create(property, context);
binding.setValue('bonjour');
expect(binding.constructor.name).toBe('VariableBinding');
expect(binding.getValue()).toBe('bonjour');
});
it('should throw an error when property type is unknown', function () {
property.type = 'unknown';
expect(function() {
bindingService.create(property, context);
}).toThrowError();
});
});
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