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=========================================================
23 var parameterBlockDirective = function($log, PARAMETER, UtilityService) {
25 * If "IS_SINGLE_OPTION_AUTO_SELECTED" is set to "true" ...
27 * IF these 3 conditions all exist:
29 * 1) The parameter type is PARAMETER.SELECT
31 * 2) AND the "prompt" attribute is set to a string.
33 * 3) AND the optionList" only contains a single entry
35 * THEN the "prompt" will not be displayed as an initial select option.
38 var IS_SINGLE_OPTION_AUTO_SELECTED = false;
41 * Optionally remove "nameStyle" and "valueStyle" "width" entries to set
44 var tableStyle = "width: auto; margin: 0 auto; border-collapse: collapse; border: none;";
45 var nameStyle = "width: 220px; text-align: left; vertical-align: middle; font-weight: bold; padding: 3px 5px; border: none;";
46 var valueStyle = "width: 400px; text-align: left; vertical-align: middle; padding: 3px 5px; border: none;";
47 var checkboxValueStyle = "width: 400px; text-align: center; vertical-align: middle; padding: 3px 5px; border: none;"
48 var textInputStyle = "height: 25px; padding: 2px 5px;";
49 var checkboxInputStyle = "height: 18px; width: 18px; padding: 2px 5px;";
50 var selectStyle = "height: 25px; padding: 2px; text-align: center;";
51 var requiredLabelStyle = "width: 25px; padding: 5px 10px 10px 5px;";
52 var textInputPrompt = "Enter data";
54 var getParameterHtml = function(parameter, editable) {
55 var style = valueStyle;
56 var attributeString = "";
57 if (parameter.type === PARAMETER.BOOLEAN) {
58 style = checkboxValueStyle;
60 if (UtilityService.hasContents(parameter.description)) {
61 attributeString += " title=' " + parameter.description + " '";
63 var html = "<tr><td style='" + nameStyle + "'" + attributeString + ">"
64 + getNameHtml(parameter) + "</td><td style='" + style + "'>";
65 if (editable === undefined) {
66 if (UtilityService.hasContents(parameter.value)) {
67 html += parameter.value;
70 html += getValueHtml(parameter);
76 var updateParameter = function(parameter, element, editable) {
77 $(element).parent().parent().children("td").first().html(
78 getNameHtml(parameter));
79 if (editable === undefined) {
80 $(element).html(parameter.value);
82 $(element).parent().html(getValueHtml(parameter));
86 var getNameHtml = function(parameter) {
87 if (parameter.isVisible === false) {
91 if (UtilityService.hasContents(parameter.name)) {
92 name = parameter.name;
96 var requiredLabel = "";
97 if (parameter.isRequired) {
98 requiredLabel = "<img src='app/vid/images/asterisk.png' style='"
99 + requiredLabelStyle + "'></img>";
101 return name + ":" + requiredLabel;
104 var getValueHtml = function(parameter) {
105 var attributeString = " parameter-id='" + parameter.id + "'";
106 var additionalStyle = "";
107 if (parameter.isEnabled === false) {
108 attributeString += " disabled='disabled'";
110 if (parameter.isRequired) {
111 attributeString += " is-required='true'";
113 if (UtilityService.hasContents(parameter.description)) {
114 attributeString += " title=' " + parameter.description + " '";
116 if (parameter.isVisible === false) {
117 additionalStyle = "visibility: hidden;";
120 if (UtilityService.hasContents(parameter.name)) {
121 name = parameter.name;
125 attributeString += " parameter-name='" + name + "'";
127 switch (parameter.type) {
128 case PARAMETER.BOOLEAN:
129 if (parameter.value) {
130 attributeString += " checked='checked'";
132 return "<input type='checkbox'" + attributeString + " style='"
133 + checkboxInputStyle + additionalStyle + "'></input>";
135 case PARAMETER.SELECT:
136 if (UtilityService.hasContents(parameter.prompt)) {
137 attributeString += " prompt='" + parameter.prompt + "'";
139 return "<select" + attributeString + " style='" + selectStyle
140 + additionalStyle + "'>" + getOptionListHtml(parameter)
143 case PARAMETER.STRING:
146 if (UtilityService.hasContents(parameter.value)) {
147 value = " value='" + parameter.value + "'";
149 if (UtilityService.hasContents(parameter.prompt)) {
150 attributeString += " placeholder='" + parameter.prompt + "'";
151 } else if (textInputPrompt !== "") {
152 attributeString += " placeholder='" + textInputPrompt + "'";
154 return "<input type='text'" + attributeString + " style='"
155 + textInputStyle + additionalStyle + "'" + value
160 var getOptionListHtml = function(parameter) {
164 if (!angular.isArray(parameter.optionList)
165 || parameter.optionList.length === 0) {
169 if (UtilityService.hasContents(parameter.prompt)) {
170 if (!(IS_SINGLE_OPTION_AUTO_SELECTED && parameter.optionList.length === 1)) {
171 html += "<option value=''>" + parameter.prompt + "</option>";
175 for (var i = 0; i < parameter.optionList.length; i++) {
176 var option = parameter.optionList[i];
177 var name = option.name;
179 if (option.id === undefined) {
182 if (name === undefined) {
187 html += "<option value='" + value + "'>" + name + "</option>";
192 var getParameter = function(element, expectedId) {
193 var id = $(element).attr("parameter-id");
194 if (expectedId !== undefined && expectedId !== id) {
200 if ($(element).prop("type") === "checkbox") {
201 parameter.value = $(element).prop("checked");
203 if ($(element).prop("type") === "text") {
204 $(element).val($(element).val().trim());
206 parameter.value = $(element).val();
208 if ($(element).prop("selectedIndex") === undefined) {
209 parameter.selectedIndex = -1;
211 parameter.selectedIndex = $(element).prop("selectedIndex");
212 if (UtilityService.hasContents($(element).attr("prompt"))) {
213 parameter.selectedIndex--;
219 var getRequiredField = function(element) {
220 if ($(element).prop("type") === "text") {
221 $(element).val($(element).val().trim());
223 if ($(element).val() === "" || $(element).val() === null) {
224 return '"' + $(element).attr("parameter-name") + '"';
230 var callback = function(element, scope) {
232 id : $(element).attr("parameter-id")
239 template : "<div><table style='" + tableStyle + "'></table></div>",
244 link : function(scope, element, attrs) {
246 var control = scope.control || {};
248 control.setList = function(parameterList) {
250 for (var i = 0; i < parameterList.length; i++) {
251 html += getParameterHtml(parameterList[i], attrs.editable);
254 element.find("input, select").bind("change", function() {
255 callback(this, scope);
259 control.updateList = function(parameterList) {
260 element.find("input, select").each(
262 for (var i = 0; i < parameterList.length; i++) {
263 if (parameterList[i].id === $(this).attr(
265 updateParameter(parameterList[i], this,
272 control.getList = function(expectedId) {
273 var parameterList = new Array();
274 element.find("input, select").each(function() {
275 var parameter = getParameter(this, expectedId);
276 if (parameter !== undefined) {
277 parameterList.push(parameter);
280 return parameterList;
283 control.getRequiredFields = function() {
284 var requiredFields = "";
286 element.find("input, select").each(function() {
287 if ($(this).attr("is-required") === "true") {
288 var requiredField = getRequiredField(this);
289 if (requiredField !== "") {
291 requiredFields = requiredField;
297 return requiredFields;
298 } else if (count == 1) {
299 return requiredFields + " and 1 other field";
301 return requiredFields + " and " + count + " other fields";
308 app.directive('parameterBlock', [ "$log", "PARAMETER", "UtilityService",
309 parameterBlockDirective ]);