Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- 'use strict';
- angular.module('common.shared.personalization')
- .factory('ComplexPersonalization', personalizationFactory)
- .factory('PersonalizationCase', personalizationCaseFactory)
- .factory('PersonalizationCaseCondition', personalizationCaseConditionFactory);
- personalizationFactory.$inject = ['Filter'];
- function personalizationFactory(Filter) {
- /**
- *
- * @param {Object?} defaultCase
- * @param {String} defaultCase.property
- * @param {String} defaultCase.operator
- * @param {String?} defaultText
- * @constructor
- */
- function ComplexPersonalization( defaultCase, defaultText ) {
- this.defaultText = defaultText || '';
- this.defaultCase = defaultCase || {};
- this.cases = [];
- this.addCase();
- }
- ComplexPersonalization.prototype.addCase = function() {
- this.cases.push( new PersonalizationCase(this.defaultCase.property, this.defaultCase.operator) );
- };
- ComplexPersonalization.prototype.removeCase = function( personalizationCase ) {
- var index = this.cases.indexOf( personalizationCase );
- this.cases.splice( index, 1 );
- };
- /**
- *
- * @param {ColumnCollection} recipientColumns
- * @returns {string}
- */
- ComplexPersonalization.prototype.toTwig = function(recipientColumns) {
- var twig = '',
- me = this;
- if ( this.cases && this.cases.length ) {
- this.cases.forEach( function( personalizationCase, index ) {
- if ( personalizationCase.conditions && personalizationCase.conditions.length && personalizationCase.booleanOperator ) {
- twig += '{% ';
- if ( index == 0 ) {
- twig += 'if ';
- } else {
- twig += 'elseif ';
- }
- personalizationCase.conditions.forEach( function( condition, conditionIndex ) {
- if ( conditionIndex != 0 ) {
- twig += ' ' + personalizationCase.booleanOperator + ' ';
- }
- var conditionOperator;
- var operatorDirection = 'leftToRight';
- var negation = false;
- // TODO Use constants instead of strings......pls?
- switch ( condition.operator ) {
- case '=ISSET=':
- case '=NLIKE=':
- conditionOperator = '!=';
- break;
- case '=ewLIKE=':
- conditionOperator = 'ends with';
- break;
- case '=newLIKE=':
- conditionOperator = 'ends with';
- negation = true;
- break;
- case '=swLIKE=':
- conditionOperator = 'starts with';
- break;
- case '=nswLIKE=':
- conditionOperator = 'starts with';
- negation = true;
- break;
- case '=EMPTY=':
- case '=LIKE=':
- case '==':
- conditionOperator = '==';
- break;
- case '=cLIKE=':
- case '=IN=':
- case '=moCONTAINS=':
- case '=CONTAINS=':
- conditionOperator = 'in';
- operatorDirection = 'rightToLeft';
- break;
- case '=ncLIKE=':
- case '=NIN=':
- case '=NCONTAINS=':
- case '=mnCONTAINS=':
- conditionOperator = 'in';
- operatorDirection = 'rightToLeft';
- negation = true;
- break;
- // TODO: '=maCONTAINS='
- case '=ne=':
- conditionOperator = '!=';
- break;
- case '=gt=':
- conditionOperator = '>';
- break;
- case '=ge=':
- conditionOperator = '>=';
- break;
- case '=lt=':
- conditionOperator = '<';
- break;
- case '=le=':
- conditionOperator = '<=';
- break;
- default:
- conditionOperator = condition.operator;
- }
- if (negation) {
- twig += '(not ';
- }
- twig += me.conditionToTwig(condition, conditionOperator, recipientColumns, operatorDirection === 'leftToRight');
- if (negation) {
- twig += ')';
- }
- } );
- twig += ' %}' + personalizationCase.text
- }
- } );
- }
- if ( this.defaultText && this.defaultText.length && twig.length ) {
- twig += '{% else %}' + this.defaultText;
- }
- if ( twig.length ) {
- twig += '{% endif %}';
- }
- return twig;
- };
- /**
- *
- * @param {Object} condition
- * @param {String} condition.value
- * @param {String} condition.property
- * @param {String} conditionOperator
- * @param {ColumnCollection} columns
- * @param {Boolean} leftToRight
- */
- ComplexPersonalization.prototype.conditionToTwig = function (condition, conditionOperator, columns, leftToRight) {
- var result = '',
- isRecipientProperty = condition.property.indexOf('recipient.') !== -1,
- property = isRecipientProperty ? condition.property.substr(10) : condition.property,
- column = columns.getColumn(property) || {},
- isNumber = column.type === 'number' || column.useNumberOperators, // enum with numbers :)
- isBoolean = column.type === 'boolean',
- isEmptyOperator = condition.operator === '=EMPTY=',
- isSetOperator = condition.operator === '=ISSET=',
- serializedValue;
- if (!isEmptyOperator && !isSetOperator && (isNumber || isBoolean)) {
- serializedValue = condition.value || 0;
- } else if (isEmptyOperator || isSetOperator) {
- serializedValue = '""';
- } else if (column.type === 'date' || column.type === 'datetime') {
- // TODO call function to serialize datetime after issue with filters is done.
- serializedValue = moment(condition.value)
- .format(condition.value.imJustDateDude ? Filter.ServerDateFormat : Filter.ServerDateTimeFormat);
- serializedValue = '"' + serializedValue + '"';
- } else {
- serializedValue = '"' + condition.value + '"';
- }
- if (leftToRight) {
- result += '( ' + condition.property + ' ' + conditionOperator + ' ' + serializedValue + ' )';
- } else {
- result += '( ' + serializedValue + ' ' + conditionOperator + ' ' + condition.property + ' )';
- }
- return result;
- };
- return ComplexPersonalization;
- }
- function personalizationCaseFactory() {
- return PersonalizationCase;
- }
- function personalizationCaseConditionFactory() {
- return PersonalizationCaseCondition;
- }
- // *********************
- // PersonalizationCase
- // *********************
- function PersonalizationCase(property, operator) {
- this.text = '';
- this.booleanOperator = 'or';
- this.conditions = [];
- this.defaultCaseCondition = {
- property: property,
- operator: operator
- };
- this.addCondition();
- }
- PersonalizationCase.prototype.addCondition = function() {
- this.conditions.push(new PersonalizationCaseCondition(this.defaultCaseCondition.property, this.defaultCaseCondition.operator));
- };
- PersonalizationCase.prototype.removeCondition = function( condition ) {
- var index = this.conditions.indexOf( condition );
- this.conditions.splice( index, 1 );
- };
- /**
- * @class
- * @param {String?} property
- * @param {String?} operator
- */
- function PersonalizationCaseCondition(property, operator) {
- this.property = property;
- this.operator = operator;
- this.value = '';
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement