var abp = abp || {}; (function ($) { /* application paths *****************************************/ //current application root path (including virtual directory if exists). abp.apppath = abp.apppath || '/'; abp.pageloadtime = new date(); //converts given path to absolute path using abp.apppath variable. abp.toabsapppath = function (path) { if (path.indexof('/') == 0) { path = path.substring(1); } return abp.apppath + path; }; /* multitenancy */ abp.multitenancy = abp.multitenancy || {}; abp.multitenancy.isenabled = false; abp.multitenancy.sides = { tenant: 1, host: 2 }; abp.multitenancy.tenantidcookiename = 'mt.tenantid'; abp.multitenancy.settenantidcookie = function (tenantid) { if (tenantid) { abp.utils.setcookievalue( abp.multitenancy.tenantidcookiename, tenantid.tostring(), new date(new date().gettime() + 5 * 365 * 86400000), //5 years abp.apppath, abp.domain ); } else { abp.utils.deletecookie(abp.multitenancy.tenantidcookiename, abp.apppath); } }; abp.multitenancy.gettenantidcookie = function () { var value = abp.utils.getcookievalue(abp.multitenancy.tenantidcookiename); if (!value) { return null; } return parseint(value); } /* session */ abp.session = abp.session || { multitenancyside: abp.multitenancy.sides.host }; /* localization ***********************************************/ //implements localization api that simplifies usage of localization scripts generated by abp. abp.localization = abp.localization || {}; abp.localization.languages = []; abp.localization.currentlanguage = {}; abp.localization.sources = []; abp.localization.values = {}; abp.localization.localize = function (key, sourcename) { sourcename = sourcename || abp.localization.defaultsourcename; var source = abp.localization.values[sourcename]; if (!source) { abp.log.warn('could not find localization source: ' + sourcename); return key; } var value = source[key]; if (value == undefined) { return key; } var copiedarguments = array.prototype.slice.call(arguments, 0); copiedarguments.splice(1, 1); copiedarguments[0] = value; return abp.utils.formatstring.apply(this, copiedarguments); }; abp.localization.getsource = function (sourcename) { return function (key) { var copiedarguments = array.prototype.slice.call(arguments, 0); copiedarguments.splice(1, 0, sourcename); return abp.localization.localize.apply(this, copiedarguments); }; }; abp.localization.iscurrentculture = function (name) { return abp.localization.currentculture && abp.localization.currentculture.name && abp.localization.currentculture.name.indexof(name) == 0; }; abp.localization.defaultsourcename = undefined; abp.localization.abpweb = abp.localization.getsource('mtweb'); /* authorization **********************************************/ //implements authorization api that simplifies usage of authorization scripts generated by abp. abp.auth = abp.auth || {}; abp.auth.allpermissions = abp.auth.allpermissions || {}; abp.auth.grantedpermissions = abp.auth.grantedpermissions || {}; //deprecated. use abp.auth.isgranted instead. abp.auth.haspermission = function (permissionname) { return abp.auth.isgranted.apply(this, arguments); }; //deprecated. use abp.auth.isanygranted instead. abp.auth.hasanyofpermissions = function () { return abp.auth.isanygranted.apply(this, arguments); }; //deprecated. use abp.auth.areallgranted instead. abp.auth.hasallofpermissions = function () { return abp.auth.areallgranted.apply(this, arguments); }; abp.auth.isgranted = function (permissionname) { return abp.auth.allpermissions[permissionname] != undefined && abp.auth.grantedpermissions[permissionname] != undefined; }; abp.auth.isanygranted = function () { if (!arguments || arguments.length <= 0) { return true; } for (var i = 0; i < arguments.length; i++) { if (abp.auth.isgranted(arguments[i])) { return true; } } return false; }; abp.auth.areallgranted = function () { if (!arguments || arguments.length <= 0) { return true; } for (var i = 0; i < arguments.length; i++) { if (!abp.auth.isgranted(arguments[i])) { return false; } } return true; }; abp.auth.tokencookiename = 'mt.authtoken'; abp.auth.settoken = function (authtoken, expiredate) { abp.utils.setcookievalue(abp.auth.tokencookiename, authtoken, expiredate, abp.apppath, abp.domain); }; abp.auth.gettoken = function () { return abp.utils.getcookievalue(abp.auth.tokencookiename); } abp.auth.cleartoken = function () { abp.auth.settoken(); } /* feature system *********************************************/ //implements features api that simplifies usage of feature scripts generated by abp. abp.features = abp.features || {}; abp.features.allfeatures = abp.features.allfeatures || {}; abp.features.get = function (name) { return abp.features.allfeatures[name]; } abp.features.getvalue = function (name) { var feature = abp.features.get(name); if (feature == undefined) { return undefined; } return feature.value; } abp.features.isenabled = function (name) { var value = abp.features.getvalue(name); return value == 'true' || value == 'true'; } /* settings **************************************************/ //implements settings api that simplifies usage of setting scripts generated by abp. abp.setting = abp.setting || {}; abp.setting.values = abp.setting.values || {}; abp.setting.get = function (name) { return abp.setting.values[name]; }; abp.setting.getboolean = function (name) { var value = abp.setting.get(name); return value == 'true' || value == 'true'; }; abp.setting.getint = function (name) { return parseint(abp.setting.values[name]); }; /* realtime notifications ************************************/ abp.notifications = abp.notifications || {}; abp.notifications.severity = { info: 0, success: 1, warn: 2, error: 3, fatal: 4 }; abp.notifications.usernotificationstate = { unread: 0, read: 1 }; abp.notifications.getusernotificationstateasstring = function (usernotificationstate) { switch (usernotificationstate) { case abp.notifications.usernotificationstate.read: return 'read'; case abp.notifications.usernotificationstate.unread: return 'unread'; default: abp.log.warn('unknown user notification state value: ' + usernotificationstate) return '?'; } }; abp.notifications.getuinotifyfuncbyseverity = function (severity) { switch (severity) { case abp.notifications.severity.success: return abp.notify.success; case abp.notifications.severity.warn: return abp.notify.warn; case abp.notifications.severity.error: return abp.notify.error; case abp.notifications.severity.fatal: return abp.notify.error; case abp.notifications.severity.info: default: return abp.notify.info; } }; abp.notifications.messageformatters = {}; abp.notifications.messageformatters['mt.notifications.messagenotificationdata'] = function (usernotification) { return usernotification.notification.data.message || usernotification.notification.data.properties.message; }; abp.notifications.messageformatters['mt.notifications.localizablemessagenotificationdata'] = function (usernotification) { var message = usernotification.notification.data.message || usernotification.notification.data.properties.message; var localizedmessage = abp.localization.localize( message.name, message.sourcename ); if (usernotification.notification.data.properties) { if ($) { //prefer to use jquery if possible $.each(usernotification.notification.data.properties, function (key, value) { localizedmessage = localizedmessage.replace('{' + key + '}', value); }); } else { //alternative for $.each var properties = object.keys(usernotification.notification.data.properties); for (var i = 0; i < properties.length; i++) { localizedmessage = localizedmessage.replace('{' + properties[i] + '}', usernotification.notification.data.properties[properties[i]]); } } } return localizedmessage; }; abp.notifications.getformattedmessagefromusernotification = function (usernotification) { var formatter = abp.notifications.messageformatters[usernotification.notification.data.type]; if (!formatter) { abp.log.warn('no message formatter defined for given data type: ' + usernotification.notification.data.type) return '?'; } if (!abp.utils.isfunction(formatter)) { abp.log.warn('message formatter should be a function! it is invalid for data type: ' + usernotification.notification.data.type) return '?'; } return formatter(usernotification); } abp.notifications.showuinotifyforusernotification = function (usernotification, options) { var message = abp.notifications.getformattedmessagefromusernotification(usernotification); var uinotifyfunc = abp.notifications.getuinotifyfuncbyseverity(usernotification.notification.severity); uinotifyfunc(message, undefined, options); } /* logging ***************************************************/ //implements logging api that provides secure & controlled usage of console.log abp.log = abp.log || {}; abp.log.levels = { debug: 1, info: 2, warn: 3, error: 4, fatal: 5 }; abp.log.level = abp.log.levels.debug; abp.log.log = function (logobject, loglevel) { if (!window.console || !window.console.log) { return; } if (loglevel != undefined && loglevel < abp.log.level) { return; } console.log(logobject); }; abp.log.debug = function (logobject) { abp.log.log("debug: ", abp.log.levels.debug); abp.log.log(logobject, abp.log.levels.debug); }; abp.log.info = function (logobject) { abp.log.log("info: ", abp.log.levels.info); abp.log.log(logobject, abp.log.levels.info); }; abp.log.warn = function (logobject) { abp.log.log("warn: ", abp.log.levels.warn); abp.log.log(logobject, abp.log.levels.warn); }; abp.log.error = function (logobject) { abp.log.log("error: ", abp.log.levels.error); abp.log.log(logobject, abp.log.levels.error); }; abp.log.fatal = function (logobject) { abp.log.log("fatal: ", abp.log.levels.fatal); abp.log.log(logobject, abp.log.levels.fatal); }; /* notification *********************************************/ //defines notification api, not implements it abp.notify = abp.notify || {}; abp.notify.success = function (message, title, options) { abp.log.warn('abp.notify.success is not implemented!'); }; abp.notify.info = function (message, title, options) { abp.log.warn('abp.notify.info is not implemented!'); }; abp.notify.warn = function (message, title, options) { abp.log.warn('abp.notify.warn is not implemented!'); }; abp.notify.error = function (message, title, options) { abp.log.warn('abp.notify.error is not implemented!'); }; /* message **************************************************/ //defines message api, not implements it abp.message = abp.message || {}; var showmessage = function (message, title) { alert((title || '') + ' ' + message); if (!$) { abp.log.warn('abp.message can not return promise since jquery is not defined!'); return null; } return $.deferred(function ($dfd) { $dfd.resolve(); }); }; abp.message.info = function (message, title) { abp.log.warn('abp.message.info is not implemented!'); return showmessage(message, title); }; abp.message.success = function (message, title) { abp.log.warn('abp.message.success is not implemented!'); return showmessage(message, title); }; abp.message.warn = function (message, title) { abp.log.warn('abp.message.warn is not implemented!'); return showmessage(message, title); }; abp.message.error = function (message, title) { abp.log.warn('abp.message.error is not implemented!'); return showmessage(message, title); }; abp.message.confirm = function (message, titleorcallback, callback) { abp.log.warn('abp.message.confirm is not implemented!'); if (titleorcallback && !(typeof titleorcallback == 'string')) { callback = titleorcallback; } var result = confirm(message); callback && callback(result); if (!$) { abp.log.warn('abp.message can not return promise since jquery is not defined!'); return null; } return $.deferred(function ($dfd) { $dfd.resolve(); }); }; /* ui *******************************************************/ abp.ui = abp.ui || {}; /* ui block */ //defines ui block api, not implements it abp.ui.block = function (elm) { abp.log.warn('abp.ui.block is not implemented!'); }; abp.ui.unblock = function (elm) { abp.log.warn('abp.ui.unblock is not implemented!'); }; /* ui busy */ //defines ui busy api, not implements it abp.ui.setbusy = function (elm, optionsorpromise) { abp.log.warn('abp.ui.setbusy is not implemented!'); }; abp.ui.clearbusy = function (elm) { abp.log.warn('abp.ui.clearbusy is not implemented!'); }; /* simple event bus *****************************************/ abp.event = (function () { var _callbacks = {}; var on = function (eventname, callback) { if (!_callbacks[eventname]) { _callbacks[eventname] = []; } _callbacks[eventname].push(callback); }; var off = function (eventname, callback) { var callbacks = _callbacks[eventname]; if (!callbacks) { return; } var index = -1; for (var i = 0; i < callbacks.length; i++) { if (callbacks[i] === callback) { index = i; break; } } if (index < 0) { return; } _callbacks[eventname].splice(index, 1); }; var trigger = function (eventname) { var callbacks = _callbacks[eventname]; if (!callbacks || !callbacks.length) { return; } var args = array.prototype.slice.call(arguments, 1); for (var i = 0; i < callbacks.length; i++) { callbacks[i].apply(this, args); } }; // public interface /////////////////////////////////////////////////// return { on: on, off: off, trigger: trigger }; })(); /* utils ***************************************************/ abp.utils = abp.utils || {}; /* creates a name namespace. * example: * var taskservice = abp.utils.createnamespace(abp, 'services.task'); * taskservice will be equal to abp.services.task * first argument (root) must be defined first ************************************************************/ abp.utils.createnamespace = function (root, ns) { var parts = ns.split('.'); for (var i = 0; i < parts.length; i++) { if (typeof root[parts[i]] == 'undefined') { root[parts[i]] = {}; } root = root[parts[i]]; } return root; }; /* find and replaces a string (search) to another string (replacement) in * given string (str). * example: * abp.utils.replaceall('this is a test string', 'is', 'x') = 'thx x a test string' ************************************************************/ abp.utils.replaceall = function (str, search, replacement) { var fix = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return str.replace(new regexp(fix, 'g'), replacement); }; /* formats a string just like string.format in c#. * example: * abp.utils.formatstring('hello {0}','tuana') = 'hello tuana' ************************************************************/ abp.utils.formatstring = function () { if (arguments.length < 1) { return null; } var str = arguments[0]; for (var i = 1; i < arguments.length; i++) { var placeholder = '{' + (i - 1) + '}'; str = abp.utils.replaceall(str, placeholder, arguments[i]); } return str; }; abp.utils.topascalcase = function (str) { if (!str || !str.length) { return str; } if (str.length === 1) { return str.charat(0).touppercase(); } return str.charat(0).touppercase() + str.substr(1); } abp.utils.tocamelcase = function (str) { if (!str || !str.length) { return str; } if (str.length === 1) { return str.charat(0).tolowercase(); } return str.charat(0).tolowercase() + str.substr(1); } abp.utils.truncatestring = function (str, maxlength) { if (!str || !str.length || str.length <= maxlength) { return str; } return str.substr(0, maxlength); }; abp.utils.truncatestringwithpostfix = function (str, maxlength, postfix) { postfix = postfix || '...'; if (!str || !str.length || str.length <= maxlength) { return str; } if (maxlength <= postfix.length) { return postfix.substr(0, maxlength); } return str.substr(0, maxlength - postfix.length) + postfix; }; abp.utils.isfunction = function (obj) { if ($) { //prefer to use jquery if possible return $.isfunction(obj); } //alternative for $.isfunction return !!(obj && obj.constructor && obj.call && obj.apply); }; /** * parameterinfos should be an array of { name, value } objects * where name is query string parameter name and value is it's value. * includequestionmark is true by default. */ abp.utils.buildquerystring = function (parameterinfos, includequestionmark) { if (includequestionmark === undefined) { includequestionmark = true; } var qs = ''; function addseperator() { if (!qs.length) { if (includequestionmark) { qs = qs + '?'; } } else { qs = qs + '&'; } } for (var i = 0; i < parameterinfos.length; ++i) { var parameterinfo = parameterinfos[i]; if (parameterinfo.value === undefined) { continue; } if (parameterinfo.value === null) { parameterinfo.value = ''; } addseperator(); if (parameterinfo.value.tojson && typeof parameterinfo.value.tojson === "function") { qs = qs + parameterinfo.name + '=' + encodeuricomponent(parameterinfo.value.tojson()); } else if (array.isarray(parameterinfo.value) && parameterinfo.value.length) { for (var j = 0; j < parameterinfo.value.length; j++) { if (j > 0) { addseperator(); } qs = qs + parameterinfo.name + '[' + j + ']=' + encodeuricomponent(parameterinfo.value[j]); } } else { qs = qs + parameterinfo.name + '=' + encodeuricomponent(parameterinfo.value); } } return qs; } /** * sets a cookie value for given key. * this is a simple implementation created to be used by abp. * please use a complete cookie library if you need. * @param {string} key * @param {string} value * @param {date} expiredate (optional). if not specified the cookie will expire at the end of session. * @param {string} path (optional) */ abp.utils.setcookievalue = function (key, value, expiredate, path, domain) { var cookievalue = encodeuricomponent(key) + '='; if (value) { cookievalue = cookievalue + encodeuricomponent(value); } if (expiredate) { cookievalue = cookievalue + "; expires=" + expiredate.toutcstring(); } if (path) { cookievalue = cookievalue + "; path=" + path; } if (domain) { cookievalue = cookievalue + "; domain=" + domain; } document.cookie = cookievalue; }; /** * gets a cookie with given key. * this is a simple implementation created to be used by abp. * please use a complete cookie library if you need. * @param {string} key * @returns {string} cookie value or null */ abp.utils.getcookievalue = function (key) { var equalities = document.cookie.split('; '); for (var i = 0; i < equalities.length; i++) { if (!equalities[i]) { continue; } var splitted = equalities[i].split('='); if (splitted.length != 2) { continue; } if (decodeuricomponent(splitted[0]) === key) { return decodeuricomponent(splitted[1] || ''); } } return null; }; /** * deletes cookie for given key. * this is a simple implementation created to be used by abp. * please use a complete cookie library if you need. * @param {string} key * @param {string} path (optional) */ abp.utils.deletecookie = function (key, path) { var cookievalue = encodeuricomponent(key) + '='; cookievalue = cookievalue + "; expires=" + (new date(new date().gettime() - 86400000)).toutcstring(); if (path) { cookievalue = cookievalue + "; path=" + path; } document.cookie = cookievalue; } /** * gets the domain of given url * @param {string} url * @returns {string} */ abp.utils.getdomain = function (url) { var domainregex = /(https?:){0,1}\/\/((?:[\w\d-]+\.)+[\w\d]{2,})/i; var matches = domainregex.exec(url); return (matches && matches[2]) ? matches[2] : ''; } /* timing *****************************************/ abp.timing = abp.timing || {}; abp.timing.utcclockprovider = (function () { var toutc = function (date) { return date.utc( date.getutcfullyear() , date.getutcmonth() , date.getutcdate() , date.getutchours() , date.getutcminutes() , date.getutcseconds() , date.getutcmilliseconds() ); } var now = function () { return new date(); }; var normalize = function (date) { if (!date) { return date; } return new date(toutc(date)); }; // public interface /////////////////////////////////////////////////// return { now: now, normalize: normalize, supportsmultipletimezone: true }; })(); abp.timing.localclockprovider = (function () { var tolocal = function (date) { return new date( date.getfullyear() , date.getmonth() , date.getdate() , date.gethours() , date.getminutes() , date.getseconds() , date.getmilliseconds() ); } var now = function () { return tolocal(new date()); } var normalize = function (date) { if (!date) { return date; } return tolocal(date); } // public interface /////////////////////////////////////////////////// return { now: now, normalize: normalize, supportsmultipletimezone: false }; })(); abp.timing.unspecifiedclockprovider = (function () { var now = function () { return new date(); } var normalize = function (date) { return date; } // public interface /////////////////////////////////////////////////// return { now: now, normalize: normalize, supportsmultipletimezone: false }; })(); abp.timing.converttousertimezone = function (date) { var localtime = date.gettime(); var utctime = localtime + (date.gettimezoneoffset() * 60000); var targettime = parseint(utctime) + parseint(abp.timing.timezoneinfo.windows.currentutcoffsetinmilliseconds); return new date(targettime); }; /* clock *****************************************/ abp.clock = abp.clock || {}; abp.clock.now = function () { if (abp.clock.provider) { return abp.clock.provider.now(); } return new date(); } abp.clock.normalize = function (date) { if (abp.clock.provider) { return abp.clock.provider.normalize(date); } return date; } abp.clock.provider = abp.timing.unspecifiedclockprovider; /* security ***************************************/ abp.security = abp.security || {}; abp.security.antiforgery = abp.security.antiforgery || {}; abp.security.antiforgery.tokencookiename = 'xsrf-token'; abp.security.antiforgery.tokenheadername = 'x-xsrf-token'; abp.security.antiforgery.gettoken = function () { return abp.utils.getcookievalue(abp.security.antiforgery.tokencookiename); }; abp.security.antiforgery.shouldsendtoken = function (settings) { if (settings.crossdomain === undefined || settings.crossdomain === null) { return abp.utils.getdomain(location.href) === abp.utils.getdomain(settings.url); } return !settings.crossdomain; }; })(jquery);