<commit_before>﻿define('vm.speaker',
    ['ko', 'datacontext', 'config', 'messenger', 'sort', 'router'],
    function (ko, datacontext, config, messenger, sort, router) {

        var
            logger = config.logger,
            currentSpeakerId = ko.observable(),
            speaker = ko.observable(),
            speakerSessions = ko.observableArray(),
            
            tmplName = function() {
                return canEdit() ? 'speaker.edit' : 'speaker.view';
            },
            
            canEdit = ko.computed(function () {
                return speaker() && config.currentUser().id() === speaker().id();
            }),

            validationErrors = ko.observableArray([]), // Override this after we get a session

            isDirty = ko.computed(function () {
                if (canEdit()) {
                    return speaker().dirtyFlag().isDirty();
                }
                return false;
            }),

            goBack = ko.asyncCommand({
                execute: function (complete) {
                    router.navigateBack();
                    complete();
                },
                canExecute: function (isExecuting) {
                    return !isDirty();
                }
            }),

            cancel = ko.asyncCommand({
                execute: function (complete) {
                    var callback = function () {
                        complete();
                        logger.success('Refreshed');
                    };
                    getSpeaker(callback, true);
                },
                canExecute: function (isExecuting) {
                    return isDirty();
                }
            }),

            save = ko.asyncCommand({
                execute: function (complete) {
                    if (canEdit()) {
                        $.when(
                            datacontext.persons.updateData(
                                speaker(), {
                                    success: function () { },
                                    error: function () { }
                                }
                            )
                        ).always(function () {
                            complete();
                        });
                        return;
                    } else {
                        complete();
                    }
                },
                canExecute: function (isExecuting) {
                    return isDirty() && validationErrors().length === 0;
                }
            }),

            canLeave = function () {
                return !isDirty() && validationErrors().length === 0;
            },

            activate = function (routeData) {
                messenger.publish.viewModelActivated({ canleaveCallback: canLeave });

                currentSpeakerId(routeData.id);
                getSpeaker();
            },
            
            getSpeaker = function (completeCallback, forceRefresh) {
                var callback = function () {
                    if (completeCallback) {
                        completeCallback();
                    }
                    validationErrors = ko.validation.group(speaker());
                };

                datacontext.persons.getFullPersonById(
                    currentSpeakerId(), {
                        success: function (s) {
                            speaker(s);
                            // Cause the speakerSession computed to reevaluate
                            speaker().personRefresh.notifySubscribers();
                            //getLocalSpeakerSessions();
                            callback();
                        },
                        error: function () {
                            callback();
                        }
                    },
                    forceRefresh
                );
            },
            
            //getLocalSpeakerSessions = function () {
            //    // Cause the speakerSession computed to reevaluate
            //    speaker().personRefresh.notifySubscribers(); 
            //    var results = speaker().speakerSessions();
            //    results.sort(sort.speakerSessionSort);
            //    speakerSessions(results);
            //},

            init = function () {
            };

        // Initialization
        init();

        return {
            activate: activate,
            cancel: cancel,
            canEdit: canEdit,
            canLeave: canLeave,
            goBack: goBack,
            save: save,
            speaker: speaker,
            speakerSessions: speakerSessions,
            tmplName: tmplName,
            isDirty: isDirty
        };
    });<commit_msg>Fixed bug. If data did not pass validation on the speaker view, and the user could not edit the view, it still would not let them leave. Added logic to ignore validation when user cannot change data.<commit_after>﻿define('vm.speaker',
    ['ko', 'datacontext', 'config', 'messenger', 'sort', 'router'],
    function (ko, datacontext, config, messenger, sort, router) {

        var
            logger = config.logger,
            currentSpeakerId = ko.observable(),
            speaker = ko.observable(),
            speakerSessions = ko.observableArray(),
            
            tmplName = function() {
                return canEdit() ? 'speaker.edit' : 'speaker.view';
            },
            
            canEdit = ko.computed(function () {
                return speaker() && config.currentUser().id() === speaker().id();
            }),

            validationErrors = ko.observableArray([]), // Override this after we get a session

            isDirty = ko.computed(function () {
                if (canEdit()) {
                    return speaker().dirtyFlag().isDirty();
                }
                return false;
            }),

            goBack = ko.asyncCommand({
                execute: function (complete) {
                    router.navigateBack();
                    complete();
                },
                canExecute: function (isExecuting) {
                    return !isDirty();
                }
            }),

            cancel = ko.asyncCommand({
                execute: function (complete) {
                    var callback = function () {
                        complete();
                        logger.success('Refreshed');
                    };
                    getSpeaker(callback, true);
                },
                canExecute: function (isExecuting) {
                    return isDirty();
                }
            }),

            save = ko.asyncCommand({
                execute: function (complete) {
                    if (canEdit()) {
                        $.when(
                            datacontext.persons.updateData(
                                speaker(), {
                                    success: function () { },
                                    error: function () { }
                                }
                            )
                        ).always(function () {
                            complete();
                        });
                        return;
                    } else {
                        complete();
                    }
                },
                canExecute: function (isExecuting) {
                    return isDirty() && validationErrors().length === 0;
                }
            }),

            canLeave = function () {
                if (canEdit()) {
                    return !isDirty() && validationErrors().length === 0;
                } else {
                    return true;
                }
            },

            activate = function (routeData) {
                messenger.publish.viewModelActivated({ canleaveCallback: canLeave });

                currentSpeakerId(routeData.id);
                getSpeaker();
            },
            
            getSpeaker = function (completeCallback, forceRefresh) {
                var callback = function () {
                    if (completeCallback) {
                        completeCallback();
                    }
                    validationErrors = ko.validation.group(speaker());
                };

                datacontext.persons.getFullPersonById(
                    currentSpeakerId(), {
                        success: function (s) {
                            speaker(s);
                            // Cause the speakerSession computed to reevaluate
                            speaker().personRefresh.notifySubscribers();
                            //getLocalSpeakerSessions();
                            callback();
                        },
                        error: function () {
                            callback();
                        }
                    },
                    forceRefresh
                );
            },
            
            //getLocalSpeakerSessions = function () {
            //    // Cause the speakerSession computed to reevaluate
            //    speaker().personRefresh.notifySubscribers(); 
            //    var results = speaker().speakerSessions();
            //    results.sort(sort.speakerSessionSort);
            //    speakerSessions(results);
            //},

            init = function () {
            };

        // Initialization
        init();

        return {
            activate: activate,
            cancel: cancel,
            canEdit: canEdit,
            canLeave: canLeave,
            goBack: goBack,
            save: save,
            speaker: speaker,
            speakerSessions: speakerSessions,
            tmplName: tmplName,
            isDirty: isDirty
        };
    });
{'fraction_non_alphanumeric': 0.09804122654086761, 'fraction_numerical': 0.00041021433699107784, 'mean_word_length': 0.9507901580316063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5923306', 'n_tokens_mistral': 2147, 'n_tokens_neox': 1997, 'n_words': 424}