# all files / Core/Components/StepInput/ StepInputController.js

30.43% Statements 14/46
0% Branches 0/8
12.5% Functions 1/8
30.43% Lines 14/46

    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

    1
    1

    1

    1
    1
    1

    1
    1

    1

    1

    1

    1

    1

    1
     'use strict';

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

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

    // Dependencies:
    var camel = require('change-case').camel;
    var title = require('change-case').title;
    require('../../Services/FileStructureService');

    var StepInputController = (function () {
        var StepInputController = function StepInputController (
            fileStructureService
        ) {
            this.fileStructureService = fileStructureService;
            this.items = []
            this.selectedIndex = -1;
            this.isOpen = false;
        };

        StepInputController.prototype.handleSearch = function (searchKey) {
            if (searchKey) {
                this.searchKey = searchKey;
                return getSearchData(this)
                   .then(getSuggestions.bind(this));
            } else {
                this.isOpen = false;
            }
         };

        StepInputController.prototype.itemSelected = function (index) {
            this.model.step = this.items[index];
            this.isOpen = false;
        };

        function getSearchData (that){
            return that.fileStructureService.getFileStructure('features')
            .then(function (results) {
                return results;
            })
            .then(function (fileStructure) {
                return Array.from ( new Set (
                fileStructure.directory.allFiles
                .reduce(((p, feature) => p.concat(feature.tokens)), [])
                .reduce((p, token) => p.concat(token.elements), [])
                .reduce((p, element) => p.concat(element.stepDeclarations), [])
              //.filter(declaration => declaration.type === typeSelected)
                .map(declaration => declaration.step)
                ));
            })
       };

       function getSuggestions (searchData) {
            this.items=[];
            if (searchData.length > 0) {
                var searchTextSmallLetters = angular.lowercase(this.searchKey);
                var searchTerms = searchTextSmallLetters.split(' ');
                for(var i=0; i< searchData.length; i++) {
                    var searchItemsSmallLetters = angular.lowercase(searchData[i]);
                    var isMissingTerm = false;
                    for(var j=0; j < searchTerms.length; j++){
                        if( searchItemsSmallLetters.indexOf(searchTerms[j]) === -1) {
                            isMissingTerm = true;
                            break;
                        }
                    }
                    if (!isMissingTerm) {
                        this.items.push(searchItemsSmallLetters);
                    }
                }
                this.isOpen = true;
            } else {
                this.model.step = this.searchKey;
            }
        };

        return StepInputController;
    })();

    Core.controller('StepInputController', StepInputController);