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 | Emulates a sprintf function. Placeholders are {1}âŠ{N}. |
---|
25 | Some versions of this function were zero-indexed; this one is not. |
---|
26 | --------------------------------------------------------------------- |
---|
27 | "{1} is dead, but {2} is alive! {1} {3}".format("ASP", "ASP.NET") |
---|
28 | |
---|
29 | outputs |
---|
30 | |
---|
31 | ASP is dead, but ASP.NET is alive! ASP {3} |
---|
32 | --------------------------------------------------------------------- |
---|
33 | * |
---|
34 | * @access public |
---|
35 | * @param string multiple Strings to pass to the formatted string. |
---|
36 | * @author http://stackoverflow.com/a/4673436/277303 |
---|
37 | * @version 1.0 |
---|
38 | * @since 30 May 2014 18:02:39 |
---|
39 | */ |
---|
40 | if (!String.prototype.format) { |
---|
41 | String.prototype.format = function() { |
---|
42 | var args = arguments; |
---|
43 | return this.replace(/{(\d+)}/g, function(match, number) { |
---|
44 | return typeof args[number-1] != 'undefined' ? args[number-1] : match; |
---|
45 | }); |
---|
46 | }; |
---|
47 | } |
---|
48 | |
---|
49 | /* |
---|
50 | * Displays 'user at domain dot com' as 'user@domain.com'. |
---|
51 | --------------------------------------------------------------------- |
---|
52 | <span class="sc-email">user at domain dot com</span> |
---|
53 | <a href="mailto:user at domain dot com" class="sc-email">Email me</a> |
---|
54 | <script> |
---|
55 | $('.sc-email').nospam(); |
---|
56 | </script> |
---|
57 | --------------------------------------------------------------------- |
---|
58 | * |
---|
59 | * @access public |
---|
60 | * @version 2.0 |
---|
61 | * @since 30 Jun 2008 12:32:19 |
---|
62 | */ |
---|
63 | jQuery.fn.nospam = function() { |
---|
64 | return this.each(function(){ |
---|
65 | $(this).text($(this).text().replace(' at ', '@').replace(' dot ', '.')); |
---|
66 | if (this.href) { |
---|
67 | this.href = this.href.replace(' at ', '@').replace(' dot ', '.'); |
---|
68 | } |
---|
69 | }); |
---|
70 | }; |
---|
71 | |
---|
72 | /* |
---|
73 | * Encode html entities by specific mapping table. |
---|
74 | * Decode HTML by proxying content via an in-memory div, setting its inner text which jQuery automatically encodes. |
---|
75 | Then we pull the encoded contents back out. The div never exists on the page. |
---|
76 | --------------------------------------------------------------------- |
---|
77 | $('input').val(Strangecode.htmlEncode(string)); |
---|
78 | --------------------------------------------------------------------- |
---|
79 | |
---|
80 | @access public |
---|
81 | @version 2.0 |
---|
82 | @since 30 Jun 2013 |
---|
83 | */ |
---|
84 | Strangecode.htmlEncode = function (str) { |
---|
85 | var entityMap = { |
---|
86 | '&': '&', |
---|
87 | '<': '<', |
---|
88 | '>': '>', |
---|
89 | '"': '"', |
---|
90 | "'": ''', |
---|
91 | '/': '/', |
---|
92 | '`': '`' |
---|
93 | }; |
---|
94 | return String(str).replace(/[&<>"'\/`]/g, function (s) { |
---|
95 | return entityMap[s]; |
---|
96 | }); |
---|
97 | }; |
---|
98 | Strangecode.htmlDecode = function (str) { |
---|
99 | return $('<div>').html(str).text(); |
---|
100 | }; |
---|
101 | |
---|
102 | |
---|
103 | /* |
---|
104 | Returns a string with URL-unsafe characters removed. |
---|
105 | --------------------------------------------------------------------- |
---|
106 | var urlslug = $('.url').val().slug(); |
---|
107 | --------------------------------------------------------------------- |
---|
108 | * @access public |
---|
109 | * @version 1.0 |
---|
110 | * @since 30 Jun 2013 |
---|
111 | */ |
---|
112 | $.fn.slug = function() { |
---|
113 | str = this.text().trim().toLowerCase(); |
---|
114 | var from = 'áéÃóúà ÚìòùÀëïöÌÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃâêîÎûÃÃÃÃÃñçÃ@·/_,:;'; |
---|
115 | var to = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa------'; |
---|
116 | for (var i=0, l=from.length; i<l; i++) { |
---|
117 | str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); |
---|
118 | } |
---|
119 | return str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-'); |
---|
120 | }; |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | /* |
---|
125 | Remove rounding errors caused by representation of finite binary floating point numbers. |
---|
126 | --------------------------------------------------------------------- |
---|
127 | > (.1*.2) |
---|
128 | 0.020000000000000004 |
---|
129 | > (.1*.2).trim() |
---|
130 | 0.02 |
---|
131 | --------------------------------------------------------------------- |
---|
132 | * @access public |
---|
133 | * @version 1.0 |
---|
134 | * @since 24 Nov 2015 |
---|
135 | */ |
---|
136 | if (!Number.prototype.trim) { |
---|
137 | Number.prototype.trim = function (precision) { |
---|
138 | var precision = precision || 11; |
---|
139 | return Math.round(this * Math.pow(10, precision)) / Math.pow(10, precision); |
---|
140 | }; |
---|
141 | } |
---|
142 | |
---|
143 | /* |
---|
144 | Uppercase the first letter of string. |
---|
145 | --------------------------------------------------------------------- |
---|
146 | > 'hello world'.trim() |
---|
147 | Hello world |
---|
148 | --------------------------------------------------------------------- |
---|
149 | * @access public |
---|
150 | * @version 1.0 |
---|
151 | * @since 24 Nov 2015 |
---|
152 | */ |
---|
153 | if (!String.prototype.ucfirst) { |
---|
154 | String.prototype.ucfirst = function() { |
---|
155 | return this.charAt(0).toUpperCase() + this.slice(1); |
---|
156 | }; |
---|
157 | } |
---|
158 | |
---|