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
This commit is contained in:
Maxime Vidori 2014-02-06 20:32:18 +01:00
parent 2d591beff5
commit 135631529a
6 changed files with 74 additions and 65 deletions

View File

@ -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;
}]);

View File

@ -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]);
}());

View File

@ -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.
*/
}]);
}
};

View File

@ -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;
}]);
}]);
</script>
{% endcompress %}

View File

@ -9,6 +9,7 @@
<script src='{{ STATIC_URL }}horizon/js/angular/controllers/dummy.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/directives/forms.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/horizon.conf.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/services/horizon.utils.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/lib/jquery/jquery.cookie.js' type='text/javascript' charset="utf-8"></script>
<script src='{{ STATIC_URL }}horizon/lib/jquery/jquery.quicksearch.js' type='text/javascript' charset="utf-8"></script>
@ -38,7 +39,6 @@
<script src='{{ STATIC_URL }}horizon/js/horizon.tabs.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.templates.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.users.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.utils.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.membership.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.networktopology.js' type='text/javascript' charset='utf-8'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.d3piechart.js' type='text/javascript' charset='utf-8'></script>

View File

@ -17,7 +17,7 @@
{% include "horizon/_scripts.html" %}
</head>
<body>
<body ng-app="hz">
<h1 id="qunit-header">Horizon JavaScript Tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>