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,CAAC,CAAC;IAC7F,OAAO,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,cAAO,EAAE,EAAE,CAAC,CAAC;AACtD,CAAC;AAED,iEAAiE;AACjE,qGAAqG;AACrG,IAAM,WAAW,GAAG,UAAC,GAAQ;IAC3B,IAAI,eAAQ,CAAC,GAAG,CAAC,gBAAgB,CAAC;QAAE,OAAO,aAAa,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAC/E,OAAO,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC,CAAC;AAOF,yDAAyD;AACzD,gGAAgG;AAChG,IAAM,aAAa,GAAG,UAAC,WAAgB;IACrC,OAAA,MAAM,CAAC,IAAI,CAAC,WAAW,IAAI,EAAE,CAAC;QAC5B,sCAAsC;SACrC,GAAG,CAAC,UAAC,GAAG,IAAK,OAAA,CAAC,GAAG,EAAE,mBAAmB,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAjD,CAAiD,CAAC;QAChE,wBAAwB;SACvB,MAAM,CAAC,UAAC,KAAK,IAAK,OAAA,gBAAS,CAAC,KAAK,CAAC,IAAI,cAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAArC,CAAqC,CAAC;QACzD,0CAA0C;SACzC,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAmB,CAAA,EAAtE,CAAsE,CAAC;AANzF,CAMyF,CAAC",
10 "sourcesContent": [
11 "/** @publicapi @module view */ /** */\nimport { ng as angular } from './angular';\nimport { IAugmentedJQuery } from 'angular';\nimport {\n isArray,\n isDefined,\n isFunction,\n isObject,\n services,\n Obj,\n IInjectable,\n tail,\n kebobString,\n unnestR,\n ResolveContext,\n Resolvable,\n RawParams,\n} from '@uirouter/core';\nimport { Ng1ViewDeclaration, TemplateFactoryProvider } from './interface';\n\n/**\n * Service which manages loading of templates from a ViewConfig.\n */\nexport class TemplateFactory implements TemplateFactoryProvider {\n /** @hidden */ private _useHttp = angular.version.minor < 3;\n /** @hidden */ private $templateRequest;\n /** @hidden */ private $templateCache;\n /** @hidden */ private $http;\n\n /** @hidden */ $get = [\n '$http',\n '$templateCache',\n '$injector',\n ($http, $templateCache, $injector) => {\n this.$templateRequest = $injector.has && $injector.has('$templateRequest') && $injector.get('$templateRequest');\n this.$http = $http;\n this.$templateCache = $templateCache;\n return this;\n },\n ];\n\n /** @hidden */\n useHttpService(value: boolean) {\n this._useHttp = value;\n }\n\n /**\n * Creates a template from a configuration object.\n *\n * @param config Configuration object for which to load a template.\n * The following properties are search in the specified order, and the first one\n * that is defined is used to create the template:\n *\n * @param params Parameters to pass to the template function.\n * @param context The resolve context associated with the template's view\n *\n * @return {string|object} The template html as a string, or a promise for\n * that string,or `null` if no template is configured.\n */\n fromConfig(\n config: Ng1ViewDeclaration,\n params: any,\n context: ResolveContext\n ): Promise<{ template?: string; component?: string }> {\n const defaultTemplate = '<ui-view></ui-view>';\n\n const asTemplate = (result) => services.$q.when(result).then((str) => ({ template: str }));\n const asComponent = (result) => services.$q.when(result).then((str) => ({ component: str }));\n\n return isDefined(config.template)\n ? asTemplate(this.fromString(config.template, params))\n : isDefined(config.templateUrl)\n ? asTemplate(this.fromUrl(config.templateUrl, params))\n : isDefined(config.templateProvider)\n ? asTemplate(this.fromProvider(config.templateProvider, params, context))\n : isDefined(config.component)\n ? asComponent(config.component)\n : isDefined(config.componentProvider)\n ? asComponent(this.fromComponentProvider(config.componentProvider, params, context))\n : asTemplate(defaultTemplate);\n }\n\n /**\n * Creates a template from a string or a function returning a string.\n *\n * @param template html template as a string or function that returns an html template as a string.\n * @param params Parameters to pass to the template function.\n *\n * @return {string|object} The template html as a string, or a promise for that\n * string.\n */\n fromString(template: string | Function, params?: RawParams) {\n return isFunction(template) ? (<any>template)(params) : template;\n }\n\n /**\n * Loads a template from the a URL via `$http` and `$templateCache`.\n *\n * @param {string|Function} url url of the template to load, or a function\n * that returns a url.\n * @param {Object} params Parameters to pass to the url function.\n * @return {string|Promise.<string>} The template html as a string, or a promise\n * for that string.\n */\n fromUrl(url: string | Function, params: any) {\n if (isFunction(url)) url = (<any>url)(params);\n if (url == null) return null;\n\n if (this._useHttp) {\n return this.$http\n .get(url, { cache: this.$templateCache, headers: { Accept: 'text/html' } })\n .then(function (response) {\n return response.data;\n });\n }\n\n return this.$templateRequest(url);\n }\n\n /**\n * Creates a template by invoking an injectable provider function.\n *\n * @param provider Function to invoke via `locals`\n * @param {Function} injectFn a function used to invoke the template provider\n * @return {string|Promise.<string>} The template html as a string, or a promise\n * for that string.\n */\n fromProvider(provider: IInjectable, params: any, context: ResolveContext) {\n const deps = services.$injector.annotate(provider);\n const providerFn = isArray(provider) ? tail(<any[]>provider) : provider;\n const resolvable = new Resolvable('', <Function>providerFn, deps);\n return resolvable.get(context);\n }\n\n /**\n * Creates a component's template by invoking an injectable provider function.\n *\n * @param provider Function to invoke via `locals`\n * @param {Function} injectFn a function used to invoke the template provider\n * @return {string} The template html as a string: \"<component-name input1='::$resolve.foo'></component-name>\".\n */\n fromComponentProvider(provider: IInjectable, params: any, context: ResolveContext) {\n const deps = services.$injector.annotate(provider);\n const providerFn = isArray(provider) ? tail(<any[]>provider) : provider;\n const resolvable = new Resolvable('', <Function>providerFn, deps);\n return resolvable.get(context);\n }\n\n /**\n * Creates a template from a component's name\n *\n * This implements route-to-component.\n * It works by retrieving the component (directive) metadata from the injector.\n * It analyses the component's bindings, then constructs a template that instantiates the component.\n * The template wires input and output bindings to resolves or from the parent component.\n *\n * @param uiView {object} The parent ui-view (for binding outputs to callbacks)\n * @param context The ResolveContext (for binding outputs to callbacks returned from resolves)\n * @param component {string} Component's name in camel case.\n * @param bindings An object defining the component's bindings: {foo: '<'}\n * @return {string} The template as a string: \"<component-name input1='::$resolve.foo'></component-name>\".\n */\n makeComponentTemplate(uiView: IAugmentedJQuery, context: ResolveContext, component: string, bindings?: any) {\n bindings = bindings || {};\n\n // Bind once prefix\n const prefix = angular.version.minor >= 3 ? '::' : '';\n // Convert to kebob name. Add x- prefix if the string starts with `x-` or `data-`\n const kebob = (camelCase: string) => {\n const kebobed = kebobString(camelCase);\n return /^(x|data)-/.exec(kebobed) ? `x-${kebobed}` : kebobed;\n };\n\n const attributeTpl = (input: BindingTuple) => {\n const { name, type } = input;\n const attrName = kebob(name);\n // If the ui-view has an attribute which matches a binding on the routed component\n // then pass that attribute through to the routed component template.\n // Prefer ui-view wired mappings to resolve data, unless the resolve was explicitly bound using `bindings:`\n if (uiView.attr(attrName) && !bindings[name]) return `${attrName}='${uiView.attr(attrName)}'`;\n\n const resolveName = bindings[name] || name;\n // Pre-evaluate the expression for \"@\" bindings by enclosing in {{ }}\n // some-attr=\"{{ ::$resolve.someResolveName }}\"\n if (type === '@') return `${attrName}='{{${prefix}$resolve.${resolveName}}}'`;\n\n // Wire \"&\" callbacks to resolves that return a callback function\n // Get the result of the resolve (should be a function) and annotate it to get its arguments.\n // some-attr=\"$resolve.someResolveResultName(foo, bar)\"\n if (type === '&') {\n const res = context.getResolvable(resolveName);\n const fn = res && res.data;\n const args = (fn && services.$injector.annotate(fn)) || [];\n // account for array style injection, i.e., ['foo', function(foo) {}]\n const arrayIdxStr = isArray(fn) ? `[${fn.length - 1}]` : '';\n return `${attrName}='$resolve.${resolveName}${arrayIdxStr}(${args.join(',')})'`;\n }\n\n // some-attr=\"::$resolve.someResolveName\"\n return `${attrName}='${prefix}$resolve.${resolveName}'`;\n };\n\n const attrs = getComponentBindings(component).map(attributeTpl).join(' ');\n const kebobName = kebob(component);\n return `<${kebobName} ${attrs}></${kebobName}>`;\n }\n}\n\n// Gets all the directive(s)' inputs ('@', '=', and '<') and outputs ('&')\nfunction getComponentBindings(name: string) {\n const cmpDefs = <any[]>services.$injector.get(name + 'Directive'); // could be multiple\n if (!cmpDefs || !cmpDefs.length) throw new Error(`Unable to find component named '${name}'`);\n return cmpDefs.map(getBindings).reduce(unnestR, []);\n}\n\n// Given a directive definition, find its object input attributes\n// Use different properties, depending on the type of directive (component, bindToController, normal)\nconst getBindings = (def: any) => {\n if (isObject(def.bindToController)) return scopeBindings(def.bindToController);\n return scopeBindings(def.scope);\n};\n\ninterface BindingTuple {\n name: string;\n type: string;\n}\n\n// for ng 1.2 style, process the scope: { input: \"=foo\" }\n// for ng 1.3 through ng 1.5, process the component's bindToController: { input: \"=foo\" } object\nconst scopeBindings = (bindingsObj: Obj) =>\n Object.keys(bindingsObj || {})\n // [ 'input', [ '=foo', '=', 'foo' ] ]\n .map((key) => [key, /^([=<@&])[?]?(.*)/.exec(bindingsObj[key])])\n // skip malformed values\n .filter((tuple) => isDefined(tuple) && isArray(tuple[1]))\n // { name: ('foo' || 'input'), type: '=' }\n .map((tuple) => ({ name: tuple[1][2] || tuple[0], type: tuple[1][1] } as BindingTuple));\n"
12 ]
13}
\No newline at end of file