<commit_before>/*
 * Copyright (c) 2013-2016 LabKey Corporation
 *
 * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
 */
/**
 * This field is used to display EHR projects.  It contains a custom template for the combo list which displays both the project and protocol.
 * It also listens for participantchange events and will display only the set of allowable projects for the selected animal.
 *
 * @cfg includeDefaultProjects defaults to true
 */
Ext4.define('WNPRC.form.field.PregnancyIdField', {
    extend: 'Ext.form.field.ComboBox',
    alias: 'widget.wnprc-pregnancyidfield',

    fieldLabel: 'pregnancyid',
    typeAhead: true,
    forceSelection: true, //NOTE: has been re-enabled, but it is important to let the field get set prior to loading
    emptyText:'',
    disabled: false,
    matchFieldWidth: false,
    includeDefaultProjects: true,

    initComponent: function(){
        var ctx = EHR.Utils.getEHRContext();

        this.allPregnancyStore = new LABKEY.ext4.data.Store({
            type: 'labkey-store',
            containerPath: ctx ? ctx['EHRStudyContainer'] : null,
            schemaName: 'study',
            queryName: 'pregnancies',
            columns: 'lsid,date_conception_early',
            //filterArray: [LABKEY.Filter.create('enddate', null, LABKEY.Filter.Types.ISBLANK)],
            sort: 'date_conception_early',
            storeId: ['study', 'pregnancies', 'Id', 'lsid'].join('||'),
            autoLoad: true
        });

        this.trigger2Cls = Ext4.form.field.ComboBox.prototype.triggerCls;
        this.onTrigger2Click = Ext4.form.field.ComboBox.prototype.onTriggerClick;

        Ext4.apply(this, {
            displayField: 'date_conception_early',
            valueField: 'lsid',
            queryMode: 'local',
            validationDelay: 500,
            //NOTE: unless i have this empty store an error is thrown
            store: {
                type: 'labkey-store',
                schemaName: 'study',
                sql: this.makeSql(),
                sort: 'date_conception_early',
                autoLoad: false,
                loading: true,
                listeners: {
                    scope: this,
                    delay: 50,
                    load: function(store){
                        this.resolvePregnancyFromStore();
                        this.getPicker().refresh();
                    }
                }
            },
            listeners: {
                scope: this,
                beforerender: function(field){
                    var target = field.up('form');
                    if (!target)
                        target = field.up('grid');

                    LDK.Assert.assertNotEmpty('Unable to find form or grid', target);
                    if (target) {
                        field.mon(target, 'animalchange', field.getPregnancies, field);
                    }
                    else {
                        console.error('Unable to find target');
                    }

                    //attempt to load for the bound Id
                    this.getPregnancies();
                }
            }
        });

        this.listConfig = this.listConfig || {};
        Ext4.apply(this.listConfig, {
            innerTpl: this.getInnerTpl(),
            getInnerTpl: function(){
                return this.innerTpl;
            },
            style: 'border-top-width: 1px;' //this was added in order to restore the border above the boundList if it is wider than the field
        });

        this.callParent(arguments);

        // this.addEvents('projectchange');
        // this.enableBubble('projectchange');
        //
        // this.on('change', function(field, val, oldVal){
        //     this.fireEvent('projectchange', val);
        // }, this, {buffer: 200});
        //
        // this.on('render', function(){
        //     Ext4.QuickTips.register({
        //         target: this.triggerEl.elements[0],
        //         text: 'Click to recalculate allowable projects'
        //     });
        // }, this);
    },

    getInnerTpl: function(){
        //return ['{[values["displayName"] + " " + (values["shortname"] ? ("(" + values["shortname"] + ")") : (values["investigator"] ? "(" + (values["investigator"] ? values["investigator"] : "") : "") + (values["account"] ? ": " + values["account"] : "") + (values["investigator"] ? ")" : ""))]}&nbsp;</span>'];
        return '{[values["date_conception_early"]]}';
    },

    /*<span style="white-space:nowrap;{[values["isAssigned"] ? "font-weight:bold;" : ""]}">*/

    trigger1Cls: 'x4-form-search-trigger',

    onTrigger1Click: function(){
        var boundRecord = EHR.DataEntryUtils.getBoundRecord(this);
        if (!boundRecord){
            Ext4.Msg.alert('Error', 'Unable to locate associated animal Id');
            return;
        }

        var id = boundRecord.get('Id');
        if (!id){
            Ext4.Msg.alert('Error', 'No Animal Id Provided');
            return;
        }

        this.getPregnancies(id);
    },

    getDisallowedProtocols: function(){
        return null;
    },

    makeSql: function(id){
        if (!id && !this.includeDefaultProjects)
            return;

        //avoid unnecessary reloading
        var key = id;
        if (this.loadedKey == key){
            return;
        }
        this.loadedKey = key;

        var sql = 'select lsid,date_conception_early \
               from (select p.lsid \
                           ,p.date_conception_early \
                       from pregnancies p \
                      where p.Id = \'' + id + '\' \
                        and not exists (select * \
                                          from pregnancy_outcomes po \
                                         where po.pregnancyid = p.lsid) \
                                      order by date_conception_early desc) \
              current_pregnancies limit 1'.replace(/\s+/g, ' ');
<commit_msg>changed date formatting for pregnancyid field<commit_after>/*
 * Copyright (c) 2013-2016 LabKey Corporation
 *
 * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
 */
/**
 * This field is used to display EHR projects.  It contains a custom template for the combo list which displays both the project and protocol.
 * It also listens for participantchange events and will display only the set of allowable projects for the selected animal.
 *
 * @cfg includeDefaultProjects defaults to true
 */
Ext4.define('WNPRC.form.field.PregnancyIdField', {
    extend: 'Ext.form.field.ComboBox',
    alias: 'widget.wnprc-pregnancyidfield',

    fieldLabel: 'pregnancyid',
    typeAhead: true,
    forceSelection: true, //NOTE: has been re-enabled, but it is important to let the field get set prior to loading
    emptyText:'',
    disabled: false,
    matchFieldWidth: false,
    includeDefaultProjects: true,

    initComponent: function(){
        var ctx = EHR.Utils.getEHRContext();

        this.allPregnancyStore = new LABKEY.ext4.data.Store({
            type: 'labkey-store',
            containerPath: ctx ? ctx['EHRStudyContainer'] : null,
            schemaName: 'study',
            queryName: 'pregnancies',
            columns: 'lsid,date_conception_early',
            //filterArray: [LABKEY.Filter.create('enddate', null, LABKEY.Filter.Types.ISBLANK)],
            sort: '-date_conception_early',
            storeId: ['study', 'pregnancies', 'Id', 'lsid'].join('||'),
            autoLoad: true
        });

        this.trigger2Cls = Ext4.form.field.ComboBox.prototype.triggerCls;
        this.onTrigger2Click = Ext4.form.field.ComboBox.prototype.onTriggerClick;

        Ext4.apply(this, {
            displayField: 'date_conception_early',
            valueField: 'lsid',
            queryMode: 'local',
            validationDelay: 500,
            //NOTE: unless i have this empty store an error is thrown
            store: {
                type: 'labkey-store',
                schemaName: 'study',
                sql: this.makeSql(),
                sort: '-date_conception_early',
                autoLoad: false,
                loading: true,
                listeners: {
                    scope: this,
                    delay: 50,
                    load: function(store){
                        this.resolvePregnancyFromStore();
                        this.getPicker().refresh();
                    }
                }
            },
            listeners: {
                scope: this,
                beforerender: function(field){
                    var target = field.up('form');
                    if (!target)
                        target = field.up('grid');

                    LDK.Assert.assertNotEmpty('Unable to find form or grid', target);
                    if (target) {
                        field.mon(target, 'animalchange', field.getPregnancies, field);
                    }
                    else {
                        console.error('Unable to find target');
                    }

                    //attempt to load for the bound Id
                    this.getPregnancies();
                }
            }
        });

        this.listConfig = this.listConfig || {};
        Ext4.apply(this.listConfig, {
            innerTpl: this.getInnerTpl(),
            getInnerTpl: function(){
                return this.innerTpl;
            },
            style: 'border-top-width: 1px;' //this was added in order to restore the border above the boundList if it is wider than the field
        });

        this.callParent(arguments);

        // this.addEvents('projectchange');
        // this.enableBubble('projectchange');
        //
        // this.on('change', function(field, val, oldVal){
        //     this.fireEvent('projectchange', val);
        // }, this, {buffer: 200});
        //
        // this.on('render', function(){
        //     Ext4.QuickTips.register({
        //         target: this.triggerEl.elements[0],
        //         text: 'Click to recalculate allowable projects'
        //     });
        // }, this);
    },

    getInnerTpl: function(){
        //return ['{[values["displayName"] + " " + (values["shortname"] ? ("(" + values["shortname"] + ")") : (values["investigator"] ? "(" + (values["investigator"] ? values["investigator"] : "") : "") + (values["account"] ? ": " + values["account"] : "") + (values["investigator"] ? ")" : ""))]}&nbsp;</span>'];
        return ['{[values["date_conception_early"]]}'];
    },

    /*<span style="white-space:nowrap;{[values["isAssigned"] ? "font-weight:bold;" : ""]}">*/

    trigger1Cls: 'x4-form-search-trigger',

    onTrigger1Click: function(){
        var boundRecord = EHR.DataEntryUtils.getBoundRecord(this);
        if (!boundRecord){
            Ext4.Msg.alert('Error', 'Unable to locate associated animal Id');
            return;
        }

        var id = boundRecord.get('Id');
        if (!id){
            Ext4.Msg.alert('Error', 'No Animal Id Provided');
            return;
        }

        this.getPregnancies(id);
    },

    getDisallowedProtocols: function(){
        return null;
    },

    makeSql: function(id){
        if (!id && !this.includeDefaultProjects)
            return;

        //avoid unnecessary reloading
        var key = id;
        if (this.loadedKey == key){
            return;
        }
        this.loadedKey = key;

        var sql = 'select lsid,to_char(date_conception_early, \'Mon DD, YYYY HH24:MI\') as date_conception_early \
               from (select p.lsid \
                           ,p.date_conception_early \
                       from pregnancies p \
                      where p.Id = \'' + id + '\' \
                        and not exists (select * \
                                          from pregnancy_outcomes po \
                                         where po.pregnancyid = p.lsid) \
                                      order by date_conception_early desc) \
              current_pregnancies limit 1'.replace(/\s+/g, ' ');

{'fraction_non_alphanumeric': 0.12586891757696128, 'fraction_numerical': 0.006289308176100629, 'mean_word_length': 1.650219298245614, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5046435', 'n_tokens_mistral': 3383, 'n_tokens_neox': 3133, 'n_words': 749}