Skip to content

Commit

Permalink
Replace horizon.utils with an angular one
Browse files Browse the repository at this point in the history
    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
  • Loading branch information
Maxime Vidori committed Feb 17, 2014
1 parent 2d591be commit 1356315
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 65 deletions.
8 changes: 7 additions & 1 deletion 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;
}]);

64 changes: 64 additions & 0 deletions 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]);
}());
57 changes: 0 additions & 57 deletions horizon/static/horizon/js/horizon.utils.js

This file was deleted.

6 changes: 1 addition & 5 deletions horizon/templates/horizon/_conf.html
Expand Up @@ -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;
}]);
}]);

</script>
{% endcompress %}
2 changes: 1 addition & 1 deletion horizon/templates/horizon/_scripts.html
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion horizon/templates/horizon/qunit.html
Expand Up @@ -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>
Expand Down

0 comments on commit 1356315

Please sign in to comment.