#3211 - Add colorpicker prevalue editor (#3212)

This commit is contained in:
Bjarne Fyrstenborg
2018-10-23 16:52:57 +02:00
committed by Sebastiaan Janssen
parent 1053a349f6
commit 20c215ef8c
4 changed files with 93 additions and 5 deletions

View File

@@ -42,8 +42,8 @@
&.with-labels {
.umb-color-box {
width: 120px;
height: 100%;
width: 130px;
height: auto;
display: flex;
flex-flow: row wrap;
@@ -53,15 +53,21 @@
flex: 0 0 100%;
max-width: 100%;
min-height: 80px;
padding-top: 10px;
padding: 0;
.check_circle {
margin: 15px auto;
}
.umb-color-box__label {
background: @white;
font-size: 14px;
display: flex;
flex-flow: column wrap;
flex: 0 0 100%;
flex: 1 0 100%;
justify-content: flex-end;
padding: 1px 5px;
min-height: 45px;
max-width: 100%;
margin-top: auto;
margin-bottom: -3px;

View File

@@ -6,7 +6,7 @@
<i class="icon icon-check small"></i>
</div>
<div class="umb-color-box__label" ng-if="useLabel">
<div class="umb-color-box__name truncate">{{ color.label }}</div>
<div class="umb-color-box__name truncate">{{ color.label || color.value }}</div>
<div class="umb-color-box__description">#{{ color.value }}</div>
</div>
</div>

View File

@@ -0,0 +1,64 @@
angular.module("umbraco").controller("Umbraco.PrevalueEditors.ColorPickerController",
function ($scope) {
//setup the default config
var config = {
useLabel: false
};
//map the user config
angular.extend(config, $scope.model.config);
//map back to the model
$scope.model.config = config;
$scope.isConfigured = $scope.model.prevalues && _.keys($scope.model.prevalues).length > 0;
$scope.model.items = [];
// Make an array from the dictionary
var items = [];
if (angular.isArray($scope.model.prevalues)) {
for (var i in $scope.model.prevalues) {
var oldValue = $scope.model.prevalues[i];
if (!isValidHex(oldValue.value || oldValue))
continue;
if (oldValue.hasOwnProperty("value")) {
var hexCode = toFullHex(oldValue.value);
items.push({
value: hexCode.substr(1, hexCode.length),
label: oldValue.label,
id: i
});
} else {
var hexCode = toFullHex(oldValue);
items.push({
value: hexCode.substr(1, hexCode.length),
label: oldValue,
id: i
});
}
}
// Now make the editor model the array
$scope.model.items = items;
}
function toFullHex(hex) {
if (hex.length === 4 && hex.charAt(0) === "#") {
hex = "#" + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2) + hex.charAt(3) + hex.charAt(3);
}
return hex.toLowerCase();
}
function isValidHex(str) {
console.log("str", str);
console.log("test", /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(str));
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(str);
}
});

View File

@@ -0,0 +1,18 @@
<div ng-controller="Umbraco.PrevalueEditors.ColorPickerController">
<div ng-if="!isConfigured">
<localize key="colorpicker_noColors">You haven't defined any colors</localize>
</div>
<umb-color-swatches colors="model.items"
selected-color="model.value"
size="s"
use-label="model.config.useLabel">
</umb-color-swatches>
{{model.items | json}}
<br /><br />
{{model.value}}
<input type="hidden" name="modelValue" ng-model="model.value" />
</div>