You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by GitBox <gi...@apache.org> on 2019/03/29 10:52:34 UTC

[GitHub] [brooklyn-ui] tbouron commented on a change in pull request #129: UI for defining parameters

tbouron commented on a change in pull request #129: UI for defining parameters
URL: https://github.com/apache/brooklyn-ui/pull/129#discussion_r270354008
 
 

 ##########
 File path: ui-modules/blueprint-composer/app/components/spec-editor/spec-editor.directive.js
 ##########
 @@ -888,13 +1037,151 @@ export function specEditorDirective($rootScope, $templateCache, $injector, $sani
 
                 // If we need an integer, check if the value is a number
                 if (config.type === 'java.lang.Integer' && !angular.isNumber(value) && !(value instanceof Dsl)) {
-                    model.addIssue(Issue.builder().group('config').ref(key).message('<code>'+$sanitize(value)+'</code> is not a number').build());
+                    model.addIssue(Issue.builder().group('config').ref(key).message('<code>' + $sanitize(value) + '</code> is not a number').build());
                 }
                 if (scope.state.config.codeModeError[key]) {
-                    model.addIssue(Issue.builder().group('config').ref(key).message('<code>'+$sanitize(value)+'</code> is not valid JSON').build());
+                    model.addIssue(Issue.builder().group('config').ref(key).message('<code>' + $sanitize(value) + '</code> is not valid JSON').build());
                 }
             });
         }
+
+        /**
+         * The parameter data for each item is stored in multiple places, similar to configuration data:
+         *
+         * scope.parameters
+         *   An array of values used/set by editor
+         *
+         * scope.model.parameters
+         *   An array of values used in internal model
+         *
+         * scope.state.parameters.codeModeActive
+         *   A map of booleans indicating whether to edit the parameter definition as JSON
+         */
+
+        function loadLocalParametersFromModel() {
+            let modelParams = scope.model.parameters;
+            let result = [];
+            for (let paramRef of modelParams) {
+                result.push(paramRef);
+            }
+            scope.parameters = result;
+        }
+
+        function getParameter(name) {
+            return scope.parameters.find(p => p.name === name);
+        }
+
+        function checkNameChange(oldName, newName) {
+            if (!oldName) {
+                return true;
+            }
+            if (oldName && oldName!=newName) {
+                scope.state.parameters.codeModeActive[newName] = scope.state.parameters.codeModeActive[oldName];
+                scope.state.parameters.codeModeError[newName] = scope.state.parameters.codeModeError[oldName]; 
+                delete scope.state.parameters.codeModeActive[oldName]; 
+                delete scope.state.parameters.codeModeError[oldName];
+                return true;
+            }
+            return false;            
+        }
+        function setModelFromLocalParameters() {
+            if (scope.state.parameters && scope.state.parameters.edit && scope.state.parameters.edit.item) {
+                let item = scope.state.parameters.edit.item;
+                scope.state.parameters.edit.errors = [];
+                if (scope.state.parameters.codeModeActive[item.name]) {
+                    try {
+                        let parsed = JSON.parse(scope.state.parameters.edit.json);
+                        checkNameChange(item.name, parsed.name);
+                        if (JSON.stringify(item)==JSON.stringify(parsed)) {
+                            // no change; don't change else we get a digest cycle
+                        } else {
+                            Object.keys(item).forEach((k) => delete item[k]);
+                            Object.assign(item, parsed);
+                        }
+                        
+                    } catch (e) {
+                        // $log.warn("ERROR parsing json", scope.state.parameters.edit.json, e);
+                        scope.state.parameters.edit.errors.push({ message: "Invalid JSON for parameter" });
+                    }
+                    
+                } else {
+                    if (scope.state.parameters.edit.newName) {
+                        if (blueprintService.isReservedKey(scope.state.parameters.edit.newName)) {
+                            scope.state.parameters.edit.errors.push({ message: "Illegal key name" });
+                        } else if (checkNameChange(item.name, scope.state.parameters.edit.newName)) {
+                            item.name = scope.state.parameters.edit.newName;
+                        }
+                    } else {
+                        scope.state.parameters.edit.errors.push({ message: "Key name must not be blank" });
+                    }
+                     
+                    try {
+                        let c = scope.state.parameters.edit.constraints ? JSON.parse(scope.state.parameters.edit.constraints) : [];
+                        if (Array.isArray(c)) {
+                            if (c.length==0) { 
+                                delete item['constraints'];
+                            } else {
+                                item.constraints = c;
+                            }
+                        } else {
+                            scope.state.parameters.edit.errors.push({ message: "Constraint JSON must be a list" });
+                        }
+                    } catch (e) {
+                        // $log.warn("ERROR parsing constraints", scope.state.parameters.edit.constraints, e);
+                        scope.state.parameters.edit.errors.push({ message: "Invalid constraint JSON" });
+                    }
+                    
+                    // empty values are removed
+                    if (item.description == '') {
+                        delete item['description'];
+                    } 
+                    if (item.label == '') {
 
 Review comment:
   ```suggestion
                       if (item.label === '') {
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services