Fixed clock item VC

This commit is contained in:
Daniel Barbero Martin 2020-01-13 17:13:52 +01:00
parent e54740ec19
commit 602ce39907
10 changed files with 288 additions and 87 deletions

View File

@ -2316,6 +2316,125 @@ function getImagesVisualConsole(baseUrl, vcId, nameImg, only, callback) {
};
}
/**
* Onchange time-zone.
* @return {void}
*/
// eslint-disable-next-line no-unused-vars
function timeZoneVCChange(baseUrl, vcId) {
var zone = document.getElementById("zone").value;
var fncallback = function(error, data) {
if (error || !data) {
console.log(
"[ERROR]",
"[VISUAL-CONSOLE-CLIENT]",
"[API]",
error ? error.message : "Invalid response"
);
return;
}
if (typeof data === "string") {
try {
data = JSON.parse(data);
} catch (error) {
console.log(
"[ERROR]",
"[VISUAL-CONSOLE-CLIENT]",
"[API]",
error ? error.message : "Invalid response"
);
return; // Stop task execution.
}
}
removeAllOptions();
Object.keys(data).forEach(addOption);
function addOption(item) {
var select = document.getElementById("clockTimezone");
select.options[select.options.length] = new Option(item, item);
}
function removeAllOptions() {
var select = document.getElementById("clockTimezone");
select.options.length = 0;
}
return;
};
getTimeZoneVisualConsole(baseUrl, vcId, zone, fncallback);
}
/**
* TimeZone for zones.
* @param {string} baseUrl Base URL to build the API path.
* @param {int} vcId Identifier of the Visual Console.
* @param {string} zone Name zone.
* @param {function} callback Function to be executed on request success or fail.
* @return {Object} Cancellable. Object which include and .abort([statusText]) function.
*/
// eslint-disable-next-line no-unused-vars
function getTimeZoneVisualConsole(baseUrl, vcId, zone, callback) {
var apiPath = baseUrl + "/ajax.php";
var jqXHR = null;
// Cancel the ajax requests.
var abort = function(textStatus) {
if (textStatus == null) textStatus = "abort";
// -- XMLHttpRequest.readyState --
// Value State Description
// 0 UNSENT Client has been created. open() not called yet.
// 4 DONE The operation is complete.
if (jqXHR.readyState !== 0 && jqXHR.readyState !== 4)
jqXHR.abort(textStatus);
};
// Failed request handler.
var handleFail = function(jqXHR, textStatus, errorThrown) {
abort();
// Manually aborted or not.
if (textStatus === "abort") {
callback();
} else {
var error = new Error(errorThrown);
error.request = jqXHR;
callback(error);
}
};
// Function which handle success case.
var handleSuccess = function(data) {
callback(null, data);
};
// Visual Console container request.
jqXHR = jQuery
.get(
apiPath,
{
page: "include/rest-api/index",
getTimeZoneVisualConsole: 1,
visualConsoleId: vcId,
zone: zone
},
"json"
)
.done(handleSuccess)
.fail(handleFail);
// Abortable.
return {
abort: abort
};
}
// TODO: Delete the functions below when you can.
/**************************************
These functions require jQuery library

View File

@ -41,6 +41,7 @@ $getGroupsVisualConsoleItem = (bool) get_parameter(
);
$getAllVisualConsole = (bool) get_parameter('getAllVisualConsole');
$getImagesVisualConsole = (bool) get_parameter('getImagesVisualConsole');
$getTimeZoneVisualConsole = (bool) get_parameter('getTimeZoneVisualConsole');
$autocompleteAgentsVisualConsole = (bool) get_parameter(
'autocompleteAgentsVisualConsole'
);
@ -270,6 +271,11 @@ if ($getVisualConsole === true) {
$count = Item::imagesElementsVC($img, $only);
echo json_encode($count);
return;
} else if ($getTimeZoneVisualConsole) {
$zone = get_parameter('zone', 'Europe');
$zones = Item::zonesVC($zone);
echo json_encode($zones);
return;
} else if ($autocompleteAgentsVisualConsole) {
$params = (array) get_parameter('data', []);

View File

@ -1541,52 +1541,6 @@ class Item extends CachedModel
$result['show_on_top'] = static::parseBool($show_on_top);
}
$clock_animation = static::notEmptyStringOr(
static::issetInArray(
$data,
[
'clockType',
'clock_animation',
'clockAnimation',
]
),
null
);
if ($clock_animation !== null) {
$result['clock_animation'] = $clock_animation;
}
$time_format = static::notEmptyStringOr(
static::issetInArray(
$data,
[
'clockFormat',
'time_format',
'timeFormat',
]
),
null
);
if ($time_format !== null) {
$result['time_format'] = $time_format;
}
$timezone = static::notEmptyStringOr(
static::issetInArray(
$data,
[
'timezone',
'timeZone',
'time_zone',
'clockTimezone',
]
),
null
);
if ($timezone !== null) {
$result['timezone'] = $timezone;
}
$show_last_value = static::notEmptyStringOr(
static::issetInArray($data, ['showLastValueTooltip']),
null
@ -2464,4 +2418,25 @@ class Item extends CachedModel
}
/**
* Return zones.
*
* @param string $zone Name zone.
*
* @return array Zones.
*/
public static function zonesVC(string $zone):array
{
$result = [];
$timezones = timezone_identifiers_list();
foreach ($timezones as $timezone) {
if (strpos($timezone, $zone) !== false) {
$result[$timezone] = $timezone;
}
}
return $result;
}
}

View File

@ -20,6 +20,96 @@ final class Clock extends Item
protected static $useLinkedVisualConsole = true;
/**
* Encode type item.
*
* @param array $data Data for encode.
*
* @return string Return color.
*/
protected function encodeColor(array $data): ?string
{
$color = null;
if (isset($data['color']) === true) {
if (empty($data['color']) === true) {
$color = '#F0F0F0';
} else {
$color = $data['color'];
}
}
return $color;
}
/**
* Return a valid representation of a record in database.
*
* @param array $data Input data.
*
* @return array Data structure representing a record in database.
*
* @overrides Item->encode.
*/
protected function encode(array $data): array
{
$return = parent::encode($data);
$color = static::encodeColor($data);
if ($color !== null) {
$return['fill_color'] = $color;
}
$clock_animation = static::notEmptyStringOr(
static::issetInArray(
$data,
[
'clockType',
'clock_animation',
'clockAnimation',
]
),
null
);
if ($clock_animation !== null) {
$return['clock_animation'] = $clock_animation;
}
$time_format = static::notEmptyStringOr(
static::issetInArray(
$data,
[
'clockFormat',
'time_format',
'timeFormat',
]
),
null
);
if ($time_format !== null) {
$return['time_format'] = $time_format;
}
$timezone = static::notEmptyStringOr(
static::issetInArray(
$data,
[
'timezone',
'timeZone',
'time_zone',
'clockTimezone',
]
),
null
);
if ($timezone !== null) {
$return['timezone'] = $timezone;
}
return $return;
}
/**
* Returns a valid representation of the model.
*
@ -220,6 +310,7 @@ final class Clock extends Item
];
// Time zone.
$baseUrl = ui_get_full_url('/', false, false, false);
$fields = [
'Africa' => __('Africa'),
'America' => __('America'),
@ -234,14 +325,45 @@ final class Clock extends Item
'UTC' => __('UTC'),
];
if (isset($values['clockTimezone']) === false
&& empty($values['clockTimezone']) === true
) {
$values['zone'] = 'Europe';
$values['clockTimezone'] = 'Europe/Amsterdam';
} else {
$zone = explode('/', $values['clockTimezone']);
$values['zone'] = $zone[0];
}
$zones = self::zonesVC($values['zone']);
$inputs[] = [
'label' => __('Time zone'),
'arguments' => [
'type' => 'select',
'fields' => $fields,
'name' => 'clockTimezone',
'selected' => $values['clockTimezone'],
'return' => true,
'block_id' => 'timeZone-item',
'class' => 'flex-row flex-start w100p',
'direct' => 1,
'block_content' => [
[
'label' => __('Time zone'),
],
[
'arguments' => [
'type' => 'select',
'fields' => $fields,
'name' => 'zone',
'selected' => $values['zone'],
'script' => 'timeZoneVCChange(\''.$baseUrl.'\',\''.$values['vCId'].'\')',
'return' => true,
],
],
[
'arguments' => [
'type' => 'select',
'fields' => $zones,
'name' => 'clockTimezone',
'selected' => $values['clockTimezone'],
'return' => true,
],
],
],
];

View File

@ -548,12 +548,17 @@ li#li-image-item label img {
width: inherit;
}
li#li-timeZone-item > label:not(:first-child),
.discovery.modal li#div-textarea-label > label {
-webkit-box-flex: inherit;
-ms-flex: inherit;
flex: inherit;
}
li#li-timeZone-item > select:not(:first-child) {
margin-left: 10px;
}
.discovery.modal li#div-textarea-label table tbody td.mceIframeContainer {
background-color: #ededed;
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -265,35 +265,6 @@ class ClockTimezoneInputGroup extends InputGroup<Partial<ClockProps>> {
}
}
/**
* Class to add item to the clock item form
* This item consists of a label and a color type input.
* Element color is stored in the color property
*/
class FIllColorInputGroup extends InputGroup<Partial<ClockProps>> {
protected createContent(): HTMLElement | HTMLElement[] {
const fillcolorLabel = document.createElement("label");
fillcolorLabel.textContent = t("Fill color");
const fillColorInput = document.createElement("input");
fillColorInput.type = "color";
fillColorInput.required = true;
fillColorInput.value = `${this.currentData.color ||
this.initialData.color}`;
fillColorInput.addEventListener("change", e => {
this.updateData({
color: (e.target as HTMLInputElement).value
});
});
fillcolorLabel.appendChild(fillColorInput);
return fillcolorLabel;
}
}
export default class Clock extends Item<ClockProps> {
public static readonly TICK_INTERVAL = 1000; // In ms.
private intervalRef: number | null = null;
@ -862,8 +833,6 @@ export default class Clock extends Item<ClockProps> {
new ClockTimezoneInputGroup("clockTimezone", props)
);
formContainer.addInputGroup(new FIllColorInputGroup("fillColor", props));
return formContainer;
}
}

View File

@ -427,10 +427,15 @@ li#li-image-item label img {
width: inherit;
}
li#li-timeZone-item > label:not(:first-child),
.discovery.modal li#div-textarea-label > label {
flex: inherit;
}
li#li-timeZone-item > select:not(:first-child) {
margin-left: 10px;
}
.discovery.modal li#div-textarea-label table tbody td.mceIframeContainer {
background-color: #ededed;
}