WindowedApp.js

From disqus.com, 5 Days ago, written in JavaScript, viewed 3 times. This paste is a reply to BaseApp.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/69efdd1b Embed
Download Paste or View Raw
  1. define('core/apps/WindowedApp',['require','core/utils/object/extend','core/shared/urls','core/apps/BaseApp','core/host/kernel'],function (require) {
  2.     'use strict';
  3.  
  4.     var extend = require('core/utils/object/extend');
  5.  
  6.     var urls = require('core/shared/urls');
  7.  
  8.     var BaseApp = require('core/apps/BaseApp');
  9.  
  10.     var kernel = require('core/host/kernel');
  11.  
  12.  
  13.     var doc = window.document;
  14.  
  15.     // The base class for any "windowed" app, that is using another "window"
  16.     // other than the window object on the page itself. Makes use of
  17.     // Channel or Popup according to settings.
  18.     // Having a "windowName" attribute defined on the settings object will
  19.     // cause it to create a popup using that name, otherwise it will use the
  20.     // Channel class with an expected container to put the iframe that Channel
  21.     // uses. Covers the following:
  22.     //    * Expects the instance or the derived class to have a "url" property
  23.     //      defining url for the application. It is assumed to be a protocol-
  24.     //      relative URL.
  25.     //    * If a `getFrameSettings` method is defined on the instance or the
  26.     //      derived class it will be called right before creating the "window"
  27.     //      passing the automatically built settings object. The method is
  28.     //      expected to return a settings object. It can be the same object
  29.     //      that was passed to it or a new one, but it SHOULD return a valid
  30.     //      settings object to be passed to Channel or Popup constructor.
  31.     //    * Provides an `init` method as the main entry point. Calling this
  32.     //      method will call trigger "state:INIT" event, then call `getFrame`
  33.     //      to create the frame, which will call `getFrameSettings` as
  34.     //      explained above. Assign the returned "frame" instance to `this.frame`
  35.     //      proxy all events on the frame to the instance itself under the
  36.     //      namespace "frame:" (eg frame:ready) and trigger "change:frame" after
  37.     //      this. Then it will load the frame using the `target` attribute on
  38.     //      frame settings, trigger 'state:LOADED' event when frame is loaded.
  39.     //    * It will clean-up its frame when the instance is destroyed.
  40.     //    * It will do and support everything BaseApp does and supports.
  41.     var WindowedApp = BaseApp.extend({
  42.         // You need a name that matches a key in next.host.urls.apps for the
  43.         // app to get proper URLs
  44.         name: null,
  45.         loaderVersion: null,
  46.         frame: null,
  47.         origin: urls.ensureHTTPSProtocol('{{ DISQUS_URL }}'),
  48.         state: null,
  49.  
  50.         getUrl: function (params, hashParams) {
  51.             // If applicable, add the loader version to the params
  52.             if (this.loaderVersion) {
  53.                 hashParams = extend({
  54.                     version: this.loaderVersion,
  55.                 }, hashParams);
  56.             }
  57.  
  58.             // NOTE: urls.get already does this but without the HTTPS enforcement
  59.             //       This is a minor hack and should be fixed once the HTTPS
  60.             //       experiment is finalized.
  61.  
  62.             return urls.ensureHTTPSProtocol(urls.get(this.name, params, hashParams));
  63.         },
  64.  
  65.         getFrameSettings: function () {
  66.             var frameSettings = {
  67.                 target: this.getUrl(),
  68.                 origin: this.origin,
  69.                 uid: this.uid,
  70.                 sandbox: this.sandbox,
  71.             };
  72.  
  73.             var settings = this.settings;
  74.             if (settings.windowName)
  75.                 frameSettings.windowName = settings.windowName;
  76.             else
  77.                 frameSettings.container = settings.container || doc.body;
  78.  
  79.             if (settings.styles)
  80.                 frameSettings.styles = settings.styles;
  81.  
  82.             return frameSettings;
  83.         },
  84.  
  85.         getFrame: function () {
  86.             var frameSettings = this.getFrameSettings();
  87.             var FrameConstructor = frameSettings.windowName ? kernel.Popup : kernel.Channel;
  88.             return new FrameConstructor(frameSettings);
  89.         },
  90.  
  91.         setState: function (state) {
  92.             var constructor = this.constructor;
  93.  
  94.             if (!(state in constructor.states))
  95.                 return false;
  96.  
  97.             this.state = constructor.states[state];
  98.             this.trigger('state:' + state);
  99.         },
  100.  
  101.         init: function () {
  102.             var self = this;
  103.             var frame;
  104.  
  105.             self.frame = frame = this.getFrame();
  106.  
  107.             // We relay all events originating from the frame on our actual
  108.             // instance by prefixing them with "frame:" which allows one to
  109.             // listen to all "frame" events too even if the underlying frame
  110.             // object is changed or not ready yet. This is almost the same
  111.             // pattern we use in SessionModel to relay User events to session
  112.             self.listenTo(frame, 'all', function (name, data) {
  113.                 self.trigger('frame:' + name, data, frame);
  114.             });
  115.  
  116.             self.listenTo(frame, 'resize', function (data) {
  117.                 self.lastResizedHeight = data.height;
  118.             });
  119.  
  120.             self.trigger('change:frame', frame);
  121.  
  122.             self.frame.load(function () {
  123.                 self.setState('LOADED');
  124.             });
  125.  
  126.             self.setState('INIT');
  127.         },
  128.  
  129.         destroy: function () {
  130.             var frame = this.frame;
  131.             if (frame) {
  132.                 this.stopListening(frame);
  133.                 frame.destroy();
  134.             }
  135.  
  136.             this.setState('KILLED');
  137.             this.frame = null;
  138.             BaseApp.prototype.destroy.call(this);
  139.         },
  140.  
  141.         events: {
  142.             'frame:ready': function () {
  143.                 this.setState('READY');
  144.             },
  145.         },
  146.     }, {
  147.         states: {
  148.             INIT: 0,  // Iframe has been created but received no further messages
  149.             LOADED: 1,  // Received the iframe's load event
  150.             READY: 2,  // Received a message from the iframe client indicating the code is ready
  151.             RUNNING: 3,  // Iframe is operational and visible
  152.             KILLED: 4,
  153.         },
  154.     });
  155.  
  156.     return WindowedApp;
  157. });
  158.  
  159.  
  160. //https://c.disquscdn.com/next/next-core/core/apps/WindowedApp.js

Replies to WindowedApp.js rss

Title Name Language When
exploreModuleConstants.js disqus.com javascript 5 Days ago.

Reply to "WindowedApp.js"

Here you can reply to the paste above