# all files / features/ComponentEditor/Models/ FilterModel.js

100% Statements 50/50
100% Branches 10/10
100% Functions 10/10
100% Lines 50/50

    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

    1

    1

    1
    1

    1

    51

    51
    34

    1

    16

    24

    15

    34
    34

    51

    51

    1
    15
    15
    4

    11

    1
    4
    4

    4
    4
    2

    2
    2
    2
    2
    2
    2

    1
    11
    11
    11
    11
    1
    1

    11
    10
    10
    10

    1

    1
    1
    1

    1

    51

     'use strict';

    // Utilities:
    var _ = require('lodash');

    // Module:
    var ComponentEditor = require('../ComponentEditor');

    // Dependencies:
    require('../../../Core/Services/ASTCreatorService');
    require('../../../Core/Services/StringToLiteralService');

    var createFilterModelConstructor = function (
        astCreatorService,
        stringToLiteralService
    ) {
        var ast = astCreatorService;

        var FilterModel = function FilterModel (element) {
            Object.defineProperties(this, {
                element: {
                    get: function () {
                        return element;
                    }
                },

                isGroup: {
                    get: function () {
                        return this.type === 'options' || this.type === 'repeater';
                    }
                },
                isText: {
                    get: function () {
                        return this.type === 'text';
                    }
                },

                ast: {
                    get: function () {
                        return toAST.call(this);
                    }
                }
            });

            this.type = _.first(this.types);
            this.locator = '';
        };

        FilterModel.prototype.types = ['model', 'binding', 'text', 'css', 'options', 'repeater', 'cssContainingText'];

        return FilterModel;

        function toAST () {
            console.log("ast fileter")
            if (this.isNested) {
                return toNestedAST.call(this);
            } else {
                return toSingleAST.call(this);
            }
        }

        function toNestedAST () {
            var locatorLiteral = ast.literal(this.locator);
            var template = '';

            var number = stringToLiteralService.toLiteral(locatorLiteral.value);
            if (_.isNumber(number)) {
                return ast.literal(number);
            } else {
                template += '(function (element) {';
                template += '    return element.getText().then(function (text) {';
                template += '        return text.indexOf(<%= locator %>) !== -1;';
                template += '    });';
                template += '});';
                return ast.expression(template, {
                    locator: locatorLiteral
                });
            }
        }

        function toSingleAST () {
            var template = '';
            var locatorElement = '';
            var cssLiteral = '';
            if (this.isText){
                locatorElement = this.locator.split(",");
                cssLiteral = ast.literal(locatorElement[0]);
            }

            if (!this.isText) {
                var locatorLiteral = ast.literal(this.locator);
                template += 'by.<%= type %>(<%= locator %>)';
                return ast.expression(template, {
                    type: ast.identifier(this.type),
                    locator: locatorLiteral
                });
            } else {
                console.log("ast fileter text", locatorElement[1])

                // var locatorElement = this.locator.split(",");
                // var cssLiteral = ast.literal(locatorElement[0]);
                var stringLiteral = ast.literal(locatorElement[1]);
                template += 'by.cssContainingText(<%= locator %>,<%= searchString %>)';
                return ast.expression(template, {
                    locator: cssLiteral,
                    searchString: stringLiteral
                });

            }
        }
    };

    ComponentEditor.factory('FilterModel', function (
        astCreatorService,
        stringToLiteralService
    ) {
        return createFilterModelConstructor(astCreatorService, stringToLiteralService);
    });