<commit_before>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
({
	PANELS_OWNER    : {},            // Owner relationship who creates the panel (key) owned by -> value
    PANELS_STACK    : [],            // The Panel Stack ordering
    PANELS_INSTANCE : {},            // Registered instances
    containerManager: null,          // a reference to containerManager
    hasLocationChangeHandler: false, // Indicate if we already attach a locationChange handler

	initialize: function(cmp) {
        var containerManager = this.cmLib.containerManager;
        var sharedContainer  = cmp.get('v.useSharedContainer');

        this.containerManager = sharedContainer ? containerManager.getSharedInstance() : containerManager.createInstance(cmp.find('container'));
        this.initializeRegisteredPanels(cmp);
    },

    /*
    * Store internal defs
    * @private
    */
    initializeRegisteredPanels: function (cmp, newPanels) {
        this.containerManager.registerContainers(newPanels || cmp.get('v.registeredPanels') || []);
    },
    
    /*
     * Dynamically register panels. Used to register feature specific panels.
     * @private
     */
    registerPanels: function(cmp, params) {
    	var panels = params.panels;
    	if (panels) {
    		this.containerManager.registerContainers(panels);
    		if ($A.util.isFunction(params.callback)) {
    			params.callback();
            }
    	}
    },

    /*
    * Create panel
    * @public
    */
    createPanel: function (cmp, config) {
        var panelConfig      = config.panelConfig || {};
        var referenceElement = panelConfig.referenceElement;

        panelConfig.referenceElement = null;

        // Create panel instance
        var panel = this.createPanelInstance(cmp, config);

        if (referenceElement) {
            panel.set('v.referenceElement', referenceElement);
        }

        
        // Save instance config
        this.PANELS_INSTANCE[panel.getGlobalId()] = {
            panel           : panel,
            destroyCallback : config.onDestroy,
            closeOnLocationChange: this.getLocationChangeFlag(cmp, config)
        };

        // Set owner
        this.setPanelOwner(panel, config.owner);

        // Render
        this.renderPanelInstance(cmp, panel, config);

        // Stack panel
        this.stackPanel(panel);

        if (config.onCreate) {
            config.onCreate(panel);
        }
    },
    /*
    * Get Active panel
    * @public
    */
    getActivePanel: function (cmp, callback) {
        // TODO: Instead of assuming is the last one (which doesnt guarantee that is "active")
        // Change the logic on active to make sure we save that state internally
        var stack = this.PANELS_STACK;
        var panel = stack[stack.length - 1];
        if (panel && $A.util.isFunction(callback)) {
            callback(panel);
        } else if($A.util.isFunction(callback)) {
            callback(null);
        }
    },
    /*
    * Sets the context in which the panel is created
    * This is mostly to figure out the the relationship between two panels
    * This function should be called before stacking a new panel
    */
    setPanelOwner: function (panel, givenOwner) {
        var owner = givenOwner;
        if (!owner) {
            var previousBody = null;
        	if (this.PANELS_STACK.length > 0) {
                var previousPanel = this.PANELS_STACK[this.PANELS_STACK.length - 1];
                previousBody = previousPanel && previousPanel.get('v.body');
        	}
            owner = $A.util.isEmpty(previousBody) ? panel.getGlobalId() : previousBody[0].getGlobalId();
        }

        this.PANELS_OWNER[panel.getGlobalId()] = owner;
    },

    /*
    * Stack a panel in our internal structures
    */<commit_msg>Null check for gack in panels. @bug W-3348215@ @rev stephen.woods@<commit_after>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
({
	PANELS_OWNER    : {},            // Owner relationship who creates the panel (key) owned by -> value
    PANELS_STACK    : [],            // The Panel Stack ordering
    PANELS_INSTANCE : {},            // Registered instances
    containerManager: null,          // a reference to containerManager
    hasLocationChangeHandler: false, // Indicate if we already attach a locationChange handler

	initialize: function(cmp) {
        var containerManager = this.cmLib.containerManager;
        var sharedContainer  = cmp.get('v.useSharedContainer');

        this.containerManager = sharedContainer ? containerManager.getSharedInstance() : containerManager.createInstance(cmp.find('container'));
        this.initializeRegisteredPanels(cmp);
    },

    /*
    * Store internal defs
    * @private
    */
    initializeRegisteredPanels: function (cmp, newPanels) {
        this.containerManager.registerContainers(newPanels || cmp.get('v.registeredPanels') || []);
    },
    
    /*
     * Dynamically register panels. Used to register feature specific panels.
     * @private
     */
    registerPanels: function(cmp, params) {
    	var panels = params.panels;
    	if (panels) {
    		this.containerManager.registerContainers(panels);
    		if ($A.util.isFunction(params.callback)) {
    			params.callback();
            }
    	}
    },

    /*
    * Create panel
    * @public
    */
    createPanel: function (cmp, config) {
        var panelConfig      = config.panelConfig || {};
        var referenceElement = panelConfig.referenceElement;

        panelConfig.referenceElement = null;

        // Create panel instance
        var panel = this.createPanelInstance(cmp, config);

        if (referenceElement) {
            panel.set('v.referenceElement', referenceElement);
        }

        
        // Save instance config
        this.PANELS_INSTANCE[panel.getGlobalId()] = {
            panel           : panel,
            destroyCallback : config.onDestroy,
            closeOnLocationChange: this.getLocationChangeFlag(cmp, config)
        };

        // Set owner
        this.setPanelOwner(panel, config.owner);

        // Render
        this.renderPanelInstance(cmp, panel, config);

        // Stack panel
        this.stackPanel(panel);

        if (config.onCreate) {
            config.onCreate(panel);
        }
    },
    /*
    * Get Active panel
    * @public
    */
    getActivePanel: function (cmp, callback) {
        // TODO: Instead of assuming is the last one (which doesnt guarantee that is "active")
        // Change the logic on active to make sure we save that state internally
        var stack = this.PANELS_STACK;
        var panel = stack[stack.length - 1];
        if (panel && $A.util.isFunction(callback)) {
            callback(panel);
        } else if($A.util.isFunction(callback)) {
            callback(null);
        }
    },
    /*
    * Sets the context in which the panel is created
    * This is mostly to figure out the the relationship between two panels
    * This function should be called before stacking a new panel
    */
    setPanelOwner: function (panel, givenOwner) {
        var owner = givenOwner;
        if (!owner) {
            var previousBody = null;
        	if (this.PANELS_STACK.length > 0) {
                var previousPanel = this.PANELS_STACK[this.PANELS_STACK.length - 1];
                previousBody = previousPanel.isValid() ? previousPanel.get('v.body') : previousBody;
        	}
            owner = $A.util.isEmpty(previousBody) ? panel.getGlobalId() : previousBody[0].getGlobalId();
        }

        this.PANELS_OWNER[panel.getGlobalId()] = owner;
    },

    /*
    * Stack a panel in our internal structures
    */
{'fraction_non_alphanumeric': 0.09916409023245162, 'fraction_numerical': 0.003549753807397229, 'mean_word_length': 2.3058289174867523, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2499526', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2324, 'n_words': 670}