(function () { "use strict"; angular .module("app.utils") .service("appUtils.uiNotifications", uiNotifications); /** * Special anglar.js factory to manage modal notifications. * @param {"webApi.statusCodes"} statusCodes */ function uiNotifications(statusCodes, $translate, localizationHelper) { var translationObject = {}; var loadTranslation = function(){ $translate(['Info', 'Warning', 'Confirmation', 'Success', 'Error', 'DescriptionP', 'DoYouReallyWantDoThis', 'Yes', 'No', 'OK']).then(function (tr) { translationObject = tr; }); }; localizationHelper.eventEmitter.subscribe('onLanguageChangedEvent', function (lang) { loadTranslation(); }); loadTranslation(); var defaultMessages = { "CallbackIsNotFunction": "Specified parameter tried to implement the function behavior, however it doesn't have an appropriate functionality.", "ConnectionRefused": "Connection was refused due to following reasons: 1) Your connection responding slowly. 2) Maybe the server is not available.", "Conflict409": "Conflict [409]: This error seldom occurs in most Web traffic, particularly when the client system is a Web browser. The problem can only be resolved by examining what your client system is trying to do then discussing with your ISP why that behaviour is not allowed.", "DontHavePermission": "You don't have a permission to invoke that request. Please, authorize and try again. If problem exists, check a specified 'headers.Authorization' parameter.", "LoginFailed": "Authorization error: login or password is incorrect. Please, check it and try again.", "BadRequest": "Bad request [400]: The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).", "NotFound404": "Not found [404]: The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.", "ResponseNull": "The data in response is 'null'. Please, check you request settings and connection with API.", "ServerIsOffline": "Internal Server Error [500]: A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.", "ServerUnexpectedError": "Unexpected server error.", "VariableIsUndefined": "Attempt to get a non-existent variable. Make sure that the variable is declared.", "Unauthorized": "Unauthorized [401]: The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource." }; return { code: statusCodes, msg: defaultMessages, popup: popup, confirm: confirm, inform: inform, MessageModel: MessageModel }; /** * Generate callback-function with an appropriate actions method. * @param action {Function | null} * @param msg {String | null} * @param opts {Object | null} * @returns {*} */ function confirm(action, msg, opts){ if (action == null) throw new Error (defaultMessages.VariableIsUndefined); return Lobibox.confirm({ msg: msg || translationObject['DoYouReallyWantDoThis'], callback: function ($this, type, ev) { if (type == "yes") { try { action(); } catch (err) { console.log(err); throw new Error([err, ". ", defaultMessages.CallbackIsNotFunction].join("")); } } }, title: opts ? opts.title : translationObject['Confirmation'], buttons: { yes: { 'class': 'lobibox-btn lobibox-btn-yes', text: translationObject['Yes'], closeOnClick: true }, no: { 'class': 'lobibox-btn lobibox-btn-no', text: translationObject['No'], closeOnClick: true } } } ); } /** * Plain and simple window to describe a specific user action. * @param msg {String|null} * @param type {String|null} * @param opts {Object|null} */ function inform(msg, type, opts){ Lobibox.notify(type || 'success', { msg: msg || translationObject["DescriptionP"], sound: opts ? opts.sound : false, title: opts ? opts.title : (translationObject[upperFirstLetter(type)] || translationObject['Success']) }); } /** * Validate data and if received type 'array' - concatenate all messages or just display itself. * @param {Array|String|Number} data * @param {String} msgType * @param {Function} callback */ function popup(data, msgType, callback) { if (angular.isArray(data)) { var errorList = data.join(' '); simplePopup(errorList, msgType, callback); } else if (angular.isString(data)) { simplePopup(data, msgType, callback); } else { simplePopup(data ? data.Exception : defaultMessages.ResponseNull, msgType, callback); } } /** * Default popup messenger. * @param {String|Number} msg * @param {String} type * @param {Function} callback */ function simplePopup(msg, type, callback) { Lobibox.alert(type || 'error', { msg: msg, closeButton: false, draggable: true, buttons: { ok: { 'class': 'btn btn-info', text: translationObject['Ok'], closeOnClick: true } }, callback: function($this, type, ev){ if (type == 'ok' && callback) { callback(); } } }); $(".lobibox").css({ 'z-index': '999999999' }); } function upperFirstLetter(string) { if(string){ return string.charAt(0).toUpperCase() + string.slice(1); } return string; } function MessageModel(type, text){ this.type = type || 'info'; this.text = text || null; } } // IoC container. uiNotifications.$inject = [ "webApi.statusCodes", "$translate", "helpers.localization" ]; })();