utils.js 919 B

123456789101112131415161718192021222324252627282930313233
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. 'stability': 'unstable'
  7. };
  8. const privateNS = require('../../core/namespace').ns();
  9. function getOwnerWindow(thing) {
  10. try {
  11. // check for and return associated window
  12. let fn = (privateNS(thing.prototype) || privateNS(thing) || {}).getOwnerWindow;
  13. if (fn)
  14. return fn.apply(fn, [thing].concat(arguments));
  15. }
  16. // stuff like numbers and strings throw errors with namespaces
  17. catch(e) {}
  18. // default
  19. return undefined;
  20. }
  21. getOwnerWindow.define = function(Type, fn) {
  22. privateNS(Type.prototype).getOwnerWindow = fn;
  23. }
  24. getOwnerWindow.implement = function(instance, fn) {
  25. privateNS(instance).getOwnerWindow = fn;
  26. }
  27. exports.getOwnerWindow = getOwnerWindow;