abp.js 27 KB

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