{"version":3,"sources":["PipelineContext.min__d957946aa95219e5e0e2.js","./src/ui/PipelineContext/PipelineContext.ts"],"names":["webpackJsonpCoveo__temporary","261","module","exports","__webpack_require__","__extends","this","extendStatics","Object","setPrototypeOf","__proto__","Array","d","b","p","hasOwnProperty","__","constructor","prototype","create","defineProperty","value","element","options","bindings","PipelineContext","ID","contextContent","ComponentOptions","initComponentOptions","setContext","$$","text","trim","bind","onRootElement","QueryEvents","buildingQuery","args","handleBuildingQuery","_this","newContext","_","isString","contextParsed","tryParseContextFromString","getContext","keys","getContextKeys","object","map","key","getContextValue","setContextValue","contextKey","contextValue","isArray","each","push","getModifiedData","queryBuilder","addContextValue","contextAsString","isEmpty","JSON","parse","Utils","decodeHTMLEntities","e","logger","error","replace","all","trimmedKey","Coveo","context","CURRENT_URL","window","location","href","doExport","exportGlobally","Component","Initialization","registerAutoCreateComponent"],"mappings":"AAAAA,8BAA8B,KAExBC,IACA,SAAUC,EAAQC,EAASC,GAEjC,YAEA,IAAIC,GAAaC,MAAQA,KAAKD,WAAc,WACxC,GAAIE,GAAgBC,OAAOC,iBACpBC,uBAA2BC,QAAS,SAAUC,EAAGC,GAAKD,EAAEF,UAAYG,IACvE,SAAUD,EAAGC,GAAK,IAAK,GAAIC,KAAKD,GAAOA,EAAEE,eAAeD,KAAIF,EAAEE,GAAKD,EAAEC,IACzE,OAAO,UAAUF,EAAGC,GAEhB,QAASG,KAAOV,KAAKW,YAAcL,EADnCL,EAAcK,EAAGC,GAEjBD,EAAEM,UAAkB,OAANL,EAAaL,OAAOW,OAAON,IAAMG,EAAGE,UAAYL,EAAEK,UAAW,GAAIF,OAGvFR,QAAOY,eAAejB,EAAS,cAAgBkB,OAAO,GCjBtD,YACA,QACA,OACA,OACA,OACA,OAEA,OACA,OAeA,cAYE,WAA0BC,EAA6BC,EAA0CC,GAAjG,MACE,YAAMF,EAASG,EAAgBC,GAAIF,IAAS,IDWxC,OCZoB,GAAAF,UAA6B,EAAAC,UAA0C,EAAAC,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AAKC,MAAMH,GAClB,MAAOM,GAEP,MADA9D,MAAK+D,OAAOC,MAAM,yEAA0EF,GACrF,QAKL,YAAAT,gBAAR,SAAwBtC,GAUtB,MAAOA,GAAMkD,QAAQ,kBAAmB,SAACC,EAAalB,GACpD,GAAMmB,GAAanB,EAAWrB,MAC9B,OAAIyC,OAAMC,SAAWF,IAAcC,OAAMC,QAChCD,MAAMC,QAAQF,GACZA,GAAchD,EAAgBmD,YAChCC,OAAOC,SAASC,KAElB,MAjIJ,EAAArD,GAAK,kBACL,EAAAkD,YAAc,aAEd,EAAAI,SAAW,WAChB,EAAAC,gBACExD,gBAAiBA,KA+HvB,GArIqC,EAAAyD,UAAxB,GAAAzD,kBAuIb,EAAA0D,eAAeC,4BAA4B3D","file":"PipelineContext.min__d957946aa95219e5e0e2.js","sourcesContent":["webpackJsonpCoveo__temporary([80],{\n\n/***/ 261:\n/***/ (function(module, exports, __webpack_require__) {\n\n\"use strict\";\n\nvar __extends = (this && this.__extends) || (function () {\n    var extendStatics = Object.setPrototypeOf ||\n        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\n        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };\n    return function (d, b) {\n        extendStatics(d, b);\n        function __() { this.constructor = d; }\n        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n    };\n})();\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar _ = __webpack_require__(0);\nvar QueryEvents_1 = __webpack_require__(11);\nvar GlobalExports_1 = __webpack_require__(3);\nvar Dom_1 = __webpack_require__(1);\nvar Utils_1 = __webpack_require__(4);\nvar Component_1 = __webpack_require__(7);\nvar ComponentOptions_1 = __webpack_require__(8);\nvar Initialization_1 = __webpack_require__(2);\n/**\n * The `PipelineContext` component injects custom contextual information into the search requests and usage analytics search events sent from a search interface.\n *\n * This component is meant to be initialized on a `script` HTML tag whose `type` attribute is set to `text/context` and whose optional JSON content defines the custom information to send (each value can be set to a string or array of strings).\n *\n * See [Sending Custom Context Information](https://docs.coveo.com/en/399/).\n * Note: To customize the context sent on all usage analytics events, see [Sending Custom Metadata with Search, Click, or Custom Events](https://docs.coveo.com/en/2004/javascript-search-framework/sending-custom-metadata-with-search-click-or-custom-events).\n */\nvar PipelineContext = /** @class */ (function (_super) {\n    __extends(PipelineContext, _super);\n    function PipelineContext(element, options, bindings) {\n        var _this = _super.call(this, element, PipelineContext.ID, bindings) || this;\n        _this.element = element;\n        _this.options = options;\n        _this.bindings = bindings;\n        _this.contextContent = {};\n        _this.options = ComponentOptions_1.ComponentOptions.initComponentOptions(element, PipelineContext, options);\n        _this.setContext(Dom_1.$$(_this.element)\n            .text()\n            .trim());\n        _this.bind.onRootElement(QueryEvents_1.QueryEvents.buildingQuery, function (args) { return _this.handleBuildingQuery(args); });\n        return _this;\n    }\n    /**\n     * **Available since the [December 2017 Release](https://docs.coveo.com/en/373).**\n     *\n     * Sets a new context, replacing the previous context if applicable.\n     *\n     * @param newContext The new context to set, which can be directly passed as a JSON, or as a stringified JSON.\n     */\n    PipelineContext.prototype.setContext = function (newContext) {\n        if (_.isString(newContext)) {\n            var contextParsed = this.tryParseContextFromString(newContext);\n            this.contextContent = contextParsed;\n        }\n        else {\n            this.contextContent = newContext;\n        }\n    };\n    /**\n     * Returns the current context\n     */\n    PipelineContext.prototype.getContext = function () {\n        var _this = this;\n        var keys = this.getContextKeys();\n        return _.object(keys, _.map(keys, function (key) { return _this.getContextValue(key); }));\n    };\n    /**\n     * **Available since the [December 2017 Release](https://docs.coveo.com/en/373).**\n     *\n     * Sets a value for a context key, replacing the previous value if applicable.\n     * @param contextKey\n     * @param contextValue\n     */\n    PipelineContext.prototype.setContextValue = function (contextKey, contextValue) {\n        this.contextContent[contextKey] = contextValue;\n    };\n    /**\n     * Return all the context keys configured for context.\n     * @returns {string[]}\n     */\n    PipelineContext.prototype.getContextKeys = function () {\n        return _.keys(this.contextContent);\n    };\n    /**\n     * Get the context value associated to the given key.\n     *\n     * If the global variable Coveo.context contains the requested key, this method will return the value contained in Coveo.context instead of the one contained internally.\n     *\n     * This is especially useful in a Coveo for Salesforce context, where context values can be extracted from a backend service.\n     * @param key\n     * @returns {string}\n     */\n    PipelineContext.prototype.getContextValue = function (key) {\n        var _this = this;\n        var contextValue = this.contextContent[key];\n        if (_.isArray(contextValue)) {\n            var contextValues_1 = [];\n            _.each(this.contextContent[key], function (value) {\n                contextValues_1.push(_this.getModifiedData(value));\n            });\n            return contextValues_1;\n        }\n        else if (_.isString(contextValue)) {\n            return this.getModifiedData(contextValue);\n        }\n        return '';\n    };\n    PipelineContext.prototype.handleBuildingQuery = function (args) {\n        var _this = this;\n        var keys = this.getContextKeys();\n        _.each(keys, function (key) {\n            args.queryBuilder.addContextValue(key, _this.getContextValue(key));\n        });\n    };\n    PipelineContext.prototype.tryParseContextFromString = function (contextAsString) {\n        if (_.isEmpty(contextAsString)) {\n            return {};\n        }\n        try {\n            // Context could be HTML encoded (eg: Coveo for Salesforce)\n            return JSON.parse(Utils_1.Utils.decodeHTMLEntities(contextAsString));\n        }\n        catch (e) {\n            try {\n                return JSON.parse(contextAsString);\n            }\n            catch (e) {\n                this.logger.error(\"Error while trying to parse context from the PipelineContext component\", e);\n                return null;\n            }\n        }\n    };\n    PipelineContext.prototype.getModifiedData = function (value) {\n        /* We need to modify the data to escape special salesforce characters. eg: {! }\n         If we find the matching value in the global Coveo.context variable, we return that one instead of the one present locally.\n         So, concretely, the component could contain :\n         {\n           \"productName\" : \"{! productValueFromSalesforce }\"\n         }\n    \n         This means that in those case, we would try to access Coveo.context.productValueFromSalesforce (which would in theory be a \"real\" product value from salesforce, and not a placeholder/variable)\n        */\n        return value.replace(/\\{\\!([^\\}]+)\\}/g, function (all, contextKey) {\n            var trimmedKey = contextKey.trim();\n            if (Coveo.context && trimmedKey in Coveo.context) {\n                return Coveo.context[trimmedKey];\n            }\n            else if (trimmedKey == PipelineContext.CURRENT_URL) {\n                return window.location.href;\n            }\n            return '';\n        });\n    };\n    PipelineContext.ID = 'PipelineContext';\n    PipelineContext.CURRENT_URL = 'CurrentUrl';\n    PipelineContext.doExport = function () {\n        GlobalExports_1.exportGlobally({\n            PipelineContext: PipelineContext\n        });\n    };\n    return PipelineContext;\n}(Component_1.Component));\nexports.PipelineContext = PipelineContext;\nInitialization_1.Initialization.registerAutoCreateComponent(PipelineContext);\n\n\n/***/ })\n\n});\n\n\n// WEBPACK FOOTER //\n// PipelineContext.min__d957946aa95219e5e0e2.js","import * as _ from 'underscore';\nimport { IBuildingQueryEventArgs, QueryEvents } from '../../events/QueryEvents';\nimport { exportGlobally } from '../../GlobalExports';\nimport { $$ } from '../../utils/Dom';\nimport { Utils } from '../../utils/Utils';\nimport { Component } from '../Base/Component';\nimport { IComponentBindings } from '../Base/ComponentBindings';\nimport { ComponentOptions } from '../Base/ComponentOptions';\nimport { Initialization } from '../Base/Initialization';\nimport { Context, IPipelineContextProvider } from './PipelineGlobalExports';\n\ndeclare const Coveo;\n\nexport interface IPipelineContextOptions {}\n\n/**\n * The `PipelineContext` component injects custom contextual information into the search requests and usage analytics search events sent from a search interface.\n *\n * This component is meant to be initialized on a `script` HTML tag whose `type` attribute is set to `text/context` and whose optional JSON content defines the custom information to send (each value can be set to a string or array of strings).\n *\n * See [Sending Custom Context Information](https://docs.coveo.com/en/399/).\n * Note: To customize the context sent on all usage analytics events, see [Sending Custom Metadata with Search, Click, or Custom Events](https://docs.coveo.com/en/2004/javascript-search-framework/sending-custom-metadata-with-search-click-or-custom-events).\n */\nexport class PipelineContext extends Component implements IPipelineContextProvider {\n  static ID = 'PipelineContext';\n  static CURRENT_URL = 'CurrentUrl';\n\n  static doExport = () => {\n    exportGlobally({\n      PipelineContext: PipelineContext\n    });\n  };\n\n  private contextContent: Context = {};\n\n  public constructor(public element: HTMLElement, public options?: IPipelineContextOptions, public bindings?: IComponentBindings) {\n    super(element, PipelineContext.ID, bindings);\n    this.options = ComponentOptions.initComponentOptions(element, PipelineContext, options);\n    this.setContext(\n      $$(this.element)\n        .text()\n        .trim()\n    );\n    this.bind.onRootElement(QueryEvents.buildingQuery, (args: IBuildingQueryEventArgs) => this.handleBuildingQuery(args));\n  }\n\n  /**\n   * **Available since the [December 2017 Release](https://docs.coveo.com/en/373).**\n   *\n   * Sets a new context, replacing the previous context if applicable.\n   *\n   * @param newContext The new context to set, which can be directly passed as a JSON, or as a stringified JSON.\n   */\n  public setContext(newContext: string | Context) {\n    if (_.isString(newContext)) {\n      const contextParsed = this.tryParseContextFromString(newContext);\n      this.contextContent = contextParsed;\n    } else {\n      this.contextContent = newContext;\n    }\n  }\n\n  /**\n   * Returns the current context\n   */\n  public getContext(): Context {\n    const keys = this.getContextKeys();\n    return _.object(keys, _.map(keys, key => this.getContextValue(key)));\n  }\n\n  /**\n   * **Available since the [December 2017 Release](https://docs.coveo.com/en/373).**\n   *\n   * Sets a value for a context key, replacing the previous value if applicable.\n   * @param contextKey\n   * @param contextValue\n   */\n  public setContextValue(contextKey: string, contextValue: string | string[]) {\n    this.contextContent[contextKey] = contextValue;\n  }\n\n  /**\n   * Return all the context keys configured for context.\n   * @returns {string[]}\n   */\n  public getContextKeys(): string[] {\n    return _.keys(this.contextContent);\n  }\n\n  /**\n   * Get the context value associated to the given key.\n   *\n   * If the global variable Coveo.context contains the requested key, this method will return the value contained in Coveo.context instead of the one contained internally.\n   *\n   * This is especially useful in a Coveo for Salesforce context, where context values can be extracted from a backend service.\n   * @param key\n   * @returns {string}\n   */\n  public getContextValue(key: string): string | string[] {\n    const contextValue = this.contextContent[key];\n    if (_.isArray(contextValue)) {\n      const contextValues = [];\n      _.each(this.contextContent[key], value => {\n        contextValues.push(this.getModifiedData(value));\n      });\n      return contextValues;\n    } else if (_.isString(contextValue)) {\n      return this.getModifiedData(contextValue);\n    }\n    return '';\n  }\n\n  private handleBuildingQuery(args: IBuildingQueryEventArgs) {\n    let keys = this.getContextKeys();\n    _.each(keys, (key: string) => {\n      args.queryBuilder.addContextValue(key, this.getContextValue(key));\n    });\n  }\n\n  private tryParseContextFromString(contextAsString: string): Context {\n    if (_.isEmpty(contextAsString)) {\n      return {};\n    }\n    try {\n      // Context could be HTML encoded (eg: Coveo for Salesforce)\n      return JSON.parse(Utils.decodeHTMLEntities(contextAsString));\n    } catch (e) {\n      try {\n        return JSON.parse(contextAsString);\n      } catch (e) {\n        this.logger.error(`Error while trying to parse context from the PipelineContext component`, e);\n        return null;\n      }\n    }\n  }\n\n  private getModifiedData(value: string) {\n    /* We need to modify the data to escape special salesforce characters. eg: {! }\n     If we find the matching value in the global Coveo.context variable, we return that one instead of the one present locally.\n     So, concretely, the component could contain : \n     {\n       \"productName\" : \"{! productValueFromSalesforce }\"\n     }\n\n     This means that in those case, we would try to access Coveo.context.productValueFromSalesforce (which would in theory be a \"real\" product value from salesforce, and not a placeholder/variable)\n    */\n    return value.replace(/\\{\\!([^\\}]+)\\}/g, (all: string, contextKey: string) => {\n      const trimmedKey = contextKey.trim();\n      if (Coveo.context && trimmedKey in Coveo.context) {\n        return Coveo.context[trimmedKey];\n      } else if (trimmedKey == PipelineContext.CURRENT_URL) {\n        return window.location.href;\n      }\n      return '';\n    });\n  }\n}\n\nInitialization.registerAutoCreateComponent(PipelineContext);\n\n\n\n// WEBPACK FOOTER //\n// ./src/ui/PipelineContext/PipelineContext.ts"],"sourceRoot":""}