Changeset 636 for trunk/js


Ignore:
Timestamp:
Sep 29, 2018 6:50:13 PM (6 years ago)
Author:
anonymous
Message:

Add fadeout_delay (including auto)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/js/Msg.js

    r622 r636  
    4848    // Merge options with defaults
    4949    var options = this.o = $.extend({
    50         container: '.sc-msg:last',
     50        container: '.sc-msg:first',
    5151        above_msg: '',
    5252        gotohash: true
     
    6767* @param    string message  The message to display.
    6868* @param    string class    The class to apply to the message div (when using codebase CSS, it is useful to use one of: sc-msg-success, sc-msg-notice, sc-msg-warning, sc-msg-error)
     69* @param    string fadeout_delay    If set to an integer or the special value 'auto', remove the message automatically after the specified duration (auto = calculate duration based on string length).
    6970* @author   Quinn Comendant <quinn@strangecode.com>
    7071* @version  1.0
    7172* @since    18 Mar 2014 23:24:15
    7273*/
    73 Strangecode.Msg.prototype.raise = function (message, msg_class) {
     74Strangecode.Msg.prototype.raise = function (message, msg_class, fadeout_delay) {
     75    var options = this.o;
    7476    // If an "above msg" is provided, and one doesn't already exist, add it to the message box.
    75     if (this.o.above_msg.length && !$(this.o.container).find('.sc-above').length) {
    76         $(this.o.container).append($('<div class="sc-js-msg sc-above"></div>').text(this.o.above_msg));
     77    if (options.above_msg.length && !$(options.container).find('.sc-above').length) {
     78        $(options.container).append($('<div class="sc-js-msg sc-above"></div>').text(options.above_msg));
    7779    }
    7880    // Use 'sc-msg-error' as default class string.
    7981    var msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class;
    8082    // Append this raised message to the sc-msg stack, if it doesn't exist already.
    81     if (!$(this.o.container).find(':contains("' + message + '")').length) {
    82         $(this.o.container).append($('<div data-alert class="alert-box sc-js-msg ' + msg_class + '"></div>').hide().html(message).append('<a href="#" class="close">×</a>'));
     83    var msghash = Strangecode.Msg.hash(message);
     84    if (!$(options.container).find('[data-msghash="' + msghash + '"]').length) {
     85        $(options.container).append($('<div data-alert class="alert-box sc-js-msg ' + msg_class + '" data-msghash="' + msghash + '"></div>').hide().html(message).append('<a href="#" class="close">×</a>'));
    8386        $('.sc-js-msg').slideDown('fast');
    8487        if ($.fn.foundation) {
    8588            $(document).foundation('reflow');
    8689        }
     90        var fadeout_delay = (typeof fadeout_delay === 'undefined') ? false : fadeout_delay;
     91        if (fadeout_delay) {
     92            if (fadeout_delay == 'auto') {
     93                var num_words = message.trim().split(/\s+/).length;
     94                // Average english reading speed is about 200 words per minute = 300ms per word.
     95                fadeout_delay = num_words * 300;
     96                fadeout_delay = fadeout_delay < 3000 ? 3000 : fadeout_delay;
     97            }
     98            setTimeout(function () {
     99                $(options.container).find('[data-msghash="' + msghash + '"]').slideUp('slow', function () {
     100                    $(this).remove();
     101                });
     102                if ($.fn.foundation) {
     103                    $(document).foundation('reflow');
     104                }
     105            }, fadeout_delay);
     106        }
    87107    }
    88108
    89     if (this.o.gotohash) {
     109    if (options.gotohash) {
    90110        $(document.body).animate({
    91             'scrollTop': $(this.o.container).offset().top
     111            'scrollTop': $(options.container).offset().top
    92112        }, 'fast');
    93113    }
     
    107127*/
    108128Strangecode.Msg.prototype.clear = function (msg_class) {
     129    var options = this.o;
    109130    var msg_class = (typeof msg_class === 'undefined') ? 'sc-js-msg' : msg_class;
    110     $(this.o.container).find('.' + msg_class).remove();
     131    $(options.container).find('.' + msg_class).remove();
    111132};
     133
     134// String hashing function based on https://stackoverflow.com/a/7616484
     135Strangecode.Msg.hash = function(str) {
     136    var hash = 0, i, chr;
     137    if (str.length === 0) return hash;
     138    for (i = 0; i < str.length; i++) {
     139        chr = str.charCodeAt(i);
     140        hash = ((hash << 5) - hash) + chr;
     141        hash |= 0;
     142    }
     143    return hash;
     144};
Note: See TracChangeset for help on using the changeset viewer.