Re: kernel.js

From disqus.com, 2 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to Re: iframe.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/03daa500 Embed
Download Paste or View Raw
  1. /*
  2.  * Disqus Kernel
  3.  *
  4.  * The kernel is the main component of most embeddable Disqus products;
  5.  * it is a bridge between applications and the actual publisher's DOM.
  6.  * In addition to that, the kernel also manages all communications
  7.  * between applications.
  8.  *
  9.  */
  10.  
  11. // For local development only, will get removed on production builds automatically by dead code elimination
  12. if (!'{{ BUILD_OPTIMIZED }}' && !window.geturl) { // eslint-disable-line
  13.     /* eslint-disable strict */
  14.     window.geturl = function (url) {
  15.         // eslint-disable-next-line no-constant-condition
  16.         var appDir = '{{ APP_NAME }}' ? '{{ APP_NAME }}/' : '';
  17.         return '{{ NEXT_BASE }}/' + appDir + url;
  18.     };
  19.     /* eslint-enable strict */
  20. }
  21.  
  22. define('core/host/kernel',['require','exports','module','core/Events','core/utils/lang/isString','core/utils/object/has','core/utils/object/extend','core/common/kernel/Iframe','core/common/kernel/utils','core/common/kernel/WindowBase','core/host/json','core/utils/function/throttle'],function (require, exports) {
  23.     'use strict';
  24.  
  25.     var Events = require('core/Events');
  26.     var isString = require('core/utils/lang/isString');
  27.     var has = require('core/utils/object/has');
  28.     var extend = require('core/utils/object/extend');
  29.     var Iframe = require('core/common/kernel/Iframe');
  30.     var utils = require('core/common/kernel/utils');
  31.     var WindowBase = require('core/common/kernel/WindowBase');
  32.  
  33.     var JSON = require('core/host/json'); // eslint-disable-line no-shadow
  34.  
  35.     var document = window.document;
  36.  
  37.     exports.throttle = require('core/utils/function/throttle');
  38.  
  39.     // window.postMessage receiver
  40.     //
  41.     // NOTE: Instead of having a separate receiver for each channel,
  42.     //       we have a single eventListener that delegates to the
  43.     //       appropriate channel.
  44.     window.addEventListener('message', function (event) {
  45.         var message;
  46.         try {
  47.             message = JSON.parse(event.data);
  48.         } catch (err) {
  49.             return;  // quit
  50.         }
  51.  
  52.         // Does the message has a valid sender UID
  53.         var senderId = message.sender;
  54.         var sender = has(WindowBase.windows, senderId) && WindowBase.windows[senderId];
  55.  
  56.         // Message is not valid, ignore
  57.         if (!(sender && utils.getHost(event.origin) === sender.host)) {
  58.             return;
  59.         }
  60.  
  61.         // If the message is valid, but the origins don't match, that means that
  62.         // the iframe was redirected http->https. We need to update our corresponding
  63.         // origin value in order to send a return message later.
  64.         //
  65.         // NOTE: This relies on us receiving a message from the iframe with the new origin
  66.         //       *before* we attempt to send a message back.
  67.         if (event.origin !== sender.origin) {
  68.             sender.origin = event.origin;
  69.         }
  70.  
  71.         if (message.scope === 'host') {  // ignore any other scopes for now
  72.             sender.trigger(message.name, message.data);
  73.         }
  74.  
  75.         if (message.name === 'error') {
  76.             exports.trigger('error', message.data);
  77.         }
  78.     });
  79.  
  80.     // TODO: pass all these events into the frame in a transparent
  81.     // way on a `require('host/window')` module or something like
  82.     // that. Then in the frame, we could
  83.     //      `$(require('host/window')).on('hashchange', ...)`
  84.     window.addEventListener('hashchange', function () {
  85.         exports.trigger('window.hashchange', {
  86.             hash: window.location.hash,
  87.         });
  88.     });
  89.  
  90.     /* eslint-disable no-magic-numbers */
  91.     window.addEventListener('resize', exports.throttle(function () {
  92.         exports.trigger('window.resize');
  93.     }, 250, 50));
  94.  
  95.     document.addEventListener('mousemove', exports.throttle(function () {
  96.         exports.trigger('window.mousemove');
  97.     }, 250, 50));
  98.  
  99.     var triggerScroll = function () {
  100.         exports.trigger('window.scroll');
  101.     };
  102.  
  103.     window.addEventListener('scroll', exports.throttle(triggerScroll, 250, 50), false);
  104.     /* eslint-enable no-magic-numbers */
  105.     document.addEventListener('click', function () {
  106.         // NOTE: It appears that the browser doesn't fire the click event on the
  107.         // parent window if a click occurs inside the iframe. So we don't need
  108.         // to verify the event target (checked in Chrome, Firefox)
  109.         exports.trigger('window.click');
  110.     });
  111.  
  112.     //
  113.     // Popup
  114.     //
  115.     // Wraps a popup window.
  116.     //
  117.     var Popup = exports.Popup = function (opts) {
  118.         opts.uid = opts.windowName;
  119.  
  120.         WindowBase.call(this, opts);
  121.     };
  122.  
  123.     extend(Popup.prototype, WindowBase.prototype);
  124.  
  125.     Popup.prototype.load = function () {
  126.         var win = this.window = window.open('', this.uid || '_blank');
  127.  
  128.         win.location = this.url();
  129.     };
  130.  
  131.     Popup.prototype.isKilled = function () {
  132.         // NOTE: In iOS Safari, popup windows don't invoke onbeforeunload,
  133.         //       and cannot inform the parent document that they have become
  134.         //       closed. So, we additionally check for the window.closed property,
  135.         //       which reports accurately in that browser. (All other Disqus-
  136.         //       supported browsers correctly update the _isKilled property.)
  137.         return WindowBase.prototype.isKilled() || this.window.closed;
  138.     };
  139.  
  140.     // Channel is an external iframe that communicates back to the
  141.     // parent page (this context).
  142.  
  143.     var Channel = exports.Channel = function (opts) {
  144.         var self = this;
  145.  
  146.         self.window = null;
  147.  
  148.         Iframe.call(self, opts);
  149.  
  150.         // Optional: an additional element to position around
  151.         // Only one of the two options should be specified
  152.         this.insertBeforeEl = opts.insertBeforeEl;
  153.         this.insertAfterEl = opts.insertAfterEl;
  154.  
  155.         // min-width 100% with a width constant is to allow iframe to be responsive on iOS devices, which
  156.         // have a buggy width:100% implementation on iframes
  157.         // fix from: http://stackoverflow.com/q/20123960/90297
  158.         self.styles = extend({
  159.             'width': '1px',
  160.             'min-width': '100%',
  161.             'border': 'none',
  162.             'overflow': 'hidden',
  163.             'height': '0',
  164.         }, opts.styles || {});
  165.  
  166.     };
  167.  
  168.     extend(Channel.prototype, Iframe.prototype);
  169.  
  170.     Channel.prototype.load = function (onload) {
  171.         var self = this;
  172.  
  173.         Iframe.prototype.load.call(self);
  174.  
  175.         var elem = self.elem;
  176.         elem.setAttribute('width', '100%');
  177.         elem.setAttribute('src', self.url());
  178.  
  179.         elem.addEventListener('load', function () {
  180.             self.window = elem.contentWindow;
  181.  
  182.             if (onload) {
  183.                 onload();
  184.             }
  185.  
  186.         });
  187.  
  188.         var container = isString(self.container) ?
  189.             utils.getContainer(self.container) : self.container;
  190.  
  191.         // WARNING: Make sure you use `null` here instead of `undefined` since
  192.         //          this value is directly used in the `insertBefore` call as
  193.         //          the second argument and IE10 blows when it is `undefined`.
  194.         //          See: https://github.com/lhorie/mithril.js/issues/353
  195.         //          See: http://stackoverflow.com/q/15342341/90297
  196.         var beforeElement = (self.insertAfterEl ? self.insertAfterEl.nextSibling : self.insertBeforeEl) || null;
  197.         container.insertBefore(elem, beforeElement);
  198.     };
  199.  
  200.     Channel.prototype.destroy = function () {
  201.         this.window = null;
  202.  
  203.         return Iframe.prototype.destroy.call(this);
  204.     };
  205.  
  206.     // Expose events interface
  207.     exports.on = Events.on;
  208.     exports.off = Events.off;
  209.     exports.trigger = Events.trigger;
  210. });
  211.  
  212.  
  213. // https://c.disquscdn.com/next/next-core/core/host/kernel.js

Replies to Re: kernel.js rss

Title Name Language When
Re:vote.js disqus.com javascript 2 Months ago.

Reply to "Re: kernel.js"

Here you can reply to the paste above