Re:windowBase.js

From disqus.com, 2 Months ago, written in JavaScript, viewed 3 times.
URL https://pastebin.freepbx.org/view/cb7a7927 Embed
Download Paste or View Raw
  1. define('core/common/kernel/WindowBase',['require','core/Events','core/utils/object/extend','core/utils/uniqueId','core/common/kernel/utils','core/host/json'],function (require) {
  2.     'use strict';
  3.  
  4.     var Events = require('core/Events');
  5.     var extend = require('core/utils/object/extend');
  6.     var uniqueId = require('core/utils/uniqueId');
  7.  
  8.     var utils = require('core/common/kernel/utils');
  9.  
  10.     var JSON = require('core/host/json');
  11.  
  12.     //
  13.     // WindowBase
  14.     //
  15.     // Base window class. Inherited by Popup and Iframe.
  16.     //
  17.  
  18.     var WindowBase = function (opts) {
  19.         opts = opts || {};
  20.  
  21.         this.state = WindowBase.INIT;
  22.         this.uid = opts.uid || uniqueId('dsq-frame');  // each window gets a unique ID
  23.         this.origin = opts.origin;
  24.         this.host = utils.getHost(this.origin);
  25.         this.target = opts.target;  // target url
  26.         this.sandbox = opts.sandbox;
  27.         this.window = null;
  28.  
  29.         WindowBase.windows[this.uid] = this;  // register the window instance
  30.  
  31.         this.on('ready', function () {
  32.             this.state = WindowBase.READY;
  33.         }, this);
  34.  
  35.         this.on('die', function () {
  36.             this.state = WindowBase.KILLED;
  37.         }, this);
  38.     };
  39.  
  40.     extend(WindowBase, {
  41.         INIT: 0,
  42.         READY: 1,
  43.         KILLED: 2,
  44.  
  45.         windows: {},
  46.  
  47.         // Make postMessage called this way so that we can stub it in IE.
  48.         postMessage: function (win, message, origin) {
  49.             return win.postMessage(message, origin);
  50.         },
  51.  
  52.     });
  53.  
  54.     extend(WindowBase.prototype, Events);
  55.  
  56.     /**
  57.      * Wraps a function so that its execution will be delayed until
  58.      * the window is ready. Also executes the function in the same
  59.      * context as requiresWindow.
  60.      *
  61.      * Note that, since execution may be asynchronous, the return
  62.      * value of the wrapped function is discarded.
  63.      *
  64.      * @param {function} fn - The function to wrap
  65.      * @returns {function} - The wrapped function
  66.      */
  67.     WindowBase.prototype.requiresWindow = function (fn) {
  68.         var self = this;
  69.  
  70.         return function () {
  71.             var args = Array.prototype.slice.call(arguments);
  72.  
  73.             var pollForWindow = function () {
  74.                 var win = self.window;
  75.                 if (win)
  76.                     fn.apply(self, args);
  77.                 else
  78.                     setTimeout(pollForWindow, 500); // eslint-disable-line no-magic-numbers
  79.             };
  80.  
  81.             if (self.isReady())
  82.                 pollForWindow();
  83.             else  // queue until the ready event occurs.
  84.                 self.on('ready', pollForWindow);
  85.         };
  86.     };
  87.  
  88.     WindowBase.prototype.sendMessage = function (name, data) {
  89.         var message = JSON.stringify({
  90.             scope: 'client',
  91.             name: name,
  92.             data: data,
  93.         });
  94.  
  95.         this.requiresWindow(function (msg) {
  96.             WindowBase.postMessage(this.window, msg, this.origin);
  97.         })(message);
  98.     };
  99.  
  100.     // Overload these where applicable
  101.     WindowBase.prototype.hide = function () {};
  102.  
  103.     WindowBase.prototype.show = function () {};
  104.  
  105.     WindowBase.prototype.url = function () {
  106.         return this.target;
  107.     };
  108.  
  109.     WindowBase.prototype.destroy = function () {
  110.         this.state = WindowBase.KILLED;
  111.  
  112.         this.off(); // Unbind any events
  113.     };
  114.  
  115.     WindowBase.prototype.isReady = function () {
  116.         return this.state === WindowBase.READY;
  117.     };
  118.  
  119.     // Returns true if this window has become destroyed - either
  120.     // by us, or by the user (i.e. user manually closes a window).
  121.     WindowBase.prototype.isKilled = function () {
  122.         return this.state === WindowBase.KILLED;
  123.     };
  124.  
  125.     return WindowBase;
  126. });
  127.  
  128. // https://c.disquscdn.com/next/next-core/core/common/kernel/WindowBase.js

Reply to "Re:windowBase.js"

Here you can reply to the paste above