Files
old-wiaas-legacy/api-wiaas/client/js/components/utils.service.js
2018-06-11 11:09:35 +02:00

118 lines
3.6 KiB
JavaScript

(function () {
global.dashModule.service('utilsService', ['$translate', '$exceptionHandler', 'Notification', 'ORDER_STATUSES_ICONS', utilsService]);
function utilsService($translate, $exceptionHandler, Notification, ORDER_STATUSES_ICONS) {
let registeredFunction = {};
return {
displayMessage,
sortByAttributeName,
verifyAmountAdded,
sortByAtribute,
onHttpError,
registerFunction,
executeRegisteredFunction,
getStatusIcon,
getTynimceOptions
};
function displayMessage(status, message) {
switch (status) {
case 'success':
return Notification.success(getMessageDisplayParams(message));
case 'error':
return Notification.error(getMessageDisplayParams(message));
case 'warning':
return Notification.warning(getMessageDisplayParams(message));
default:
break;
}
}
function onHttpError(response) {
const err = {
message: response.data || 'no reponse from server',
stack: response.config && response.config.url ? response.config.url : 'no url'
};
$exceptionHandler(err, 'HTTP request failed');
}
function getMessageDisplayParams(message) {
return {
message: message,
delay: 10000,
positionY: 'top',
positionX: 'right',
startTop: 20,
startRight: 20
};
}
function sortByAttributeName(attribute) {
return function (a, b) {
const nameA = a[attribute].toLowerCase();
const nameB = b[attribute].toLowerCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
};
}
function verifyAmountAdded(productQuantity) {
if (productQuantity < 1 || productQuantity > 100 || typeof productQuantity === 'undefined') {
const message = $translate.instant('packages.forms.messages.ONLY_NUMBERS');
displayMessage('error', message);
}
}
function sortByAtribute(a, b, attribute) {
if (a[attribute] < b[attribute]) {
return -1;
}
if (a[attribute] > b[attribute]) {
return 1;
}
return 0;
}
function registerFunction(functionName, calbackFunction) {
registeredFunction[functionName] = calbackFunction;
}
function executeRegisteredFunction(functionName, aditionalParam = '') {
if (registeredFunction[functionName]) {
registeredFunction[functionName](aditionalParam);
}
}
function getStatusIcon(status) {
return ORDER_STATUSES_ICONS[status];
}
function getTynimceOptions(options) {
let tinymceOptions = {
height: '250px',
plugins: 'link image textcolor lists colorpicker code',
toolbar: 'undo redo | styleselect | bold italic underline forecolor fontsizeselect | link | alignleft aligncenter alignright | numlist bullist | outdent indent | code'
};
if (typeof options === 'object') {
Object.assign(tinymceOptions, options);
}
return tinymceOptions;
}
}
})();