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. |
---|
21 | var Strangecode = Strangecode || {}; |
---|
22 | |
---|
23 | /* |
---|
24 | * Class for managing user messages. This is the constructor 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 | --------------------------------------------------------------------- |
---|
38 | var sc_msg = new Strangecode.Msg({ |
---|
39 | container: '#sc-msg-formvalidator', |
---|
40 | above_msg: 'The following errors occurred:' |
---|
41 | }); |
---|
42 | sc_msg.raise('Oops, you fuxt up!', 'sc-msg-error'); |
---|
43 | sc_msg.clear(); |
---|
44 | sc_msg.raise('Ok, now youâre cool.', 'sc-msg-success'); |
---|
45 | --------------------------------------------------------------------- |
---|
46 | */ |
---|
47 | Strangecode.Msg = function (options) { |
---|
48 | // Merge options with defaults |
---|
49 | var options = this.o = $.extend({ |
---|
50 | container: '.sc-msg:last', |
---|
51 | above_msg: '', |
---|
52 | gotohash: true |
---|
53 | }, options || {}); |
---|
54 | |
---|
55 | document.addEventListener('DOMContentLoaded', function (e) { |
---|
56 | // Warn if the target doesn't exist. |
---|
57 | if (!$(options.container).length) { |
---|
58 | console.warn('Strangecode.Msg container not found: ' + this.o.container); |
---|
59 | } |
---|
60 | }); |
---|
61 | }; |
---|
62 | |
---|
63 | /* |
---|
64 | * Raises a message to the user. |
---|
65 | * |
---|
66 | * @access public |
---|
67 | * @param string message The message to display. |
---|
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 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
70 | * @version 1.0 |
---|
71 | * @since 18 Mar 2014 23:24:15 |
---|
72 | */ |
---|
73 | Strangecode.Msg.prototype.raise = function (message, msg_class) { |
---|
74 | // 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 | } |
---|
78 | // Use 'sc-msg-error' as default class string. |
---|
79 | var msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class; |
---|
80 | // 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 | $('.sc-js-msg').slideDown('fast'); |
---|
84 | if ($.fn.foundation) { |
---|
85 | $(document).foundation('reflow'); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | if (this.o.gotohash) { |
---|
90 | $(document.body).animate({ |
---|
91 | 'scrollTop': $(this.o.container).offset().top |
---|
92 | }, 'fast'); |
---|
93 | } |
---|
94 | }; |
---|
95 | |
---|
96 | /* |
---|
97 | * Removes all previously raised messages. By default, only messages raised by this JS class |
---|
98 | * are cleared (matching class="sc-js-msg"), but you can clear *all* messages by assigning a more general |
---|
99 | * class (e.g., "sc-msg-error"). |
---|
100 | * |
---|
101 | * @access public |
---|
102 | * @param string message The message to display. |
---|
103 | * @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) |
---|
104 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
105 | * @version 1.1 |
---|
106 | * @since 18 Mar 2014 23:24:15 |
---|
107 | */ |
---|
108 | Strangecode.Msg.prototype.clear = function (msg_class) { |
---|
109 | var msg_class = (typeof msg_class === 'undefined') ? 'sc-js-msg' : msg_class; |
---|
110 | $(this.o.container).find('.' + msg_class).remove(); |
---|
111 | }; |
---|