Files
iTop/js/selectize/plugin_combodo_update_operations.js
Stephen Abello ceba1ad1e9 N°7157 - Allow users to unsubscribe from notification channels (#611)
* N°7157 - Allow users to unsubscribe from notification channels

* Fix type hinting

* Add missing dict entries

* Allows to subscribe/unsubscribe from notifications individually

* Refactor NotificationsService to singleton pattern

* Refactor NotificationsRepository to singleton pattern and rename methods to a more functional naming

* Add PHPDoc and type hints

* Dump autoloaders

* Replace modals with toasts

* Add dict entry

---------

Co-authored-by: Molkobain <lajarige.guillaume@free.fr>
2024-02-19 09:25:47 +01:00

145 lines
3.7 KiB
JavaScript

/*
* Copyright (C) 2013-2023 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 enable function
oSelf.enable = (function () {
let oOriginal = oSelf.enable;
return function () {
oOriginal.apply(oSelf, arguments);
oSelf.$operationsInput.prop('disabled', false);
}
})();
// Override disable function
oSelf.disable = (function () {
let oOriginal = oSelf.disable;
return function () {
oOriginal.apply(oSelf, arguments);
if(oSelf.$operationsInput !== undefined)
oSelf.$operationsInput.prop('disabled', true);
}
})();
// 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();
};
})();
});