From 135631529ae6bff12635a5d9800134af36d4eb88 Mon Sep 17 00:00:00 2001 From: Maxime Vidori Date: Thu, 6 Feb 2014 20:32:18 +0100 Subject: [PATCH] Replace horizon.utils with an angular one Horizon utilitaries are now fully integrated into the angular application. The object horizon.utils is still available in the application in order to keep the compatibility, it is a link to the angular constant object. Change-Id: I3d44c5c281a8e61e93f4d1b7c3b8b61c128e36ea Implements: blueprint horizon-angular --- horizon/static/horizon/js/angular/horizon.js | 8 ++- .../js/angular/services/horizon.utils.js | 64 +++++++++++++++++++ horizon/static/horizon/js/horizon.utils.js | 57 ----------------- horizon/templates/horizon/_conf.html | 6 +- horizon/templates/horizon/_scripts.html | 2 +- horizon/templates/horizon/qunit.html | 2 +- 6 files changed, 74 insertions(+), 65 deletions(-) create mode 100644 horizon/static/horizon/js/angular/services/horizon.utils.js delete mode 100644 horizon/static/horizon/js/horizon.utils.js diff --git a/horizon/static/horizon/js/angular/horizon.js b/horizon/static/horizon/js/angular/horizon.js index e81feebae5e..e2bd1b443fc 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 00000000000..4ecd7cd531b --- /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 58f31e576e0..00000000000 --- 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 d6f23439ebb..6564afcc586 100644 --- a/horizon/templates/horizon/_conf.html +++ b/horizon/templates/horizon/_conf.html @@ -25,11 +25,7 @@ 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 9e49a17266f..6e9f1c55eab 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 2a81d7725fc..f0a68e3f032 100644 --- a/horizon/templates/horizon/qunit.html +++ b/horizon/templates/horizon/qunit.html @@ -17,7 +17,7 @@ {% include "horizon/_scripts.html" %} - +

Horizon JavaScript Tests