diff --git a/horizon/static/horizon/js/angular/horizon.js b/horizon/static/horizon/js/angular/horizon.js index e81feebae5..e2bd1b443f 100644 --- a/horizon/static/horizon/js/angular/horizon.js +++ b/horizon/static/horizon/js/angular/horizon.js @@ -1,5 +1,11 @@ -var horizonApp = angular.module('hz', ['hz.conf']) +var horizonApp = angular.module('hz', ['hz.conf', 'hz.utils']) .config(['$interpolateProvider', function ($interpolateProvider) { $interpolateProvider.startSymbol('{$'); $interpolateProvider.endSymbol('$}'); + }]) + .run(['hzConfig', 'hzUtils', function (hzConfig, hzUtils) { + //expose the configuration for horizon legacy variable + horizon.conf = hzConfig; + horizon.utils = hzUtils; }]); + diff --git a/horizon/static/horizon/js/angular/services/horizon.utils.js b/horizon/static/horizon/js/angular/services/horizon.utils.js new file mode 100644 index 0000000000..4ecd7cd531 --- /dev/null +++ b/horizon/static/horizon/js/angular/services/horizon.utils.js @@ -0,0 +1,64 @@ +/*global angular*/ +(function () { + 'use strict'; + function utils(hzConfig, $log, $rootScope, $compile) { + return { + /* + Use the log levels of http://docs.angularjs.org/api/ng.$log + default to log level. + */ + log: function (msg, lvl) { + if (hzConfig.debug) { + ($log[lvl] || $log.log)(msg); + } + }, + capitalize: function (string) { + return string.charAt(0).toUpperCase() + string.slice(1); + }, + /* + Adds commas to any integer or numbers within a string for human display. + + EG: + horizon.utils.humanizeNumbers(1234); -> "1,234" + horizon.utils.humanizeNumbers("My Total: 1234"); -> "My Total: 1,234" + */ + humanizeNumbers: function (number) { + return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); + }, + + /* + Truncate a string at the desired length. Optionally append an ellipsis + to the end of the string. + + EG: + horizon.utils.truncate("String that is too long.", 18, true); -> + "String that is too…" + */ + truncate: function (string, size, includeEllipsis) { + if (string.length > size) { + if (includeEllipsis) { + return string.substring(0, (size - 3)) + "…"; + } + + return string.substring(0, size); + } + + return string; + }, + loadAngular: function (element) { + try { + $compile(element)($rootScope); + $rootScope.$apply(); + } catch (err) {} + /* + Compilation fails when it could not find a directive, + fails silently on this, it is an angular behaviour. + */ + + } + }; + } + + angular.module('hz.utils', ['hz.conf']) + .service('hzUtils', ['hzConfig', '$log', '$rootScope', '$compile', utils]); +}()); \ No newline at end of file diff --git a/horizon/static/horizon/js/horizon.utils.js b/horizon/static/horizon/js/horizon.utils.js deleted file mode 100644 index 58f31e576e..0000000000 --- a/horizon/static/horizon/js/horizon.utils.js +++ /dev/null @@ -1,57 +0,0 @@ -/* Utilities for common needs which aren't JS builtins. */ -horizon.utils = { - // Log function which checks for DEBUG and the existence of a console. - log: function () { - if (horizon.conf.debug && typeof(console) !== "undefined" && typeof(console.log) !== "undefined") { - console.log(arguments); - } - }, - - capitalize: function(string) { - return string.charAt(0).toUpperCase() + string.slice(1); - }, - - /* - Adds commas to any integer or numbers within a string for human display. - - EG: - horizon.utils.humanizeNumbers(1234); -> "1,234" - horizon.utils.humanizeNumbers("My Total: 1234"); -> "My Total: 1,234" - */ - humanizeNumbers: function(number) { - return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); - }, - - /* - Truncate a string at the desired length. Optionally append an ellipsis - to the end of the string. - - EG: - horizon.utils.truncate("String that is too long.", 18, true); -> - "String that is too…" - */ - truncate: function(string, size, includeEllipsis) { - if(string.length > size) { - if(includeEllipsis) { - return string.substring(0, (size - 3)) + "…"; - } else { - return string.substring(0, size); - } - } else { - return string; - } - }, - - loadAngular: function (element) { - angular.injector(['ng', 'hz']). - invoke(['$rootScope', '$compile', function ($rootScope, $compile) { - try { - $compile(element)($rootScope); - $rootScope.$apply(); - } catch (err) {} - /* - Compilation fails when it could not find a directive, fails silently on this, it is an angular behaviour. - */ - }]); - } -}; diff --git a/horizon/templates/horizon/_conf.html b/horizon/templates/horizon/_conf.html index d6f23439eb..6564afcc58 100644 --- a/horizon/templates/horizon/_conf.html +++ b/horizon/templates/horizon/_conf.html @@ -25,11 +25,7 @@ horizonApp fade_duration: {{ HORIZON_CONFIG.auto_fade_alerts.fade_duration|default:"1500" }}, types: {{ HORIZON_CONFIG.auto_fade_alerts.types|default:"[]"|safe }} }; -}]) - .run(['hzConfig', function (hzConfig) { - //expose the configuration for horizon legacy variable - horizon.conf = hzConfig; - }]); +}]); {% endcompress %} diff --git a/horizon/templates/horizon/_scripts.html b/horizon/templates/horizon/_scripts.html index 9e49a17266..6e9f1c55ea 100644 --- a/horizon/templates/horizon/_scripts.html +++ b/horizon/templates/horizon/_scripts.html @@ -9,6 +9,7 @@ + @@ -38,7 +39,6 @@ - diff --git a/horizon/templates/horizon/qunit.html b/horizon/templates/horizon/qunit.html index 2a81d7725f..f0a68e3f03 100644 --- a/horizon/templates/horizon/qunit.html +++ b/horizon/templates/horizon/qunit.html @@ -17,7 +17,7 @@ {% include "horizon/_scripts.html" %} -
+