abp.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. (function (define) {
  2. define(['jquery'], function ($) {
  3. return (function () {
  4. var abp = abp || {};
  5. /* Application paths *****************************************/
  6. //Current application root path (including virtual directory if exists).
  7. abp.appPath = abp.appPath || '/';
  8. abp.pageLoadTime = new Date();
  9. //Converts given path to absolute path using abp.appPath variable.
  10. abp.toAbsAppPath = function (path) {
  11. if (path.indexOf('/') == 0) {
  12. path = path.substring(1);
  13. }
  14. return abp.appPath + path;
  15. };
  16. /* MULTITENANCY */
  17. abp.multiTenancy = abp.multiTenancy || {};
  18. abp.multiTenancy.isEnabled = false;
  19. abp.multiTenancy.ignoreFeatureCheckForHostUsers = false;
  20. abp.multiTenancy.sides = {
  21. TENANT: 1,
  22. HOST: 2
  23. };
  24. abp.multiTenancy.tenantIdCookieName = 'Abp.TenantId';
  25. abp.multiTenancy.setTenantIdCookie = function (tenantId) {
  26. if (tenantId) {
  27. abp.utils.setCookieValue(
  28. abp.multiTenancy.tenantIdCookieName,
  29. tenantId.toString(),
  30. new Date(new Date().getTime() + 5 * 365 * 86400000), //5 years
  31. abp.appPath,
  32. abp.domain
  33. );
  34. } else {
  35. abp.utils.deleteCookie(abp.multiTenancy.tenantIdCookieName, abp.appPath);
  36. }
  37. };
  38. abp.multiTenancy.getTenantIdCookie = function () {
  39. var value = abp.utils.getCookieValue(abp.multiTenancy.tenantIdCookieName);
  40. if (!value) {
  41. return null;
  42. }
  43. return parseInt(value);
  44. }
  45. /* SESSION */
  46. abp.session = abp.session ||
  47. {
  48. multiTenancySide: abp.multiTenancy.sides.HOST
  49. };
  50. /* LOCALIZATION ***********************************************/
  51. //Implements Localization API that simplifies usage of localization scripts generated by Abp.
  52. abp.localization = abp.localization || {};
  53. abp.localization.languages = [];
  54. abp.localization.currentLanguage = {};
  55. abp.localization.sources = [];
  56. abp.localization.values = {};
  57. abp.localization.localize = function (key, sourceName) {
  58. sourceName = sourceName || abp.localization.defaultSourceName;
  59. var source = abp.localization.values[sourceName];
  60. if (!source) {
  61. abp.log.warn('Could not find localization source: ' + sourceName);
  62. return key;
  63. }
  64. var value = source[key];
  65. if (value == undefined) {
  66. return key;
  67. }
  68. var copiedArguments = Array.prototype.slice.call(arguments, 0);
  69. copiedArguments.splice(1, 1);
  70. copiedArguments[0] = value;
  71. return abp.utils.formatString.apply(this, copiedArguments);
  72. };
  73. abp.localization.getSource = function (sourceName) {
  74. return function (key) {
  75. var copiedArguments = Array.prototype.slice.call(arguments, 0);
  76. copiedArguments.splice(1, 0, sourceName);
  77. return abp.localization.localize.apply(this, copiedArguments);
  78. };
  79. };
  80. abp.localization.isCurrentCulture = function (name) {
  81. return abp.localization.currentCulture
  82. && abp.localization.currentCulture.name
  83. && abp.localization.currentCulture.name.indexOf(name) == 0;
  84. };
  85. abp.localization.defaultSourceName = undefined;
  86. abp.localization.abpWeb = abp.localization.getSource('AbpWeb');
  87. /* AUTHORIZATION **********************************************/
  88. //Implements Authorization API that simplifies usage of authorization scripts generated by Abp.
  89. abp.auth = abp.auth || {};
  90. abp.auth.allPermissions = abp.auth.allPermissions || {};
  91. abp.auth.grantedPermissions = abp.auth.grantedPermissions || {};
  92. //Deprecated. Use abp.auth.isGranted instead.
  93. abp.auth.hasPermission = function (permissionName) {
  94. return abp.auth.isGranted.apply(this, arguments);
  95. };
  96. //Deprecated. Use abp.auth.isAnyGranted instead.
  97. abp.auth.hasAnyOfPermissions = function () {
  98. return abp.auth.isAnyGranted.apply(this, arguments);
  99. };
  100. //Deprecated. Use abp.auth.areAllGranted instead.
  101. abp.auth.hasAllOfPermissions = function () {
  102. return abp.auth.areAllGranted.apply(this, arguments);
  103. };
  104. abp.auth.isGranted = function (permissionName) {
  105. return abp.auth.allPermissions[permissionName] != undefined &&
  106. abp.auth.grantedPermissions[permissionName] != undefined;
  107. };
  108. abp.auth.isAnyGranted = function () {
  109. if (!arguments || arguments.length <= 0) {
  110. return true;
  111. }
  112. for (var i = 0; i < arguments.length; i++) {
  113. if (abp.auth.isGranted(arguments[i])) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. };
  119. abp.auth.areAllGranted = function () {
  120. if (!arguments || arguments.length <= 0) {
  121. return true;
  122. }
  123. for (var i = 0; i < arguments.length; i++) {
  124. if (!abp.auth.isGranted(arguments[i])) {
  125. return false;
  126. }
  127. }
  128. return true;
  129. };
  130. abp.auth.tokenCookieName = 'Abp.AuthToken';
  131. abp.auth.setToken = function (authToken, expireDate) {
  132. abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath, abp.domain);
  133. };
  134. abp.auth.getToken = function () {
  135. return abp.utils.getCookieValue(abp.auth.tokenCookieName);
  136. }
  137. abp.auth.clearToken = function () {
  138. abp.auth.setToken();
  139. }
  140. /* FEATURE SYSTEM *********************************************/
  141. //Implements Features API that simplifies usage of feature scripts generated by Abp.
  142. abp.features = abp.features || {};
  143. abp.features.allFeatures = abp.features.allFeatures || {};
  144. abp.features.get = function (name) {
  145. return abp.features.allFeatures[name];
  146. }
  147. abp.features.getValue = function (name) {
  148. var feature = abp.features.get(name);
  149. if (feature == undefined) {
  150. return undefined;
  151. }
  152. return feature.value;
  153. }
  154. abp.features.isEnabled = function (name) {
  155. var value = abp.features.getValue(name);
  156. return value == 'true' || value == 'True';
  157. }
  158. /* SETTINGS **************************************************/
  159. //Implements Settings API that simplifies usage of setting scripts generated by Abp.
  160. abp.setting = abp.setting || {};
  161. abp.setting.values = abp.setting.values || {};
  162. abp.setting.get = function (name) {
  163. return abp.setting.values[name];
  164. };
  165. abp.setting.getBoolean = function (name) {
  166. var value = abp.setting.get(name);
  167. return value == 'true' || value == 'True';
  168. };
  169. abp.setting.getInt = function (name) {
  170. return parseInt(abp.setting.values[name]);
  171. };
  172. /* REALTIME NOTIFICATIONS ************************************/
  173. abp.notifications = abp.notifications || {};
  174. abp.notifications.severity = {
  175. INFO: 0,
  176. SUCCESS: 1,
  177. WARN: 2,
  178. ERROR: 3,
  179. FATAL: 4
  180. };
  181. abp.notifications.userNotificationState = {
  182. UNREAD: 0,
  183. READ: 1
  184. };
  185. abp.notifications.getUserNotificationStateAsString = function (userNotificationState) {
  186. switch (userNotificationState) {
  187. case abp.notifications.userNotificationState.READ:
  188. return 'READ';
  189. case abp.notifications.userNotificationState.UNREAD:
  190. return 'UNREAD';
  191. default:
  192. abp.log.warn('Unknown user notification state value: ' + userNotificationState)
  193. return '?';
  194. }
  195. };
  196. abp.notifications.getUiNotifyFuncBySeverity = function (severity) {
  197. switch (severity) {
  198. case abp.notifications.severity.SUCCESS:
  199. return abp.notify.success;
  200. case abp.notifications.severity.WARN:
  201. return abp.notify.warn;
  202. case abp.notifications.severity.ERROR:
  203. return abp.notify.error;
  204. case abp.notifications.severity.FATAL:
  205. return abp.notify.error;
  206. case abp.notifications.severity.INFO:
  207. default:
  208. return abp.notify.info;
  209. }
  210. };
  211. abp.notifications.messageFormatters = {};
  212. abp.notifications.messageFormatters['Abp.Notifications.MessageNotificationData'] = function (userNotification) {
  213. return userNotification.notification.data.message || userNotification.notification.data.properties.Message;
  214. };
  215. abp.notifications.messageFormatters['Abp.Notifications.LocalizableMessageNotificationData'] =
  216. function (userNotification) {
  217. var message = userNotification.notification.data.message ||
  218. userNotification.notification.data.properties.Message;
  219. var localizedMessage = abp.localization.localize(
  220. message.name,
  221. message.sourceName
  222. );
  223. if (userNotification.notification.data.properties) {
  224. if ($) {
  225. //Prefer to use jQuery if possible
  226. $.each(userNotification.notification.data.properties, function (key, value) {
  227. localizedMessage = localizedMessage.replace('{' + key + '}', value);
  228. });
  229. } else {
  230. //alternative for $.each
  231. var properties = Object.keys(userNotification.notification.data.properties);
  232. for (var i = 0; i < properties.length; i++) {
  233. localizedMessage = localizedMessage.replace('{' + properties[i] + '}',
  234. userNotification.notification.data.properties[properties[i]]);
  235. }
  236. }
  237. }
  238. return localizedMessage;
  239. };
  240. abp.notifications.getFormattedMessageFromUserNotification = function (userNotification) {
  241. var formatter = abp.notifications.messageFormatters[userNotification.notification.data.type];
  242. if (!formatter) {
  243. abp.log.warn('No message formatter defined for given data type: ' + userNotification.notification.data.type)
  244. return '?';
  245. }
  246. if (!abp.utils.isFunction(formatter)) {
  247. abp.log.warn('Message formatter should be a function! It is invalid for data type: ' +
  248. userNotification.notification.data.type)
  249. return '?';
  250. }
  251. return formatter(userNotification);
  252. }
  253. abp.notifications.showUiNotifyForUserNotification = function (userNotification, options) {
  254. var message = abp.notifications.getFormattedMessageFromUserNotification(userNotification);
  255. var uiNotifyFunc = abp.notifications.getUiNotifyFuncBySeverity(userNotification.notification.severity);
  256. uiNotifyFunc(message, undefined, options);
  257. }
  258. /* LOGGING ***************************************************/
  259. //Implements Logging API that provides secure & controlled usage of console.log
  260. abp.log = abp.log || {};
  261. abp.log.levels = {
  262. DEBUG: 1,
  263. INFO: 2,
  264. WARN: 3,
  265. ERROR: 4,
  266. FATAL: 5
  267. };
  268. abp.log.level = abp.log.levels.DEBUG;
  269. abp.log.log = function (logObject, logLevel) {
  270. if (!window.console || !window.console.log) {
  271. return;
  272. }
  273. if (logLevel != undefined && logLevel < abp.log.level) {
  274. return;
  275. }
  276. console.log(logObject);
  277. };
  278. abp.log.debug = function (logObject) {
  279. abp.log.log("DEBUG: ", abp.log.levels.DEBUG);
  280. abp.log.log(logObject, abp.log.levels.DEBUG);
  281. };
  282. abp.log.info = function (logObject) {
  283. abp.log.log("INFO: ", abp.log.levels.INFO);
  284. abp.log.log(logObject, abp.log.levels.INFO);
  285. };
  286. abp.log.warn = function (logObject) {
  287. abp.log.log("WARN: ", abp.log.levels.WARN);
  288. abp.log.log(logObject, abp.log.levels.WARN);
  289. };
  290. abp.log.error = function (logObject) {
  291. abp.log.log("ERROR: ", abp.log.levels.ERROR);
  292. abp.log.log(logObject, abp.log.levels.ERROR);
  293. };
  294. abp.log.fatal = function (logObject) {
  295. abp.log.log("FATAL: ", abp.log.levels.FATAL);
  296. abp.log.log(logObject, abp.log.levels.FATAL);
  297. };
  298. /* NOTIFICATION *********************************************/
  299. //Defines Notification API, not implements it
  300. abp.notify = abp.notify || {};
  301. abp.notify.success = function (message, title, options) {
  302. abp.log.warn('abp.notify.success is not implemented!');
  303. };
  304. abp.notify.info = function (message, title, options) {
  305. abp.log.warn('abp.notify.info is not implemented!');
  306. };
  307. abp.notify.warn = function (message, title, options) {
  308. abp.log.warn('abp.notify.warn is not implemented!');
  309. };
  310. abp.notify.error = function (message, title, options) {
  311. abp.log.warn('abp.notify.error is not implemented!');
  312. };
  313. /* MESSAGE **************************************************/
  314. //Defines Message API, not implements it
  315. abp.message = abp.message || {};
  316. var showMessage = function (message, title, options) {
  317. alert((title || '') + ' ' + message);
  318. if (!$) {
  319. abp.log.warn('abp.message can not return promise since jQuery is not defined!');
  320. return null;
  321. }
  322. return $.Deferred(function ($dfd) {
  323. $dfd.resolve();
  324. });
  325. };
  326. abp.message.info = function (message, title, options) {
  327. abp.log.warn('abp.message.info is not implemented!');
  328. return showMessage(message, title, options);
  329. };
  330. abp.message.success = function (message, title, options) {
  331. abp.log.warn('abp.message.success is not implemented!');
  332. return showMessage(message, title, options);
  333. };
  334. abp.message.warn = function (message, title, options) {
  335. abp.log.warn('abp.message.warn is not implemented!');
  336. return showMessage(message, title, options);
  337. };
  338. abp.message.error = function (message, title, options) {
  339. abp.log.warn('abp.message.error is not implemented!');
  340. return showMessage(message, title, options);
  341. };
  342. abp.message.confirm = function (message, title, callback, options) {
  343. abp.log.warn('abp.message.confirm is not implemented!');
  344. var result = confirm(message);
  345. callback && callback(result);
  346. if (!$) {
  347. abp.log.warn('abp.message can not return promise since jQuery is not defined!');
  348. return null;
  349. }
  350. return $.Deferred(function ($dfd) {
  351. $dfd.resolve();
  352. });
  353. };
  354. /* UI *******************************************************/
  355. abp.ui = abp.ui || {};
  356. /* UI BLOCK */
  357. //Defines UI Block API, not implements it
  358. abp.ui.block = function (elm) {
  359. abp.log.warn('abp.ui.block is not implemented!');
  360. };
  361. abp.ui.unblock = function (elm) {
  362. abp.log.warn('abp.ui.unblock is not implemented!');
  363. };
  364. /* UI BUSY */
  365. //Defines UI Busy API, not implements it
  366. abp.ui.setBusy = function (elm, optionsOrPromise) {
  367. abp.log.warn('abp.ui.setBusy is not implemented!');
  368. };
  369. abp.ui.clearBusy = function (elm) {
  370. abp.log.warn('abp.ui.clearBusy is not implemented!');
  371. };
  372. /* SIMPLE EVENT BUS *****************************************/
  373. abp.event = (function () {
  374. var _callbacks = {};
  375. var on = function (eventName, callback) {
  376. if (!_callbacks[eventName]) {
  377. _callbacks[eventName] = [];
  378. }
  379. _callbacks[eventName].push(callback);
  380. };
  381. var off = function (eventName, callback) {
  382. var callbacks = _callbacks[eventName];
  383. if (!callbacks) {
  384. return;
  385. }
  386. var index = -1;
  387. for (var i = 0; i < callbacks.length; i++) {
  388. if (callbacks[i] === callback) {
  389. index = i;
  390. break;
  391. }
  392. }
  393. if (index < 0) {
  394. return;
  395. }
  396. _callbacks[eventName].splice(index, 1);
  397. };
  398. var trigger = function (eventName) {
  399. var callbacks = _callbacks[eventName];
  400. if (!callbacks || !callbacks.length) {
  401. return;
  402. }
  403. var args = Array.prototype.slice.call(arguments, 1);
  404. for (var i = 0; i < callbacks.length; i++) {
  405. callbacks[i].apply(this, args);
  406. }
  407. };
  408. // Public interface ///////////////////////////////////////////////////
  409. return {
  410. on: on,
  411. off: off,
  412. trigger: trigger
  413. };
  414. })();
  415. /* UTILS ***************************************************/
  416. abp.utils = abp.utils || {};
  417. /* Creates a name namespace.
  418. * Example:
  419. * var taskService = abp.utils.createNamespace(abp, 'services.task');
  420. * taskService will be equal to abp.services.task
  421. * first argument (root) must be defined first
  422. ************************************************************/
  423. abp.utils.createNamespace = function (root, ns) {
  424. var parts = ns.split('.');
  425. for (var i = 0; i < parts.length; i++) {
  426. if (typeof root[parts[i]] == 'undefined') {
  427. root[parts[i]] = {};
  428. }
  429. root = root[parts[i]];
  430. }
  431. return root;
  432. };
  433. /* Find and replaces a string (search) to another string (replacement) in
  434. * given string (str).
  435. * Example:
  436. * abp.utils.replaceAll('This is a test string', 'is', 'X') = 'ThX X a test string'
  437. ************************************************************/
  438. abp.utils.replaceAll = function (str, search, replacement) {
  439. var fix = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  440. return str.replace(new RegExp(fix, 'g'), replacement);
  441. };
  442. /* Formats a string just like string.format in C#.
  443. * Example:
  444. * abp.utils.formatString('Hello {0}','Tuana') = 'Hello Tuana'
  445. ************************************************************/
  446. abp.utils.formatString = function () {
  447. if (arguments.length < 1) {
  448. return null;
  449. }
  450. var str = arguments[0];
  451. for (var i = 1; i < arguments.length; i++) {
  452. var placeHolder = '{' + (i - 1) + '}';
  453. str = abp.utils.replaceAll(str, placeHolder, arguments[i]);
  454. }
  455. return str;
  456. };
  457. abp.utils.toPascalCase = function (str) {
  458. if (!str || !str.length) {
  459. return str;
  460. }
  461. if (str.length === 1) {
  462. return str.charAt(0).toUpperCase();
  463. }
  464. return str.charAt(0).toUpperCase() + str.substr(1);
  465. }
  466. abp.utils.toCamelCase = function (str) {
  467. if (!str || !str.length) {
  468. return str;
  469. }
  470. if (str.length === 1) {
  471. return str.charAt(0).toLowerCase();
  472. }
  473. return str.charAt(0).toLowerCase() + str.substr(1);
  474. }
  475. abp.utils.truncateString = function (str, maxLength) {
  476. if (!str || !str.length || str.length <= maxLength) {
  477. return str;
  478. }
  479. return str.substr(0, maxLength);
  480. };
  481. abp.utils.truncateStringWithPostfix = function (str, maxLength, postfix) {
  482. postfix = postfix || '...';
  483. if (!str || !str.length || str.length <= maxLength) {
  484. return str;
  485. }
  486. if (maxLength <= postfix.length) {
  487. return postfix.substr(0, maxLength);
  488. }
  489. return str.substr(0, maxLength - postfix.length) + postfix;
  490. };
  491. abp.utils.isFunction = function (obj) {
  492. if ($) {
  493. //Prefer to use jQuery if possible
  494. return $.isFunction(obj);
  495. }
  496. //alternative for $.isFunction
  497. return !!(obj && obj.constructor && obj.call && obj.apply);
  498. };
  499. /**
  500. * parameterInfos should be an array of { name, value } objects
  501. * where name is query string parameter name and value is it's value.
  502. * includeQuestionMark is true by default.
  503. */
  504. abp.utils.buildQueryString = function (parameterInfos, includeQuestionMark) {
  505. if (includeQuestionMark === undefined) {
  506. includeQuestionMark = true;
  507. }
  508. var qs = '';
  509. function addSeperator() {
  510. if (!qs.length) {
  511. if (includeQuestionMark) {
  512. qs = qs + '?';
  513. }
  514. } else {
  515. qs = qs + '&';
  516. }
  517. }
  518. for (var i = 0; i < parameterInfos.length; ++i) {
  519. var parameterInfo = parameterInfos[i];
  520. if (parameterInfo.value === undefined) {
  521. continue;
  522. }
  523. if (parameterInfo.value === null) {
  524. parameterInfo.value = '';
  525. }
  526. addSeperator();
  527. if (parameterInfo.value.toJSON && typeof parameterInfo.value.toJSON === "function") {
  528. qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value.toJSON());
  529. } else if (Array.isArray(parameterInfo.value) && parameterInfo.value.length) {
  530. for (var j = 0; j < parameterInfo.value.length; j++) {
  531. if (j > 0) {
  532. addSeperator();
  533. }
  534. qs = qs + parameterInfo.name + '[' + j + ']=' + encodeURIComponent(parameterInfo.value[j]);
  535. }
  536. } else {
  537. qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value);
  538. }
  539. }
  540. return qs;
  541. }
  542. /**
  543. * Sets a cookie value for given key.
  544. * This is a simple implementation created to be used by ABP.
  545. * Please use a complete cookie library if you need.
  546. * @param {string} key
  547. * @param {string} value
  548. * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session.
  549. * @param {string} path (optional)
  550. */
  551. abp.utils.setCookieValue = function (key, value, expireDate, path, domain) {
  552. var cookieValue = encodeURIComponent(key) + '=';
  553. if (value) {
  554. cookieValue = cookieValue + encodeURIComponent(value);
  555. }
  556. if (expireDate) {
  557. cookieValue = cookieValue + "; expires=" + expireDate.toUTCString();
  558. }
  559. if (path) {
  560. cookieValue = cookieValue + "; path=" + path;
  561. }
  562. if (domain) {
  563. cookieValue = cookieValue + "; domain=" + domain;
  564. }
  565. document.cookie = cookieValue;
  566. };
  567. /**
  568. * Gets a cookie with given key.
  569. * This is a simple implementation created to be used by ABP.
  570. * Please use a complete cookie library if you need.
  571. * @param {string} key
  572. * @returns {string} Cookie value or null
  573. */
  574. abp.utils.getCookieValue = function (key) {
  575. var equalities = document.cookie.split('; ');
  576. for (var i = 0; i < equalities.length; i++) {
  577. if (!equalities[i]) {
  578. continue;
  579. }
  580. var splitted = equalities[i].split('=');
  581. if (splitted.length != 2) {
  582. continue;
  583. }
  584. if (decodeURIComponent(splitted[0]) === key) {
  585. return decodeURIComponent(splitted[1] || '');
  586. }
  587. }
  588. return null;
  589. };
  590. /**
  591. * Deletes cookie for given key.
  592. * This is a simple implementation created to be used by ABP.
  593. * Please use a complete cookie library if you need.
  594. * @param {string} key
  595. * @param {string} path (optional)
  596. */
  597. abp.utils.deleteCookie = function (key, path) {
  598. var cookieValue = encodeURIComponent(key) + '=';
  599. cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString();
  600. if (path) {
  601. cookieValue = cookieValue + "; path=" + path;
  602. }
  603. document.cookie = cookieValue;
  604. }
  605. /**
  606. * Gets the domain of given url
  607. * @param {string} url
  608. * @returns {string}
  609. */
  610. abp.utils.getDomain = function (url) {
  611. var domainRegex = /(https?:){0,1}\/\/((?:[\w\d-]+\.)+[\w\d]{2,})/i;
  612. var matches = domainRegex.exec(url);
  613. return (matches && matches[2]) ? matches[2] : '';
  614. }
  615. /* TIMING *****************************************/
  616. abp.timing = abp.timing || {};
  617. abp.timing.utcClockProvider = (function () {
  618. var toUtc = function (date) {
  619. return Date.UTC(
  620. date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(),
  621. date.getUTCSeconds(), date.getUTCMilliseconds()
  622. );
  623. }
  624. var now = function () {
  625. return toUtc(new Date());
  626. };
  627. var normalize = function (date) {
  628. if (!date) {
  629. return date;
  630. }
  631. return new Date(toUtc(date));
  632. };
  633. // Public interface ///////////////////////////////////////////////////
  634. return {
  635. now: now,
  636. normalize: normalize,
  637. supportsMultipleTimezone: true
  638. };
  639. })();
  640. abp.timing.localClockProvider = (function () {
  641. var toLocal = function (date) {
  642. return new Date(
  643. date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(),
  644. date.getMilliseconds()
  645. );
  646. }
  647. var now = function () {
  648. return toLocal(new Date());
  649. }
  650. var normalize = function (date) {
  651. if (!date) {
  652. return date;
  653. }
  654. return toLocal(date);
  655. }
  656. // Public interface ///////////////////////////////////////////////////
  657. return {
  658. now: now,
  659. normalize: normalize,
  660. supportsMultipleTimezone: false
  661. };
  662. })();
  663. abp.timing.unspecifiedClockProvider = (function () {
  664. var now = function () {
  665. return new Date();
  666. }
  667. var normalize = function (date) {
  668. return date;
  669. }
  670. // Public interface ///////////////////////////////////////////////////
  671. return {
  672. now: now,
  673. normalize: normalize,
  674. supportsMultipleTimezone: false
  675. };
  676. })();
  677. abp.timing.convertToUserTimezone = function (date) {
  678. var localTime = date.getTime();
  679. var utcTime = localTime + (date.getTimezoneOffset() * 60000);
  680. var targetTime = parseInt(utcTime) + parseInt(abp.timing.timeZoneInfo.windows.currentUtcOffsetInMilliseconds);
  681. return new Date(targetTime);
  682. };
  683. /* CLOCK *****************************************/
  684. abp.clock = abp.clock || {};
  685. abp.clock.now = function () {
  686. if (abp.clock.provider) {
  687. return abp.clock.provider.now();
  688. }
  689. return new Date();
  690. }
  691. abp.clock.normalize = function (date) {
  692. if (abp.clock.provider) {
  693. return abp.clock.provider.normalize(date);
  694. }
  695. return date;
  696. }
  697. abp.clock.provider = abp.timing.unspecifiedClockProvider;
  698. /* SECURITY ***************************************/
  699. abp.security = abp.security || {};
  700. abp.security.antiForgery = abp.security.antiForgery || {};
  701. abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN';
  702. abp.security.antiForgery.tokenHeaderName = 'X-XSRF-TOKEN';
  703. abp.security.antiForgery.getToken = function () {
  704. return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName);
  705. };
  706. abp.security.antiForgery.shouldSendToken = function (settings) {
  707. if (settings.crossDomain === undefined || settings.crossDomain === null) {
  708. return abp.utils.getDomain(location.href) === abp.utils.getDomain(settings.url);
  709. }
  710. return !settings.crossDomain;
  711. };
  712. return abp;
  713. })();
  714. });
  715. }(typeof define === 'function' && define.amd
  716. ? define
  717. : function (deps, factory) {
  718. if (typeof module !== 'undefined' && module.exports) {
  719. module.exports = factory(require('jquery'));
  720. } else {
  721. window.abp = factory(window.jQuery);
  722. }
  723. }));