<commit_before>/*************************************************
 * Copyright (c) 2015 Ansible, Inc.
 *
 * All Rights Reserved
 *************************************************/


/**
 *  @ngdoc function
 *  @name shared.function:directives
 *  @description
 * Custom directives for form validation
 *
 */

export default
angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper'])

// awpassmatch:  Add to password_confirm field. Will test if value
//               matches that of 'input[name="password"]'
.directive('awpassmatch', function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            ctrl.$parsers.unshift(function(viewValue) {
                var associated = attrs.awpassmatch,
                    password = $('input[name="' + associated + '"]').val();
                if (viewValue === password) {
                    // it is valid
                    ctrl.$setValidity('awpassmatch', true);
                    return viewValue;
                }
                // Invalid, return undefined (no model update)
                ctrl.$setValidity('awpassmatch', false);
                return viewValue;
            });
        }
    };
})

// caplitalize  Add to any input field where the first letter of each
//              word should be capitalized. Use in place of css test-transform.
//              For some reason "text-transform: capitalize" in breadcrumbs
//              causes a break at each blank space. And of course,
//              "autocapitalize='word'" only works in iOS. Use this as a fix.
.directive('capitalize', function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            ctrl.$parsers.unshift(function(viewValue) {
                var values = viewValue.split(" "),
                    result = "",
                    i;
                for (i = 0; i < values.length; i++) {
                    result += values[i].charAt(0).toUpperCase() + values[i].substr(1) + ' ';
                }
                result = result.trim();
                if (result !== viewValue) {
                    ctrl.$setViewValue(result);
                    ctrl.$render();
                }
                return result;
            });
        }
    };
})

// chkPass
//
// Enables use of js/shared/pwdmeter.js to check strengh of passwords.
// See controllers/Users.js for example.
//
.directive('chkPass', [function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            $(elm).keyup(function() {
                var validity = true;
                if (elm.val()) {
                    if ($AnsibleConfig.password_length) {
                        validity = (ctrl.$modelValue.length >= $AnsibleConfig.password_length);
                        ctrl.$setValidity('password_length', validity);
                    }
                    if ($AnsibleConfig.password_hasLowercase) {
                        validity = (/[a-z]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasLowercase', validity);
                    }
                    if ($AnsibleConfig.password_hasUppercase) {
                        validity = (/[A-Z]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasUppercase', validity);
                    }
                    if ($AnsibleConfig.password_hasNumber) {
                        validity = (/[0-9]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasNumber', validity);
                    }
                    if ($AnsibleConfig.password_hasSymbol) {
                        validity = (/[\\#@$-/:-?{-~!"^_`\[\]]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasSymbol', validity);
                    }
                } else {
                    validity = true;
                    if ($AnsibleConfig.password_length) {
                        ctrl.$setValidity('password_length', validity);
                    }
                    if ($AnsibleConfig.password_hasLowercase) {
                        ctrl.$setValidity('hasLowercase', validity);
                    }
                    if ($AnsibleConfig.password_hasUppercase) {
                        ctrl.$setValidity('hasUppercase', validity);
                    }
                    if ($AnsibleConfig.password_hasNumber) {
                        ctrl.$setValidity('hasNumber', validity);
                    }
                    if ($AnsibleConfig.password_hasSymbol) {
                        ctrl.$setValidity('hasSymbol', validity);
                    }
                }
                if (!scope.$$phase) {
                    scope.$digest();
                }
            });
        }
    };
}])

// imageUpload
//
// Accepts image and returns base64 information with basic validation
// Can eventually expand to handle all uploads with different endpoints and handlers
//
.directive('imageUpload', ['ConfigurationUtils', 'i18n', '$rootScope',
function(ConfigurationUtils, i18n, $rootScope) {
    var browseText = i18n._('BROWSE'),
    placeholderText = i18n._('Choose file'),
    uploadedText = i18n._('Current Image: '),
    removeText = i18n._('REMOVE');

    return {
        restrict: 'E',
        scope: {
            key: '@'
        },
        template: `
                <div class="input-group">
                      <label class="input-group-addon Form-filePicker--pickerButton" id="filePickerButton" for="filePicker" ng-click="update($event)">${browseText}</label>
                      <input type="text" class="form-control Form-filePicker--textBox" id="filePickerText" placeholder="${placeholderText}" readonly>
                      <input type="file" name="file" class="Form-filePicker" id="filePicker"  onchange="angular.element(this).scope().fileChange(this.files)"/>
                    </div>

                <div ng-if="imagePresent" class="Form-filePicker--selectedFile">
                ${uploadedText}
                    <img data-ng-src="{{imageData}}" alt="Current logo" class="Form-filePicker--thumbnail">
                </div>

                <div class="error" id="filePickerError"></div>`,

        link: function(scope) {
            var fieldKey = scope.key;
            var filePickerText = angular.element(document.getElementById('filePickerText'));
            var filePickerError = angular.element(document.getElementById('filePickerError'));
            var filePickerButton = angular.element(document.getElementById('filePickerButton'));
            var filePicker = angular.element(document.getElementById('filePicker'));

            scope.imagePresent = global.$AnsibleConfig.custom_logo || false;
            scope.imageData = $rootScope.custom_logo;

            scope.$on('loginUpdated', function() {
                scope.imagePresent = global.$AnsibleConfig.custom_logo;
                scope.imageData = $rootScope.custom_logo;
            });

            scope.$watch('imagePresent', () => {
                if(!scope.imagePresent){
                    filePickerButton.html(browseText);
                }
                else{
                    filePickerButton.html(removeText);
                }
            });
<commit_msg>using "val" instead of scope value<commit_after>/*************************************************
 * Copyright (c) 2015 Ansible, Inc.
 *
 * All Rights Reserved
 *************************************************/


/**
 *  @ngdoc function
 *  @name shared.function:directives
 *  @description
 * Custom directives for form validation
 *
 */

export default
angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper'])

// awpassmatch:  Add to password_confirm field. Will test if value
//               matches that of 'input[name="password"]'
.directive('awpassmatch', function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            ctrl.$parsers.unshift(function(viewValue) {
                var associated = attrs.awpassmatch,
                    password = $('input[name="' + associated + '"]').val();
                if (viewValue === password) {
                    // it is valid
                    ctrl.$setValidity('awpassmatch', true);
                    return viewValue;
                }
                // Invalid, return undefined (no model update)
                ctrl.$setValidity('awpassmatch', false);
                return viewValue;
            });
        }
    };
})

// caplitalize  Add to any input field where the first letter of each
//              word should be capitalized. Use in place of css test-transform.
//              For some reason "text-transform: capitalize" in breadcrumbs
//              causes a break at each blank space. And of course,
//              "autocapitalize='word'" only works in iOS. Use this as a fix.
.directive('capitalize', function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            ctrl.$parsers.unshift(function(viewValue) {
                var values = viewValue.split(" "),
                    result = "",
                    i;
                for (i = 0; i < values.length; i++) {
                    result += values[i].charAt(0).toUpperCase() + values[i].substr(1) + ' ';
                }
                result = result.trim();
                if (result !== viewValue) {
                    ctrl.$setViewValue(result);
                    ctrl.$render();
                }
                return result;
            });
        }
    };
})

// chkPass
//
// Enables use of js/shared/pwdmeter.js to check strengh of passwords.
// See controllers/Users.js for example.
//
.directive('chkPass', [function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            $(elm).keyup(function() {
                var validity = true;
                if (elm.val()) {
                    if ($AnsibleConfig.password_length) {
                        validity = (ctrl.$modelValue.length >= $AnsibleConfig.password_length);
                        ctrl.$setValidity('password_length', validity);
                    }
                    if ($AnsibleConfig.password_hasLowercase) {
                        validity = (/[a-z]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasLowercase', validity);
                    }
                    if ($AnsibleConfig.password_hasUppercase) {
                        validity = (/[A-Z]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasUppercase', validity);
                    }
                    if ($AnsibleConfig.password_hasNumber) {
                        validity = (/[0-9]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasNumber', validity);
                    }
                    if ($AnsibleConfig.password_hasSymbol) {
                        validity = (/[\\#@$-/:-?{-~!"^_`\[\]]/.test(ctrl.$modelValue));
                        ctrl.$setValidity('hasSymbol', validity);
                    }
                } else {
                    validity = true;
                    if ($AnsibleConfig.password_length) {
                        ctrl.$setValidity('password_length', validity);
                    }
                    if ($AnsibleConfig.password_hasLowercase) {
                        ctrl.$setValidity('hasLowercase', validity);
                    }
                    if ($AnsibleConfig.password_hasUppercase) {
                        ctrl.$setValidity('hasUppercase', validity);
                    }
                    if ($AnsibleConfig.password_hasNumber) {
                        ctrl.$setValidity('hasNumber', validity);
                    }
                    if ($AnsibleConfig.password_hasSymbol) {
                        ctrl.$setValidity('hasSymbol', validity);
                    }
                }
                if (!scope.$$phase) {
                    scope.$digest();
                }
            });
        }
    };
}])

// imageUpload
//
// Accepts image and returns base64 information with basic validation
// Can eventually expand to handle all uploads with different endpoints and handlers
//
.directive('imageUpload', ['ConfigurationUtils', 'i18n', '$rootScope',
function(ConfigurationUtils, i18n, $rootScope) {
    var browseText = i18n._('BROWSE'),
    placeholderText = i18n._('Choose file'),
    uploadedText = i18n._('Current Image: '),
    removeText = i18n._('REMOVE');

    return {
        restrict: 'E',
        scope: {
            key: '@'
        },
        template: `
                <div class="input-group">
                      <label class="input-group-addon Form-filePicker--pickerButton" id="filePickerButton" for="filePicker" ng-click="update($event)">${browseText}</label>
                      <input type="text" class="form-control Form-filePicker--textBox" id="filePickerText" placeholder="${placeholderText}" readonly>
                      <input type="file" name="file" class="Form-filePicker" id="filePicker"  onchange="angular.element(this).scope().fileChange(this.files)"/>
                    </div>

                <div ng-if="imagePresent" class="Form-filePicker--selectedFile">
                ${uploadedText}
                    <img data-ng-src="{{imageData}}" alt="Current logo" class="Form-filePicker--thumbnail">
                </div>

                <div class="error" id="filePickerError"></div>`,

        link: function(scope) {
            var fieldKey = scope.key;
            var filePickerText = angular.element(document.getElementById('filePickerText'));
            var filePickerError = angular.element(document.getElementById('filePickerError'));
            var filePickerButton = angular.element(document.getElementById('filePickerButton'));
            var filePicker = angular.element(document.getElementById('filePicker'));

            scope.imagePresent = global.$AnsibleConfig.custom_logo || false;
            scope.imageData = $rootScope.custom_logo;

            scope.$on('loginUpdated', function() {
                scope.imagePresent = global.$AnsibleConfig.custom_logo;
                scope.imageData = $rootScope.custom_logo;
            });

            scope.$watch('imagePresent', (val) => {
                if(val){
                    filePickerButton.html(removeText);
                }
                else{
                    filePickerButton.html(browseText);
                }
            });

{'fraction_non_alphanumeric': 0.13805406888980376, 'fraction_numerical': 0.0031563057499656923, 'mean_word_length': 1.630391626060278, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3000908', 'n_tokens_mistral': 3811, 'n_tokens_neox': 3536, 'n_words': 726}