/* * The Strangecode Codebase - a general application development framework for PHP * For details visit the project site: * Copyright © 2014 Strangecode, LLC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ // Codebase functions will be under the Strangecode namespace, unless they are added to the jQuery object for chaining. var Strangecode = Strangecode || {}; /* * Class for managing user messages. This is the constructor function. * * @access public * @param object containing object options: { container: '#sc-msg-formvalidator', above_msg: 'Oops!' } * @author Quinn Comendant * @version 1.0 * @since 18 Mar 2014 23:24:15 * * Example of use: --------------------------------------------------------------------- var sc_msg = new Strangecode.Msg({ container: '#sc-msg-formvalidator', above_msg: 'The following errors occurred:' }); sc_msg.raise('Oops, you fuxt up!', 'sc-msg-error'); sc_msg.clear(); sc_msg.raise('Ok, now you’re cool.', 'sc-msg-success'); --------------------------------------------------------------------- */ Strangecode.Msg = function (options) { // Merge options with defaults var options = this.o = $.extend({ container: '.sc-msg:first', above_msg: '', gotohash: true }, options || {}); document.addEventListener('DOMContentLoaded', function (e) { // Warn if the target doesn't exist. if (!$(options.container).length) { console.warn('Strangecode.Msg container not found: ' + options.container); } }); }; /* * Raises a message to the user. * * @access public * @param string message The message to display. * @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) * @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). * @author Quinn Comendant * @version 1.0 * @since 18 Mar 2014 23:24:15 */ Strangecode.Msg.prototype.raise = function (message, msg_class, fadeout_delay) { var options = this.o; // If an "above msg" is provided, and one doesn't already exist, add it to the message box. if (options.above_msg.length && !$(options.container).find('.sc-above').length) { $(options.container).append($('
').text(options.above_msg)); } // Use 'sc-msg-error' as default class string. var msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class; // Append this raised message to the sc-msg stack, if it doesn't exist already. var msghash = Strangecode.Msg.hash(message); if (!$(options.container).find('[data-msghash="' + msghash + '"]').length) { $(options.container).append($('
').hide().html(message).append('×')); $('.sc-js-msg').slideDown('fast'); if ($.fn.foundation) { $(document).foundation('reflow'); } var fadeout_delay = (typeof fadeout_delay === 'undefined') ? false : fadeout_delay; if (fadeout_delay) { if (fadeout_delay == 'auto') { var num_words = message.trim().split(/\s+/).length; // Average english reading speed is about 200 words per minute = 300ms per word. fadeout_delay = num_words * 300; fadeout_delay = fadeout_delay < 3000 ? 3000 : fadeout_delay; } setTimeout(function () { $(options.container).find('[data-msghash="' + msghash + '"]').slideUp('slow', function () { $(this).remove(); }); if ($.fn.foundation) { $(document).foundation('reflow'); } }, fadeout_delay); } } if (options.gotohash) { $(document.body).animate({ 'scrollTop': $(options.container).offset().top }, 'fast'); } }; /* * Removes all previously raised messages. By default, only messages raised by this JS class * are cleared (matching class="sc-js-msg"), but you can clear *all* messages by assigning a more general * class (e.g., "sc-msg-error"). * * @access public * @param string message The message to display. * @param string class Remove message divs matching this class (default: sc-js-msg; or use one of: sc-msg-success, sc-msg-notice, sc-msg-warning, sc-msg-error) * @author Quinn Comendant * @version 1.1 * @since 18 Mar 2014 23:24:15 */ Strangecode.Msg.prototype.clear = function (msg_class) { var options = this.o; var msg_class = (typeof msg_class === 'undefined') ? 'sc-js-msg' : msg_class; $(options.container).find('.' + msg_class).remove(); }; // String hashing function based on https://stackoverflow.com/a/7616484 Strangecode.Msg.hash = function(str) { var hash = 0, i, chr; if (str.length === 0) return hash; for (i = 0; i < str.length; i++) { chr = str.charCodeAt(i); hash = ((hash << 5) - hash) + chr; hash |= 0; } return hash; };