- Timestamp:
- Sep 29, 2018 6:50:13 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/js/Msg.js
r622 r636 48 48 // Merge options with defaults 49 49 var options = this.o = $.extend({ 50 container: '.sc-msg: last',50 container: '.sc-msg:first', 51 51 above_msg: '', 52 52 gotohash: true … … 67 67 * @param string message The message to display. 68 68 * @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). 69 70 * @author Quinn Comendant <quinn@strangecode.com> 70 71 * @version 1.0 71 72 * @since 18 Mar 2014 23:24:15 72 73 */ 73 Strangecode.Msg.prototype.raise = function (message, msg_class) { 74 Strangecode.Msg.prototype.raise = function (message, msg_class, fadeout_delay) { 75 var options = this.o; 74 76 // 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)); 77 79 } 78 80 // Use 'sc-msg-error' as default class string. 79 81 var msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class; 80 82 // 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>')); 83 86 $('.sc-js-msg').slideDown('fast'); 84 87 if ($.fn.foundation) { 85 88 $(document).foundation('reflow'); 86 89 } 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 } 87 107 } 88 108 89 if ( this.o.gotohash) {109 if (options.gotohash) { 90 110 $(document.body).animate({ 91 'scrollTop': $( this.o.container).offset().top111 'scrollTop': $(options.container).offset().top 92 112 }, 'fast'); 93 113 } … … 107 127 */ 108 128 Strangecode.Msg.prototype.clear = function (msg_class) { 129 var options = this.o; 109 130 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(); 111 132 }; 133 134 // String hashing function based on https://stackoverflow.com/a/7616484 135 Strangecode.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.