source: trunk/js/Msg.js @ 496

Last change on this file since 496 was 496, checked in by anonymous, 10 years ago

Preparing for v2.2. This version will be less conservative towards backwards compatability. Users of codebase <= 2.1.8 or trunk installed before 07 Sept 2014 should use /tags/2.1.8, otherwise upgrade their implementation.

File size: 3.7 KB
Line 
1/*
2* The Strangecode Codebase - a general application development framework for PHP
3* For details visit the project site: <http://trac.strangecode.com/codebase/>
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
20// All Codebase functions will be under the Strangecode namespace.
21var Strangecode = Strangecode || {};
22
23/*
24* Class for managing user messages. This is the contructor function.
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',
40    above_msg: 'The following errors occured:'
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*/
47Strangecode.Msg = function(options) {
48    // Merge options with defaults
49    this.o = $.extend({
50        container: '#sc-msg',
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*/
66Strangecode.Msg.prototype.raise = function(message, msg_class) {
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) {
69        $(this.o.container).append($('<div class="sc-js-msg sc-above"></div>').text(this.o.above_msg));
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) {
75        $(this.o.container).append($('<div class="sc-js-msg ' + msg_class + '"></div>').text(message)).show();
76    }
77
78    if (this.o.gotohash) {
79        $(document.body).animate({
80            'scrollTop': $(this.o.container).offset().top
81        }, 'fast');
82    }
83};
84
85/*
86* Removes all messages previously created by Msg.raise.
87*
88* @access   public
89* @param    string message  The message to display.
90* @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)
91* @author   Quinn Comendant <quinn@strangecode.com>
92* @version  1.0
93* @since    18 Mar 2014 23:24:15
94*/
95Strangecode.Msg.prototype.clear = function() {
96    // Append this raised message to the sc-msg stack.
97    var msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class;
98    $(this.o.container).find('.sc-js-msg').remove();
99};
Note: See TracBrowser for help on using the repository browser.