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:first', |
---|
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: ' + options.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 | * @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). |
---|
70 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
71 | * @version 1.0 |
---|
72 | * @since 18 Mar 2014 23:24:15 |
---|
73 | */ |
---|
74 | Strangecode.Msg.prototype.raise = function (message, msg_class, fadeout_delay) { |
---|
75 | var options = this.o; |
---|
76 | // If an "above msg" is provided, and one doesn't already exist, add it to the message box. |
---|
77 | if (options.above_msg.length && !$(options.container).find('.sc-above').length) { |
---|
78 | $(options.container).append($('<div class="sc-js-msg sc-above"></div>').text(options.above_msg)); |
---|
79 | } |
---|
80 | // Use 'sc-msg-error' as default class string. |
---|
81 | var msg_class = (typeof msg_class === 'undefined') ? 'sc-msg-error' : msg_class; |
---|
82 | // Append this raised message to the sc-msg stack, if it doesn't exist already. |
---|
83 | var msghash = Strangecode.Msg.hash(message); |
---|
84 | if (!$(options.container).find('[data-msghash="' + msghash + '"]').length) { |
---|
85 | $(options.container).append($('<div data-alert class="alert-box sc-js-msg ' + msg_class + '" data-msghash="' + msghash + '"></div>').hide().html(message).append('<a href="#" class="close">Ã</a>')).show(); |
---|
86 | $('.sc-js-msg').slideDown('fast'); |
---|
87 | if ($.fn.foundation) { |
---|
88 | $(document).foundation('reflow'); |
---|
89 | } |
---|
90 | var fadeout_delay = (typeof fadeout_delay === 'undefined') ? false : fadeout_delay; |
---|
91 | if (fadeout_delay) { |
---|
92 | if (fadeout_delay == 'auto') { |
---|
93 | var num_words = message.trim().split(/\s+/).length; |
---|
94 | // Average english reading speed is about 200 words per minute = 300ms per word. |
---|
95 | fadeout_delay = num_words * 300; |
---|
96 | fadeout_delay = fadeout_delay < 3000 ? 3000 : fadeout_delay; |
---|
97 | } |
---|
98 | setTimeout(function () { |
---|
99 | $(options.container).find('[data-msghash="' + msghash + '"]').slideUp('slow', function () { |
---|
100 | $(this).remove(); |
---|
101 | }); |
---|
102 | if ($.fn.foundation) { |
---|
103 | $(document).foundation('reflow'); |
---|
104 | } |
---|
105 | }, fadeout_delay); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | if (options.gotohash && $('html, body').scrollTop() > $(options.container).offset().top) { |
---|
110 | $('html, body').animate({ |
---|
111 | 'scrollTop': $(options.container).offset().top |
---|
112 | }, 'fast'); |
---|
113 | } |
---|
114 | }; |
---|
115 | |
---|
116 | /* |
---|
117 | * Removes all previously raised messages. By default, only messages raised by this JS class |
---|
118 | * are cleared (matching class="sc-js-msg"), but you can clear *all* messages by assigning a more general |
---|
119 | * class (e.g., "sc-msg-error"). |
---|
120 | * |
---|
121 | * @access public |
---|
122 | * @param string message The message to display. |
---|
123 | * @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) |
---|
124 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
125 | * @version 1.1 |
---|
126 | * @since 18 Mar 2014 23:24:15 |
---|
127 | */ |
---|
128 | Strangecode.Msg.prototype.clear = function (msg_class) { |
---|
129 | var options = this.o; |
---|
130 | var msg_class = (typeof msg_class === 'undefined') ? 'sc-js-msg' : msg_class; |
---|
131 | $(options.container).find('.' + msg_class).remove(); |
---|
132 | }; |
---|
133 | |
---|
134 | // String hashing function based on https://stackoverflow.com/a/7616484 |
---|
135 | Strangecode.Msg.hash = function(str) { |
---|
136 | var hash = 0, i, chr; |
---|
137 | if (str.length === 0) return hash; |
---|
138 | for (i = 0; i < str.length; i++) { |
---|
139 | chr = str.charCodeAt(i); |
---|
140 | hash = ((hash << 5) - hash) + chr; |
---|
141 | hash |= 0; |
---|
142 | } |
---|
143 | return hash; |
---|
144 | }; |
---|