From 7c982febad64271ecec3efda4ea6901134926092 Mon Sep 17 00:00:00 2001 From: Bjarne Fyrstenborg Date: Wed, 22 Jul 2020 20:13:11 +0200 Subject: [PATCH] Add config to define true and false value (#8229) --- .../boolean/boolean.controller.js | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/boolean/boolean.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/boolean/boolean.controller.js index df76c2d63a..f06beed6cd 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/boolean/boolean.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/boolean/boolean.controller.js @@ -1,6 +1,23 @@ function booleanEditorController($scope, angularHelper) { + // Setup the default config + // This allow to overwrite the configuration when property editor is re-used + // in e.g. third party packages, dashboard or content app. For example when using umb-property-editor. + // At the moment this use "1/0" as default for "truevalue" and "falsevalue", but allow "True/False" as well. + // Maybe sometime later we can make it support "Yes/No" or "On/Off" as well similar to ng-true-value and ng-false-value in Angular. + var config = { + truevalue: "1", + falsevalue: "0" + }; + + // Map the user config + Utilities.extend(config, $scope.model.config); + + // Map back to the model + $scope.model.config = config; + function setupViewModel() { + $scope.renderModel = { value: false }; @@ -16,12 +33,12 @@ function booleanEditorController($scope, angularHelper) { setupViewModel(); - if( $scope.model && !$scope.model.value ) { - $scope.model.value = ($scope.renderModel.value === true) ? '1' : '0'; + if ($scope.model && !$scope.model.value) { + $scope.model.value = ($scope.renderModel.value === true) ? $scope.model.config.truevalue : $scope.model.config.falsevalue; } - //here we declare a special method which will be called whenever the value has changed from the server - //this is instead of doing a watch on the model.value = faster + // Here we declare a special method which will be called whenever the value has changed from the server + // this is instead of doing a watch on the model.value = faster $scope.model.onValueChanged = function (newVal, oldVal) { //update the display val again if it has changed from the server setupViewModel(); @@ -30,13 +47,13 @@ function booleanEditorController($scope, angularHelper) { // Update the value when the toggle is clicked $scope.toggle = function(){ angularHelper.getCurrentForm($scope).$setDirty(); - if($scope.renderModel.value){ - $scope.model.value = "0"; + if ($scope.renderModel.value){ + $scope.model.value = $scope.model.config.falsevalue; setupViewModel(); return; } - $scope.model.value = "1"; + $scope.model.value = $scope.model.config.truevalue; setupViewModel(); };