Fix can't rename Default

This commit is contained in:
Rich Lott / Artful Robot 2024-02-28 17:23:25 +00:00
parent 7b611c343f
commit 2f2e458ab2

View file

@ -1,8 +1,8 @@
(function(angular, $, _) { (function(angular, $, _) {
// Declare a list of dependencies. // Declare a list of dependencies.
angular.module('crmContactcats', CRM.angRequires('crmContactcats')); angular.module("crmContactcats", CRM.angRequires("crmContactcats"));
angular.module('crmContactcats').component('crmContactCategorySettings', { angular.module("crmContactcats").component("crmContactCategorySettings", {
templateUrl: '~/crmContactcats/crmContactCategorySettings.html', templateUrl: "~/crmContactcats/crmContactCategorySettings.html",
bindings: { bindings: {
// things listed here become properties on the controller using // things listed here become properties on the controller using
// values from attributes. @ means a fixed string is passed // values from attributes. @ means a fixed string is passed
@ -16,72 +16,93 @@
// v: '<' // v: '<'
}, },
controller: function($scope, $timeout, crmApi4, $document) { controller: function($scope, $timeout, crmApi4, $document) {
var ts = $scope.ts = CRM.ts(null), var ts = ($scope.ts = CRM.ts(null)),
ctrl = this; ctrl = this;
// this.$onInit gets run after the this controller is called, and after the bindings have been applied. // this.$onInit gets run after the this controller is called, and after the bindings have been applied.
this.$onInit = async function() { this.$onInit = async function() {
ctrl.saved = false; ctrl.saved = false;
const various = await crmApi4({ const various = await crmApi4({
settings: ['Setting', 'get', { select: ["contact_categories"] }, 0], settings: ["Setting", "get", { select: ["contact_categories"] }, 0],
groups: ['Group', 'get', { groups: [
where: [['is_active', '=', 1], ['is_hidden', '=', 0]], "Group",
orderBy: { "name": "ASC" } "get",
}], {
cats: ['OptionValue', 'get', { where: [
select: ['value', "label"], ["is_active", "=", 1],
where: [["option_group_id:name", "=", "contact_categories"]], ["is_hidden", "=", 0]
}] ],
orderBy: { name: "ASC" }
}
],
cats: [
"OptionValue",
"get",
{
select: ["value", "label"],
where: [["option_group_id:name", "=", "contact_categories"]]
}
]
}); });
ctrl.catmap = []; ctrl.catmap = [];
if (!various.settings.value || !various.settings.value.groupIDs) { if (!various.settings.value || !various.settings.value.groupIDs) {
various.settings.value = { various.settings.value = {
groupIDs: ["0"], groupIDs: ["0"],
updateAfter: 0, updateAfter: 0
};
}; };
}
various.settings.value.groupIDs.forEach(groupID => { various.settings.value.groupIDs.forEach(groupID => {
let cat = various.cats.find(c => c.value == groupID); let cat = various.cats.find(c => c.value == groupID);
ctrl.catmap.push({ ctrl.catmap.push({
groupID, groupID,
name: cat ? cat.label : '', name: cat ? cat.label : ""
}); });
}); });
console.log({ various, catmap: ctrl.catmap }); console.log({ various, catmap: ctrl.catmap });
ctrl.groups = various.groups; ctrl.groups = various.groups;
ctrl.nameKeydown = (keyEvt, idx) => { ctrl.nameKeydown = (keyEvt, idx) => {
if (keyEvt.key === 'ArrowUp' || keyEvt.key === 'ArrowDown') { if (keyEvt.key === "ArrowUp" || keyEvt.key === "ArrowDown") {
keyEvt.preventDefault(); keyEvt.preventDefault();
keyEvt.stopPropagation(); keyEvt.stopPropagation();
if (keyEvt.key === 'ArrowUp' && idx > 0 && idx < ctrl.catmap.length - 1) { if (
keyEvt.key === "ArrowUp" &&
idx > 0 &&
idx < ctrl.catmap.length - 1
) {
ctrl.catmap.splice(idx, 0, ...ctrl.catmap.splice(idx - 1, 1)); ctrl.catmap.splice(idx, 0, ...ctrl.catmap.splice(idx - 1, 1));
console.log('up', { keyEvt }); console.log("up", { keyEvt });
$timeout(() => keyEvt.target.focus(), 10); $timeout(() => keyEvt.target.focus(), 10);
} } else if (
else if (keyEvt.key === 'ArrowDown' && idx < ctrl.catmap.length - 2) { keyEvt.key === "ArrowDown" &&
idx < ctrl.catmap.length - 2
) {
ctrl.catmap.splice(idx + 1, 0, ...ctrl.catmap.splice(idx, 1)); ctrl.catmap.splice(idx + 1, 0, ...ctrl.catmap.splice(idx, 1));
console.log('down', ctrl.catmap.map(e => e.name)); console.log(
"down",
ctrl.catmap.map(e => e.name)
);
} }
} }
}; };
$scope.$digest(); $scope.$digest();
ctrl.deleteRow = (idx) => { ctrl.deleteRow = idx => {
ctrl.catmap.splice(idx, 1); ctrl.catmap.splice(idx, 1);
}; };
ctrl.getGroupsFor = (idx) => { ctrl.getGroupsFor = idx => {
let groupsInUse = ctrl.catmap.map(c => c.groupID); let groupsInUse = ctrl.catmap.map(c => c.groupID);
groupsInUse.splice(idx, 1); groupsInUse.splice(idx, 1);
return ctrl.groups.filter(g => !groupsInUse.includes(g.id.toString())); return ctrl.groups.filter(
g => !groupsInUse.includes(g.id.toString())
);
}; };
ctrl.save = async () => { ctrl.save = async () => {
console.log("save"); console.log("save", ctrl.catmap);
// reconstruct everything. // reconstruct everything.
const optValsRecords = []; const optValsRecords = [];
let isInvalid = false;
ctrl.catmap.forEach(r => { ctrl.catmap.forEach(r => {
if (!(r.name) || !r.groupID) { if (!r.name || r.groupID === "") {
return; return;
} }
// Do we have an option value for this group ID? // Do we have an option value for this group ID?
@ -90,11 +111,10 @@
if (c.label != r.name) { if (c.label != r.name) {
optValsRecords.push({ optValsRecords.push({
id: c.id, id: c.id,
label: r.name, label: r.name
}); });
} }
} } else {
else {
optValsRecords.push({ optValsRecords.push({
label: r.name, label: r.name,
value: r.groupID, value: r.groupID,
@ -104,18 +124,25 @@
}); });
console.log("optionValue updates", optValsRecords, ctrl.catmap); console.log("optionValue updates", optValsRecords, ctrl.catmap);
const updates = { const updates = {
saveSetting: ['Setting', 'set', { saveSetting: [
values: { "Setting",
contact_categories: "set",
{ {
values: {
contact_categories: {
groupIDs: ctrl.catmap.map(i => i.groupID), groupIDs: ctrl.catmap.map(i => i.groupID),
updateAfter: 0 updateAfter: 0
} }
} }
}], }
]
}; };
if (optValsRecords.length) { if (optValsRecords.length) {
updates.saveOptions = ['OptionValue', 'save', { records: optValsRecords }]; updates.saveOptions = [
"OptionValue",
"save",
{ records: optValsRecords }
];
} }
await crmApi4(updates); await crmApi4(updates);
console.log("saved", updates); console.log("saved", updates);
@ -128,7 +155,6 @@
// // the parent (e.g. 'v'), and value is another obj with keys // // the parent (e.g. 'v'), and value is another obj with keys
// // something like previous, new, ...?... // // something like previous, new, ...?...
// }; // };
} }
}); });
})(angular, CRM.$, CRM._); })(angular, CRM.$, CRM._);