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

100% Statements 70/70
100% Branches 13/13
100% Functions 22/22
100% Lines 70/70

    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

    1

    1

    1
    1
    1
    1

    1

    40

    40
    24
    24

    24

    27

    44

    25

    9

    4

    1

    8

    24

    40
    3

    40
    1
    1

    40
    17
    17
    17

    40
    1
    1

    40
    1

    40

    1
    8
    8
    8
    1

    8

    8
    8
    6
    6

    8

    8

    1
    8
    8
    8
    9

    9
    6

    3
    3
    3

    9
    9

    9
    9
    1

    9

    8

    1
    9
    9
    1

    1

    40

     'use strict';

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

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

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

    var createActionModelConstructor = function (
        astCreatorService,
        ParameterModel,
        InteractionModel
    ) {
        var ast = astCreatorService;

        var ActionModel = function ActionModel (component) {
            var interactions = [];
            var parameters = [];

            Object.defineProperties(this, {
                component: {
                    get: function () {
                        return component;
                    }
                },
                interactions: {
                    get: function () {
                        return interactions;
                    }
                },
                parameters: {
                    get: function () {
                        return parameters;
                    }
                },
                variableName: {
                    get: function () {
                        return camelcase(this.name);
                    }
                },
                meta: {
                    get: function () {
                        return {
                            name: this.name,
                            parameters: this.parameters.map(function (parameter) {
                                return parameter.meta;
                            })
                        };
                    }
                },
                ast: {
                    get: function () {
                        return toAST.call(this);
                    }
                }
            });

            this.name = '';
        };

        ActionModel.prototype.addParameter = function () {
            this.parameters.push(new ParameterModel(this));
        };

        ActionModel.prototype.removeParameter = function (toRemove) {
            _.remove(this.parameters, function (parameter) {
                return parameter === toRemove;
            });
        };

        ActionModel.prototype.addInteraction = function () {
            var interaction = new InteractionModel(this);
            interaction.element = this.component.browser;
            this.interactions.push(interaction);
        };

        ActionModel.prototype.removeInteraction = function (toRemove) {
            _.remove(this.interactions, function (interaction) {
                return interaction === toRemove;
            });
        };

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

        return ActionModel;

        function toAST () {
            var component = ast.identifier(this.component.variableName);
            var action = ast.identifier(this.variableName);
            var parameters = _.map(this.parameters, function (parameter) {
                return parameter.ast;
            });
            var interactions = interactionsAST.call(this);

            var template = '<%= component %>.prototype.<%= action %> = function (%= parameters %) {';
            if (interactions) {
                template += 'var self = this;';
                template += 'return <%= interactions %>;';
            }
            template += '};';

            return ast.expression(template, {
                component: component,
                action: action,
                parameters: parameters,
                interactions: interactions
            });
        }

        function interactionsAST () {
            var template = '';
            var fragments = {};
            _.reduce(this.interactions, function (previousInteraction, interaction, index) {
                var interactionTemplate = '<%= interaction' + index + ' %>';

                if (!template.length) {
                    template += interactionTemplate;
                } else {
                    template += '.then(function (%= parameter' + index + '%) {';
                    template += '    return ' + interactionTemplate + ';';
                    template += '})';
                }

                fragments['interaction' + index] = interaction.ast;
                fragments['parameter' + index] = [];

                var previousResult = previousInteractionResult(previousInteraction);
                if (previousResult) {
                    fragments['parameter' + index].push(ast.identifier(previousResult));
                }

                return interaction;
            }, {});

            return ast.expression(template, fragments);
        }

        function previousInteractionResult (previous) {
            var returns = previous && previous.method && previous.method.returns;
            if (returns && previous.method[returns]) {
                return previous.method[returns].name;
            }
        }
    };

    ComponentEditor.factory('ActionModel', function (
        astCreatorService,
        ParameterModel,
        InteractionModel
    ) {
        return createActionModelConstructor(astCreatorService, ParameterModel, InteractionModel);
    });