webapis-media-query.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. (function (global, factory) {
  9. typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
  10. typeof define === 'function' && define.amd ? define(factory) :
  11. (factory());
  12. }(this, (function () { 'use strict';
  13. /**
  14. * @license
  15. * Copyright Google Inc. All Rights Reserved.
  16. *
  17. * Use of this source code is governed by an MIT-style license that can be
  18. * found in the LICENSE file at https://angular.io/license
  19. */
  20. Zone.__load_patch('mediaQuery', function (global, Zone, api) {
  21. function patchAddListener(proto) {
  22. api.patchMethod(proto, 'addListener', function (delegate) { return function (self, args) {
  23. var callback = args.length > 0 ? args[0] : null;
  24. if (typeof callback === 'function') {
  25. var wrapperedCallback = Zone.current.wrap(callback, 'MediaQuery');
  26. callback[api.symbol('mediaQueryCallback')] = wrapperedCallback;
  27. return delegate.call(self, wrapperedCallback);
  28. }
  29. else {
  30. return delegate.apply(self, args);
  31. }
  32. }; });
  33. }
  34. function patchRemoveListener(proto) {
  35. api.patchMethod(proto, 'removeListener', function (delegate) { return function (self, args) {
  36. var callback = args.length > 0 ? args[0] : null;
  37. if (typeof callback === 'function') {
  38. var wrapperedCallback = callback[api.symbol('mediaQueryCallback')];
  39. if (wrapperedCallback) {
  40. return delegate.call(self, wrapperedCallback);
  41. }
  42. else {
  43. return delegate.apply(self, args);
  44. }
  45. }
  46. else {
  47. return delegate.apply(self, args);
  48. }
  49. }; });
  50. }
  51. if (global['MediaQueryList']) {
  52. var proto = global['MediaQueryList'].prototype;
  53. patchAddListener(proto);
  54. patchRemoveListener(proto);
  55. }
  56. else if (global['matchMedia']) {
  57. api.patchMethod(global, 'matchMedia', function (delegate) { return function (self, args) {
  58. var mql = delegate.apply(self, args);
  59. if (mql) {
  60. // try to patch MediaQueryList.prototype
  61. var proto = Object.getPrototypeOf(mql);
  62. if (proto && proto['addListener']) {
  63. // try to patch proto, don't need to worry about patch
  64. // multiple times, because, api.patchEventTarget will check it
  65. patchAddListener(proto);
  66. patchRemoveListener(proto);
  67. patchAddListener(mql);
  68. patchRemoveListener(mql);
  69. }
  70. else if (mql['addListener']) {
  71. // proto not exists, or proto has no addListener method
  72. // try to patch mql instance
  73. patchAddListener(mql);
  74. patchRemoveListener(mql);
  75. }
  76. }
  77. return mql;
  78. }; });
  79. }
  80. });
  81. })));