mirror of
https://github.com/grocy/grocy.git
synced 2025-08-16 10:44:37 +00:00
Added userfield default values for userfield types date & datetime (closes #1166)
This commit is contained in:
@@ -89,86 +89,123 @@ Grocy.Components.UserfieldsForm.Save = function(success, error)
|
||||
|
||||
Grocy.Components.UserfieldsForm.Load = function()
|
||||
{
|
||||
if (!$("#userfields-form").length || typeof Grocy.EditObjectId == "undefined")
|
||||
if (!$("#userfields-form").length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Grocy.Api.Get('userfields/' + $("#userfields-form").data("entity") + '/' + Grocy.EditObjectId,
|
||||
function(result)
|
||||
{
|
||||
$.each(result, function(key, value)
|
||||
if (typeof Grocy.EditObjectId == "undefined")
|
||||
{
|
||||
// Init fields by configured default values
|
||||
|
||||
Grocy.Api.Get("objects/userfields?query[]=entity=" + $("#userfields-form").data("entity"),
|
||||
function(result)
|
||||
{
|
||||
var input = $(".userfield-input[data-userfield-name='" + key + "']");
|
||||
$.each(result, function(key, userfield)
|
||||
{
|
||||
var input = $(".userfield-input[data-userfield-name='" + userfield.name + "']");
|
||||
|
||||
if (input.attr("type") == "checkbox" && value == 1)
|
||||
{
|
||||
input.prop("checked", true);
|
||||
}
|
||||
else if (input.hasAttr("multiple"))
|
||||
{
|
||||
input.val(value.split(","));
|
||||
$(".selectpicker").selectpicker("render");
|
||||
}
|
||||
else if (input.attr('type') == "file")
|
||||
{
|
||||
if (value != null && !value.isEmpty())
|
||||
if (userfield.type == "datetime" && userfield.default_value == "now")
|
||||
{
|
||||
var fileName = atob(value.split('_')[1]);
|
||||
var fileSrc = value.split('_')[0];
|
||||
var formGroup = input.parent().parent().parent();
|
||||
|
||||
formGroup.find("label.custom-file-label").text(fileName);
|
||||
formGroup.find(".userfield-file-show").attr('href', U('/files/userfiles/' + value));
|
||||
formGroup.find('.userfield-file-show').removeClass('d-none');
|
||||
formGroup.find('img.userfield-current-file')
|
||||
.attr('src', U('/files/userfiles/' + value + '?force_serve_as=picture&best_fit_width=250&best_fit_height=250'));
|
||||
LoadImagesLazy();
|
||||
|
||||
formGroup.find('.userfield-file-delete').click(
|
||||
function()
|
||||
{
|
||||
formGroup.find("label.custom-file-label").text(__t("No file selected"));
|
||||
formGroup.find(".userfield-file-show").addClass('d-none');
|
||||
input.attr('data-old-file', fileSrc);
|
||||
}
|
||||
);
|
||||
|
||||
input.on("change", function(e)
|
||||
{
|
||||
formGroup.find(".userfield-file-show").addClass('d-none');
|
||||
});
|
||||
input.val(moment().format("YYYY-MM-DD HH:mm:ss"))
|
||||
}
|
||||
}
|
||||
else if (input.attr("data-userfield-type") == "link")
|
||||
{
|
||||
if (!value.isEmpty())
|
||||
else if (userfield.type == "date" && userfield.default_value == "now")
|
||||
{
|
||||
var data = JSON.parse(value);
|
||||
input.val(moment().format("YYYY-MM-DD"))
|
||||
}
|
||||
});
|
||||
|
||||
var formRow = input.parent().parent();
|
||||
formRow.find(".userfield-link-title").val(data.title);
|
||||
formRow.find(".userfield-link-link").val(data.link);
|
||||
$("form").each(function()
|
||||
{
|
||||
Grocy.FrontendHelpers.ValidateForm(this.id);
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Load object field values
|
||||
|
||||
Grocy.Api.Get('userfields/' + $("#userfields-form").data("entity") + '/' + Grocy.EditObjectId,
|
||||
function(result)
|
||||
{
|
||||
$.each(result, function(key, value)
|
||||
{
|
||||
var input = $(".userfield-input[data-userfield-name='" + key + "']");
|
||||
|
||||
if (input.attr("type") == "checkbox" && value == 1)
|
||||
{
|
||||
input.prop("checked", true);
|
||||
}
|
||||
else if (input.hasAttr("multiple"))
|
||||
{
|
||||
input.val(value.split(","));
|
||||
$(".selectpicker").selectpicker("render");
|
||||
}
|
||||
else if (input.attr('type') == "file")
|
||||
{
|
||||
if (value != null && !value.isEmpty())
|
||||
{
|
||||
var fileName = atob(value.split('_')[1]);
|
||||
var fileSrc = value.split('_')[0];
|
||||
var formGroup = input.parent().parent().parent();
|
||||
|
||||
formGroup.find("label.custom-file-label").text(fileName);
|
||||
formGroup.find(".userfield-file-show").attr('href', U('/files/userfiles/' + value));
|
||||
formGroup.find('.userfield-file-show').removeClass('d-none');
|
||||
formGroup.find('img.userfield-current-file')
|
||||
.attr('src', U('/files/userfiles/' + value + '?force_serve_as=picture&best_fit_width=250&best_fit_height=250'));
|
||||
LoadImagesLazy();
|
||||
|
||||
formGroup.find('.userfield-file-delete').click(
|
||||
function()
|
||||
{
|
||||
formGroup.find("label.custom-file-label").text(__t("No file selected"));
|
||||
formGroup.find(".userfield-file-show").addClass('d-none');
|
||||
input.attr('data-old-file', fileSrc);
|
||||
}
|
||||
);
|
||||
|
||||
input.on("change", function(e)
|
||||
{
|
||||
formGroup.find(".userfield-file-show").addClass('d-none');
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (input.attr("data-userfield-type") == "link")
|
||||
{
|
||||
if (!value.isEmpty())
|
||||
{
|
||||
var data = JSON.parse(value);
|
||||
|
||||
var formRow = input.parent().parent();
|
||||
formRow.find(".userfield-link-title").val(data.title);
|
||||
formRow.find(".userfield-link-link").val(data.link);
|
||||
|
||||
input.val(value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
input.val(value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
input.val(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$("form").each(function()
|
||||
$("form").each(function()
|
||||
{
|
||||
Grocy.FrontendHelpers.ValidateForm(this.id);
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.ValidateForm(this.id);
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Grocy.Components.UserfieldsForm.Clear = function()
|
||||
|
Reference in New Issue
Block a user