From df8859beb731f1d269bb980d5d0f45b54a546ead Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Tue, 14 Jul 2015 13:16:19 -0400 Subject: [PATCH] Bundle angular templates for app We were already bundling angular templates for node tests; this commit reuses the same setup to include them in the app; this means any new-style template (those under js folder) will be included in angular's template cache when the app loads, thereby skipping the ajax request for that template. Huzzah! --- awx/ui/static/js/app.js | 1 + .../static/js/shared/icon/icon.directive.js | 44 ++++++++++++------- .../js/shared/icon/include-svg.directive.js | 3 +- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/awx/ui/static/js/app.js b/awx/ui/static/js/app.js index 113d16a268..00f466c37c 100644 --- a/awx/ui/static/js/app.js +++ b/awx/ui/static/js/app.js @@ -86,6 +86,7 @@ var tower = angular.module('Tower', [ dashboard.name, moment.name, templateUrl.name, + 'templates', 'AuthService', 'Utilities', 'LicenseHelper', diff --git a/awx/ui/static/js/shared/icon/icon.directive.js b/awx/ui/static/js/shared/icon/icon.directive.js index d4da46b5a6..c38abc7698 100644 --- a/awx/ui/static/js/shared/icon/icon.directive.js +++ b/awx/ui/static/js/shared/icon/icon.directive.js @@ -1,30 +1,44 @@ export default [ 'templateUrl', - function(templateUrl) { + '$rootScope', + function(templateUrl, $rootScope) { return { restrict: 'E', templateUrl: templateUrl('shared/icon/icon'), scope: { }, link: function(scope, element, attrs) { - var svg = $('svg', element); - var iconPath = '#' + attrs.name; - // Make a copy of the tag to insert its contents into this - // element's svg tag - var content = $(iconPath).clone(); + function buildSvgs() { + var svg = $('svg', element); + var iconPath = '#' + attrs.name; - // Copy classes & viewBox off the so that we preserve any styling - // when we copy the item inline - var classes = $(iconPath).attr('class'); + if ($(iconPath).length === 0) { + return; + } - // viewBox needs to be access via native - // javascript's setAttribute function - var viewBox = $(iconPath)[0].getAttribute('viewBox'); + // Make a copy of the tag to insert its contents into this + // element's svg tag + var content = $(iconPath).clone(); - svg[0].setAttribute('viewBox', viewBox); - svg.attr('class', classes) - .html(content.contents()); + // Copy classes & viewBox off the so that we preserve any styling + // when we copy the item inline + var classes = $(iconPath).attr('class'); + + // viewBox needs to be access via native + // javascript's setAttribute function + var viewBox = $(iconPath)[0].getAttribute('viewBox'); + + svg[0].setAttribute('viewBox', viewBox); + svg.attr('class', classes) + .html(content.contents()); + } + + $rootScope.$on('include-svg.svg-ready', function() { + buildSvgs(); + }); + + buildSvgs(); } }; diff --git a/awx/ui/static/js/shared/icon/include-svg.directive.js b/awx/ui/static/js/shared/icon/include-svg.directive.js index 13556de1f3..dcfa55f045 100644 --- a/awx/ui/static/js/shared/icon/include-svg.directive.js +++ b/awx/ui/static/js/shared/icon/include-svg.directive.js @@ -1,4 +1,4 @@ -export default ['$http', function($http) { +export default ['$http', '$rootScope', function($http, $rootScope) { return { restrict: 'E', link: function(scope, element, attrs) { @@ -6,6 +6,7 @@ export default ['$http', function($http) { $http.get(path).then(function(response) { element.append(response.data); + $rootScope.$emit('include-svg.svg-ready'); }); } };