Skip to content

Commit

Permalink
add web-api.ts to patch mediaQuery (angular#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Jan 12, 2017
1 parent 7cd570e commit e92f934
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ gulp.task('build/zone.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/rollup-main.ts', 'zone.min.js', true, cb);
});

gulp.task('build/web-api.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/web-api.ts', 'web-api.js', true, cb);
});

gulp.task('build/jasmine-patch.js', ['compile-esm'], function(cb) {
return generateScript('./lib/jasmine/jasmine.ts', 'jasmine-patch.js', false, cb);
});
Expand Down Expand Up @@ -151,6 +155,7 @@ gulp.task('build', [
'build/zone.js.d.ts',
'build/zone.min.js',
'build/zone-node.js',
'build/web-api.js',
'build/jasmine-patch.js',
'build/jasmine-patch.min.js',
'build/mocha-patch.js',
Expand Down
45 changes: 45 additions & 0 deletions lib/browser/web-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {NestedEventListenerOrEventListenerObject, patchEventTargetMethods} from '../common/utils';

((_global: any) => {
// patch MediaQuery
patchMediaQuery(_global);

function patchMediaQuery(_global: any) {
if (!_global['MediaQueryList']) {
return;
}
patchEventTargetMethods(
_global['MediaQueryList'].prototype, 'addListener', 'removeListener', (self, args) => {
return {
useCapturing: false,
eventName: 'mediaQuery',
handler: args[0],
target: self || _global,
name: 'mediaQuery',
invokeAddFunc: function(
addFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
if (delegate && (<Task>delegate).invoke) {
return this.target[addFnSymbol]((<Task>delegate).invoke);
} else {
return this.target[addFnSymbol](delegate);
}
},
invokeRemoveFunc: function(
removeFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
if (delegate && (<Task>delegate).invoke) {
return this.target[removeFnSymbol]((<Task>delegate).invoke);
} else {
return this.target[removeFnSymbol](delegate);
}
}
};
});
}
})(typeof window === 'object' && window || typeof self === 'object' && self || global);
27 changes: 27 additions & 0 deletions test/browser/MediaQuery.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import '../../lib/browser/web-api';

import {zoneSymbol} from '../../lib/common/utils';
import {ifEnvSupports} from '../test-util';

function supportMediaQuery() {
const _global =
typeof window === 'object' && window || typeof self === 'object' && self || global;
return _global['MediaQueryList'] && _global['matchMedia'];
}

describe('test mediaQuery patch', ifEnvSupports(supportMediaQuery, () => {
it('test whether addListener is patched', () => {
const mqList = window.matchMedia('min-width:500px');
if (mqList && mqList['addListener']) {
expect(mqList[zoneSymbol('addListener')]).not.toBe(undefined);
}
});
}));
1 change: 1 addition & 0 deletions test/browser_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ import './browser/registerElement.spec';
import './browser/requestAnimationFrame.spec';
import './browser/WebSocket.spec';
import './browser/XMLHttpRequest.spec';
import './browser/MediaQuery.spec';
import './mocha-patch.spec';

0 comments on commit e92f934

Please sign in to comment.