<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.
 */
/*jslint sub: true, evil: true */

/**
 * @description The Aura Component Service, accessible using $A.service.component.  Creates and Manages Components.
 * @constructor
 * @export
 */
function AuraComponentService () {
    // Def registries
    this.componentDefRegistry   = {};
    this.controllerDefRegistry  = {};
    this.actionDefRegistry      = {};
    this.modelDefRegistry       = {};
    this.libraryDefRegistry     = new Aura.Library.LibraryDefRegistry(); // To abstract lib logic
    this.componentClassRegistry = new Aura.Component.ComponentClassRegistry(); // To abstract class logic
    this.componentDefStorage    = new Aura.Component.ComponentDefStorage();

    // holds ComponentDef configs to be created
    this.savedComponentConfigs = {};

    // references ControllerDef descriptor to its ComponentDef descriptor
    this.controllerDefRelationship = {};

    // references ActionDef descriptor to its ComponentDef descriptor
    this.actionDefRelationship = {};

    // Global registry for cmp instances
    this.indexes = { globalId : {} };

    this.dynamicNamespaces = []; // TODO: @dval: delete after createComponent refactor

    // Static attr names
    this.flavorable    = "auraFlavorable";
    this.renderedBy    = "auraRenderedBy";
    this["renderedBy"] = this.renderedBy;   // originally exposed using exp()
}

/**
 * Gets an instance of a component.
 * @param {String} globalId The generated globally unique Id of the component that changes across pageloads.
 *
 * @public
 * @deprecated use getComponent instead
 * @export
 */
AuraComponentService.prototype.get = function(globalId) {
    return this.indexes.globalId[globalId];
};

/**
 * Gets an instance of a component from either a GlobalId or a DOM element that was created via a Component Render.
 * @param {Object} identifier that is either a globalId or an element.
 *
 * @public
 * @platform
 * @export
 */
AuraComponentService.prototype.getComponent = function(identifier) {
    return this.get(identifier) || this.getRenderingComponentForElement(identifier);
};

/**
 * Gets descriptor from the config object (for normalization)
 * @param {Object} Controller descriptor config
 * @returns {String} Descriptor
 * @private
 */
AuraComponentService.prototype.getDescriptorFromConfig = function(descriptorConfig) {
    var descriptor = descriptorConfig && descriptorConfig["descriptor"];
    $A.assert(descriptor, "Descriptor for Config required for registration");
    return descriptor;
};

/**
 * Gets descriptor from the config object (for normalization)
 * @param {Object} Controller descriptor config
 * @returns {String} Descriptor
 * @private
 */
AuraComponentService.prototype.createDescriptorConfig = function(descriptor) {
    descriptor = typeof descriptor === 'string' ? descriptor : descriptor["descriptor"].toString();
    descriptor = descriptor.indexOf("://") < 0 ? "markup://" + descriptor : descriptor;
    return { "descriptor" : descriptor };
};


/**
 * Counts all the components currently created in the application.
 * @example
 * var count = $A.componentService.countComponents();
 *
 * @public
 * @platform
 * @export
 */
AuraComponentService.prototype.countComponents = function() {
    return Object.keys(this.indexes.globalId).length;
};

/**
 * Gets the rendering component for the provided element recursively.
 * @param {Object} element The element that is used to find the rendering component
 * @memberOf AuraComponentService
 * @public
 * @export
 */
AuraComponentService.prototype.getRenderingComponentForElement = function(element) {
    var ret;

    if ($A.util.isUndefinedOrNull(element)) {
        return null;
    }

    if ($A.util.hasDataAttribute(element, this.renderedBy)) {
        var id = $A.util.getDataAttribute(element, this.renderedBy);
        ret = this.get(id);

    } else if(element.parentNode) {
        ret = this.getRenderingComponentForElement(element.parentNode);
    }

    return ret;
};

/**
 * Gets the attribute provider for the provided element.
 * @param {Object} element The element whose attribute provider is to be returned
 * @memberOf AuraComponentService
 * @public
 * @export
 */
AuraComponentService.prototype.getAttributeProviderForElement = function(element) {
    return this.getRenderingComponentForElement(element).getAttributeValueProvider();
};

/**
 * Create a new component array.
 * @private
 */
AuraComponentService.prototype.newComponentArray = function(config, attributeValueProvider, localCreation, doForce){
    var ret = [];
    for (var i = 0; i < config.length; i++) {
        ret.push(this.newComponentDeprecated(config[i], attributeValueProvider, localCreation, doForce));
    }
    return ret;
};

/**
 * Create a component from a type and a set of attributes.
 * It accepts the name of a type of component, a map of attributes,
 * and a callback to notify callers.
 *
 * @param {String} type The type of component to create, e.g. "ui:button".
 * @param {Object} attributes A map of attributes to send to the component. These take the same form as on the markup,
 * including events <code>{"press":component.getReference("c.handlePress")}</code>, and id <code>{"aura:id":"myComponentId"}</code>.
 * @param {Function} callback The method to call, to which it returns the newly created component.
 *
 * @example
 * $A.createComponent("aura:text",{value:'Hello World'}, function(auraTextComponent, status, statusMessagesList){
 *      // auraTextComponent is an instance of aura:text containing the value Hello World
 * });
 *
 * @public
 * @platform
 * @function
 * @export
 */
AuraComponentService.prototype.createComponent = function(type, attributes, callback) {
    $A.assert($A.util.isString(type), "ComponentService.createComponent(): 'type' must be a valid String.");
    $A.assert(!attributes || $A.util.isObject(attributes), "ComponentService.createComponent(): 'attributes' must be a valid Object.");
    $A.assert($A.util.isFunction(callback), "ComponentService.createComponent(): 'callback' must be a Function pointer.");

    var config = {
        "componentDef" : this.createDescriptorConfig(type),
        "attributes"   : { "values" : attributes },
        "localId"      : attributes && attributes["aura:id"],
        "flavor"       : (attributes && attributes["aura:flavor"])
    };

    return this.createComponentPrivAsync(config, callback);
};


/*
 * Creates an internal object config for a component from a public config.
 *
 * This is only used by createComponentFromConfig to separate the internal
 * representation from the external representation.
 * If we change the component format, we could change this method
 * without breaking anyone's code.
 *
 * @function
 */
AuraComponentService.prototype.createInternalConfig = function (config) {
    var descriptor = config["descriptor"];
    $A.assert(descriptor.indexOf("markup://") === 0, "Descriptor needs to be of the format markup://ns:name");

    return {
        "componentDef" : this.createDescriptorConfig(config["descriptor"]),
        "localId"      : config["localId"],
        "attributes"   : {
            "values"        : config["attributes"],
            "valueProvider" : config["valueProvider"]
        }
    };
};

/**
 * Creates a component from a config.
 *
 * It accepts a config Object generated directly by the framework
 * or a custom manually created config (see notes for details).
 *
 * IMPORTANT NOTES:
 *
 * - It's key that we separate the internal representation of a component
 * from the external one (publicly available), so we can always improve
 * and change the framework implementation without breaking anything.
 *
 * - Passing a user generated config is discouraged (instead createComponent
 * should be used). This method will only work for clientCreatable components
 * and for very simple use cases.
 *
 * @param {Object} config A map with the component tree configuration,
 * the configuration can be external (publicly exposed) or internal.
 *
 * {
        descriptor    : "markup://ns:cmpName",
        localId       : "localId",
        attributes    : { attr1: value1, ... },
        valueProvider : myValueProviderComponent
 * }
 *
 * @public
 * @function
 * @export
 */
AuraComponentService.prototype.createComponentFromConfig = function(config) {<commit_msg>Adding flavor to signature<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.
 */
/*jslint sub: true, evil: true */

/**
 * @description The Aura Component Service, accessible using $A.service.component.  Creates and Manages Components.
 * @constructor
 * @export
 */
function AuraComponentService () {
    // Def registries
    this.componentDefRegistry   = {};
    this.controllerDefRegistry  = {};
    this.actionDefRegistry      = {};
    this.modelDefRegistry       = {};
    this.libraryDefRegistry     = new Aura.Library.LibraryDefRegistry(); // To abstract lib logic
    this.componentClassRegistry = new Aura.Component.ComponentClassRegistry(); // To abstract class logic
    this.componentDefStorage    = new Aura.Component.ComponentDefStorage();

    // holds ComponentDef configs to be created
    this.savedComponentConfigs = {};

    // references ControllerDef descriptor to its ComponentDef descriptor
    this.controllerDefRelationship = {};

    // references ActionDef descriptor to its ComponentDef descriptor
    this.actionDefRelationship = {};

    // Global registry for cmp instances
    this.indexes = { globalId : {} };

    this.dynamicNamespaces = []; // TODO: @dval: delete after createComponent refactor

    // Static attr names
    this.flavorable    = "auraFlavorable";
    this.renderedBy    = "auraRenderedBy";
    this["renderedBy"] = this.renderedBy;   // originally exposed using exp()
}

/**
 * Gets an instance of a component.
 * @param {String} globalId The generated globally unique Id of the component that changes across pageloads.
 *
 * @public
 * @deprecated use getComponent instead
 * @export
 */
AuraComponentService.prototype.get = function(globalId) {
    return this.indexes.globalId[globalId];
};

/**
 * Gets an instance of a component from either a GlobalId or a DOM element that was created via a Component Render.
 * @param {Object} identifier that is either a globalId or an element.
 *
 * @public
 * @platform
 * @export
 */
AuraComponentService.prototype.getComponent = function(identifier) {
    return this.get(identifier) || this.getRenderingComponentForElement(identifier);
};

/**
 * Gets descriptor from the config object (for normalization)
 * @param {Object} Controller descriptor config
 * @returns {String} Descriptor
 * @private
 */
AuraComponentService.prototype.getDescriptorFromConfig = function(descriptorConfig) {
    var descriptor = descriptorConfig && descriptorConfig["descriptor"];
    $A.assert(descriptor, "Descriptor for Config required for registration");
    return descriptor;
};

/**
 * Gets descriptor from the config object (for normalization)
 * @param {Object} Controller descriptor config
 * @returns {String} Descriptor
 * @private
 */
AuraComponentService.prototype.createDescriptorConfig = function(descriptor) {
    descriptor = typeof descriptor === 'string' ? descriptor : descriptor["descriptor"].toString();
    descriptor = descriptor.indexOf("://") < 0 ? "markup://" + descriptor : descriptor;
    return { "descriptor" : descriptor };
};


/**
 * Counts all the components currently created in the application.
 * @example
 * var count = $A.componentService.countComponents();
 *
 * @public
 * @platform
 * @export
 */
AuraComponentService.prototype.countComponents = function() {
    return Object.keys(this.indexes.globalId).length;
};

/**
 * Gets the rendering component for the provided element recursively.
 * @param {Object} element The element that is used to find the rendering component
 * @memberOf AuraComponentService
 * @public
 * @export
 */
AuraComponentService.prototype.getRenderingComponentForElement = function(element) {
    var ret;

    if ($A.util.isUndefinedOrNull(element)) {
        return null;
    }

    if ($A.util.hasDataAttribute(element, this.renderedBy)) {
        var id = $A.util.getDataAttribute(element, this.renderedBy);
        ret = this.get(id);

    } else if(element.parentNode) {
        ret = this.getRenderingComponentForElement(element.parentNode);
    }

    return ret;
};

/**
 * Gets the attribute provider for the provided element.
 * @param {Object} element The element whose attribute provider is to be returned
 * @memberOf AuraComponentService
 * @public
 * @export
 */
AuraComponentService.prototype.getAttributeProviderForElement = function(element) {
    return this.getRenderingComponentForElement(element).getAttributeValueProvider();
};

/**
 * Create a new component array.
 * @private
 */
AuraComponentService.prototype.newComponentArray = function(config, attributeValueProvider, localCreation, doForce){
    var ret = [];
    for (var i = 0; i < config.length; i++) {
        ret.push(this.newComponentDeprecated(config[i], attributeValueProvider, localCreation, doForce));
    }
    return ret;
};

/**
 * Create a component from a type and a set of attributes.
 * It accepts the name of a type of component, a map of attributes,
 * and a callback to notify callers.
 *
 * @param {String} type The type of component to create, e.g. "ui:button".
 * @param {Object} attributes A map of attributes to send to the component. These take the same form as on the markup,
 * including events <code>{"press":component.getReference("c.handlePress")}</code>, and id <code>{"aura:id":"myComponentId"}</code>.
 * @param {Function} callback The method to call, to which it returns the newly created component.
 *
 * @example
 * $A.createComponent("aura:text",{value:'Hello World'}, function(auraTextComponent, status, statusMessagesList){
 *      // auraTextComponent is an instance of aura:text containing the value Hello World
 * });
 *
 * @public
 * @platform
 * @function
 * @export
 */
AuraComponentService.prototype.createComponent = function(type, attributes, callback) {
    $A.assert($A.util.isString(type), "ComponentService.createComponent(): 'type' must be a valid String.");
    $A.assert(!attributes || $A.util.isObject(attributes), "ComponentService.createComponent(): 'attributes' must be a valid Object.");
    $A.assert($A.util.isFunction(callback), "ComponentService.createComponent(): 'callback' must be a Function pointer.");

    var config = {
        "componentDef" : this.createDescriptorConfig(type),
        "attributes"   : { "values" : attributes },
        "localId"      : attributes && attributes["aura:id"],
        "flavor"       : (attributes && attributes["aura:flavor"])
    };

    return this.createComponentPrivAsync(config, callback);
};


/*
 * Creates an internal object config for a component from a public config.
 *
 * This is only used by createComponentFromConfig to separate the internal
 * representation from the external representation.
 * If we change the component format, we could change this method
 * without breaking anyone's code.
 *
 * @function
 */
AuraComponentService.prototype.createInternalConfig = function (config) {
    var descriptor = config["descriptor"];
    $A.assert(descriptor.indexOf("markup://") === 0, "Descriptor needs to be of the format markup://ns:name");

    return {
        "componentDef" : this.createDescriptorConfig(config["descriptor"]),
        "localId"      : config["localId"],
        "flavor"       : config["flavor"],
        "attributes"   : {
            "values"        : config["attributes"],
            "valueProvider" : config["valueProvider"]
        }
    };
};

/**
 * Creates a component from a config.
 *
 * It accepts a config Object generated directly by the framework
 * or a custom manually created config (see notes for details).
 *
 * IMPORTANT NOTES:
 *
 * - It's key that we separate the internal representation of a component
 * from the external one (publicly available), so we can always improve
 * and change the framework implementation without breaking anything.
 *
 * - Passing a user generated config is discouraged (instead createComponent
 * should be used). This method will only work for clientCreatable components
 * and for very simple use cases.
 *
 * @param {Object} config A map with the component tree configuration,
 * the configuration can be external (publicly exposed) or internal.
 *
 * {
        descriptor    : "markup://ns:cmpName",
        localId       : "localId",
        flavor        : "flavor",
        attributes    : { attr1: value1, ... },
        valueProvider : myValueProviderComponent
 * }
 *
 * @public
 * @function
 * @export
 */
AuraComponentService.prototype.createComponentFromConfig = function(config) {
{'fraction_non_alphanumeric': 0.10881465758015864, 'fraction_numerical': 0.0014523516925483186, 'mean_word_length': 3.8061744966442954, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 11, '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': '977519', 'n_tokens_mistral': 4647, 'n_tokens_neox': 4566, 'n_words': 1553}