Added the cache expiration to the new visual console

This commit is contained in:
Alejandro Gallardo Escobar 2019-08-05 12:58:25 +02:00
parent b30baffb5d
commit 3333cb2b59
2 changed files with 94 additions and 29 deletions

View File

@ -187,18 +187,19 @@ class Item extends CachedModel
protected function decode(array $data): array
{
$decodedData = [
'id' => (int) $data['id'],
'type' => (int) $data['type'],
'label' => static::extractLabel($data),
'labelPosition' => static::extractLabelPosition($data),
'isLinkEnabled' => static::extractIsLinkEnabled($data),
'isOnTop' => static::extractIsOnTop($data),
'parentId' => static::extractParentId($data),
'aclGroupId' => static::extractAclGroupId($data),
'width' => (int) $data['width'],
'height' => (int) $data['height'],
'x' => static::extractX($data),
'y' => static::extractY($data),
'id' => (int) $data['id'],
'type' => (int) $data['type'],
'label' => static::extractLabel($data),
'labelPosition' => static::extractLabelPosition($data),
'isLinkEnabled' => static::extractIsLinkEnabled($data),
'isOnTop' => static::extractIsOnTop($data),
'parentId' => static::extractParentId($data),
'aclGroupId' => static::extractAclGroupId($data),
'width' => (int) $data['width'],
'height' => (int) $data['height'],
'x' => static::extractX($data),
'y' => static::extractY($data),
'cacheExpiration' => static::extractCacheExpiration($data),
];
if (static::$useLinkedModule === true) {
@ -434,6 +435,28 @@ class Item extends CachedModel
}
/**
* Extract the cache expiration value.
*
* @param array $data Unknown input data structure.
*
* @return integer Cache expiration time.
*/
private static function extractCacheExpiration(array $data)
{
return static::parseIntOr(
static::issetInArray(
$data,
[
'cacheExpiration',
'cache_expiration',
]
),
null
);
}
/**
* Extract an module Id value.
*
@ -1254,6 +1277,9 @@ class Item extends CachedModel
/**
* TODO: CRITICAL. This function contains values which belong to its
* subclasses. This function should be overrided there to add them.
*
* Return a valid representation of a record in database.
*
* @param array $data Input data.
@ -1568,18 +1594,9 @@ class Item extends CachedModel
$result['show_last_value'] = $show_last_value;
}
$cache_expiration = static::parseIntOr(
static::issetInArray(
$data,
[
'cache_expiration',
'cacheExpiration',
]
),
null
);
if ($cache_expiration !== null) {
$result['cache_expiration'] = $cache_expiration;
$cacheExpiration = static::extractCacheExpiration($data);
if ($cacheExpiration !== null) {
$result['cache_expiration'] = $cacheExpiration;
}
return $result;

View File

@ -19,7 +19,8 @@ import {
debounce,
addResizementListener,
t,
helpTip
helpTip,
periodSelector
} from "./lib";
import TypedEvent, { Listener, Disposable } from "./lib/TypedEvent";
import { FormContainer, InputGroup } from "./Form";
@ -66,6 +67,7 @@ export interface ItemProps extends Position, Size {
isOnTop: boolean;
parentId: number | null;
aclGroupId: number | null;
cacheExpiration: number | null;
}
export interface ItemClickEvent {
@ -351,6 +353,33 @@ class AclGroupInputGroup extends InputGroup<Partial<ItemProps>> {
}
}
// TODO: Document
class CacheExpirationInputGroup extends InputGroup<Partial<ItemProps>> {
protected createContent(): HTMLElement | HTMLElement[] {
const periodLabel = document.createElement("label");
periodLabel.textContent = t("Cache expiration");
const periodControl = periodSelector(
this.currentData.cacheExpiration || this.initialData.cacheExpiration || 0,
{ text: t("No cache"), value: 0 },
[
{ text: t("10 seconds"), value: 10 },
{ text: t("30 seconds"), value: 30 },
{ text: t("60 seconds"), value: 60 },
{ text: t("5 minutes"), value: 300 },
{ text: t("15 minutes"), value: 900 },
{ text: t("30 minutes"), value: 1800 },
{ text: t("1 hour"), value: 3600 }
],
value => this.updateData({ cacheExpiration: value })
);
periodLabel.appendChild(periodControl);
return periodLabel;
}
}
/**
* Class to add item to the general items form
* This item consists of a label and a color type select.
@ -800,6 +829,7 @@ export function itemBasePropsDecoder(data: AnyObject): ItemProps | never {
isOnTop: parseBoolean(data.isOnTop),
parentId: parseIntOr(data.parentId, null),
aclGroupId: parseIntOr(data.aclGroupId, null),
cacheExpiration: parseIntOr(data.cacheExpiration, null),
...sizePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
...positionPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
@ -1656,9 +1686,18 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
new LinkInputGroup("link", this.props),
new OnTopInputGroup("show-on-top", this.props),
new ParentInputGroup("parent", this.props),
new AclGroupInputGroup("acl-group", this.props)
new AclGroupInputGroup("acl-group", this.props),
new CacheExpirationInputGroup("cache-expiration", this.props)
],
["position", "size", "link", "show-on-top", "parent", "acl-group"]
[
"position",
"size",
"link",
"show-on-top",
"parent",
"acl-group",
"cache-expiration"
]
);
//return VisualConsoleItem.getFormContainer(this.props);
@ -1674,9 +1713,18 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
new LinkInputGroup("link", props),
new OnTopInputGroup("show-on-top", props),
new ParentInputGroup("parent", props),
new AclGroupInputGroup("acl-group", props)
new AclGroupInputGroup("acl-group", props),
new CacheExpirationInputGroup("cache-expiration", props)
],
["position", "size", "link", "show-on-top", "parent", "acl-group"]
[
"position",
"size",
"link",
"show-on-top",
"parent",
"acl-group",
"cache-expiration"
]
);
}
}