From fca46ab60ffd798fda67ea045905f7932ed1347b Mon Sep 17 00:00:00 2001 From: Tyr Johanson Date: Tue, 31 May 2016 12:52:14 -0600 Subject: [PATCH] Pre-populate the Angular template cache and allow template overrides This patch populates the Angular template cache from Django. This eliminates the need for Angular to do an http get for every HTML fragment. In addition, now that we are filling the template cache, this patch introduces the logic needed to override any Angular template HTML from the current theme. How it works: A new template tag is created called "template_cache_preloads". This tag is used in _scripts.html to generate a list of text/javascript script tags, each one containing an Angular "run" method that loads a template contents into the Angular template cache. The first time any Horizon page is loaded after server start, the template cache preloads are computed for the current theme. The output of this tag is cached for 30 days in Django using the "cache" tag. Further, that cached result is wrapped in a "compress js" tag to collapse the individual +{% endfor %} \ No newline at end of file diff --git a/openstack_dashboard/templates/angular/angular_templates.js b/openstack_dashboard/templates/angular/angular_templates.js new file mode 100644 index 000000000..167e5ae69 --- /dev/null +++ b/openstack_dashboard/templates/angular/angular_templates.js @@ -0,0 +1,11 @@ +{% autoescape off %} +{% load angular_escapes from angular %} +angular + .module('horizon.app') + .run(['$templateCache', function($templateCache) { + $templateCache.put( + "{{ static_path }}", + "{{ template_html|angular_escapes }}" + ); +}]); +{% endautoescape %} diff --git a/openstack_dashboard/templates/horizon/_scripts.html b/openstack_dashboard/templates/horizon/_scripts.html index 345083542..409312337 100644 --- a/openstack_dashboard/templates/horizon/_scripts.html +++ b/openstack_dashboard/templates/horizon/_scripts.html @@ -1,7 +1,13 @@ {% load compress %} {% load datepicker_locale from horizon %} +{% load template_cache_age from horizon %} +{% load themes %} +{% load cache %} +{% load angular_templates from angular %} {% datepicker_locale as DATEPICKER_LOCALE %} +{% current_theme as THEME %} +{% template_cache_age as NG_TEMPLATE_CACHE_AGE %} {% include "horizon/_script_i18n.html" %} @@ -66,6 +72,13 @@ {% endfor %} {% block custom_js_files %}{% endblock %} + +{% endcompress %} + +{% compress js file angular_template_cache_preloads %} + {% cache NG_TEMPLATE_CACHE_AGE angular 'template_cache_preloads' THEME %} + {% angular_templates %} + {% endcache %} {% endcompress %} {% comment %} Client-side Templates (These should *not* be inside the "compress" tag.) {% endcomment %} diff --git a/openstack_dashboard/test/settings.py b/openstack_dashboard/test/settings.py index fee5aa0e2..c62c298fc 100644 --- a/openstack_dashboard/test/settings.py +++ b/openstack_dashboard/test/settings.py @@ -12,6 +12,7 @@ import os +from django.utils.translation import pgettext_lazy from horizon.test.settings import * # noqa from horizon.utils import secret_key from openstack_dashboard import exceptions @@ -43,6 +44,22 @@ TEMPLATE_DIRS = ( CUSTOM_THEME_PATH = 'themes/default' +# 'key', 'label', 'path' +AVAILABLE_THEMES = [ + ( + 'default', + pgettext_lazy('Default style theme', 'Default'), + 'themes/default' + ), ( + 'material', + pgettext_lazy("Google's Material Design style theme", "Material"), + 'themes/material' + ), +] + +# Theme Static Directory +THEME_COLLECTION_DIR = 'themes' + TEMPLATE_CONTEXT_PROCESSORS += ( 'openstack_dashboard.context_processors.openstack', ) @@ -101,7 +118,8 @@ INSTALLED_APPS[0:0] = [] # the stacks MappingsTests are updated with the new URL path. HORIZON_CONFIG['swift_panel'] = 'legacy' -find_static_files(HORIZON_CONFIG) +find_static_files(HORIZON_CONFIG, AVAILABLE_THEMES, + THEME_COLLECTION_DIR, ROOT_PATH) # Set to True to allow users to upload images to glance via Horizon server. # When enabled, a file form field will appear on the create image form. diff --git a/releasenotes/notes/bp-angular-template-overrides-9f05ffd61367245a.yaml b/releasenotes/notes/bp-angular-template-overrides-9f05ffd61367245a.yaml new file mode 100644 index 000000000..71cab054d --- /dev/null +++ b/releasenotes/notes/bp-angular-template-overrides-9f05ffd61367245a.yaml @@ -0,0 +1,11 @@ +--- +features: + - > + [`blueprint angular-template-overrides `_] + This blueprint provides a way for deployers to use a theme to override HTML + fragments used by Angular code in Horizon. For example, to override the + launch instance help panel when the 'material' theme is used, create + openstack_dashboard/themes/material/static/templates/framework + /widgets/help-panel/help-panel.html. All of the client side templates are + now compiled into a single JavaScript file that is minified and is given + as an additional file in the manifest.json file. \ No newline at end of file