arnaucube
arnaucube/commonroutesAppPublic Self-hosted repository
commonroutesApp/www/lib/angular-ui-router/src/templateFactory.js
www/lib/angular-ui-router/src/templateFactory.jsRaw
  1/**
  2 * @ngdoc object
  3 * @name ui.router.util.$templateFactory
  4 *
  5 * @requires $http
  6 * @requires $templateCache
  7 * @requires $injector
  8 *
  9 * @description
 10 * Service. Manages loading of templates.
 11 */
 12$TemplateFactory.$inject = ['$http', '$templateCache', '$injector'];
 13function $TemplateFactory(  $http,   $templateCache,   $injector) {
 14
 15  /**
 16   * @ngdoc function
 17   * @name ui.router.util.$templateFactory#fromConfig
 18   * @methodOf ui.router.util.$templateFactory
 19   *
 20   * @description
 21   * Creates a template from a configuration object. 
 22   *
 23   * @param {object} config Configuration object for which to load a template. 
 24   * The following properties are search in the specified order, and the first one 
 25   * that is defined is used to create the template:
 26   *
 27   * @param {string|object} config.template html string template or function to 
 28   * load via {@link ui.router.util.$templateFactory#fromString fromString}.
 29   * @param {string|object} config.templateUrl url to load or a function returning 
 30   * the url to load via {@link ui.router.util.$templateFactory#fromUrl fromUrl}.
 31   * @param {Function} config.templateProvider function to invoke via 
 32   * {@link ui.router.util.$templateFactory#fromProvider fromProvider}.
 33   * @param {object} params  Parameters to pass to the template function.
 34   * @param {object} locals Locals to pass to `invoke` if the template is loaded 
 35   * via a `templateProvider`. Defaults to `{ params: params }`.
 36   *
 37   * @return {string|object}  The template html as a string, or a promise for 
 38   * that string,or `null` if no template is configured.
 39   */
 40  this.fromConfig = function (config, params, locals) {
 41    return (
 42      isDefined(config.template) ? this.fromString(config.template, params) :
 43      isDefined(config.templateUrl) ? this.fromUrl(config.templateUrl, params) :
 44      isDefined(config.templateProvider) ? this.fromProvider(config.templateProvider, params, locals) :
 45      null
 46    );
 47  };
 48
 49  /**
 50   * @ngdoc function
 51   * @name ui.router.util.$templateFactory#fromString
 52   * @methodOf ui.router.util.$templateFactory
 53   *
 54   * @description
 55   * Creates a template from a string or a function returning a string.
 56   *
 57   * @param {string|object} template html template as a string or function that 
 58   * returns an html template as a string.
 59   * @param {object} params Parameters to pass to the template function.
 60   *
 61   * @return {string|object} The template html as a string, or a promise for that 
 62   * string.
 63   */
 64  this.fromString = function (template, params) {
 65    return isFunction(template) ? template(params) : template;
 66  };
 67
 68  /**
 69   * @ngdoc function
 70   * @name ui.router.util.$templateFactory#fromUrl
 71   * @methodOf ui.router.util.$templateFactory
 72   * 
 73   * @description
 74   * Loads a template from the a URL via `$http` and `$templateCache`.
 75   *
 76   * @param {string|Function} url url of the template to load, or a function 
 77   * that returns a url.
 78   * @param {Object} params Parameters to pass to the url function.
 79   * @return {string|Promise.<string>} The template html as a string, or a promise 
 80   * for that string.
 81   */
 82  this.fromUrl = function (url, params) {
 83    if (isFunction(url)) url = url(params);
 84    if (url == null) return null;
 85    else return $http
 86        .get(url, { cache: $templateCache, headers: { Accept: 'text/html' }})
 87        .then(function(response) { return response.data; });
 88  };
 89
 90  /**
 91   * @ngdoc function
 92   * @name ui.router.util.$templateFactory#fromProvider
 93   * @methodOf ui.router.util.$templateFactory
 94   *
 95   * @description
 96   * Creates a template by invoking an injectable provider function.
 97   *
 98   * @param {Function} provider Function to invoke via `$injector.invoke`
 99   * @param {Object} params Parameters for the template.
100   * @param {Object} locals Locals to pass to `invoke`. Defaults to 
101   * `{ params: params }`.
102   * @return {string|Promise.<string>} The template html as a string, or a promise 
103   * for that string.
104   */
105  this.fromProvider = function (provider, params, locals) {
106    return $injector.invoke(provider, null, locals || { params: params });
107  };
108}
109
110angular.module('ui.router.util').service('$templateFactory', $TemplateFactory);