abp.js 27 KB

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