<commit_before>﻿///<reference path="../lib/jquery/dist/jquery.js" />
///<reference path="../lib/knockout/dist/knockout.js" />
(function (ko, $, tasks, skills, userSkills, isVolunteeredForActivity, signupModelSeed) {

    function RequiredSkill(userSkills, skill)
    {
        var obj = ko.utils.extend({}, skill);
        obj.IsUserSkill = ko.observable(userSkills && userSkills.length && userSkills.filter(function (userSkill) { return obj.Id === userSkill.Id }).length > 0);
        return obj;
    }

    function ActivityViewModel(tasks, skills, userSkills, signupModel) {
        var self = this;
        self.skills = ko.observableArray(skills.map(RequiredSkill.bind(null, userSkills)));
        self.unassociatedSkills = ko.computed(function () { return self.skills().filter(function (skill) { return !skill.IsUserSkill(); }) }); 
        tasks = tasks.map(function (task) { task.NotCompleteReason = ko.observable(); return task; });
        self.tasks = ko.observableArray(tasks).filterBeforeDate("EndDateTime").textFilter(["Name", "Description"]);
        self.enrolled = ko.observable(isVolunteeredForActivity);
        self.errorUnenrolling = ko.observable(false);

        self.signupForActivity = function() {
             HTBox.showModal({
                 viewModel: new SignupViewModel(signupModel, self.unassociatedSkills),
                 template: "VolunteerModal",
                 context: self
             }).then(activitySignupSuccess);
        };

        function activitySignupSuccess(signUpViewModel) {
            self.skills().forEach(function(skill) {
                if (signUpViewModel.AddSkillIds().indexOf(skill.Id) >= 0){ skill.IsUserSkill(true) }
            });
            self.enrolled(true);
            showalert("<strong>Thanks for volunteering! Your request has been processed and one of the activity coordinators will be in contact with you soon.</strong>", "alert-success", 30);
        }

        self.unenroll = function (activity) {
            // TODO: set up a spinner
            self.errorUnenrolling(false);
            $("#enrollUnenrollSpinner").show();
            $.ajax({
                type: "DELETE",
                url: '/api/activity/' + activity + '/signup',
                contentType: "application/json"
            }).then(function (data) {
                self.enrolled(false);
                $("#enrollUnenrollSpinner").hide();
                showalert("<strong>Thanks for your interest. Your request has been processed and you are no longer signed up for this activity We hope to see you soon!.</strong>", "alert-success", 30);
            }).fail(function (fail) {
                self.errorUnenrolling(true);
                console.log(fail);
            });
        }
    }

    function showalert(message, alerttype, timeoutInSecs) {
        $('#alert_placeholder').append('<div id="alertdiv" class="alert ' +  alerttype + ' fade in"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>')
        if (timeoutInSecs) {
            setTimeout(function() {
                $(".alert").alert('close');
            }, timeoutInSecs * 1000);
        };
    };

    function SignupViewModel (signupModelSeed, unassociatedSkills) {
        var self = this;
        ko.mapping.fromJS(signupModelSeed, {}, self);
        self.unassociatedSkills = unassociatedSkills;
        self.validationErrors = ko.observableArray([]);

        self.PreferredPhoneNumber.extend({
            validate: {
                required: "'Phone Number' is required",
                phoneNumber: ""
            }
        });

        self.PreferredEmail.extend({
            validate: {
                required: "'Email' is required",
                email: ""
            }
        });

        self.isValid = ko.computed(function () {
            var allValidatablesAreValid = true;
            for (var property in self) {
                if (self.hasOwnProperty(property) && self[property]["hasError"]) {
                    allValidatablesAreValid = !self[property].hasError();
                }
                if (!allValidatablesAreValid) break;
            }
            return allValidatablesAreValid;
        });

        self.submitForm = function() {
            // TODO: set up a spinner
            var dataToSend = ko.mapping.toJS(self);
            self.validationErrors([]);<commit_msg>use typeof self[property].hasError === "function"<commit_after>﻿///<reference path="../lib/jquery/dist/jquery.js" />
///<reference path="../lib/knockout/dist/knockout.js" />
(function (ko, $, tasks, skills, userSkills, isVolunteeredForActivity, signupModelSeed) {

    function RequiredSkill(userSkills, skill)
    {
        var obj = ko.utils.extend({}, skill);
        obj.IsUserSkill = ko.observable(userSkills && userSkills.length && userSkills.filter(function (userSkill) { return obj.Id === userSkill.Id }).length > 0);
        return obj;
    }

    function ActivityViewModel(tasks, skills, userSkills, signupModel) {
        var self = this;
        self.skills = ko.observableArray(skills.map(RequiredSkill.bind(null, userSkills)));
        self.unassociatedSkills = ko.computed(function () { return self.skills().filter(function (skill) { return !skill.IsUserSkill(); }) }); 
        tasks = tasks.map(function (task) { task.NotCompleteReason = ko.observable(); return task; });
        self.tasks = ko.observableArray(tasks).filterBeforeDate("EndDateTime").textFilter(["Name", "Description"]);
        self.enrolled = ko.observable(isVolunteeredForActivity);
        self.errorUnenrolling = ko.observable(false);

        self.signupForActivity = function() {
             HTBox.showModal({
                 viewModel: new SignupViewModel(signupModel, self.unassociatedSkills),
                 template: "VolunteerModal",
                 context: self
             }).then(activitySignupSuccess);
        };

        function activitySignupSuccess(signUpViewModel) {
            self.skills().forEach(function(skill) {
                if (signUpViewModel.AddSkillIds().indexOf(skill.Id) >= 0){ skill.IsUserSkill(true) }
            });
            self.enrolled(true);
            showalert("<strong>Thanks for volunteering! Your request has been processed and one of the activity coordinators will be in contact with you soon.</strong>", "alert-success", 30);
        }

        self.unenroll = function (activity) {
            // TODO: set up a spinner
            self.errorUnenrolling(false);
            $("#enrollUnenrollSpinner").show();
            $.ajax({
                type: "DELETE",
                url: '/api/activity/' + activity + '/signup',
                contentType: "application/json"
            }).then(function (data) {
                self.enrolled(false);
                $("#enrollUnenrollSpinner").hide();
                showalert("<strong>Thanks for your interest. Your request has been processed and you are no longer signed up for this activity We hope to see you soon!.</strong>", "alert-success", 30);
            }).fail(function (fail) {
                self.errorUnenrolling(true);
                console.log(fail);
            });
        }
    }

    function showalert(message, alerttype, timeoutInSecs) {
        $('#alert_placeholder').append('<div id="alertdiv" class="alert ' +  alerttype + ' fade in"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>')
        if (timeoutInSecs) {
            setTimeout(function() {
                $(".alert").alert('close');
            }, timeoutInSecs * 1000);
        };
    };

    function SignupViewModel (signupModelSeed, unassociatedSkills) {
        var self = this;
        ko.mapping.fromJS(signupModelSeed, {}, self);
        self.unassociatedSkills = unassociatedSkills;
        self.validationErrors = ko.observableArray([]);

        self.PreferredPhoneNumber.extend({
            validate: {
                required: "'Phone Number' is required",
                phoneNumber: ""
            }
        });

        self.PreferredEmail.extend({
            validate: {
                required: "'Email' is required",
                email: ""
            }
        });

        self.isValid = ko.computed(function () {
            var allValidatablesAreValid = true;
            for (var property in self) {
                if (self.hasOwnProperty(property) && typeof self[property].hasError === "function") {
                    allValidatablesAreValid = !self[property].hasError();
                }
                if (!allValidatablesAreValid) break;
            }
            return allValidatablesAreValid;
        });

        self.submitForm = function() {
            // TODO: set up a spinner
            var dataToSend = ko.mapping.toJS(self);
            self.validationErrors([]);
{'fraction_non_alphanumeric': 0.1316536147996822, 'fraction_numerical': 0.0022698899103393484, 'mean_word_length': 2.1904417089065893, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, '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': '6433877', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2334, 'n_words': 476}