2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
24 * "progressBarDirective.js" provides a progress bar directive.
28 * <div progress-bar value="percentProgress"></div>
30 * "percentProgress" is the numeric percent progress to be displayed (0 to 100)
31 * expressed as a number only (i.e. no trailing "%"). An "scoped" Angular value
32 * can be used (e.g. "{{percentProgress}}").
34 * Two additional attributes can also be included:
36 * increases-only="true | false"
38 * Normally, the progress bar always updates the display with whatever value is
39 * set. Alternatively, if "increases-only" is set to "true", the display will
40 * only be updated if "percentProgress" is >= the previous value.
44 * Provides a method ... $scope.control.reset()" ... that a controller can call
45 * to reset the progress to it's initial zero state. This would only expected to
46 * be needed if A) increases-only is set to true and B) there is a need to reset
47 * the controller to 0. If increases-only is set to false or not present, an
48 * alternative method of resetting the progress is to just tset percentProgress
53 * 1) The extended attribute "ngx-show" should be used instead of "ng-show" if
54 * the control needs to be conditionally visible. Example:
55 * ngx-show="{{isProgressVisible}}"
57 * 2) $scope.control.reset() should be conditionally called as follows IF it is
58 * called immediately after HTML is rendered. This is due to a timing-related
61 * 3) The progress bar displays values of "0" and "100" if precentProgress is,
62 * respectively, less than 0 or greater than 100.
66 * The ECOMP portal styling uses the class named "progress". The class
67 * attributes can be overridden in CSS. This example was tested:
69 * .progress { margin: 0px 10px; height: 40px }
71 * Additional styling can be applied to the progress-bar element. Example:
73 * div[progress-bar=""] { padding-top: 10px; }
75 * if (angular.isFunction($scope.control.reset)) { $scope.control.reset(); }
79 * This code assumes dependency files provided by the ECOMP Portal framework are
80 * included. For example, ".../app/fusion/external/ebz/sandbox/styles/base.css"
81 * is one required dependency. There may also be others.
84 var progressBarDirective = function() {
86 var style = "font-weight: bold;";
88 * The 3 "aria-*" properties were added as per an Internet reference
89 * example. These appear to have no impact on current behavior but are
90 * retained for future reference.
92 var properties = "class='progress-bar' role='progressbar' "
93 + "aria-valuenow='' aria-valuemin='0' aria-valuemax='100'";
94 var previousValue = 0;
96 var updateProgress = function(element, attrs, valueAsString) {
97 if (valueAsString === undefined || valueAsString === null
98 || valueAsString === "") {
101 var valueAsInteger = parseInt(valueAsString);
102 if (valueAsInteger > 100) {
103 valueAsInteger = 100;
104 valueAsString = "100";
106 if (attrs.increasesOnly === "true") {
107 if (valueAsInteger >= previousValue) {
108 previousValue = valueAsInteger;
113 element.css("width", valueAsString + "%");
114 if (valueAsInteger >= 100) {
115 element.removeClass("progress-bar-info").addClass(
116 "progress-bar-success");
118 element.removeClass("progress-bar-success").addClass(
119 "progress-bar-info");
121 if (valueAsInteger >= 5) {
122 element.html(valueAsString + " %");
125 * Hide text since color combination is barely visible when progress
136 template : "<div ng-transclude " + properties + " style='" + style
142 link : function(scope, element, attrs) {
145 * It should be possible to alternatively add this wrapper in the
146 * template instead of using "element.wrap". Some techniques were
147 * attempted but were unsuccessful.
149 element.wrap("<div class='progress'></div");
151 var control = scope.control || {};
153 control.reset = function() {
155 updateProgress(element, attrs, 0);
158 attrs.$observe("value", function(valueString) {
159 updateProgress(element, attrs, valueString);
162 attrs.$observe("ngxShow", function(valueString) {
163 if (valueString === "false") {
164 element.parent().hide();
166 element.parent().show();
173 app.directive("progressBar", progressBarDirective);