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

100% Statements 42/42
100% Branches 8/8
100% Functions 10/10
100% Lines 42/42

    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

    1

    1

    1
    1

    1

    51

    51
    34

    1

    16

    13

    15

    34
    34

    51

    51

    1
    15
    4

    11

    1
    4
    4

    4
    4
    2

    2
    2
    2
    2
    2
    2

    1
    11
    11

    11
    10
    10

    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'];

        return FilterModel;

        function toAST () {
            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 locatorLiteral = ast.literal(this.locator);
            var template = '';

            if (!this.isText) {
                template += 'by.<%= type %>(<%= locator %>)';
                return ast.expression(template, {
                    type: ast.identifier(this.type),
                    locator: locatorLiteral
                });
            } else {
                template += 'by.cssContainingText(\'*\', <%= locator %>)';
                return ast.expression(template, {
                    locator: locatorLiteral
                });
            }
        }
    };

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