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

100% Statements 68/68
100% Branches 14/14
100% Functions 20/20
100% Lines 68/68

    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
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251

    1

    1

    1
    1
    1
    1

    1

    39

    39
    23

    2

    1

    8

    2

    7

    23
    23
    23

    39
    22
    22
    22
    6

    39
    2
    3

    2
    1

    39
    1

    39

    39

    1
    7
    7

    7

    7

    1
    7
    7
    7
    11
    11
    7

    4

    11

    11

    7

    1
    7
    3

    4

    1
    4
    2
    2

    2

    1
    2
    2
    1

    1

    1
    2
    1

    1

    1

    39

     'use strict';

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

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

    // Dependencies:
    var camelcase = require('change-case').camel;
    require('../../../Core/Services/ASTCreatorService');
    require('../../../Core/Services/StringToLiteralService');
    require('./FilterModel');

    var createElementModelConstructor = function (
        astCreatorService,
        stringToLiteralService,
        FilterModel
    ) {
        var ast = astCreatorService;

        var ElementModel = function ElementModel (component) {
            Object.defineProperties(this, {
                component: {
                    get: function () {
                        return component;
                    }
                },
                selector: {
                    get: function () {
                        return _.first(this.filters);
                    }
                },
                variableName: {
                    get: function () {
                        return camelcase(this.name);
                    }
                },
                meta: {
                    get: function () {
                        return {
                            name: this.name
                        };
                    }
                },
                ast: {
                    get: function () {
                        return toAST.call(this);
                    }
                }
            });

            this.name = '';
            this.filters = [];
            this.sortableFilters = [];
        };

        ElementModel.prototype.addFilter = function (filter) {
            filter = filter || new FilterModel(this);
            this.filters.push(filter);
            if (this.filters.length > 1) {
                this.sortableFilters.push(filter);
            }
        };

        ElementModel.prototype.removeFilter = function (toRemove) {
            _.remove(this.filters, function (filter) {
                return filter === toRemove;
            });
            _.remove(this.sortableFilters, function (sortableFilter) {
                return sortableFilter === toRemove;
            });
        };

        ElementModel.prototype.getAllVariableNames = function () {
            return this.component.getAllVariableNames(this);
        };

        ElementModel.prototype.methods = [{
            name: 'click',
            description: 'Schedules a command to click on this element.',
            returns: 'promise'
        }, {
            name: 'sendKeys',
            description: 'Schedules a command to type a sequence on the DOM element represented by this instance.',
            arguments: [{
                name: 'keys',
                description: 'The sequence of keys to type.',
                type: 'string',
                required: true
            }],
            returns: 'promise'
        }, {
            name: 'getText',
            description: 'Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.',
            returns: 'promise',
            promise: {
                name: 'text',
                type: 'string',
                required: true
            }
        }, {
            name: 'isEnabled',
            description: 'Schedules a command to query whether the DOM element represented by this instance is enabled, as dictated by the `disabled` attribute.',
            returns: 'promise',
            promise: {
                name: 'enabled',
                type: 'boolean',
                required: true
            }
        }, {
            name: 'isSelected',
            description: 'Schedules a command to query whether this element is selected.',
            returns: 'promise',
            promise: {
                name: 'selected',
                type: 'boolean',
                required: true
            }
        }, {
            name: 'submit',
            description: 'Schedules a command to submit the form containing this element (or this element if it is a FORM element). This command is a no-op if the element is not contained in a form.',
            returns: 'promise'
        }, {
            name: 'clear',
            description: 'Schedules a command to clear the {@code value} of this element. This command has no effect if the underlying DOM element is neither a text INPUT element nor a TEXTAREA element.',
            returns: 'promise'
        }, {
            name: 'isDisplayed',
            description: 'Schedules a command to test whether this element is currently displayed. If isDisplayed equal to false, it means the element is invisible on browser page, but it still be the present DOM element',
            returns: 'promise',
            promise: {
                name: 'displayed',
                type: 'boolean',
                required: true
            }
        }, {
            name: 'isPresent',
            description: 'Schedules a command to test whether this element is currently present as the DOM element.',
            returns: 'promise',
            promise: {
                name: 'present',
                type: 'boolean',
                required: true
            }
        }, {
            name: 'getOuterHtml',
            description: 'Schedules a command to retrieve the outer HTML of this element.',
            returns: 'promise',
            promise: {
                name: 'outerHtml',
                type: 'string',
                required: true
            }
        }, {
            name: 'getInnerHtml',
            description: 'Schedules a command to retrieve the inner HTML of this element.',
            returns: 'promise',
            promise: {
                name: 'innerHtml',
                type: 'string',
                required: true
            }
        }, {
            name: 'getAttribute',
            description: 'Schedules a command to get attribute of this element.',
            returns: 'promise',
            arguments: [{
                name: 'attribute',
                description: 'key of element attribute',
                type: 'string',
                required: true
            }],
        }];

        return ElementModel;

        function toAST () {
            var element = ast.identifier(this.variableName);
            var filters = filtersAST.call(this);

            var template = 'this.<%= element %> = <%= filters %>;';

            return ast.expression(template, {
                element: element,
                filters: filters
            });
        }

        function filtersAST () {
            var template = '';
            var fragments = {};
            _.reduce(this.filters, function (previousFilter, filter, index) {
                var filterTemplate = '<%= filter' + index + ' %>';
                if (!template.length) {
                    template += filterAST(filter, filterTemplate);
                } else {
                    template += filterAfterFilterAST(previousFilter, filter, filterTemplate);
                }

                fragments['filter' + index] = filter.ast;

                return filter;
            }, {});

            return ast.expression(template, fragments);
        }

        function filterAST (filter, filterTemplate) {
            if (filter.isGroup) {
                return 'element.all(' + filterTemplate + ')';
            } else {
                return 'element(' + filterTemplate + ')';
            }
        }

        function filterAfterFilterAST (previousFilter, filter, filterTemplate) {
            if (previousFilter.isGroup) {
                filter.isNested = true;
                return filterAfterGroupFilter(filter, filterTemplate);
            } else {
                return filterAfterSingleFilter(filter, filterTemplate);
            }
        }

        function filterAfterGroupFilter (filter, filterTemplate) {
            var locatorLiteral = stringToLiteralService.toLiteral(filter.locator);
            if (_.isNumber(locatorLiteral)) {
                return '.get(' + filterTemplate + ')';
            } else {
                return '.filter(' + filterTemplate + ').get(0)';
            }
        }

        function filterAfterSingleFilter (filter, filterTemplate) {
            if (filter.isGroup) {
                return '.all(' + filterTemplate + ')';
            } else {
                return '.element(' + filterTemplate + ')';
            }
        }
    };

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