webapis-media-query.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2022 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. Zone.__load_patch('mediaQuery', (global, Zone, api) => {
  8. function patchAddListener(proto) {
  9. api.patchMethod(proto, 'addListener', (delegate) => (self, args) => {
  10. const callback = args.length > 0 ? args[0] : null;
  11. if (typeof callback === 'function') {
  12. const wrapperedCallback = Zone.current.wrap(callback, 'MediaQuery');
  13. callback[api.symbol('mediaQueryCallback')] = wrapperedCallback;
  14. return delegate.call(self, wrapperedCallback);
  15. }
  16. else {
  17. return delegate.apply(self, args);
  18. }
  19. });
  20. }
  21. function patchRemoveListener(proto) {
  22. api.patchMethod(proto, 'removeListener', (delegate) => (self, args) => {
  23. const callback = args.length > 0 ? args[0] : null;
  24. if (typeof callback === 'function') {
  25. const wrapperedCallback = callback[api.symbol('mediaQueryCallback')];
  26. if (wrapperedCallback) {
  27. return delegate.call(self, wrapperedCallback);
  28. }
  29. else {
  30. return delegate.apply(self, args);
  31. }
  32. }
  33. else {
  34. return delegate.apply(self, args);
  35. }
  36. });
  37. }
  38. if (global['MediaQueryList']) {
  39. const proto = global['MediaQueryList'].prototype;
  40. patchAddListener(proto);
  41. patchRemoveListener(proto);
  42. }
  43. else if (global['matchMedia']) {
  44. api.patchMethod(global, 'matchMedia', (delegate) => (self, args) => {
  45. const mql = delegate.apply(self, args);
  46. if (mql) {
  47. // try to patch MediaQueryList.prototype
  48. const proto = Object.getPrototypeOf(mql);
  49. if (proto && proto['addListener']) {
  50. // try to patch proto, don't need to worry about patch
  51. // multiple times, because, api.patchEventTarget will check it
  52. patchAddListener(proto);
  53. patchRemoveListener(proto);
  54. patchAddListener(mql);
  55. patchRemoveListener(mql);
  56. }
  57. else if (mql['addListener']) {
  58. // proto not exists, or proto has no addListener method
  59. // try to patch mql instance
  60. patchAddListener(mql);
  61. patchRemoveListener(mql);
  62. }
  63. }
  64. return mql;
  65. });
  66. }
  67. });