forked from angular/zone.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add web-api.ts to patch mediaQuery (angular#571)
- Loading branch information
1 parent
7cd570e
commit e92f934
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters