source: trunk/js/Msg.js @ 614

Last change on this file since 614 was 552, checked in by anonymous, 9 years ago

Updated js.

File size: 4.1 KB
RevLine 
[469]1/*
2* The Strangecode Codebase - a general application development framework for PHP
[496]3* For details visit the project site: <http://trac.strangecode.com/codebase/>
[469]4* Copyright © 2014 Strangecode, LLC
5*
6* This program is free software: you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18*/
19
[497]20// Codebase functions will be under the Strangecode namespace, unless they are added to the jQuery object for chaining.
[469]21var Strangecode = Strangecode || {};
22
23/*
[551]24* Class for managing user messages. This is the constructor function.
[469]25*
26* @access   public
27* @param    object containing object options:
28{
29    container: '#sc-msg-formvalidator',
30    above_msg: 'Oops!'
31}
32* @author   Quinn Comendant <quinn@strangecode.com>
33* @version  1.0
34* @since    18 Mar 2014 23:24:15
35*
36* Example of use:
37---------------------------------------------------------------------
38var sc_msg = new Strangecode.Msg({
39    container: '#sc-msg-formvalidator',
[497]40    above_msg: 'The following errors occurred:'
[469]41});
42sc_msg.raise('Oops, you fuxt up!', 'sc-msg-error');
43sc_msg.clear();
44sc_msg.raise('Ok, now you’re cool.', 'sc-msg-success');
45---------------------------------------------------------------------
46*/
[552]47Strangecode.Msg = function (options) {
[469]48    // Merge options with defaults
49    this.o = $.extend({
[552]50        container: '.sc-msg:last',
[469]51        above_msg: '',
52        gotohash: true
53    }, options || {});
54};
55
56/*
57* Raises a message to the user.
58*
59* @access   public
60* @param    string message  The message to display.
61* @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)
62* @author   Quinn Comendant <quinn@strangecode.com>
63* @version  1.0
64* @since    18 Mar 2014 23:24:15
65*/
[552]66Strangecode.Msg.prototype.raise = function (message, msg_class) {
[469]67    // If an "above msg" is provided, and one doesn't already exist, add it to the message box.
68    if (this.o.above_msg.length && !$(this.o.container).find('.sc-above').length) {
[473]69        $(this.o.container).append($('<div class="sc-js-msg sc-above"></div>').text(this.o.above_msg));
[469]70    }
71    // Use 'sc-msg-error' as default class string.
72    msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class;
73    // Append this raised message to the sc-msg stack, if it doesn't exist already.
74    if (!$(this.o.container).find(':contains("' + message + '")').length) {
[552]75        $(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>'));
76        $('.sc-js-msg').slideDown('fast');
77        if ($.fn.foundation) {
78            $(document).foundation('reflow');
79        }
[469]80    }
81
82    if (this.o.gotohash) {
83        $(document.body).animate({
84            'scrollTop': $(this.o.container).offset().top
85        }, 'fast');
86    }
87};
88
89/*
[552]90* Removes all previously raised messages. By default, only messages raised by this JS class
91* are cleared (matching class="sc-js-msg"), but you can clear *all* messages by assigning a more general
92* class (e.g., "sc-msg-error").
[469]93*
94* @access   public
95* @param    string message  The message to display.
[552]96* @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)
[469]97* @author   Quinn Comendant <quinn@strangecode.com>
[552]98* @version  1.1
[469]99* @since    18 Mar 2014 23:24:15
100*/
[552]101Strangecode.Msg.prototype.clear = function (msg_class) {
102    var msg_class = (typeof msg_class === 'undefined') ? 'sc-js-msg' : msg_class;
103    $(this.o.container).find('.' + msg_class).remove();
[469]104};
Note: See TracBrowser for help on using the repository browser.