@abp/core 5.0.0-beta.1 → 5.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/abp.js CHANGED
@@ -1,776 +1,776 @@
1
- var abp = abp || {};
2
- (function () {
3
-
4
- /* Application paths *****************************************/
5
-
6
- //Current application root path (including virtual directory if exists).
7
- abp.appPath = abp.appPath || '/';
8
-
9
- abp.pageLoadTime = new Date();
10
-
11
- //Converts given path to absolute path using abp.appPath variable.
12
- abp.toAbsAppPath = function (path) {
13
- if (path.indexOf('/') == 0) {
14
- path = path.substring(1);
15
- }
16
-
17
- return abp.appPath + path;
18
- };
19
-
20
- /* LOGGING ***************************************************/
21
- //Implements Logging API that provides secure & controlled usage of console.log
22
-
23
- abp.log = abp.log || {};
24
-
25
- abp.log.levels = {
26
- DEBUG: 1,
27
- INFO: 2,
28
- WARN: 3,
29
- ERROR: 4,
30
- FATAL: 5
31
- };
32
-
33
- abp.log.level = abp.log.levels.DEBUG;
34
-
35
- abp.log.log = function (logObject, logLevel) {
36
- if (!window.console || !window.console.log) {
37
- return;
38
- }
39
-
40
- if (logLevel != undefined && logLevel < abp.log.level) {
41
- return;
42
- }
43
-
44
- console.log(logObject);
45
- };
46
-
47
- abp.log.debug = function (logObject) {
48
- abp.log.log("DEBUG: ", abp.log.levels.DEBUG);
49
- abp.log.log(logObject, abp.log.levels.DEBUG);
50
- };
51
-
52
- abp.log.info = function (logObject) {
53
- abp.log.log("INFO: ", abp.log.levels.INFO);
54
- abp.log.log(logObject, abp.log.levels.INFO);
55
- };
56
-
57
- abp.log.warn = function (logObject) {
58
- abp.log.log("WARN: ", abp.log.levels.WARN);
59
- abp.log.log(logObject, abp.log.levels.WARN);
60
- };
61
-
62
- abp.log.error = function (logObject) {
63
- abp.log.log("ERROR: ", abp.log.levels.ERROR);
64
- abp.log.log(logObject, abp.log.levels.ERROR);
65
- };
66
-
67
- abp.log.fatal = function (logObject) {
68
- abp.log.log("FATAL: ", abp.log.levels.FATAL);
69
- abp.log.log(logObject, abp.log.levels.FATAL);
70
- };
71
-
72
- /* LOCALIZATION ***********************************************/
73
-
74
- abp.localization = abp.localization || {};
75
-
76
- abp.localization.values = {};
77
-
78
- abp.localization.localize = function (key, sourceName) {
79
- if (sourceName === '_') { //A convention to suppress the localization
80
- return key;
81
- }
82
-
83
- sourceName = sourceName || abp.localization.defaultResourceName;
84
- if (!sourceName) {
85
- abp.log.warn('Localization source name is not specified and the defaultResourceName was not defined!');
86
- return key;
87
- }
88
-
89
- var source = abp.localization.values[sourceName];
90
- if (!source) {
91
- abp.log.warn('Could not find localization source: ' + sourceName);
92
- return key;
93
- }
94
-
95
- var value = source[key];
96
- if (value == undefined) {
97
- return key;
98
- }
99
-
100
- var copiedArguments = Array.prototype.slice.call(arguments, 0);
101
- copiedArguments.splice(1, 1);
102
- copiedArguments[0] = value;
103
-
104
- return abp.utils.formatString.apply(this, copiedArguments);
105
- };
106
-
107
- abp.localization.isLocalized = function (key, sourceName) {
108
- if (sourceName === '_') { //A convention to suppress the localization
109
- return true;
110
- }
111
-
112
- sourceName = sourceName || abp.localization.defaultResourceName;
113
- if (!sourceName) {
114
- return false;
115
- }
116
-
117
- var source = abp.localization.values[sourceName];
118
- if (!source) {
119
- return false;
120
- }
121
-
122
- var value = source[key];
123
- if (value === undefined) {
124
- return false;
125
- }
126
-
127
- return true;
128
- };
129
-
130
- abp.localization.getResource = function (name) {
131
- return function () {
132
- var copiedArguments = Array.prototype.slice.call(arguments, 0);
133
- copiedArguments.splice(1, 0, name);
134
- return abp.localization.localize.apply(this, copiedArguments);
135
- };
136
- };
137
-
138
- abp.localization.defaultResourceName = undefined;
139
- abp.localization.currentCulture = {
140
- cultureName: undefined
141
- };
142
-
143
- var getMapValue = function (packageMaps, packageName, language) {
144
- language = language || abp.localization.currentCulture.name;
145
- if (!packageMaps || !packageName || !language) {
146
- return language;
147
- }
148
-
149
- var packageMap = packageMaps[packageName];
150
- if (!packageMap) {
151
- return language;
152
- }
153
-
154
- for (var i = 0; i < packageMap.length; i++) {
155
- var map = packageMap[i];
156
- if (map.name === language){
157
- return map.value;
158
- }
159
- }
160
-
161
- return language;
162
- };
163
-
164
- abp.localization.getLanguagesMap = function (packageName, language) {
165
- return getMapValue(abp.localization.languagesMap, packageName, language);
166
- };
167
-
168
- abp.localization.getLanguageFilesMap = function (packageName, language) {
169
- return getMapValue(abp.localization.languageFilesMap, packageName, language);
170
- };
171
-
172
- /* AUTHORIZATION **********************************************/
173
-
174
- abp.auth = abp.auth || {};
175
-
176
- abp.auth.policies = abp.auth.policies || {};
177
-
178
- abp.auth.grantedPolicies = abp.auth.grantedPolicies || {};
179
-
180
- abp.auth.isGranted = function (policyName) {
181
- return abp.auth.policies[policyName] != undefined && abp.auth.grantedPolicies[policyName] != undefined;
182
- };
183
-
184
- abp.auth.isAnyGranted = function () {
185
- if (!arguments || arguments.length <= 0) {
186
- return true;
187
- }
188
-
189
- for (var i = 0; i < arguments.length; i++) {
190
- if (abp.auth.isGranted(arguments[i])) {
191
- return true;
192
- }
193
- }
194
-
195
- return false;
196
- };
197
-
198
- abp.auth.areAllGranted = function () {
199
- if (!arguments || arguments.length <= 0) {
200
- return true;
201
- }
202
-
203
- for (var i = 0; i < arguments.length; i++) {
204
- if (!abp.auth.isGranted(arguments[i])) {
205
- return false;
206
- }
207
- }
208
-
209
- return true;
210
- };
211
-
212
- abp.auth.tokenCookieName = 'Abp.AuthToken';
213
-
214
- abp.auth.setToken = function (authToken, expireDate) {
215
- abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath, abp.domain);
216
- };
217
-
218
- abp.auth.getToken = function () {
219
- return abp.utils.getCookieValue(abp.auth.tokenCookieName);
220
- }
221
-
222
- abp.auth.clearToken = function () {
223
- abp.auth.setToken();
224
- }
225
-
226
- /* SETTINGS *************************************************/
227
-
228
- abp.setting = abp.setting || {};
229
-
230
- abp.setting.values = abp.setting.values || {};
231
-
232
- abp.setting.get = function (name) {
233
- return abp.setting.values[name];
234
- };
235
-
236
- abp.setting.getBoolean = function (name) {
237
- var value = abp.setting.get(name);
238
- return value == 'true' || value == 'True';
239
- };
240
-
241
- abp.setting.getInt = function (name) {
242
- return parseInt(abp.setting.values[name]);
243
- };
244
-
245
- /* NOTIFICATION *********************************************/
246
- //Defines Notification API, not implements it
247
-
248
- abp.notify = abp.notify || {};
249
-
250
- abp.notify.success = function (message, title, options) {
251
- abp.log.warn('abp.notify.success is not implemented!');
252
- };
253
-
254
- abp.notify.info = function (message, title, options) {
255
- abp.log.warn('abp.notify.info is not implemented!');
256
- };
257
-
258
- abp.notify.warn = function (message, title, options) {
259
- abp.log.warn('abp.notify.warn is not implemented!');
260
- };
261
-
262
- abp.notify.error = function (message, title, options) {
263
- abp.log.warn('abp.notify.error is not implemented!');
264
- };
265
-
266
- /* MESSAGE **************************************************/
267
- //Defines Message API, not implements it
268
-
269
- abp.message = abp.message || {};
270
-
271
- abp.message._showMessage = function (message, title) {
272
- alert((title || '') + ' ' + message);
273
- };
274
-
275
- abp.message.info = function (message, title) {
276
- abp.log.warn('abp.message.info is not implemented!');
277
- return abp.message._showMessage(message, title);
278
- };
279
-
280
- abp.message.success = function (message, title) {
281
- abp.log.warn('abp.message.success is not implemented!');
282
- return abp.message._showMessage(message, title);
283
- };
284
-
285
- abp.message.warn = function (message, title) {
286
- abp.log.warn('abp.message.warn is not implemented!');
287
- return abp.message._showMessage(message, title);
288
- };
289
-
290
- abp.message.error = function (message, title) {
291
- abp.log.warn('abp.message.error is not implemented!');
292
- return abp.message._showMessage(message, title);
293
- };
294
-
295
- abp.message.confirm = function (message, titleOrCallback, callback) {
296
- abp.log.warn('abp.message.confirm is not properly implemented!');
297
-
298
- if (titleOrCallback && !(typeof titleOrCallback == 'string')) {
299
- callback = titleOrCallback;
300
- }
301
-
302
- var result = confirm(message);
303
- callback && callback(result);
304
- };
305
-
306
- /* UI *******************************************************/
307
-
308
- abp.ui = abp.ui || {};
309
-
310
- /* UI BLOCK */
311
- //Defines UI Block API and implements basically
312
-
313
- var $abpBlockArea = document.createElement('div');
314
- $abpBlockArea.classList.add('abp-block-area');
315
-
316
- /* opts: { //Can be an object with options or a string for query a selector
317
- * elm: a query selector (optional - default: document.body)
318
- * busy: boolean (optional - default: false)
319
- * promise: A promise with always or finally handler (optional - auto unblocks the ui if provided)
320
- * }
321
- */
322
- abp.ui.block = function (opts) {
323
- if (!opts) {
324
- opts = {};
325
- } else if (typeof opts == 'string') {
326
- opts = {
327
- elm: opts
328
- };
329
- }
330
-
331
- var $elm = document.querySelector(opts.elm) || document.body;
332
-
333
- if (opts.busy) {
334
- $abpBlockArea.classList.add('abp-block-area-busy');
335
- } else {
336
- $abpBlockArea.classList.remove('abp-block-area-busy');
337
- }
338
-
339
- if (document.querySelector(opts.elm)) {
340
- $abpBlockArea.style.position = 'absolute';
341
- } else {
342
- $abpBlockArea.style.position = 'fixed';
343
- }
344
-
345
- $elm.appendChild($abpBlockArea);
346
-
347
- if (opts.promise) {
348
- if (opts.promise.always) { //jQuery.Deferred style
349
- opts.promise.always(function () {
350
- abp.ui.unblock({
351
- $elm: opts.elm
352
- });
353
- });
354
- } else if (opts.promise['finally']) { //Q style
355
- opts.promise['finally'](function () {
356
- abp.ui.unblock({
357
- $elm: opts.elm
358
- });
359
- });
360
- }
361
- }
362
- };
363
-
364
- /* opts: {
365
- *
366
- * }
367
- */
368
- abp.ui.unblock = function (opts) {
369
- var element = document.querySelector('.abp-block-area');
370
- if (element) {
371
- element.classList.add('abp-block-area-disappearing');
372
- setTimeout(function () {
373
- if (element) {
374
- element.classList.remove('abp-block-area-disappearing');
375
- element.parentElement.removeChild(element);
376
- }
377
- }, 250);
378
- }
379
- };
380
-
381
- /* UI BUSY */
382
- //Defines UI Busy API, not implements it
383
-
384
- abp.ui.setBusy = function (opts) {
385
- if (!opts) {
386
- opts = {
387
- busy: true
388
- };
389
- } else if (typeof opts == 'string') {
390
- opts = {
391
- elm: opts,
392
- busy: true
393
- };
394
- }
395
-
396
- abp.ui.block(opts);
397
- };
398
-
399
- abp.ui.clearBusy = function (opts) {
400
- abp.ui.unblock(opts);
401
- };
402
-
403
- /* SIMPLE EVENT BUS *****************************************/
404
-
405
- abp.event = (function () {
406
-
407
- var _callbacks = {};
408
-
409
- var on = function (eventName, callback) {
410
- if (!_callbacks[eventName]) {
411
- _callbacks[eventName] = [];
412
- }
413
-
414
- _callbacks[eventName].push(callback);
415
- };
416
-
417
- var off = function (eventName, callback) {
418
- var callbacks = _callbacks[eventName];
419
- if (!callbacks) {
420
- return;
421
- }
422
-
423
- var index = -1;
424
- for (var i = 0; i < callbacks.length; i++) {
425
- if (callbacks[i] === callback) {
426
- index = i;
427
- break;
428
- }
429
- }
430
-
431
- if (index < 0) {
432
- return;
433
- }
434
-
435
- _callbacks[eventName].splice(index, 1);
436
- };
437
-
438
- var trigger = function (eventName) {
439
- var callbacks = _callbacks[eventName];
440
- if (!callbacks || !callbacks.length) {
441
- return;
442
- }
443
-
444
- var args = Array.prototype.slice.call(arguments, 1);
445
- for (var i = 0; i < callbacks.length; i++) {
446
- callbacks[i].apply(this, args);
447
- }
448
- };
449
-
450
- // Public interface ///////////////////////////////////////////////////
451
-
452
- return {
453
- on: on,
454
- off: off,
455
- trigger: trigger
456
- };
457
- })();
458
-
459
-
460
- /* UTILS ***************************************************/
461
-
462
- abp.utils = abp.utils || {};
463
-
464
- /* Creates a name namespace.
465
- * Example:
466
- * var taskService = abp.utils.createNamespace(abp, 'services.task');
467
- * taskService will be equal to abp.services.task
468
- * first argument (root) must be defined first
469
- ************************************************************/
470
- abp.utils.createNamespace = function (root, ns) {
471
- var parts = ns.split('.');
472
- for (var i = 0; i < parts.length; i++) {
473
- if (typeof root[parts[i]] == 'undefined') {
474
- root[parts[i]] = {};
475
- }
476
-
477
- root = root[parts[i]];
478
- }
479
-
480
- return root;
481
- };
482
-
483
- /* Find and replaces a string (search) to another string (replacement) in
484
- * given string (str).
485
- * Example:
486
- * abp.utils.replaceAll('This is a test string', 'is', 'X') = 'ThX X a test string'
487
- ************************************************************/
488
- abp.utils.replaceAll = function (str, search, replacement) {
489
- var fix = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
490
- return str.replace(new RegExp(fix, 'g'), replacement);
491
- };
492
-
493
- /* Formats a string just like string.format in C#.
494
- * Example:
495
- * abp.utils.formatString('Hello {0}','Tuana') = 'Hello Tuana'
496
- ************************************************************/
497
- abp.utils.formatString = function () {
498
- if (arguments.length < 1) {
499
- return null;
500
- }
501
-
502
- var str = arguments[0];
503
-
504
- for (var i = 1; i < arguments.length; i++) {
505
- var placeHolder = '{' + (i - 1) + '}';
506
- str = abp.utils.replaceAll(str, placeHolder, arguments[i]);
507
- }
508
-
509
- return str;
510
- };
511
-
512
- abp.utils.toPascalCase = function (str) {
513
- if (!str || !str.length) {
514
- return str;
515
- }
516
-
517
- if (str.length === 1) {
518
- return str.charAt(0).toUpperCase();
519
- }
520
-
521
- return str.charAt(0).toUpperCase() + str.substr(1);
522
- }
523
-
524
- abp.utils.toCamelCase = function (str) {
525
- if (!str || !str.length) {
526
- return str;
527
- }
528
-
529
- if (str.length === 1) {
530
- return str.charAt(0).toLowerCase();
531
- }
532
-
533
- return str.charAt(0).toLowerCase() + str.substr(1);
534
- }
535
-
536
- abp.utils.truncateString = function (str, maxLength) {
537
- if (!str || !str.length || str.length <= maxLength) {
538
- return str;
539
- }
540
-
541
- return str.substr(0, maxLength);
542
- };
543
-
544
- abp.utils.truncateStringWithPostfix = function (str, maxLength, postfix) {
545
- postfix = postfix || '...';
546
-
547
- if (!str || !str.length || str.length <= maxLength) {
548
- return str;
549
- }
550
-
551
- if (maxLength <= postfix.length) {
552
- return postfix.substr(0, maxLength);
553
- }
554
-
555
- return str.substr(0, maxLength - postfix.length) + postfix;
556
- };
557
-
558
- abp.utils.isFunction = function (obj) {
559
- return !!(obj && obj.constructor && obj.call && obj.apply);
560
- };
561
-
562
- /**
563
- * parameterInfos should be an array of { name, value } objects
564
- * where name is query string parameter name and value is it's value.
565
- * includeQuestionMark is true by default.
566
- */
567
- abp.utils.buildQueryString = function (parameterInfos, includeQuestionMark) {
568
- if (includeQuestionMark === undefined) {
569
- includeQuestionMark = true;
570
- }
571
-
572
- var qs = '';
573
-
574
- function addSeperator() {
575
- if (!qs.length) {
576
- if (includeQuestionMark) {
577
- qs = qs + '?';
578
- }
579
- } else {
580
- qs = qs + '&';
581
- }
582
- }
583
-
584
- for (var i = 0; i < parameterInfos.length; ++i) {
585
- var parameterInfo = parameterInfos[i];
586
- if (parameterInfo.value === undefined) {
587
- continue;
588
- }
589
-
590
- if (parameterInfo.value === null) {
591
- parameterInfo.value = '';
592
- }
593
-
594
- addSeperator();
595
-
596
- if (parameterInfo.value.toJSON && typeof parameterInfo.value.toJSON === "function") {
597
- qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value.toJSON());
598
- } else if (Array.isArray(parameterInfo.value) && parameterInfo.value.length) {
599
- for (var j = 0; j < parameterInfo.value.length; j++) {
600
- if (j > 0) {
601
- addSeperator();
602
- }
603
-
604
- qs = qs + parameterInfo.name + '[' + j + ']=' + encodeURIComponent(parameterInfo.value[j]);
605
- }
606
- } else {
607
- qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value);
608
- }
609
- }
610
-
611
- return qs;
612
- }
613
-
614
- /**
615
- * Sets a cookie value for given key.
616
- * This is a simple implementation created to be used by ABP.
617
- * Please use a complete cookie library if you need.
618
- * @param {string} key
619
- * @param {string} value
620
- * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session.
621
- * @param {string} path (optional)
622
- */
623
- abp.utils.setCookieValue = function (key, value, expireDate, path) {
624
- var cookieValue = encodeURIComponent(key) + '=';
625
-
626
- if (value) {
627
- cookieValue = cookieValue + encodeURIComponent(value);
628
- }
629
-
630
- if (expireDate) {
631
- cookieValue = cookieValue + "; expires=" + expireDate.toUTCString();
632
- }
633
-
634
- if (path) {
635
- cookieValue = cookieValue + "; path=" + path;
636
- }
637
-
638
- document.cookie = cookieValue;
639
- };
640
-
641
- /**
642
- * Gets a cookie with given key.
643
- * This is a simple implementation created to be used by ABP.
644
- * Please use a complete cookie library if you need.
645
- * @param {string} key
646
- * @returns {string} Cookie value or null
647
- */
648
- abp.utils.getCookieValue = function (key) {
649
- var equalities = document.cookie.split('; ');
650
- for (var i = 0; i < equalities.length; i++) {
651
- if (!equalities[i]) {
652
- continue;
653
- }
654
-
655
- var splitted = equalities[i].split('=');
656
- if (splitted.length != 2) {
657
- continue;
658
- }
659
-
660
- if (decodeURIComponent(splitted[0]) === key) {
661
- return decodeURIComponent(splitted[1] || '');
662
- }
663
- }
664
-
665
- return null;
666
- };
667
-
668
- /**
669
- * Deletes cookie for given key.
670
- * This is a simple implementation created to be used by ABP.
671
- * Please use a complete cookie library if you need.
672
- * @param {string} key
673
- * @param {string} path (optional)
674
- */
675
- abp.utils.deleteCookie = function (key, path) {
676
- var cookieValue = encodeURIComponent(key) + '=';
677
-
678
- cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString();
679
-
680
- if (path) {
681
- cookieValue = cookieValue + "; path=" + path;
682
- }
683
-
684
- document.cookie = cookieValue;
685
- }
686
-
687
- /**
688
- * Escape HTML to help prevent XSS attacks.
689
- */
690
- abp.utils.htmlEscape = function (html) {
691
- return typeof html === 'string' ? html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;') : html;
692
- }
693
-
694
- /* SECURITY ***************************************/
695
- abp.security = abp.security || {};
696
- abp.security.antiForgery = abp.security.antiForgery || {};
697
-
698
- abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN';
699
- abp.security.antiForgery.tokenHeaderName = 'RequestVerificationToken';
700
-
701
- abp.security.antiForgery.getToken = function () {
702
- return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName);
703
- };
704
-
705
- /* CLOCK *****************************************/
706
- abp.clock = abp.clock || {};
707
-
708
- abp.clock.kind = 'Unspecified';
709
-
710
- abp.clock.supportsMultipleTimezone = function () {
711
- return abp.clock.kind === 'Utc';
712
- };
713
-
714
- var toLocal = function (date) {
715
- return new Date(
716
- date.getFullYear(),
717
- date.getMonth(),
718
- date.getDate(),
719
- date.getHours(),
720
- date.getMinutes(),
721
- date.getSeconds(),
722
- date.getMilliseconds()
723
- );
724
- };
725
-
726
- var toUtc = function (date) {
727
- return Date.UTC(
728
- date.getUTCFullYear(),
729
- date.getUTCMonth(),
730
- date.getUTCDate(),
731
- date.getUTCHours(),
732
- date.getUTCMinutes(),
733
- date.getUTCSeconds(),
734
- date.getUTCMilliseconds()
735
- );
736
- };
737
-
738
- abp.clock.now = function () {
739
- if (abp.clock.kind === 'Utc') {
740
- return toUtc(new Date());
741
- }
742
- return new Date();
743
- };
744
-
745
- abp.clock.normalize = function (date) {
746
- var kind = abp.clock.kind;
747
-
748
- if (kind === 'Unspecified') {
749
- return date;
750
- }
751
-
752
- if (kind === 'Local') {
753
- return toLocal(date);
754
- }
755
-
756
- if (kind === 'Utc') {
757
- return toUtc(date);
758
- }
759
- };
760
-
761
- /* FEATURES *************************************************/
762
-
763
- abp.features = abp.features || {};
764
-
765
- abp.features.values = abp.features.values || {};
766
-
767
- abp.features.isEnabled = function(name){
768
- var value = abp.features.get(name);
769
- return value == 'true' || value == 'True';
770
- }
771
-
772
- abp.features.get = function (name) {
773
- return abp.features.values[name];
774
- };
775
-
776
- })();
1
+ var abp = abp || {};
2
+ (function () {
3
+
4
+ /* Application paths *****************************************/
5
+
6
+ //Current application root path (including virtual directory if exists).
7
+ abp.appPath = abp.appPath || '/';
8
+
9
+ abp.pageLoadTime = new Date();
10
+
11
+ //Converts given path to absolute path using abp.appPath variable.
12
+ abp.toAbsAppPath = function (path) {
13
+ if (path.indexOf('/') == 0) {
14
+ path = path.substring(1);
15
+ }
16
+
17
+ return abp.appPath + path;
18
+ };
19
+
20
+ /* LOGGING ***************************************************/
21
+ //Implements Logging API that provides secure & controlled usage of console.log
22
+
23
+ abp.log = abp.log || {};
24
+
25
+ abp.log.levels = {
26
+ DEBUG: 1,
27
+ INFO: 2,
28
+ WARN: 3,
29
+ ERROR: 4,
30
+ FATAL: 5
31
+ };
32
+
33
+ abp.log.level = abp.log.levels.DEBUG;
34
+
35
+ abp.log.log = function (logObject, logLevel) {
36
+ if (!window.console || !window.console.log) {
37
+ return;
38
+ }
39
+
40
+ if (logLevel != undefined && logLevel < abp.log.level) {
41
+ return;
42
+ }
43
+
44
+ console.log(logObject);
45
+ };
46
+
47
+ abp.log.debug = function (logObject) {
48
+ abp.log.log("DEBUG: ", abp.log.levels.DEBUG);
49
+ abp.log.log(logObject, abp.log.levels.DEBUG);
50
+ };
51
+
52
+ abp.log.info = function (logObject) {
53
+ abp.log.log("INFO: ", abp.log.levels.INFO);
54
+ abp.log.log(logObject, abp.log.levels.INFO);
55
+ };
56
+
57
+ abp.log.warn = function (logObject) {
58
+ abp.log.log("WARN: ", abp.log.levels.WARN);
59
+ abp.log.log(logObject, abp.log.levels.WARN);
60
+ };
61
+
62
+ abp.log.error = function (logObject) {
63
+ abp.log.log("ERROR: ", abp.log.levels.ERROR);
64
+ abp.log.log(logObject, abp.log.levels.ERROR);
65
+ };
66
+
67
+ abp.log.fatal = function (logObject) {
68
+ abp.log.log("FATAL: ", abp.log.levels.FATAL);
69
+ abp.log.log(logObject, abp.log.levels.FATAL);
70
+ };
71
+
72
+ /* LOCALIZATION ***********************************************/
73
+
74
+ abp.localization = abp.localization || {};
75
+
76
+ abp.localization.values = {};
77
+
78
+ abp.localization.localize = function (key, sourceName) {
79
+ if (sourceName === '_') { //A convention to suppress the localization
80
+ return key;
81
+ }
82
+
83
+ sourceName = sourceName || abp.localization.defaultResourceName;
84
+ if (!sourceName) {
85
+ abp.log.warn('Localization source name is not specified and the defaultResourceName was not defined!');
86
+ return key;
87
+ }
88
+
89
+ var source = abp.localization.values[sourceName];
90
+ if (!source) {
91
+ abp.log.warn('Could not find localization source: ' + sourceName);
92
+ return key;
93
+ }
94
+
95
+ var value = source[key];
96
+ if (value == undefined) {
97
+ return key;
98
+ }
99
+
100
+ var copiedArguments = Array.prototype.slice.call(arguments, 0);
101
+ copiedArguments.splice(1, 1);
102
+ copiedArguments[0] = value;
103
+
104
+ return abp.utils.formatString.apply(this, copiedArguments);
105
+ };
106
+
107
+ abp.localization.isLocalized = function (key, sourceName) {
108
+ if (sourceName === '_') { //A convention to suppress the localization
109
+ return true;
110
+ }
111
+
112
+ sourceName = sourceName || abp.localization.defaultResourceName;
113
+ if (!sourceName) {
114
+ return false;
115
+ }
116
+
117
+ var source = abp.localization.values[sourceName];
118
+ if (!source) {
119
+ return false;
120
+ }
121
+
122
+ var value = source[key];
123
+ if (value === undefined) {
124
+ return false;
125
+ }
126
+
127
+ return true;
128
+ };
129
+
130
+ abp.localization.getResource = function (name) {
131
+ return function () {
132
+ var copiedArguments = Array.prototype.slice.call(arguments, 0);
133
+ copiedArguments.splice(1, 0, name);
134
+ return abp.localization.localize.apply(this, copiedArguments);
135
+ };
136
+ };
137
+
138
+ abp.localization.defaultResourceName = undefined;
139
+ abp.localization.currentCulture = {
140
+ cultureName: undefined
141
+ };
142
+
143
+ var getMapValue = function (packageMaps, packageName, language) {
144
+ language = language || abp.localization.currentCulture.name;
145
+ if (!packageMaps || !packageName || !language) {
146
+ return language;
147
+ }
148
+
149
+ var packageMap = packageMaps[packageName];
150
+ if (!packageMap) {
151
+ return language;
152
+ }
153
+
154
+ for (var i = 0; i < packageMap.length; i++) {
155
+ var map = packageMap[i];
156
+ if (map.name === language){
157
+ return map.value;
158
+ }
159
+ }
160
+
161
+ return language;
162
+ };
163
+
164
+ abp.localization.getLanguagesMap = function (packageName, language) {
165
+ return getMapValue(abp.localization.languagesMap, packageName, language);
166
+ };
167
+
168
+ abp.localization.getLanguageFilesMap = function (packageName, language) {
169
+ return getMapValue(abp.localization.languageFilesMap, packageName, language);
170
+ };
171
+
172
+ /* AUTHORIZATION **********************************************/
173
+
174
+ abp.auth = abp.auth || {};
175
+
176
+ abp.auth.policies = abp.auth.policies || {};
177
+
178
+ abp.auth.grantedPolicies = abp.auth.grantedPolicies || {};
179
+
180
+ abp.auth.isGranted = function (policyName) {
181
+ return abp.auth.policies[policyName] != undefined && abp.auth.grantedPolicies[policyName] != undefined;
182
+ };
183
+
184
+ abp.auth.isAnyGranted = function () {
185
+ if (!arguments || arguments.length <= 0) {
186
+ return true;
187
+ }
188
+
189
+ for (var i = 0; i < arguments.length; i++) {
190
+ if (abp.auth.isGranted(arguments[i])) {
191
+ return true;
192
+ }
193
+ }
194
+
195
+ return false;
196
+ };
197
+
198
+ abp.auth.areAllGranted = function () {
199
+ if (!arguments || arguments.length <= 0) {
200
+ return true;
201
+ }
202
+
203
+ for (var i = 0; i < arguments.length; i++) {
204
+ if (!abp.auth.isGranted(arguments[i])) {
205
+ return false;
206
+ }
207
+ }
208
+
209
+ return true;
210
+ };
211
+
212
+ abp.auth.tokenCookieName = 'Abp.AuthToken';
213
+
214
+ abp.auth.setToken = function (authToken, expireDate) {
215
+ abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath, abp.domain);
216
+ };
217
+
218
+ abp.auth.getToken = function () {
219
+ return abp.utils.getCookieValue(abp.auth.tokenCookieName);
220
+ }
221
+
222
+ abp.auth.clearToken = function () {
223
+ abp.auth.setToken();
224
+ }
225
+
226
+ /* SETTINGS *************************************************/
227
+
228
+ abp.setting = abp.setting || {};
229
+
230
+ abp.setting.values = abp.setting.values || {};
231
+
232
+ abp.setting.get = function (name) {
233
+ return abp.setting.values[name];
234
+ };
235
+
236
+ abp.setting.getBoolean = function (name) {
237
+ var value = abp.setting.get(name);
238
+ return value == 'true' || value == 'True';
239
+ };
240
+
241
+ abp.setting.getInt = function (name) {
242
+ return parseInt(abp.setting.values[name]);
243
+ };
244
+
245
+ /* NOTIFICATION *********************************************/
246
+ //Defines Notification API, not implements it
247
+
248
+ abp.notify = abp.notify || {};
249
+
250
+ abp.notify.success = function (message, title, options) {
251
+ abp.log.warn('abp.notify.success is not implemented!');
252
+ };
253
+
254
+ abp.notify.info = function (message, title, options) {
255
+ abp.log.warn('abp.notify.info is not implemented!');
256
+ };
257
+
258
+ abp.notify.warn = function (message, title, options) {
259
+ abp.log.warn('abp.notify.warn is not implemented!');
260
+ };
261
+
262
+ abp.notify.error = function (message, title, options) {
263
+ abp.log.warn('abp.notify.error is not implemented!');
264
+ };
265
+
266
+ /* MESSAGE **************************************************/
267
+ //Defines Message API, not implements it
268
+
269
+ abp.message = abp.message || {};
270
+
271
+ abp.message._showMessage = function (message, title) {
272
+ alert((title || '') + ' ' + message);
273
+ };
274
+
275
+ abp.message.info = function (message, title) {
276
+ abp.log.warn('abp.message.info is not implemented!');
277
+ return abp.message._showMessage(message, title);
278
+ };
279
+
280
+ abp.message.success = function (message, title) {
281
+ abp.log.warn('abp.message.success is not implemented!');
282
+ return abp.message._showMessage(message, title);
283
+ };
284
+
285
+ abp.message.warn = function (message, title) {
286
+ abp.log.warn('abp.message.warn is not implemented!');
287
+ return abp.message._showMessage(message, title);
288
+ };
289
+
290
+ abp.message.error = function (message, title) {
291
+ abp.log.warn('abp.message.error is not implemented!');
292
+ return abp.message._showMessage(message, title);
293
+ };
294
+
295
+ abp.message.confirm = function (message, titleOrCallback, callback) {
296
+ abp.log.warn('abp.message.confirm is not properly implemented!');
297
+
298
+ if (titleOrCallback && !(typeof titleOrCallback == 'string')) {
299
+ callback = titleOrCallback;
300
+ }
301
+
302
+ var result = confirm(message);
303
+ callback && callback(result);
304
+ };
305
+
306
+ /* UI *******************************************************/
307
+
308
+ abp.ui = abp.ui || {};
309
+
310
+ /* UI BLOCK */
311
+ //Defines UI Block API and implements basically
312
+
313
+ var $abpBlockArea = document.createElement('div');
314
+ $abpBlockArea.classList.add('abp-block-area');
315
+
316
+ /* opts: { //Can be an object with options or a string for query a selector
317
+ * elm: a query selector (optional - default: document.body)
318
+ * busy: boolean (optional - default: false)
319
+ * promise: A promise with always or finally handler (optional - auto unblocks the ui if provided)
320
+ * }
321
+ */
322
+ abp.ui.block = function (opts) {
323
+ if (!opts) {
324
+ opts = {};
325
+ } else if (typeof opts == 'string') {
326
+ opts = {
327
+ elm: opts
328
+ };
329
+ }
330
+
331
+ var $elm = document.querySelector(opts.elm) || document.body;
332
+
333
+ if (opts.busy) {
334
+ $abpBlockArea.classList.add('abp-block-area-busy');
335
+ } else {
336
+ $abpBlockArea.classList.remove('abp-block-area-busy');
337
+ }
338
+
339
+ if (document.querySelector(opts.elm)) {
340
+ $abpBlockArea.style.position = 'absolute';
341
+ } else {
342
+ $abpBlockArea.style.position = 'fixed';
343
+ }
344
+
345
+ $elm.appendChild($abpBlockArea);
346
+
347
+ if (opts.promise) {
348
+ if (opts.promise.always) { //jQuery.Deferred style
349
+ opts.promise.always(function () {
350
+ abp.ui.unblock({
351
+ $elm: opts.elm
352
+ });
353
+ });
354
+ } else if (opts.promise['finally']) { //Q style
355
+ opts.promise['finally'](function () {
356
+ abp.ui.unblock({
357
+ $elm: opts.elm
358
+ });
359
+ });
360
+ }
361
+ }
362
+ };
363
+
364
+ /* opts: {
365
+ *
366
+ * }
367
+ */
368
+ abp.ui.unblock = function (opts) {
369
+ var element = document.querySelector('.abp-block-area');
370
+ if (element) {
371
+ element.classList.add('abp-block-area-disappearing');
372
+ setTimeout(function () {
373
+ if (element) {
374
+ element.classList.remove('abp-block-area-disappearing');
375
+ element.parentElement.removeChild(element);
376
+ }
377
+ }, 250);
378
+ }
379
+ };
380
+
381
+ /* UI BUSY */
382
+ //Defines UI Busy API, not implements it
383
+
384
+ abp.ui.setBusy = function (opts) {
385
+ if (!opts) {
386
+ opts = {
387
+ busy: true
388
+ };
389
+ } else if (typeof opts == 'string') {
390
+ opts = {
391
+ elm: opts,
392
+ busy: true
393
+ };
394
+ }
395
+
396
+ abp.ui.block(opts);
397
+ };
398
+
399
+ abp.ui.clearBusy = function (opts) {
400
+ abp.ui.unblock(opts);
401
+ };
402
+
403
+ /* SIMPLE EVENT BUS *****************************************/
404
+
405
+ abp.event = (function () {
406
+
407
+ var _callbacks = {};
408
+
409
+ var on = function (eventName, callback) {
410
+ if (!_callbacks[eventName]) {
411
+ _callbacks[eventName] = [];
412
+ }
413
+
414
+ _callbacks[eventName].push(callback);
415
+ };
416
+
417
+ var off = function (eventName, callback) {
418
+ var callbacks = _callbacks[eventName];
419
+ if (!callbacks) {
420
+ return;
421
+ }
422
+
423
+ var index = -1;
424
+ for (var i = 0; i < callbacks.length; i++) {
425
+ if (callbacks[i] === callback) {
426
+ index = i;
427
+ break;
428
+ }
429
+ }
430
+
431
+ if (index < 0) {
432
+ return;
433
+ }
434
+
435
+ _callbacks[eventName].splice(index, 1);
436
+ };
437
+
438
+ var trigger = function (eventName) {
439
+ var callbacks = _callbacks[eventName];
440
+ if (!callbacks || !callbacks.length) {
441
+ return;
442
+ }
443
+
444
+ var args = Array.prototype.slice.call(arguments, 1);
445
+ for (var i = 0; i < callbacks.length; i++) {
446
+ callbacks[i].apply(this, args);
447
+ }
448
+ };
449
+
450
+ // Public interface ///////////////////////////////////////////////////
451
+
452
+ return {
453
+ on: on,
454
+ off: off,
455
+ trigger: trigger
456
+ };
457
+ })();
458
+
459
+
460
+ /* UTILS ***************************************************/
461
+
462
+ abp.utils = abp.utils || {};
463
+
464
+ /* Creates a name namespace.
465
+ * Example:
466
+ * var taskService = abp.utils.createNamespace(abp, 'services.task');
467
+ * taskService will be equal to abp.services.task
468
+ * first argument (root) must be defined first
469
+ ************************************************************/
470
+ abp.utils.createNamespace = function (root, ns) {
471
+ var parts = ns.split('.');
472
+ for (var i = 0; i < parts.length; i++) {
473
+ if (typeof root[parts[i]] == 'undefined') {
474
+ root[parts[i]] = {};
475
+ }
476
+
477
+ root = root[parts[i]];
478
+ }
479
+
480
+ return root;
481
+ };
482
+
483
+ /* Find and replaces a string (search) to another string (replacement) in
484
+ * given string (str).
485
+ * Example:
486
+ * abp.utils.replaceAll('This is a test string', 'is', 'X') = 'ThX X a test string'
487
+ ************************************************************/
488
+ abp.utils.replaceAll = function (str, search, replacement) {
489
+ var fix = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
490
+ return str.replace(new RegExp(fix, 'g'), replacement);
491
+ };
492
+
493
+ /* Formats a string just like string.format in C#.
494
+ * Example:
495
+ * abp.utils.formatString('Hello {0}','Tuana') = 'Hello Tuana'
496
+ ************************************************************/
497
+ abp.utils.formatString = function () {
498
+ if (arguments.length < 1) {
499
+ return null;
500
+ }
501
+
502
+ var str = arguments[0];
503
+
504
+ for (var i = 1; i < arguments.length; i++) {
505
+ var placeHolder = '{' + (i - 1) + '}';
506
+ str = abp.utils.replaceAll(str, placeHolder, arguments[i]);
507
+ }
508
+
509
+ return str;
510
+ };
511
+
512
+ abp.utils.toPascalCase = function (str) {
513
+ if (!str || !str.length) {
514
+ return str;
515
+ }
516
+
517
+ if (str.length === 1) {
518
+ return str.charAt(0).toUpperCase();
519
+ }
520
+
521
+ return str.charAt(0).toUpperCase() + str.substr(1);
522
+ }
523
+
524
+ abp.utils.toCamelCase = function (str) {
525
+ if (!str || !str.length) {
526
+ return str;
527
+ }
528
+
529
+ if (str.length === 1) {
530
+ return str.charAt(0).toLowerCase();
531
+ }
532
+
533
+ return str.charAt(0).toLowerCase() + str.substr(1);
534
+ }
535
+
536
+ abp.utils.truncateString = function (str, maxLength) {
537
+ if (!str || !str.length || str.length <= maxLength) {
538
+ return str;
539
+ }
540
+
541
+ return str.substr(0, maxLength);
542
+ };
543
+
544
+ abp.utils.truncateStringWithPostfix = function (str, maxLength, postfix) {
545
+ postfix = postfix || '...';
546
+
547
+ if (!str || !str.length || str.length <= maxLength) {
548
+ return str;
549
+ }
550
+
551
+ if (maxLength <= postfix.length) {
552
+ return postfix.substr(0, maxLength);
553
+ }
554
+
555
+ return str.substr(0, maxLength - postfix.length) + postfix;
556
+ };
557
+
558
+ abp.utils.isFunction = function (obj) {
559
+ return !!(obj && obj.constructor && obj.call && obj.apply);
560
+ };
561
+
562
+ /**
563
+ * parameterInfos should be an array of { name, value } objects
564
+ * where name is query string parameter name and value is it's value.
565
+ * includeQuestionMark is true by default.
566
+ */
567
+ abp.utils.buildQueryString = function (parameterInfos, includeQuestionMark) {
568
+ if (includeQuestionMark === undefined) {
569
+ includeQuestionMark = true;
570
+ }
571
+
572
+ var qs = '';
573
+
574
+ function addSeperator() {
575
+ if (!qs.length) {
576
+ if (includeQuestionMark) {
577
+ qs = qs + '?';
578
+ }
579
+ } else {
580
+ qs = qs + '&';
581
+ }
582
+ }
583
+
584
+ for (var i = 0; i < parameterInfos.length; ++i) {
585
+ var parameterInfo = parameterInfos[i];
586
+ if (parameterInfo.value === undefined) {
587
+ continue;
588
+ }
589
+
590
+ if (parameterInfo.value === null) {
591
+ parameterInfo.value = '';
592
+ }
593
+
594
+ addSeperator();
595
+
596
+ if (parameterInfo.value.toJSON && typeof parameterInfo.value.toJSON === "function") {
597
+ qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value.toJSON());
598
+ } else if (Array.isArray(parameterInfo.value) && parameterInfo.value.length) {
599
+ for (var j = 0; j < parameterInfo.value.length; j++) {
600
+ if (j > 0) {
601
+ addSeperator();
602
+ }
603
+
604
+ qs = qs + parameterInfo.name + '[' + j + ']=' + encodeURIComponent(parameterInfo.value[j]);
605
+ }
606
+ } else {
607
+ qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value);
608
+ }
609
+ }
610
+
611
+ return qs;
612
+ }
613
+
614
+ /**
615
+ * Sets a cookie value for given key.
616
+ * This is a simple implementation created to be used by ABP.
617
+ * Please use a complete cookie library if you need.
618
+ * @param {string} key
619
+ * @param {string} value
620
+ * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session.
621
+ * @param {string} path (optional)
622
+ */
623
+ abp.utils.setCookieValue = function (key, value, expireDate, path) {
624
+ var cookieValue = encodeURIComponent(key) + '=';
625
+
626
+ if (value) {
627
+ cookieValue = cookieValue + encodeURIComponent(value);
628
+ }
629
+
630
+ if (expireDate) {
631
+ cookieValue = cookieValue + "; expires=" + expireDate.toUTCString();
632
+ }
633
+
634
+ if (path) {
635
+ cookieValue = cookieValue + "; path=" + path;
636
+ }
637
+
638
+ document.cookie = cookieValue;
639
+ };
640
+
641
+ /**
642
+ * Gets a cookie with given key.
643
+ * This is a simple implementation created to be used by ABP.
644
+ * Please use a complete cookie library if you need.
645
+ * @param {string} key
646
+ * @returns {string} Cookie value or null
647
+ */
648
+ abp.utils.getCookieValue = function (key) {
649
+ var equalities = document.cookie.split('; ');
650
+ for (var i = 0; i < equalities.length; i++) {
651
+ if (!equalities[i]) {
652
+ continue;
653
+ }
654
+
655
+ var splitted = equalities[i].split('=');
656
+ if (splitted.length != 2) {
657
+ continue;
658
+ }
659
+
660
+ if (decodeURIComponent(splitted[0]) === key) {
661
+ return decodeURIComponent(splitted[1] || '');
662
+ }
663
+ }
664
+
665
+ return null;
666
+ };
667
+
668
+ /**
669
+ * Deletes cookie for given key.
670
+ * This is a simple implementation created to be used by ABP.
671
+ * Please use a complete cookie library if you need.
672
+ * @param {string} key
673
+ * @param {string} path (optional)
674
+ */
675
+ abp.utils.deleteCookie = function (key, path) {
676
+ var cookieValue = encodeURIComponent(key) + '=';
677
+
678
+ cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString();
679
+
680
+ if (path) {
681
+ cookieValue = cookieValue + "; path=" + path;
682
+ }
683
+
684
+ document.cookie = cookieValue;
685
+ }
686
+
687
+ /**
688
+ * Escape HTML to help prevent XSS attacks.
689
+ */
690
+ abp.utils.htmlEscape = function (html) {
691
+ return typeof html === 'string' ? html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;') : html;
692
+ }
693
+
694
+ /* SECURITY ***************************************/
695
+ abp.security = abp.security || {};
696
+ abp.security.antiForgery = abp.security.antiForgery || {};
697
+
698
+ abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN';
699
+ abp.security.antiForgery.tokenHeaderName = 'RequestVerificationToken';
700
+
701
+ abp.security.antiForgery.getToken = function () {
702
+ return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName);
703
+ };
704
+
705
+ /* CLOCK *****************************************/
706
+ abp.clock = abp.clock || {};
707
+
708
+ abp.clock.kind = 'Unspecified';
709
+
710
+ abp.clock.supportsMultipleTimezone = function () {
711
+ return abp.clock.kind === 'Utc';
712
+ };
713
+
714
+ var toLocal = function (date) {
715
+ return new Date(
716
+ date.getFullYear(),
717
+ date.getMonth(),
718
+ date.getDate(),
719
+ date.getHours(),
720
+ date.getMinutes(),
721
+ date.getSeconds(),
722
+ date.getMilliseconds()
723
+ );
724
+ };
725
+
726
+ var toUtc = function (date) {
727
+ return Date.UTC(
728
+ date.getUTCFullYear(),
729
+ date.getUTCMonth(),
730
+ date.getUTCDate(),
731
+ date.getUTCHours(),
732
+ date.getUTCMinutes(),
733
+ date.getUTCSeconds(),
734
+ date.getUTCMilliseconds()
735
+ );
736
+ };
737
+
738
+ abp.clock.now = function () {
739
+ if (abp.clock.kind === 'Utc') {
740
+ return toUtc(new Date());
741
+ }
742
+ return new Date();
743
+ };
744
+
745
+ abp.clock.normalize = function (date) {
746
+ var kind = abp.clock.kind;
747
+
748
+ if (kind === 'Unspecified') {
749
+ return date;
750
+ }
751
+
752
+ if (kind === 'Local') {
753
+ return toLocal(date);
754
+ }
755
+
756
+ if (kind === 'Utc') {
757
+ return toUtc(date);
758
+ }
759
+ };
760
+
761
+ /* FEATURES *************************************************/
762
+
763
+ abp.features = abp.features || {};
764
+
765
+ abp.features.values = abp.features.values || {};
766
+
767
+ abp.features.isEnabled = function(name){
768
+ var value = abp.features.get(name);
769
+ return value == 'true' || value == 'True';
770
+ }
771
+
772
+ abp.features.get = function (name) {
773
+ return abp.features.values[name];
774
+ };
775
+
776
+ })();