<commit_before>genericModule = angular.module('greenmine.directives.generic', [])


versionConstructor = (version) -> (scope, elm, attrs) ->
    elm.text(version)

genericModule.directive('appVersion', ['version', versionConstructor])

# Select2 Directive
select2Constructor = ->
    require: "?ngModel"
    restrict: "A"
    link: (scope, elm, attrs, ngModel) ->
        element = angular.element(elm)

        ngModel.$render = ->
            if ngModel.$modelValue
                element.val(ngModel.$modelValue.join(","))

            element.select2({tags:[], tokenSeparators: [",", " "], triggerChange:true})

        element.on 'change', ->
            ngModel.$setViewValue(arguments[0].val)
            scope.$digest()

genericModule.directive('uiSelect2', select2Constructor)


uiEventConstructor = ($parse) -> (scope, elm, attrs) ->
    events = scope.$eval(attrs.uiEvent)
    angular.forEach events,  (uiEvent, eventName) ->
        fn = $parse(uiEvent)

        elm.bind eventName,  (evt) ->
            params = Array.prototype.slice.call(arguments)
            params = params.splice(1)
            scope.$apply ->
                fn(scope, {$event: evt, $params: params})

genericModule.directive('uiEvent', ['$parse', uiEventConstructor])


uiParsleyConstructor = ($parse, $http, url) -> (scope, elm, attrs) ->
    fn = $parse(attrs.uiParsley)

    onFormSubmit = (valid, event, form) ->
        return if not valid

        scope.$apply ->
            fn(scope, {$event:event})

    element = $(elm)
    element.parsley(
        listeners: {onFormSubmit: onFormSubmit}
        validators:
            remoteuserverify: (val, opt, self) ->
                result = null

                manage = (ok) ->
                    return ->
                        constraint = _.find(self.constraints, {name: "remoteuserverify"})

                        if constraint
                            constraint.isValid = ok
                            self.isValid = null
                            self.manageValidationResult()

                finalUrl = url("user") + "?" + jQuery.param({"username": val})
                $http.head(finalUrl).success(manage(false)).error(manage(true))
                return result

        messages:
            remoteuserverify: "Username taken"
    )


genericModule.directive('uiParsley', ['$parse', '$http', 'url', uiParsleyConstructor])


gmFileConstructor = ($parse) ->
    require: "?ngModel",
    restrict: "A",
    link: (scope, elm, attrs, ngModel) ->
        element = angular.element(elm)
        element.on 'change', (event) ->
            files = event.target.files
            if files.length == 1
                scope.$apply ->
                    ngModel.$setViewValue(files[0])

genericModule.directive('gmFile', ["$parse", gmFileConstructor]);<commit_msg>Refactor generic directive code structure.<commit_after>

AppVersionDirective = (version) -> (scope, elm, attrs) ->
    elm.text(version)


UiSelect2Directive = ->
    require: "?ngModel"
    restrict: "A"
    link: (scope, elm, attrs, ngModel) ->
        element = angular.element(elm)

        ngModel.$render = ->
            if ngModel.$modelValue
                element.val(ngModel.$modelValue.join(","))

            element.select2({tags:[], tokenSeparators: [",", " "], triggerChange:true})

        element.on 'change', ->
            ngModel.$setViewValue(arguments[0].val)
            scope.$digest()


UiEventDirective = ($parse) -> (scope, elm, attrs) ->
    events = scope.$eval(attrs.uiEvent)
    angular.forEach events,  (uiEvent, eventName) ->
        fn = $parse(uiEvent)

        elm.bind eventName,  (evt) ->
            params = Array.prototype.slice.call(arguments)
            params = params.splice(1)
            scope.$apply ->
                fn(scope, {$event: evt, $params: params})


UiParsleyDirective = ($parse, $http, url) -> (scope, elm, attrs) ->
    fn = $parse(attrs.uiParsley)

    onFormSubmit = (valid, event, form) ->
        return if not valid

        scope.$apply ->
            fn(scope, {$event:event})

    element = $(elm)
    element.parsley
        listeners: {onFormSubmit: onFormSubmit}
        validators:
            remoteuserverify: (val, opt, self) ->
                result = null

                manage = (ok) ->
                    return ->
                        constraint = _.find(self.constraints, {name: "remoteuserverify"})

                        if constraint
                            constraint.isValid = ok
                            self.isValid = null
                            self.manageValidationResult()

                finalUrl = url("user") + "?" + jQuery.param({"username": val})
                $http.head(finalUrl).success(manage(false)).error(manage(true))
                return result

        messages:
            remoteuserverify: "Username taken"


GmFileDirective = ($parse) ->
    require: "?ngModel",
    restrict: "A",
    link: (scope, elm, attrs, ngModel) ->
        element = angular.element(elm)
        element.on 'change', (event) ->
            files = event.target.files
            if files.length == 1
                scope.$apply ->
                    ngModel.$setViewValue(files[0])


module = angular.module('greenmine.directives.generic', [])
module.directive('appVersion', ['version', AppVersionDirective])
module.directive('uiSelect2', UiSelect2Directive)
module.directive('uiEvent', ['$parse', UiEventDirective])
module.directive('uiParsley', ['$parse', '$http', 'url', UiParsleyDirective])
module.directive('gmFile', ["$parse", GmFileDirective])
{'fraction_non_alphanumeric': 0.14109442060085836, 'fraction_numerical': 0.003040057224606581, 'mean_word_length': 2.1020521353300055, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 47, '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': '1063876', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1552, 'n_words': 314}