bv-ui-core 0.1.1
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/.eslintrc +40 -0
- package/.travis.yml +5 -0
- package/CONTRIBUTING.md +98 -0
- package/LICENSE.md +13 -0
- package/OWNERS +2 -0
- package/README.md +63 -0
- package/catalog-info.yaml +30 -0
- package/karma.conf.js +130 -0
- package/lib/README.md +61 -0
- package/lib/api/.babelrc +3 -0
- package/lib/api/.eslintrc +9 -0
- package/lib/api/README.md +134 -0
- package/lib/api/index.js +15 -0
- package/lib/api/statistics.js +148 -0
- package/lib/application/README.md +96 -0
- package/lib/application/index.js +171 -0
- package/lib/body/README.md +13 -0
- package/lib/body/index.js +19 -0
- package/lib/bvFetch/README.md +101 -0
- package/lib/bvFetch/index.js +383 -0
- package/lib/checkHighContrast/README.md +26 -0
- package/lib/checkHighContrast/index.js +36 -0
- package/lib/cookie/README.md +29 -0
- package/lib/cookie/index.js +111 -0
- package/lib/cookieConsent/README.md +41 -0
- package/lib/cookieConsent/index.js +227 -0
- package/lib/cssLoadChecker/README.md +19 -0
- package/lib/cssLoadChecker/index.js +72 -0
- package/lib/date.now/README.md +11 -0
- package/lib/date.now/index.js +40 -0
- package/lib/domainPolice/README.md +66 -0
- package/lib/domainPolice/index.js +95 -0
- package/lib/evented/README.md +42 -0
- package/lib/evented/index.js +135 -0
- package/lib/getOriginalConstructor/README.md +16 -0
- package/lib/getOriginalConstructor/index.js +52 -0
- package/lib/global/README.md +10 -0
- package/lib/global/index.js +48 -0
- package/lib/ie/README.md +13 -0
- package/lib/ie/index.js +25 -0
- package/lib/loader/README.md +73 -0
- package/lib/loader/index.js +358 -0
- package/lib/logger/README.md +82 -0
- package/lib/logger/index.js +180 -0
- package/lib/messageFormat/README.md +10 -0
- package/lib/messageFormat/index.js +42 -0
- package/lib/namespacer/README.md +57 -0
- package/lib/namespacer/index.js +98 -0
- package/lib/performance/README.md +35 -0
- package/lib/performance/entries.js +5 -0
- package/lib/performance/getEntries.js +42 -0
- package/lib/performance/getEntriesByName.js +61 -0
- package/lib/performance/getEntriesByType.js +55 -0
- package/lib/performance/index.js +11 -0
- package/lib/performance/mark.js +97 -0
- package/lib/performance/measure.js +106 -0
- package/lib/performance/now.js +63 -0
- package/lib/pixelsDisplayed/README.md +19 -0
- package/lib/pixelsDisplayed/index.js +113 -0
- package/lib/polyfills/README.md +39 -0
- package/lib/polyfills/fetch/Headers.js +3 -0
- package/lib/polyfills/fetch/Request.js +3 -0
- package/lib/polyfills/fetch/Response.js +3 -0
- package/lib/polyfills/fetch/fetch.js +3 -0
- package/lib/polyfills/fetch/index.js +57 -0
- package/lib/polyfills/promise.js +265 -0
- package/lib/queryShadowDom/README.md +50 -0
- package/lib/queryShadowDom/index.js +93 -0
- package/lib/region/README.md +64 -0
- package/lib/region/europe/index.js +94 -0
- package/lib/region/europe/territories.js +40 -0
- package/lib/region/index.js +123 -0
- package/lib/staticAssetLoader/README.md +116 -0
- package/lib/staticAssetLoader/index.js +248 -0
- package/lib/waitForBody/README.md +21 -0
- package/lib/waitForBody/index.js +36 -0
- package/mkdocs.yaml +5 -0
- package/package.json +55 -0
- package/test/.eslintrc +5 -0
- package/test/fixtures/lib.loader.document-is-wrong-in-ie8.js +5 -0
- package/test/fixtures/lib.loader.loadscript-attribute.js +3 -0
- package/test/fixtures/lib.loader.loadscript.js +3 -0
- package/test/fixtures/lib.loader.loadstylesheet.css +3 -0
- package/test/unit/api/.eslintrc +6 -0
- package/test/unit/api/index.spec.js +37 -0
- package/test/unit/api/response.json +67 -0
- package/test/unit/api/statistics.spec.js +218 -0
- package/test/unit/application/index.spec.js +313 -0
- package/test/unit/body/index.spec.js +15 -0
- package/test/unit/bvFetch/index.spec.js +246 -0
- package/test/unit/checkHighContrast/index.spec.js +16 -0
- package/test/unit/cookie/index.spec.js +191 -0
- package/test/unit/cookieConsent/index.spec.js +119 -0
- package/test/unit/cssLoadChecker/index.spec.js +75 -0
- package/test/unit/date.now/index.spec.js +21 -0
- package/test/unit/domainPolice/index.spec.js +141 -0
- package/test/unit/evented/index.spec.js +59 -0
- package/test/unit/getOriginalConstructor/index.spec.js +60 -0
- package/test/unit/global/index.spec.js +84 -0
- package/test/unit/ie/index.spec.js +14 -0
- package/test/unit/loader/index.spec.js +644 -0
- package/test/unit/logger/index.spec.js +141 -0
- package/test/unit/messageFormat/index.spec.js +31 -0
- package/test/unit/mochaInit.js +8 -0
- package/test/unit/namespacer/index.spec.js +100 -0
- package/test/unit/performance/getEntries.spec.js +30 -0
- package/test/unit/performance/getEntriesByName.spec.js +88 -0
- package/test/unit/performance/getEntriesByType.spec.js +48 -0
- package/test/unit/performance/index.spec.js +35 -0
- package/test/unit/performance/mark.spec.js +40 -0
- package/test/unit/performance/measure.spec.js +50 -0
- package/test/unit/performance/now.spec.js +21 -0
- package/test/unit/pixelsDisplayed/index.spec.js +271 -0
- package/test/unit/polyfills/fetch/Headers.js +12 -0
- package/test/unit/polyfills/fetch/Request.js +12 -0
- package/test/unit/polyfills/fetch/Response.js +12 -0
- package/test/unit/polyfills/fetch/fetch.js +12 -0
- package/test/unit/polyfills/fetch/index.js +61 -0
- package/test/unit/polyfills/promise.spec.js +48 -0
- package/test/unit/region/europe/index.spec.js +70 -0
- package/test/unit/region/index.spec.js +113 -0
- package/test/unit/staticAssetLoader/index.spec.js +215 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# namespacer
|
|
2
|
+
|
|
3
|
+
The namespacer module provides an interface to create or obtain a namespace
|
|
4
|
+
suitable for use by one or more applications running on the same web page.
|
|
5
|
+
|
|
6
|
+
```js
|
|
7
|
+
var global = require('bv-ui-core/lib/global');
|
|
8
|
+
var namespacer = require('bv-ui-core/lib/namespacer');
|
|
9
|
+
|
|
10
|
+
// If the namespace already exists it will be returned. If not it will be
|
|
11
|
+
// created and then returned.
|
|
12
|
+
var namespaceName = 'EXAMPLE';
|
|
13
|
+
var namespace = namespacer.namespace(namespaceName);
|
|
14
|
+
|
|
15
|
+
// The namespace also exists on the global object once created.
|
|
16
|
+
namespace = global[namespaceName];
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Namespace API
|
|
20
|
+
|
|
21
|
+
A namespace instance exposes the following methods:
|
|
22
|
+
|
|
23
|
+
### registerProperty (name, value)
|
|
24
|
+
|
|
25
|
+
A namespace instance allows for registration of properties to identify unwanted
|
|
26
|
+
collisions. Typically one or more applications will register themselves with the
|
|
27
|
+
namespace on a given web page, but there are also other situations in which
|
|
28
|
+
registration of properties is useful.
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
namespace.registerProperty('example', applicationInstance);
|
|
32
|
+
|
|
33
|
+
// The application is now assigned to namespace.example.
|
|
34
|
+
var exampleApplication = namespace.example;
|
|
35
|
+
|
|
36
|
+
// Trying to register another value to 'example' in this namespace will throw.
|
|
37
|
+
try {
|
|
38
|
+
namespace.registerProperty('example', anotherApplicationInstance);
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
console.error(e);
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Creating a `namespace` Module
|
|
46
|
+
|
|
47
|
+
It is good practice for an application to create a `namespace` module that
|
|
48
|
+
exports the namespace used by that application. For example:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
var namespacer = require('bv-ui-core/lib/namespacer');
|
|
52
|
+
|
|
53
|
+
module.exports = namespacer.namespace('EXAMPLE');
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
All other code can then require the `namespace` module when access to the
|
|
57
|
+
namespace is needed.
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* A namespace generator.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
var global = require('../global');
|
|
9
|
+
|
|
10
|
+
// --------------------------------------------------------------------------
|
|
11
|
+
// Namespace class definition.
|
|
12
|
+
// --------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @class
|
|
16
|
+
* A namespace.
|
|
17
|
+
*
|
|
18
|
+
* @param name The name of this Namespace instance. It should be the same as the
|
|
19
|
+
* name of the global property to which this namespace is assigned.
|
|
20
|
+
*/
|
|
21
|
+
function Namespace (name) {
|
|
22
|
+
this.name = name;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Register a property in the namespace, such as an application instance.
|
|
27
|
+
*
|
|
28
|
+
* Attempting to register the same property twice is almost always an error, and
|
|
29
|
+
* thus results in an exception.
|
|
30
|
+
*
|
|
31
|
+
* @param {String} name The property name.
|
|
32
|
+
* @param {Mixed} value The property value.
|
|
33
|
+
* @return {Namespace} The namespace instance. Useful for chaining.
|
|
34
|
+
*/
|
|
35
|
+
Namespace.prototype.registerProperty = function (name, value) {
|
|
36
|
+
if (this.hasOwnProperty(name)) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
'Cannot register ' + name +
|
|
39
|
+
' because a property with that name already exists on window.' +
|
|
40
|
+
this.name
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this[name] = value;
|
|
45
|
+
return this;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// --------------------------------------------------------------------------
|
|
49
|
+
// Exported interface.
|
|
50
|
+
// --------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
module.exports = {
|
|
53
|
+
/**
|
|
54
|
+
* Obtain a namespace object, creating one if necessary.
|
|
55
|
+
*
|
|
56
|
+
* The namespace will be assigned to the global object as a property
|
|
57
|
+
* with the provided name. i.e. namespacer.namespace('foo') creates or returns
|
|
58
|
+
* a namespace at global[foo].
|
|
59
|
+
*
|
|
60
|
+
* @param {String} name The namespace name.
|
|
61
|
+
* @return {Mixed} The namespace.
|
|
62
|
+
*/
|
|
63
|
+
namespace: function (name) {
|
|
64
|
+
// If there is no global property yet assigned for this namespace then
|
|
65
|
+
// create one.
|
|
66
|
+
if (global[name] === undefined) {
|
|
67
|
+
Object.defineProperty(global, name, {
|
|
68
|
+
value: new Namespace(name)
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// If an object already exists for this global property, check to see
|
|
72
|
+
// whether or not it is a Namespace instance or some other form of object.
|
|
73
|
+
else if (typeof global[name] === 'object') {
|
|
74
|
+
// If the existing object is not a Namespace instance, then provide it
|
|
75
|
+
// with the necessary decorations and capabilies of a Namespace instance.
|
|
76
|
+
//
|
|
77
|
+
// Other code may have a reference to the object, so we can't do anything
|
|
78
|
+
// more drastic such as replacing it.
|
|
79
|
+
if (!(global[name] instanceof Namespace)) {
|
|
80
|
+
Namespace.call(global[name], name);
|
|
81
|
+
for (var prop in Namespace.prototype) {
|
|
82
|
+
global[name][prop] = Namespace.prototype[prop];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// If something other than undefined or an object is assigned to this global
|
|
87
|
+
// property then assume that other code is using it for another purpose, so
|
|
88
|
+
// throw.
|
|
89
|
+
else {
|
|
90
|
+
throw new Error(
|
|
91
|
+
'Namespace ' + name + ' cannot be created.' +
|
|
92
|
+
' A non-object variable is already assigned to window.' + name
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return global[name];
|
|
97
|
+
}
|
|
98
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Performance
|
|
2
|
+
|
|
3
|
+
Provides a cross-browser safe way to use the Navigation Timing API.
|
|
4
|
+
|
|
5
|
+
## Performance/now
|
|
6
|
+
|
|
7
|
+
Ported from Bazaarvoice internal code and based on [a Paul Irish polyfill][1].
|
|
8
|
+
|
|
9
|
+
### Usage
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
var perfNow = require('performance/now.js');
|
|
13
|
+
|
|
14
|
+
var timing = perfNow.now();
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Notes
|
|
18
|
+
|
|
19
|
+
This is not a true polyfill in browsers that do not implement the Navigation
|
|
20
|
+
Timing API. Rather than returning the time elapsed since `navigationStart`, it
|
|
21
|
+
will return the time elapsed since the polyfill was installed.
|
|
22
|
+
|
|
23
|
+
## Performance/mark
|
|
24
|
+
|
|
25
|
+
Also ported from Bazaarvoice internal code.
|
|
26
|
+
|
|
27
|
+
### Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var perfMark = require('performance/mark.js');
|
|
31
|
+
|
|
32
|
+
perfMark.mark('mark-name');
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
[1]: https://gist.github.com/paulirish/5438650
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* Performance.getEntries uses the native User Timing API
|
|
4
|
+
* when available and falls back to a manual implementation if not
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Imports
|
|
8
|
+
var global = require('../global');
|
|
9
|
+
var timeline = require('./entries').timeline;
|
|
10
|
+
|
|
11
|
+
// Determine now (once) if we can use the native User Timing API
|
|
12
|
+
|
|
13
|
+
// Cache references to native implementation in case they are modified later
|
|
14
|
+
var performance = global.performance;
|
|
15
|
+
var nativeGetEntries = performance && performance.getEntries;
|
|
16
|
+
|
|
17
|
+
// Is the User Timing API natively supported?
|
|
18
|
+
var isNativeSupported = (typeof nativeGetEntries === 'function');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Native Implementation
|
|
22
|
+
* Uses the native-supported performance.getEntries function
|
|
23
|
+
*/
|
|
24
|
+
function nativeImplementation () {
|
|
25
|
+
return nativeGetEntries.call(performance);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Polyfill Implementation
|
|
30
|
+
* Manually provides similar functionality to native performance.getEntries
|
|
31
|
+
* using an in-memory array of timeline events
|
|
32
|
+
*/
|
|
33
|
+
function polyfillImplementation () {
|
|
34
|
+
return timeline;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = {
|
|
38
|
+
getEntries: function () {
|
|
39
|
+
var func = (isNativeSupported ? nativeImplementation : polyfillImplementation);
|
|
40
|
+
return func();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* Performance.getEntriesByName uses the native User Timing API
|
|
4
|
+
* when available and falls back to a manual implementation if not
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Imports
|
|
8
|
+
var global = require('../global');
|
|
9
|
+
var perfGetEntries = require('./getEntries').getEntries;
|
|
10
|
+
|
|
11
|
+
// Determine now (once) if we can use the native User Timing API
|
|
12
|
+
|
|
13
|
+
// Cache references to native implementation in case they are modified later
|
|
14
|
+
var performance = global.performance;
|
|
15
|
+
var nativeGetEntriesByName = performance && performance.getEntriesByName;
|
|
16
|
+
|
|
17
|
+
// Is the User Timing API natively supported?
|
|
18
|
+
var isNativeSupported = (typeof nativeGetEntriesByName === 'function');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Native Implementation
|
|
22
|
+
* Uses the native-supported performance.getEntriesByName function
|
|
23
|
+
*/
|
|
24
|
+
function nativeImplementation (name, entryType) {
|
|
25
|
+
return nativeGetEntriesByName.call(performance, name, entryType);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Polyfill Implementation
|
|
30
|
+
* Manually provides similar functionality to native
|
|
31
|
+
* performance.getEntriesByName using a filter operation on
|
|
32
|
+
* a list of all performance marks
|
|
33
|
+
*/
|
|
34
|
+
function polyfillImplementation (name, entryType) {
|
|
35
|
+
if (arguments.length === 0) {
|
|
36
|
+
throw new TypeError('Failed to execute \'getEntriesByName\' on \'Performance\': 1 argument required, but only 0 present.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return perfGetEntries().filter(function (entry) {
|
|
40
|
+
if (entry.name === name) {
|
|
41
|
+
if (typeof entryType === 'undefined' || entry.entryType === entryType) {
|
|
42
|
+
return entry;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = {
|
|
49
|
+
/**
|
|
50
|
+
* @param {String} name - The name of the mark to filter for
|
|
51
|
+
* @param {String} [entryType] - The entryType of the mark to filter for
|
|
52
|
+
*/
|
|
53
|
+
getEntriesByName: function (name, entryType) {
|
|
54
|
+
// Note: because the implementation functions explicitly check their number of arguments,
|
|
55
|
+
// we want to preserve the calls exactly as they came in
|
|
56
|
+
|
|
57
|
+
var hasArguments = (arguments.length > 0);
|
|
58
|
+
var func = (isNativeSupported ? nativeImplementation : polyfillImplementation);
|
|
59
|
+
return (hasArguments ? func(name, entryType) : func());
|
|
60
|
+
}
|
|
61
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* Performance.getEntriesByType uses the native User Timing API
|
|
4
|
+
* when available and falls back to a manual implementation if not
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Imports
|
|
8
|
+
var global = require('../global');
|
|
9
|
+
var perfGetEntries = require('./getEntries').getEntries;
|
|
10
|
+
|
|
11
|
+
// Determine now (once) if we can use the native User Timing API
|
|
12
|
+
|
|
13
|
+
// Cache references to native implementation in case they are modified later
|
|
14
|
+
var performance = global.performance;
|
|
15
|
+
var nativeGetEntriesByType = performance && performance.getEntriesByType;
|
|
16
|
+
|
|
17
|
+
// Is the User Timing API natively supported?
|
|
18
|
+
var isNativeSupported = (typeof nativeGetEntriesByType === 'function');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Native Implementation
|
|
22
|
+
* Uses the native-supported performance.getEntriesByType function
|
|
23
|
+
*/
|
|
24
|
+
function nativeImplementation (entryType) {
|
|
25
|
+
return nativeGetEntriesByType.call(performance, entryType);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Polyfill Implementation
|
|
30
|
+
* Manually provides similar functionality to native
|
|
31
|
+
* performance.getEntriesByType using a filter operation on
|
|
32
|
+
* a list of all performance marks
|
|
33
|
+
*/
|
|
34
|
+
function polyfillImplementation (entryType) {
|
|
35
|
+
if (arguments.length === 0) {
|
|
36
|
+
throw new TypeError('Failed to execute \'getEntriesByType\' on \'Performance\': 1 argument required, but only 0 present.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return perfGetEntries().filter(function (entry) {
|
|
40
|
+
return entry.entryType === entryType;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = {
|
|
45
|
+
/**
|
|
46
|
+
* @param {String} [entryType] - The entryType of the mark to filter for
|
|
47
|
+
*/
|
|
48
|
+
getEntriesByType: function (entryType) {
|
|
49
|
+
// Note: because the implementation functions explicitly check their number of arguments,
|
|
50
|
+
// we want to preserve the calls exactly as they came in
|
|
51
|
+
var hasArguments = (arguments.length > 0);
|
|
52
|
+
var func = (isNativeSupported ? nativeImplementation : polyfillImplementation);
|
|
53
|
+
return (hasArguments ? func(entryType) : func());
|
|
54
|
+
}
|
|
55
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* Groups Performance sub-modules to keep them clean and testable
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
module.exports.getEntries = require('./getEntries.js').getEntries;
|
|
7
|
+
module.exports.getEntriesByName = require('./getEntriesByName.js').getEntriesByName;
|
|
8
|
+
module.exports.getEntriesByType = require('./getEntriesByType.js').getEntriesByType;
|
|
9
|
+
module.exports.now = require('./now.js').now;
|
|
10
|
+
module.exports.mark = require('./mark.js').mark;
|
|
11
|
+
module.exports.measure = require('./measure.js').measure;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* Performance.mark uses the native User Timing API
|
|
4
|
+
* when available and falls back to a manual implementation if not
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Imports
|
|
8
|
+
var global = require('../global');
|
|
9
|
+
var perfNow = require('./now.js');
|
|
10
|
+
var timeline = require('./entries').timeline;
|
|
11
|
+
|
|
12
|
+
// Determine now (once) if we can use the native User Timing API
|
|
13
|
+
|
|
14
|
+
// Cache references to native implementation in case they are modified later
|
|
15
|
+
var performance = global.performance;
|
|
16
|
+
var nativeMark = performance && performance.mark;
|
|
17
|
+
|
|
18
|
+
// Is the User Timing API natively supported?
|
|
19
|
+
var isNativeSupported = (typeof nativeMark === 'function');
|
|
20
|
+
|
|
21
|
+
// Per http://www.w3.org/TR/user-timing/#extensions-performance-interface,
|
|
22
|
+
// the mark name cannot be any of the attributes in the PerformanceTiming
|
|
23
|
+
// interface: http://www.w3.org/TR/navigation-timing/#performancetiming.
|
|
24
|
+
var reservedNames = {
|
|
25
|
+
navigationStart: 1,
|
|
26
|
+
unloadEventStart: 1,
|
|
27
|
+
unloadEventEnd: 1,
|
|
28
|
+
redirectStart: 1,
|
|
29
|
+
redirectEnd: 1,
|
|
30
|
+
fetchStart: 1,
|
|
31
|
+
domainLookupStart: 1,
|
|
32
|
+
domainLookupEnd: 1,
|
|
33
|
+
connectStart: 1,
|
|
34
|
+
connectEnd: 1,
|
|
35
|
+
secureConnectionStart: 1,
|
|
36
|
+
requestStart: 1,
|
|
37
|
+
responseStart: 1,
|
|
38
|
+
responseEnd: 1,
|
|
39
|
+
domLoading: 1,
|
|
40
|
+
domInteractive: 1,
|
|
41
|
+
domContentLoadedEventStart: 1,
|
|
42
|
+
domContentLoadedEventEnd: 1,
|
|
43
|
+
domComplete: 1,
|
|
44
|
+
loadEventStart: 1,
|
|
45
|
+
loadEventEnd: 1
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Native Implementation
|
|
50
|
+
* Uses the native-supported performance.mark function
|
|
51
|
+
*/
|
|
52
|
+
function nativeImplementation (name) {
|
|
53
|
+
return nativeMark.call(performance, name);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Polyfill Implementation
|
|
58
|
+
* Manually provides similar functionality to native performance.mark
|
|
59
|
+
* using a marks list and an array of timeline events
|
|
60
|
+
*/
|
|
61
|
+
function polyfillImplementation (name) {
|
|
62
|
+
// Validation: a mark name must be provided
|
|
63
|
+
if (arguments.length < 1) {
|
|
64
|
+
throw new SyntaxError('Cannot set mark without a name');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Validation: the mark name cannot be one of the reserved names
|
|
68
|
+
if (name in reservedNames) {
|
|
69
|
+
throw new SyntaxError('Cannot set mark with reserved name "' + name + '"');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Validation passed, can proceed
|
|
73
|
+
|
|
74
|
+
// Create the record
|
|
75
|
+
var record = {
|
|
76
|
+
entryType: 'mark',
|
|
77
|
+
name: name,
|
|
78
|
+
startTime: perfNow.now(),
|
|
79
|
+
duration: 0
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// Push the record to the timeline
|
|
83
|
+
timeline.push(record);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
/**
|
|
88
|
+
* @param {String} name - The name of the mark to create
|
|
89
|
+
*/
|
|
90
|
+
mark: function (name) {
|
|
91
|
+
// Note: because the implementation functions explicitly check their number of arguments,
|
|
92
|
+
// we want to preserve the calls exactly as they came in
|
|
93
|
+
var hasArguments = (arguments.length > 0);
|
|
94
|
+
var func = (isNativeSupported) ? nativeImplementation : polyfillImplementation;
|
|
95
|
+
return (hasArguments) ? func(name) : func();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* Performance.measure uses the native User Timing API
|
|
4
|
+
* when available and falls back to a manual implementation if not
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Imports
|
|
8
|
+
var global = require('../global');
|
|
9
|
+
var perfNow = require('./now.js').now;
|
|
10
|
+
var perfGetEntriesByType = require('./getEntriesByType').getEntriesByType;
|
|
11
|
+
var timeline = require('./entries').timeline;
|
|
12
|
+
|
|
13
|
+
// Determine now (once) if we can use the native User Timing API
|
|
14
|
+
|
|
15
|
+
// Cache references to native implementation in case they are modified later
|
|
16
|
+
var performance = global.performance;
|
|
17
|
+
var nativeMeasure = performance && performance.measure;
|
|
18
|
+
|
|
19
|
+
// Is the User Timing API natively supported?
|
|
20
|
+
var isNativeSupported = (typeof nativeMeasure === 'function');
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Native Implementation
|
|
24
|
+
* Uses the native-supported performance.measure function
|
|
25
|
+
*/
|
|
26
|
+
function nativeImplementation (name, startMark, endMark) {
|
|
27
|
+
if (startMark === undefined) {
|
|
28
|
+
return nativeMeasure.call(performance, name);
|
|
29
|
+
}
|
|
30
|
+
else if (endMark === undefined) {
|
|
31
|
+
return nativeMeasure.call(performance, name, startMark);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return nativeMeasure.call(performance, name, startMark, endMark);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Polyfill Implementation
|
|
39
|
+
* Manually provides similar functionality to native performance.measure
|
|
40
|
+
* using a measures list and an array of timeline events
|
|
41
|
+
*/
|
|
42
|
+
function polyfillImplementation (name, startMark, endMark) {
|
|
43
|
+
// Validation: a measure name must be provided
|
|
44
|
+
if (arguments.length < 1) {
|
|
45
|
+
throw new TypeError('Failed to execute \'measure\' on \'Performance\': 1 argument required, but only 0 present.');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var marks = perfGetEntriesByType('mark');
|
|
49
|
+
var filteredMarks;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* If no startMark was passed, use navigationStart (0) as the startTime
|
|
53
|
+
* per the spec. If no endTime was passed, use performance.now() as the
|
|
54
|
+
* endTime. Otherwise, use the most recent mark(s) with the specified
|
|
55
|
+
* name being passed as startMark and endMark.
|
|
56
|
+
*/
|
|
57
|
+
var startTime = 0;
|
|
58
|
+
var endTime = perfNow();
|
|
59
|
+
|
|
60
|
+
if (startMark !== undefined) {
|
|
61
|
+
filteredMarks = marks.filter(function (entry) {
|
|
62
|
+
return entry.entryName === startMark;
|
|
63
|
+
});
|
|
64
|
+
if (!filteredMarks.length) {
|
|
65
|
+
throw new Error('Failed to execute \'measure\' on \'Performance\': The mark \'' + startMark + '\' does not exist.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// use the most recent mark with the matching name
|
|
69
|
+
startTime = filteredMarks[filteredMarks.length-1].startTime;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (endMark !== undefined) {
|
|
73
|
+
filteredMarks = marks.filter(function (entry) {
|
|
74
|
+
return entry.entryName === endMark;
|
|
75
|
+
});
|
|
76
|
+
if (!filteredMarks.length) {
|
|
77
|
+
throw new Error('Failed to execute \'measure\' on \'Performance\': The mark \'' + endMark + '\' does not exist.');
|
|
78
|
+
}
|
|
79
|
+
// use the most recent mark with the matching name
|
|
80
|
+
endTime = filteredMarks[filteredMarks.length-1].startTime;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Create the record
|
|
84
|
+
var record = {
|
|
85
|
+
entryType: 'measure',
|
|
86
|
+
name: name,
|
|
87
|
+
startTime: startTime,
|
|
88
|
+
duration: endTime - startTime
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Push the record to the timeline
|
|
92
|
+
timeline.push(record);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = {
|
|
96
|
+
/**
|
|
97
|
+
* @param {String} name - The name of the measure to create
|
|
98
|
+
*/
|
|
99
|
+
measure: function (name, startMark, endMark) {
|
|
100
|
+
// Note: because the implementation functions explicitly check their number of arguments,
|
|
101
|
+
// we want to preserve the calls exactly as they came in
|
|
102
|
+
var hasArguments = (arguments.length > 0);
|
|
103
|
+
var func = (isNativeSupported) ? nativeImplementation : polyfillImplementation;
|
|
104
|
+
return (hasArguments) ? func(name, startMark, endMark) : func();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview Provides a cross-browser safe way to use the Navigation Timing API
|
|
3
|
+
* Ported from Firebird's scout directory
|
|
4
|
+
* Based on https://gist.github.com/paulirish/5438650
|
|
5
|
+
*
|
|
6
|
+
* Note: this is not a true polyfill in browsers that do not implement the
|
|
7
|
+
* Navigation Timing API. Rather than returning the time elapsed since
|
|
8
|
+
* `navigationStart`, it will return the time elapsed since the polyfill was
|
|
9
|
+
* installed.
|
|
10
|
+
*
|
|
11
|
+
* Further reading:
|
|
12
|
+
* http://www.w3.org/TR/user-timing/
|
|
13
|
+
* http://caniuse.com/#search=User%20Timing
|
|
14
|
+
* http://caniuse.com/#search=performance.now
|
|
15
|
+
* http://caniuse.com/#search=Navigation%20Timing
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Imports
|
|
19
|
+
var global = require('../global');
|
|
20
|
+
var dateNow = require('../date.now');
|
|
21
|
+
|
|
22
|
+
// Cache references for speed and to guard against shenanigans
|
|
23
|
+
var performance = global.performance;
|
|
24
|
+
var nativeNow = performance && performance.now;
|
|
25
|
+
|
|
26
|
+
// Is the Navigation Timing API natively supported?
|
|
27
|
+
var isNativeSupported = (typeof nativeNow === 'function');
|
|
28
|
+
|
|
29
|
+
// Used by the polyfill implementation to get the moment the module was installed
|
|
30
|
+
var nowOffset = dateNow.now();
|
|
31
|
+
|
|
32
|
+
// Use navigationStart as the offset when possible
|
|
33
|
+
if (performance &&
|
|
34
|
+
performance.timing &&
|
|
35
|
+
typeof performance.timing.navigationStart === 'number'
|
|
36
|
+
) {
|
|
37
|
+
// Use navigationStart value instead
|
|
38
|
+
nowOffset = performance.timing.navigationStart;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Native Implementation
|
|
43
|
+
* Uses the natively-supported performance.now function
|
|
44
|
+
*/
|
|
45
|
+
function nativeImplementation () {
|
|
46
|
+
return nativeNow.call(performance);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Polyfill Implementation
|
|
51
|
+
* Provides similar functionality to performance.now but instead uses a default
|
|
52
|
+
* offset of the moment the polyfill was installed
|
|
53
|
+
*/
|
|
54
|
+
function polyfillImplementation () {
|
|
55
|
+
return dateNow.now() - nowOffset;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = {
|
|
59
|
+
now: function () {
|
|
60
|
+
var func = (isNativeSupported) ? nativeImplementation : polyfillImplementation;
|
|
61
|
+
return func();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# pixelsDisplayed
|
|
2
|
+
|
|
3
|
+
> A module for detecting whether an element is located within the viewport
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
var bvUIPixelsDisplayed = require('bv-ui-core/lib/pixelsDisplayed');
|
|
9
|
+
var pixelsDisplayedVertical = bvUIPixelsDisplayed.pixelsDisplayedVertical;
|
|
10
|
+
var pixelsDisplayedHorizontal = bvUIPixelsDisplayed.pixelsDisplayedHorizontal;
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
if (pixelsDisplayedVertical(element) > 0) {
|
|
14
|
+
// element is vertically within viewport
|
|
15
|
+
if (pixelsDisplayedHorizontal(element) > 0) {
|
|
16
|
+
// element is both vertically and horizontally within viewport
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|