<commit_before>﻿// todo: why isn't _references.js working?
/// <reference path="/Scripts/angular.js" />

// original source http://plnkr.co/edit/3zMsNnpNfOFwExSqLj2I?p=preview which derived from https://github.com/angular-ui/angular-ui/pull/191.

'use strict';

var textfield = angular.module('field', ['services.humanizer'])

textfield.directive('field', function ($compile, $http, $templateCache, $interpolate, humanizer) {

    function findInputElement(element) {
        return angular.element(element.find('input')[0] || element.find('textarea')[0] || element.find('select')[0]);
    };


    return {
        restrict: 'E',
        priority: 100,        // We need this directive to happen before ng-model
        terminal: true,       // We are going to deal with this element
        require: '?^form',    // If we are in a form then we can access the ngModelController
        compile: function compile(element, attrs) {

            // Find all the <validator> child elements and extract their validation message info
            var validationMessages = [];
            angular.forEach(element.find('validator'), function (validatorElement) {
                validatorElement = angular.element(validatorElement);
                validationMessages.push({
                    key: validatorElement.attr('key'),
                    getMessage: $interpolate(validatorElement.text())
                });
            });

            // Find the content that will go into the new label
            var labelContent;
            if (element.attr('label')) {
                labelContent = element.attr('label');
                element[0].removeAttribute('label');
            }
            if (element.find('label')[0]) {
                labelContent = element.find('label').html();
            }
            else {
                var fullModelName = attrs.ngModel;
                var fieldName = fullModelName.substring(fullModelName.indexOf(".") + 1);

                labelContent = humanizer.humanize(fieldName);
            }

            // Load up the template for this kind of field
            var template = attrs.template || 'field';   // Default to the simple input if none given
            var getFieldElement = $http.get('/App/infrastructure/ui/' + template + '.template.html', { cache: $templateCache }).then(function (response) {
                var newElement = angular.element(response.data);
                var inputElement = findInputElement(newElement);

                // Copy over the attributes to the input element
                // At least the ng-model attribute must be copied because we can't use interpolation in the template<commit_msg>Refactoring.<commit_after>﻿// todo: why isn't _references.js working?
/// <reference path="/Scripts/angular.js" />

// original source http://plnkr.co/edit/3zMsNnpNfOFwExSqLj2I?p=preview which derived from https://github.com/angular-ui/angular-ui/pull/191.

'use strict';

var textfield = angular.module('field', ['services.humanizer'])

textfield.directive('field', function ($compile, $http, $templateCache, $interpolate, humanizer) {

    function findInputElement(element) {
        return angular.element(element.find('input')[0] || element.find('textarea')[0] || element.find('select')[0]);
    };

    function getLabelContent(element, ngModel) {

        if (element.attr('label')) {

            return labelContent = element.attr('label');
        }
        if (element.find('label')[0]) {

            return element.find('label').html();
        }
        else {

            var fullModelName = ngModel;
            var fieldName = fullModelName.substring(fullModelName.indexOf(".") + 1);
            var humanized = humanizer.humanize(fieldName);

            return humanized;
        }

    };

    function tryRemoveAttribute(element, name) {

        var attribute = element.attr(name);

        if (attribute === undefined) {
            return;
        }

        attribute.element[0].removeAttribute(name);
    };

    return {
        restrict: 'E',
        priority: 100,        // We need this directive to happen before ng-model
        terminal: true,       // We are going to deal with this element
        require: '?^form',    // If we are in a form then we can access the ngModelController
        compile: function compile(element, attrs) {

            // Find all the <validator> child elements and extract their validation message info
            var validationMessages = [];
            angular.forEach(element.find('validator'), function (validatorElement) {
                validatorElement = angular.element(validatorElement);
                validationMessages.push({
                    key: validatorElement.attr('key'),
                    getMessage: $interpolate(validatorElement.text())
                });
            });

            var labelContent = getLabelContent(element, attrs.ngModel);

            tryRemoveAttribute(element, 'label');

            // Load up the template for this kind of field
            var template = attrs.template || 'field';   // Default to the simple input if none given
            var getFieldElement = $http.get('/App/infrastructure/ui/' + template + '.template.html', { cache: $templateCache }).then(function (response) {
                var newElement = angular.element(response.data);
                var inputElement = findInputElement(newElement);

                // Copy over the attributes to the input element
                // At least the ng-model attribute must be copied because we can't use interpolation in the template
{'fraction_non_alphanumeric': 0.11515800749866095, 'fraction_numerical': 0.004999107302267452, 'mean_word_length': 2.268378063010502, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '6539465', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1385, 'n_words': 390}