<commit_before>(function() {
    var tplAssessmentClass,
        tplSchool,
        tplBoundary,
        tplAssessmentOverall;
    klp.init = function() {
        console.log("init programme page", PROGRAMME_ID);
        klp.router = new KLPRouter();
        klp.router.init();
        initializeSelect2();
        tplAssessmentClass = swig.compile($('#tpl-assessment-class').html());
        tplSchool = swig.compile($('#tpl-school').html());
        tplBoundary = swig.compile($('#tpl-boundary').html());
        tplAssessmentOverall = swig.compile($('#tpl-assessment-overall').html());
        //fetchProgrammeDetails();
        klp.router.events.on('hashchange', function(event, params) {
            fetchProgrammeDetails();
        });
        klp.router.start();
    };

    function initializeSelect2() {
        $('#select2search').select2({
            placeholder: 'Search for schools and boundaries',
            minimumInputLength: 3,
            ajax: {
                url: "/api/v1/search",
                quietMillis: 300,
                allowClear: true,
                data: function (term, page) {
                    return {
                        text: term,
                        geometry: 'yes'
                    };
                },
                results: function (data, page) {
                    var searchResponse = {
                        results: [
                        {
                            text: "Pre-Schools",
                            children: makeResults(data.pre_schools.features, 'school')
                        },
                        {
                            text: "Primary Schools",
                            children: makeResults(data.primary_schools.features, 'school')
                        },
                        {
                            text: "Boundaries",
                            children: makeResults(data.boundaries.features, 'boundary')
                        }
                        ]
                    };
                    return {results: searchResponse.results};
                }
            }
        });

        $('#select2search').on("change", function(choice) {
            console.log(choice);
            var objectId = choice.added.data.properties.id;
            var entityType = choice.added.data.entity_type;
            var boundaryType = choice.added.data.properties.type;
            if (entityType === 'school') {
                var paramKey = entityType;
            } else if (boundaryType == 'district') {
                var paramKey = 'admin_1';
            } else if (['project', 'block'].indexOf(boundaryType) !== -1) {
                var paramKey = 'admin_2';
            } else if (['circle', 'cluster'].indexOf(boundaryType) !== -1) {
                var paramKey = 'admin_3';
            } else {
                var paramKey = 'error';
            }
            var hashParams = {
                'school': null,
                'admin1': null,
                'admin2': null,
                'admin3': null
            };
            hashParams[paramKey] = objectId;
            klp.router.setHash(null, hashParams);
        });
    }

    function makeResults(array, type) {
        var schoolDistrictMap = {
            'primaryschool': 'Primary School',
            'preschool': 'Preschool'
        };
        return _(array).map(function(obj) {
            var name = obj.properties.name;
            if (type === 'boundary') {
                if (obj.properties.type === 'district') {
                    name = obj.properties.name + ' - ' + schoolDistrictMap[obj.properties.school_type] + ' ' + obj.properties.type;
                } else {
                    name = obj.properties.name + ' - ' + obj.properties.type;
                }
            }

            obj.entity_type = type;
            return {
                id: obj.properties.id,
                text: _.str.titleize(name),
                data: obj
            };
        });
    }


    function fetchProgrammeDetails() {
        var params = klp.router.getHash().queryParams;
        var url = "programme/" + PROGRAMME_ID;
        var $xhr = klp.api.do(url, params);
        $('#assessmentsContainer').empty().text("Class Scores Loading...");
        $('#overallContainer').empty().text("Overall Scores Loading...");
        $('#entityDetails').empty();
        fetchEntityDetails(params);
        
        $xhr.done(function(data) {
            var assessmentsContext = getContext(data);
            //console.log("ass context", assessmentsContext);
            renderAssessments(assessmentsContext);
            var programme_html = "<ul id='improved' class='js-accordion-container'>" 
                    + $('#assessmentsContainer').html() + "</ul>";
            $('#assessmentsContainer').html(programme_html);
            fetchPercentile();
            klp.accordion.init();
            doPostRender();
        });
        
    }

    function fetchEntityDetails(params) {
        var entityType = _.keys(params)[0];
        if (entityType === 'school') {
            fetchSchoolDetails(params.school)
        } else {
            fetchBoundaryDetails(params[entityType]);
        }
    }

    function fetchSchoolDetails(id) {
        //console.log("school", id);
        var url = "schools/school/" + id;
        var $schoolXHR = klp.api.do(url, {});
        $schoolXHR.done(function(data) {
            var context = getSchoolContext(data);
            var html = tplSchool(data);
            $('#entityDetails').html(html);
        });
    }

    function getSchoolContext(data) {
        //console.log("school", data);
        if (data.type.id === 1) {
            data.type_name = 'school';
        } else {
            data.type_name = 'preschool';
        }
        return data;
    }

    function fetchBoundaryDetails(id) {
        var url = "boundary/admin/" + id;
        var $boundaryXHR = klp.api.do(url, {});
        $boundaryXHR.done(function(data) {
            var html = tplBoundary(data);
            $('#entityDetails').html(html);
        });
    }

    function doPostRender() {
        var typeMap = {
            'admin1': 'admin_1',
            'admin2': 'admin_2',
            'admin3': 'admin_3'
        };

        $('.js-boundaryLink').click(function(e) {
            console.log("clicked");
            e.preventDefault();
            var boundaryType = $(this).attr("data-type");
            var boundaryId = $(this).attr("data-id");
            var paramKey = typeMap[boundaryType];
            var hashParams = {
                'school': null,
                'admin_1': null,
                'admin_2': null,
                'admin_3': null<commit_msg>fix event delegation for dynamically added elements<commit_after>(function() {
    var tplAssessmentClass,
        tplSchool,
        tplBoundary,
        tplAssessmentOverall;
    klp.init = function() {
        console.log("init programme page", PROGRAMME_ID);
        klp.router = new KLPRouter();
        klp.router.init();
        initializeSelect2();
        tplAssessmentClass = swig.compile($('#tpl-assessment-class').html());
        tplSchool = swig.compile($('#tpl-school').html());
        tplBoundary = swig.compile($('#tpl-boundary').html());
        tplAssessmentOverall = swig.compile($('#tpl-assessment-overall').html());
        //fetchProgrammeDetails();
        klp.router.events.on('hashchange', function(event, params) {
            fetchProgrammeDetails();
        });
        klp.router.start();
    };

    function initializeSelect2() {
        $('#select2search').select2({
            placeholder: 'Search for schools and boundaries',
            minimumInputLength: 3,
            ajax: {
                url: "/api/v1/search",
                quietMillis: 300,
                allowClear: true,
                data: function (term, page) {
                    return {
                        text: term,
                        geometry: 'yes'
                    };
                },
                results: function (data, page) {
                    var searchResponse = {
                        results: [
                        {
                            text: "Pre-Schools",
                            children: makeResults(data.pre_schools.features, 'school')
                        },
                        {
                            text: "Primary Schools",
                            children: makeResults(data.primary_schools.features, 'school')
                        },
                        {
                            text: "Boundaries",
                            children: makeResults(data.boundaries.features, 'boundary')
                        }
                        ]
                    };
                    return {results: searchResponse.results};
                }
            }
        });

        $('#select2search').on("change", function(choice) {
            console.log(choice);
            var objectId = choice.added.data.properties.id;
            var entityType = choice.added.data.entity_type;
            var boundaryType = choice.added.data.properties.type;
            if (entityType === 'school') {
                var paramKey = entityType;
            } else if (boundaryType == 'district') {
                var paramKey = 'admin_1';
            } else if (['project', 'block'].indexOf(boundaryType) !== -1) {
                var paramKey = 'admin_2';
            } else if (['circle', 'cluster'].indexOf(boundaryType) !== -1) {
                var paramKey = 'admin_3';
            } else {
                var paramKey = 'error';
            }
            var hashParams = {
                'school': null,
                'admin1': null,
                'admin2': null,
                'admin3': null
            };
            hashParams[paramKey] = objectId;
            klp.router.setHash(null, hashParams);
        });
    }

    function makeResults(array, type) {
        var schoolDistrictMap = {
            'primaryschool': 'Primary School',
            'preschool': 'Preschool'
        };
        return _(array).map(function(obj) {
            var name = obj.properties.name;
            if (type === 'boundary') {
                if (obj.properties.type === 'district') {
                    name = obj.properties.name + ' - ' + schoolDistrictMap[obj.properties.school_type] + ' ' + obj.properties.type;
                } else {
                    name = obj.properties.name + ' - ' + obj.properties.type;
                }
            }

            obj.entity_type = type;
            return {
                id: obj.properties.id,
                text: _.str.titleize(name),
                data: obj
            };
        });
    }


    function fetchProgrammeDetails() {
        var params = klp.router.getHash().queryParams;
        var url = "programme/" + PROGRAMME_ID;
        var $xhr = klp.api.do(url, params);
        $('#assessmentsContainer').empty().text("Class Scores Loading...");
        $('#overallContainer').empty().text("Overall Scores Loading...");
        $('#entityDetails').empty();
        fetchEntityDetails(params);
        
        $xhr.done(function(data) {
            var assessmentsContext = getContext(data);
            //console.log("ass context", assessmentsContext);
            renderAssessments(assessmentsContext);
            var programme_html = "<ul id='improved' class='js-accordion-container'>" 
                    + $('#assessmentsContainer').html() + "</ul>";
            $('#assessmentsContainer').html(programme_html);
            fetchPercentile();
            klp.accordion.init();
            doPostRender();
        });
        
    }

    function fetchEntityDetails(params) {
        var entityType = _.keys(params)[0];
        if (entityType === 'school') {
            fetchSchoolDetails(params.school)
        } else {
            fetchBoundaryDetails(params[entityType]);
        }
    }

    function fetchSchoolDetails(id) {
        //console.log("school", id);
        var url = "schools/school/" + id;
        var $schoolXHR = klp.api.do(url, {});
        $schoolXHR.done(function(data) {
            var context = getSchoolContext(data);
            var html = tplSchool(data);
            $('#entityDetails').html(html);
        });
    }

    function getSchoolContext(data) {
        //console.log("school", data);
        if (data.type.id === 1) {
            data.type_name = 'school';
        } else {
            data.type_name = 'preschool';
        }
        return data;
    }

    function fetchBoundaryDetails(id) {
        var url = "boundary/admin/" + id;
        var $boundaryXHR = klp.api.do(url, {});
        $boundaryXHR.done(function(data) {
            var html = tplBoundary(data);
            $('#entityDetails').html(html);
        });
    }

    function doPostRender() {
        var typeMap = {
            'admin1': 'admin_1',
            'admin2': 'admin_2',
            'admin3': 'admin_3'
        };

        $('body').on('click', '.js-boundaryLink', function() {            
            console.log("clicked");
            e.preventDefault();
            var boundaryType = $(this).attr("data-type");
            var boundaryId = $(this).attr("data-id");
            var paramKey = typeMap[boundaryType];
            var hashParams = {
                'school': null,
                'admin_1': null,
                'admin_2': null,
                'admin_3': null
{'fraction_non_alphanumeric': 0.13213836012147248, 'fraction_numerical': 0.004295978075698096, 'mean_word_length': 1.4446858591345284, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3084416', 'n_tokens_mistral': 3627, 'n_tokens_neox': 3446, 'n_words': 629}