Re:withAlert.js

From disqus.com, 1 Week ago, written in JavaScript, viewed 3 times. This paste is a reply to Re:tracking.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/0aca4f37 Embed
Download Paste or View Raw
  1. define('core/mixins/withAlert',[
  2.     'underscore',
  3.  
  4.     'core/views/AlertView',
  5. ], function (
  6.     _,
  7.  
  8.     AlertView
  9. ) {
  10.     'use strict';
  11.  
  12.     /**
  13.      * @typedef {('error'|'info'|'success'|'warn')} AlertStyle
  14.      */
  15.  
  16.     var mixin = {
  17.         /**
  18.          * Render an alert box in above the current view
  19.          *
  20.          * @param {string} message - the message to display (required)
  21.          * @param {Object} [options] - options
  22.          * @param {boolean} [options.safe] - don't HTML-escape message contents
  23.          * @param {string} [options.target] - a selector for an element to place the alert within
  24.          * @param {AlertStyle} [options.type] - the style of alert to use
  25.          * @returns {AlertView}
  26.          */
  27.         alert: function (message, options) {
  28.             if (!_.isObject(options))
  29.                 options = {};
  30.  
  31.             var alertSelector = options.target || this._alertSelector;
  32.  
  33.             // Only one alert box at one time
  34.             this.dismissAlert();
  35.  
  36.             var alert = this._alert = new AlertView(_.extend({
  37.                 message: message,
  38.             }, options));
  39.  
  40.             this.listenToOnce(this._alert, 'dismiss', function () {
  41.                 this._alert = null;
  42.             });
  43.  
  44.             alert.render();
  45.  
  46.             // If a selector is given, insert the alert at that
  47.             // location. Otherwise insert before the element.
  48.             if (alertSelector) {
  49.  
  50.                 var alertContianerEl = this.$el.find(alertSelector);
  51.  
  52.                 /* TODO: @taylangocmen when you enable and disable comments the open/close state and alert has not
  53.                     been updated and fetched in the BE, do an optimistic rendering for those in that case */
  54.  
  55.                 // Sometimes when alert is called threadView and alertContainerEl is not rendered yet
  56.                 // in that case listen to threadView to render then try alert again
  57.                 if (alertContianerEl.length)
  58.                     alertContianerEl.prepend(alert.el);
  59.                 else
  60.                     this.listenToOnce(this, 'threadView:render', function () { return this.alert(message, options); });
  61.  
  62.             } else if (this.el.parentNode) {
  63.                 this.el.parentNode.insertBefore(alert.el, this.el);
  64.             }
  65.  
  66.             return alert;
  67.         },
  68.  
  69.         /**
  70.          * Dismiss any active alert.
  71.          *
  72.          * @param {function(AlertView): *} [filter] -
  73.          *     if provided, the alert will only be dismissed if the function returns truthy
  74.          */
  75.         dismissAlert: function (filter) {
  76.             if (!this._alert)
  77.                 return;
  78.  
  79.             if (filter && !filter(this._alert))
  80.                 return;
  81.  
  82.             this.stopListening(this._alert);
  83.             this._alert.dismiss();
  84.             this._alert = null;
  85.         },
  86.  
  87.         /**
  88.          * Returns the current alert, if any.
  89.          *
  90.          * @returns {?AlertView}
  91.          */
  92.         getAlert: function () {
  93.             return this._alert || null;
  94.         },
  95.  
  96.         /**
  97.          * Specify a selector string that designates where the alert should be prepended.
  98.          * e.g. this.setAlertSelector('.someClass');
  99.          *
  100.          * @param {string} selector - The selector string
  101.          */
  102.         setAlertSelector: function (selector) {
  103.             this._alertSelector = selector;
  104.         },
  105.     };
  106.  
  107.     var withAlert = function () {
  108.         return _.extend(this, mixin);
  109.     };
  110.  
  111.     return withAlert;
  112. });
  113.  
  114. // https://c.disquscdn.com/next/next-core/core/mixins/withAlert.js

Replies to Re:withAlert.js rss

Title Name Language When
Re:visibly.js disqus.com javascript 1 Week ago.

Reply to "Re:withAlert.js"

Here you can reply to the paste above