Files
iTop/js/selectize/plugin_combodo_update_operations.js
bdalsass 1fe9520b7e N°5904 - Use attribute linked set edit mode to enable actions (#440)
* Add corresponding buttons depending on old edit mode (need to check with piR pour récuperer l'ancienne valeur.

* N°5904 - Handle attribute linked set edit_mode

* N°5904 Move calls to private jquery widget methods to public

* N°5904 - Worker improvements add button on link tagset

* Change itop set widget to new set block UI (5)

* Change itop set widget to new set block UI (5)

* Renommage variables JS avec le prefix combodo

* Search dialog block id conflict with form id

* add moved js files in iTopWebPage compatibility list

---------

Co-authored-by: Stephen Abello <stephen.abello@combodo.com>
2023-02-06 16:07:55 +01:00

127 lines
3.3 KiB
JavaScript

/*
* Copyright (C) 2013-2022 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
Selectize.define("combodo_update_operations", function (aOptions) {
// Selectize instance
let oSelf = this;
// Plugin options
aOptions = $.extend({
initial: [],
},
aOptions
);
// Plugin variables
oSelf.bIsInitialized = false;
oSelf.operations = {};
// Override setup function
oSelf.setup = (function () {
let oOriginal = oSelf.setup;
return function () {
oOriginal.apply(oSelf, arguments);
oSelf.$operationsInput = $(`<input type="hidden" value="{}" name="${oSelf.$input.attr('name')}_operations">`)
oSelf.$wrapper.append(oSelf.$operationsInput);
oSelf.bIsInitialized = true;
oSelf.updateOperationsInput();
};
})();
// Override addItem function
oSelf.addItem = (function () {
let oOriginal = oSelf.addItem;
return function () {
oOriginal.apply(this, arguments);
if(oSelf.bIsInitialized && !arguments[1]){
this.updateOperationsInput();
}
}
})();
// Override removeItem function
oSelf.removeItem = (function () {
let oOriginal = oSelf.removeItem;
return function () {
oOriginal.apply(this, arguments);
if(oSelf.bIsInitialized){
this.updateOperationsInput();
}
}
})();
// Declare updateOperationsInput function
oSelf.updateOperationsInput = (function () {
return function () {
// update operations
oSelf.updateOperations();
// setup in progress
if(typeof(oSelf.$operationsInput) === 'undefined'){
return;
}
// update operations input
oSelf.$operationsInput.val(JSON.stringify(oSelf.operations));
};
})();
// Declare updateOperations function
oSelf.updateOperations = (function () {
return function () {
// Reset operations
oSelf.operations = {};
// Reference data
const aCurrentItems = Object.values(oSelf.items);
const aCurrentOptions = Object.values(oSelf.options);
// Scan items in current value and not in initial value
aCurrentItems.forEach(function(e){
if(!aOptions.initial.includes(e)){
oSelf.operations[e] = {
operation: 'add',
data: CombodoGlobalToolbox.ExtractArrayItemsContainingThisKeyAndValue(aCurrentOptions, oSelf.settings.valueField, e)
}
}
});
// scan items in initial value and not in current value
aOptions.initial.forEach(function(e){
if(!aCurrentItems.includes(e)){
oSelf.operations[e] = {
operation: 'remove',
data: CombodoGlobalToolbox.ExtractArrayItemsContainingThisKeyAndValue(aCurrentOptions, oSelf.settings.valueField, e)
}
}
});
};
})();
// Declare addInitialValue function
oSelf.addInitialValue = (function () {
return function (value) {
aOptions.initial.push(value);
oSelf.updateOperationsInput();
};
})();
});