abp - 复制.js 27 KB

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