source: trunk/js/Msg.js @ 500

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

Many auth and crypto changes; various other bugfixes while working on pulso.

File size: 4.0 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// Codebase functions will be under the Strangecode namespace, unless they are added to the jQuery object for chaining.
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 occurred:'
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:last',
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        // $(this.o.container).append($('<div data-alert class="alert-box sc-js-msg ' + msg_class + '"></div>').hide().text(message).append('<a href="#" class="close">×</a>'));
77        // $('.sc-js-msg').slideDown('fast');
78    }
79
80    if (this.o.gotohash) {
81        $(document.body).animate({
82            'scrollTop': $(this.o.container).offset().top
83        }, 'fast');
84    }
85};
86
87/*
88* Removes all messages previously created by Msg.raise.
89*
90* @access   public
91* @param    string message  The message to display.
92* @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)
93* @author   Quinn Comendant <quinn@strangecode.com>
94* @version  1.0
95* @since    18 Mar 2014 23:24:15
96*/
97Strangecode.Msg.prototype.clear = function() {
98    // Append this raised message to the sc-msg stack.
99    var msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class;
100    $(this.o.container).find('.sc-js-msg').remove();
101};
Note: See TracBrowser for help on using the repository browser.