mirror of
https://codeberg.org/artfulrobot/contactcats.git
synced 2025-06-25 21:28:06 +02:00
Rewrite sync to use new features
This commit is contained in:
parent
b0983603e5
commit
16f2b6235d
7 changed files with 502 additions and 365 deletions
|
@ -15,21 +15,44 @@
|
|||
// supposed to use for components.
|
||||
// v: '<'
|
||||
},
|
||||
controller: function($scope, $timeout, crmApi4, $document) {
|
||||
controller: function($scope, $timeout, crmApi4, crmStatus, $document) {
|
||||
var ts = ($scope.ts = CRM.ts(null)),
|
||||
ctrl = this;
|
||||
|
||||
// this.$onInit gets run after the this controller is called, and after the bindings have been applied.
|
||||
this.$onInit = async function() {
|
||||
ctrl.saved = false;
|
||||
ctrl.dirty = 'pristine'; //pristine|dirty
|
||||
ctrl.view = 'list';
|
||||
ctrl.moveIdx = null;
|
||||
ctrl.categoryToEdit = null;
|
||||
ctrl.categoryDefinitions = null;
|
||||
ctrl.categoryDefinitions = await crmApi4("ContactCategoryDefinition", 'get', { orderBy: { label: 'ASC' }, withLabels: true });
|
||||
updateOrders();
|
||||
console.log("Got", ctrl.categoryDefinitions);
|
||||
|
||||
$scope.$digest();
|
||||
|
||||
/**
|
||||
* Presentation order is by label. Promoting "0123 label" type labels.
|
||||
*
|
||||
* We set execution_order to the array index to keep things simpler.
|
||||
*/
|
||||
function updateOrders() {
|
||||
|
||||
// re-set execution_order.
|
||||
ctrl.categoryDefinitions.forEach((item, idx) => {
|
||||
item.execution_order = idx;
|
||||
});
|
||||
|
||||
const po = ctrl.categoryDefinitions.slice();
|
||||
po.sort((a, b) => {
|
||||
if (a.label < b.label) return -1;
|
||||
if (a.label > b.label) return 1;
|
||||
return (a.execution_order ?? 1) - (b.execution_order ?? 1);
|
||||
});
|
||||
ctrl.presentationOrder = po;
|
||||
}
|
||||
|
||||
ctrl.edit = idx => {
|
||||
if (idx === -1) {
|
||||
// New item.
|
||||
|
@ -42,7 +65,8 @@
|
|||
|
||||
// Create a blank
|
||||
ctrl.categoryToEdit = {
|
||||
idx: ctrl.categoryDefinitions.length,
|
||||
id: 0,
|
||||
execution_order: ctrl.categoryDefinitions.length,
|
||||
label: '',
|
||||
search_type: 'search',
|
||||
search_data: { saved_search_id: null },
|
||||
|
@ -52,7 +76,7 @@
|
|||
};
|
||||
}
|
||||
else {
|
||||
ctrl.categoryToEdit = Object.assign({idx}, JSON.parse(JSON.stringify(ctrl.categoryDefinitions[idx])));
|
||||
ctrl.categoryToEdit = Object.assign({}, JSON.parse(JSON.stringify(ctrl.categoryDefinitions[idx])));
|
||||
}
|
||||
ctrl.view = 'edit';
|
||||
};
|
||||
|
@ -64,7 +88,10 @@
|
|||
}
|
||||
ctrl.categoryDefinitions.splice(idx, 0, item);
|
||||
ctrl.moveIdx = null;
|
||||
updateOrders();
|
||||
$ctrl.dirty = 'dirty';
|
||||
};
|
||||
|
||||
ctrl.deleteCategory = idx => {
|
||||
if (!confirm(ts(
|
||||
'Confirm deleting category ‘%1’. You will lose history related to this category. Sure?',
|
||||
|
@ -74,36 +101,36 @@
|
|||
}
|
||||
ctrl.categoryDefinitions[idx].deleted = true;
|
||||
ctrl.categoryToEdit = null;
|
||||
updateOrders();
|
||||
ctrl.view = 'list';
|
||||
ctrl.dirty = 'dirty';
|
||||
};
|
||||
|
||||
ctrl.updateEditedThing = () => {
|
||||
const edited = ctrl.categoryToEdit;
|
||||
// @todo validate, e.g.
|
||||
if (!ctrl.categoryToEdit.label) {
|
||||
if (!edited.label) {
|
||||
alert("No name");
|
||||
return;
|
||||
}
|
||||
|
||||
const search_data = ctrl.categoryToEdit.search_data;
|
||||
console.log("search_data", search_data);
|
||||
if (ctrl.categoryToEdit.search_type === 'group') {
|
||||
const search_data = edited.search_data;
|
||||
if (edited.search_type === 'group') {
|
||||
// Only store what we need.
|
||||
const {group_id} = search_data;
|
||||
console.log("group_id", group_id, search_data);
|
||||
ctrl.categoryToEdit.search_data = {group_id};
|
||||
edited.search_data = {group_id};
|
||||
}
|
||||
else if (ctrl.categoryToEdit.search_type === 'search') {
|
||||
else if (edited.search_type === 'search') {
|
||||
const {saved_search_id} = search_data;
|
||||
ctrl.categoryToEdit.search_data = {saved_search_id};
|
||||
edited.search_data = {saved_search_id};
|
||||
}
|
||||
|
||||
const edited = ctrl.categoryToEdit;
|
||||
const idx = edited.idx;
|
||||
delete(edited.idx);
|
||||
const idx = edited.execution_order;
|
||||
ctrl.categoryDefinitions[idx] = edited;
|
||||
ctrl.categoryToEdit = null;
|
||||
updateOrders();
|
||||
ctrl.dirty = 'dirty';
|
||||
ctrl.view = 'list';
|
||||
console.log("done editing");
|
||||
}
|
||||
|
||||
// We need to ensure the search_data object contains the fields required for the selected search_type
|
||||
|
@ -121,74 +148,29 @@
|
|||
}
|
||||
};
|
||||
|
||||
ctrl.save = async () => {
|
||||
ctrl.save = () => {
|
||||
if (!confirm(ts("Confirm saving changes to categories? Note that categories will not be fully applied until tomorrow."))) { return; }
|
||||
|
||||
|
||||
// Handle deletions first.
|
||||
const deletedIds = ctrl.categoryDefinitions.filter(d => d.deleted && d.id > 0).map(d => d.id);
|
||||
|
||||
const chain = Promise.resolve();
|
||||
if (deletedIds.length) {
|
||||
chain.then(() => crmApi4('ContactCategoryDefinition', 'delete', { where: [ ['id', 'IN', deletedIds] ] }));
|
||||
}
|
||||
// Now enact the deletions on our local model.
|
||||
ctrl.categoryDefinitions = ctrl.categoryDefinitions.filter(d => !d.deleted);
|
||||
// Tidy execution_order
|
||||
updateOrders();
|
||||
if (ctrl.categoryDefinitions.length) {
|
||||
chain.then(() => crmApi4('ContactCategoryDefinition', 'save', {records:ctrl.categoryDefinitions}));
|
||||
}
|
||||
chain.then(() => {
|
||||
ctrl.dirty = 'pristine';
|
||||
})
|
||||
crmStatus({ start: ts('Saving...'), success: ts('Saved')}, chain);
|
||||
};
|
||||
|
||||
// ctrl.deleteRow = idx => {
|
||||
// ctrl.catmap.splice(idx, 1);
|
||||
// };
|
||||
// ctrl.getGroupsFor = idx => {
|
||||
// let groupsInUse = ctrl.catmap.map(c => c.groupID);
|
||||
// groupsInUse.splice(idx, 1);
|
||||
// return ctrl.groups.filter(
|
||||
// g => !groupsInUse.includes(g.id.toString())
|
||||
// );
|
||||
// };
|
||||
|
||||
// ctrl.save = async () => {
|
||||
// console.log("save", ctrl.catmap);
|
||||
// // reconstruct everything.
|
||||
//
|
||||
// const optValsRecords = [];
|
||||
// ctrl.catmap.forEach(r => {
|
||||
// if (!r.name || r.groupID === "") {
|
||||
// return;
|
||||
// }
|
||||
// // Do we have an option value for this group ID?
|
||||
// let c = various.cats.find(cat => cat.value == r.groupID);
|
||||
// if (c) {
|
||||
// if (c.label != r.name) {
|
||||
// optValsRecords.push({
|
||||
// id: c.id,
|
||||
// label: r.name
|
||||
// });
|
||||
// }
|
||||
// } else {
|
||||
// optValsRecords.push({
|
||||
// label: r.name,
|
||||
// value: r.groupID,
|
||||
// "option_group_id:name": "contact_categories"
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// console.log("optionValue updates", optValsRecords, ctrl.catmap);
|
||||
// const updates = {
|
||||
// saveSetting: [
|
||||
// "Setting",
|
||||
// "set",
|
||||
// {
|
||||
// values: {
|
||||
// contact_categories: {
|
||||
// groupIDs: ctrl.catmap.map(i => i.groupID),
|
||||
// updateAfter: 0
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// };
|
||||
// if (optValsRecords.length) {
|
||||
// updates.saveOptions = [
|
||||
// "OptionValue",
|
||||
// "save",
|
||||
// { records: optValsRecords }
|
||||
// ];
|
||||
// }
|
||||
// await crmApi4(updates);
|
||||
// console.log("saved", updates);
|
||||
// ctrl.saved = true;
|
||||
// $scope.$digest();
|
||||
// };
|
||||
};
|
||||
// this.$onChange = function(changes) {
|
||||
// // changes is object keyed by name what '<' binding changed in
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue