From disqus.com, 3 Weeks ago, written in JavaScript.
This paste is a reply to require.js from disqus.com
- view diff
Embed
  1. (function () {
  2.     'use strict';
  3.  
  4.     // VERY ugly but it is to avoid lint errors
  5.     window.IS_DEV = 'false' === 'true';
  6.  
  7.     var doc = window.document;
  8.     // Parse out the query string parameters
  9.     var params = doc.location.search.substr(1).split('&').reduce(function (all, pair) {
  10.         var parts = pair.split('=').map(decodeURIComponent);
  11.         if (parts[0])
  12.             all[parts[0]] = parts[1];
  13.         return all;
  14.     }, {});
  15.  
  16.     var lang = params.l;
  17.     // We set the `lang` property on the root element (html) on the front-end
  18.     // since we use a "dummy" backend, which cannot do this for us.
  19.     // Our require-js init code looks at this value to load proper translations
  20.     if (lang && lang !== 'en')
  21.         doc.documentElement.lang = lang;
  22.  
  23.     // Ideally we'd look up the language code in locale/mapping.json file but
  24.     // since the list of RTL languages is quite short and not changing over
  25.     // time, we simply hard-code them here as a look-up table
  26.     var rtlLanguages = {
  27.         'ar': 1,
  28.         'ar_SA': 1,
  29.         'he': 1,
  30.         'he_IL': 1,
  31.         'nqo': 1,
  32.         'fa': 1,
  33.         'fa_IR': 1,
  34.         'ur': 1,
  35.         'ur_PK': 1,
  36.     };
  37.  
  38.     var loadCss = function (path) {
  39.         var style = doc.createElement('link');
  40.         style.rel = 'stylesheet';
  41.         style.href = '//c.disquscdn.com/next/16dccc7/home/css/' + path + '.css';
  42.         doc.head.appendChild(style);
  43.     };
  44.  
  45.     var mainCSSPath = 'main';
  46.  
  47.     if (rtlLanguages[lang]) {
  48.         doc.documentElement.dir = 'rtl';
  49.         mainCSSPath += '_rtl';
  50.     }
  51.  
  52.     loadCss(mainCSSPath);
  53.     // TODO: move this into dashboard-imports.css maybe?
  54.     // TODO: make this RTL compatible too may be?
  55.     loadCss('hovercards');
  56.  
  57.     // Using addEventListener to detect IE9 and above
  58.     // Cannot use conditional comments because they don't work
  59.     // in IE10 standards mode:
  60.     // http://msdn.microsoft.com/en-us/library/ms537512.aspx
  61.     var body = doc.body;
  62.     var unsupportedBrowser = doc.getElementById('unsupportedBrowser');
  63.     if (!doc.addEventListener) {
  64.         body.innerHTML = '<p>' + unsupportedBrowser.innerHTML + '</p>';
  65.         return;
  66.     }
  67.  
  68.     var isSidebar = function () {
  69.         try {
  70.             // If window.self !== window.top then we're iFramed
  71.             // and this is the home sidebar
  72.             return window.self !== window.top;
  73.         } catch (err) {
  74.             // If we got an error, it was a same origin policy error,
  75.             // which means we _are_ in an iFrame.
  76.             return true;
  77.         }
  78.     };
  79.  
  80.     if (isSidebar())
  81.         body.setAttribute('class', body.className + ' home-drawer');
  82.  
  83.     // --- requirejs config init + fallback support
  84.     // Include module name here so we can require it after definition (same file)
  85.     define('initializer', [  // eslint-disable-line requirejs/no-named-define
  86.         'require',
  87.         'exports',
  88.  
  89.         // 'require' will be referred to as localRequire here to avoid shadowing
  90.         // the global require variable. This is necessary to let us update the
  91.         // global config via require.config.
  92.     ], function (
  93.         localRequire,
  94.         exports
  95.     ) {
  96.         exports.ready = false;
  97.  
  98.         exports.setReady = function () {
  99.             localRequire(['common/bus'], function (Bus) {
  100.                 Bus.sendHostMessage('ready');
  101.                 exports.ready = true;
  102.             });
  103.         };
  104.  
  105.         /**
  106.          * This method loads the resources necessary for the application.
  107.          *
  108.          * When the application module is loaded, this function will call the
  109.          * application module's init function. It will call this function even if
  110.          * there is an error loading the translations file.
  111.          */
  112.         exports.init = function () {
  113.             var language = doc.documentElement.lang;
  114.             var requireConfig = {
  115.                 baseUrl: '//c.disquscdn.com/next/16dccc7/home/js',
  116.                 paths: { 'translations': '../lang/' + language },
  117.             };
  118.  
  119.             if (!window.IS_DEV) {
  120.                 requireConfig.bundles = {
  121.                     'home/main': ['moment'],  // required for moment translations
  122.                 };
  123.             }
  124.  
  125.             if (!language || language === 'en')
  126.                 define('translations', {});  // eslint-disable-line requirejs/no-named-define
  127.  
  128.             var CDN_ROOT = '//c.disquscdn.com/next/16dccc7/home';
  129.  
  130.             function depsReady() {
  131.                 // NOTE: These modules should all be in the app bundle,
  132.                 //       so require won't trigger another fetch
  133.                 localRequire([
  134.                     'core/analytics/identity',
  135.  
  136.                     'home/main',
  137.                     'loglevel',
  138.                     'raven',
  139.                 ], function (
  140.                     identity,
  141.  
  142.                     app,
  143.                     logger,
  144.                     Raven
  145.                 ) {
  146.                     var logLevel = window.IS_DEV ? 'DEBUG' : 'SILENT';
  147.                     logger.setDefaultLevel(logLevel);
  148.  
  149.                     // Must instantiate identity cookie managers upon app initialization with the is_private state
  150.                     identity.init({
  151.                         isPrivate: window.home_config ? window.home_config.is_private : false,
  152.                     });
  153.  
  154.                     // Don't bother initializing Raven in IE9 or below since we don't support those actively and
  155.                     // they also generate garbage error reports most of the time.
  156.                     var isIE9OrBelow = doc.documentMode && doc.documentMode < 10;  // eslint-disable-line no-magic-numbers
  157.                     if (!window.IS_DEV && !isIE9OrBelow) {
  158.                         Raven.config('//1a2772a06861430ebd15caa640a82975@sentry.services.disqus.com/28', {
  159.                             whitelistUrls: [
  160.                                 CDN_ROOT,
  161.                                 // We need CDN_CURRENT to include errors in this file
  162.                                 '//c.disquscdn.com/next/current/home',
  163.                             ],
  164.                             ignoreErrors: [
  165.                                 // Ignore unrelated errors from TiApp
  166.                                 // See http://sentry.local.disqus.net/disqus/home/group/641903/
  167.                                 /\bTiApp\b/,
  168.                             ],
  169.                             release: '16dccc7',
  170.                         }).install();
  171.                     }
  172.                     app.init({
  173.                         CDN_ROOT: CDN_ROOT,
  174.                     });
  175.                 });
  176.             }
  177.  
  178.             function provideModuleFallback(name, fallback) {
  179.                 // Replace the module definition with the fallback
  180.                 require.undef(name);
  181.                 define(name, fallback);  // eslint-disable-line requirejs/no-invalid-define
  182.  
  183.                 // Trigger another load of the module. When RequireJS sees this
  184.                 // succeed it will automatically trigger the callbacks depending on
  185.                 // this module so we don't have to call `depsReady` manually.
  186.                 localRequire([name]);
  187.             }
  188.  
  189.             var moduleFallbacks = {
  190.                 'translations': {},  // Translations files may not exist for a specific language
  191.                 'remote/config': {  // Server may fail to generate config
  192.                     lounge: {},
  193.                     discovery: {},
  194.                     experiments: {},
  195.                 },
  196.             };
  197.  
  198.             function depsFailed(error) {
  199.                 // This callback is used for any errors associated with module loading.
  200.                 // It may be called multiple times, for each module which failed.
  201.  
  202.                 // Log the error
  203.                 /* eslint-disable no-console */
  204.                 if (typeof console !== 'undefined' && typeof console.log === 'function')
  205.                     console.log(error.toString());
  206.                 /* eslint-enable no-console */
  207.  
  208.                 // Some modules are not required for the application to function
  209.                 // Provide fallbacks if they failed to load
  210.                 var failedModules = error.requireModules || [];
  211.                 var failedModule, fallback;
  212.                 for (var i = 0; i < failedModules.length; ++i) {
  213.                     failedModule = failedModules[i];
  214.                     fallback = moduleFallbacks[failedModule];
  215.  
  216.                     if (fallback)
  217.                         provideModuleFallback(failedModule, fallback);
  218.                     // TODO: Add a proper "load failed, refresh?" handler and reporting here as the `else` clause
  219.                 }
  220.             }
  221.  
  222.             require.config(requireConfig);
  223.             localRequire(['home/main', 'translations'], depsReady, depsFailed);
  224.         };
  225.     });
  226.     require(['initializer'], function (i) { i.init(); });
  227.  
  228.     // Livereload support
  229.     // Will be removed by uglify in production
  230.     /* eslint-disable */
  231.     if ('false' === 'true')
  232.         require(['//media.dev.disqus.org:9090/livereload.js']);
  233.     /* eslint-enable */
  234. }());
  235.  
  236. // https://c.disquscdn.com/next/current/home/js/apps/src/static/js/apps/initializer.js

Replies to /initializer.js rss

Title Name Language When
npo.src.js disqus.com javascript 3 Weeks ago.