[496] | 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 | |
---|
[497] | 20 | // Codebase functions will be under the Strangecode namespace, unless they are added to the jQuery object for chaining. |
---|
[496] | 21 | var Strangecode = Strangecode || {}; |
---|
| 22 | |
---|
| 23 | /* |
---|
[590] | 24 | Emulates a sprintf function. Placeholders are {1}âŠ{N}. |
---|
| 25 | Some versions of this function were zero-indexed; this one is not. |
---|
[496] | 26 | --------------------------------------------------------------------- |
---|
[590] | 27 | "{1} is dead, but {2} is alive! {1} {3}".format("ASP", "ASP.NET") |
---|
[496] | 28 | |
---|
| 29 | outputs |
---|
| 30 | |
---|
[590] | 31 | ASP is dead, but ASP.NET is alive! ASP {3} |
---|
[496] | 32 | --------------------------------------------------------------------- |
---|
| 33 | * |
---|
| 34 | * @access public |
---|
| 35 | * @param string multiple Strings to pass to the formatted string. |
---|
[527] | 36 | * @author http://stackoverflow.com/a/4673436/277303 |
---|
[496] | 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) { |
---|
[500] | 44 | return typeof args[number-1] != 'undefined' ? args[number-1] : match; |
---|
[496] | 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) { |
---|
[654] | 67 | this.href = decodeURIComponent(this.href).replace(' at ', '@').replace(' dot ', '.'); |
---|
[496] | 68 | } |
---|
| 69 | }); |
---|
| 70 | }; |
---|
[497] | 71 | |
---|
| 72 | /* |
---|
[552] | 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. |
---|
[497] | 75 | Then we pull the encoded contents back out. The div never exists on the page. |
---|
| 76 | --------------------------------------------------------------------- |
---|
[552] | 77 | $('input').val(Strangecode.htmlEncode(string)); |
---|
[497] | 78 | --------------------------------------------------------------------- |
---|
[552] | 79 | |
---|
| 80 | @access public |
---|
| 81 | @version 2.0 |
---|
| 82 | @since 30 Jun 2013 |
---|
[497] | 83 | */ |
---|
[552] | 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 | }); |
---|
[497] | 97 | }; |
---|
[552] | 98 | Strangecode.htmlDecode = function (str) { |
---|
| 99 | return $('<div>').html(str).text(); |
---|
[506] | 100 | }; |
---|
[525] | 101 | |
---|
[654] | 102 | /* |
---|
| 103 | * Set the user's timezone in a cookie (which is used in Codebase's $app->start() method). |
---|
| 104 | --------------------------------------------------------------------- |
---|
| 105 | Strangecode.setTimezoneCookie() |
---|
| 106 | --------------------------------------------------------------------- |
---|
| 107 | * @access public |
---|
| 108 | * @version 1.0 |
---|
| 109 | * @since 24 Jan 2019 |
---|
| 110 | */ |
---|
| 111 | Strangecode.setTimezoneCookie = function () { |
---|
| 112 | try { |
---|
| 113 | // use Intl API when available and returning valid time zone |
---|
| 114 | var tz = Intl.DateTimeFormat().resolvedOptions().timeZone; |
---|
| 115 | } catch (e) { |
---|
| 116 | // Intl unavailable, fall back to manual guessing. |
---|
| 117 | tz = (new Date().getTimezoneOffset()/-60); |
---|
| 118 | } |
---|
| 119 | if ('' != tz) { |
---|
| 120 | document.cookie = 'tz=' + tz + ';path=/;max-age=86400'; |
---|
| 121 | } |
---|
| 122 | } |
---|
[525] | 123 | |
---|
| 124 | /* |
---|
[552] | 125 | Returns a string with URL-unsafe characters removed. |
---|
[525] | 126 | --------------------------------------------------------------------- |
---|
| 127 | var urlslug = $('.url').val().slug(); |
---|
| 128 | --------------------------------------------------------------------- |
---|
| 129 | * @access public |
---|
| 130 | * @version 1.0 |
---|
| 131 | * @since 30 Jun 2013 |
---|
| 132 | */ |
---|
| 133 | $.fn.slug = function() { |
---|
| 134 | str = this.text().trim().toLowerCase(); |
---|
| 135 | var from = 'áéÃóúà ÚìòùÀëïöÌÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃâêîÎûÃÃÃÃÃñçÃ@·/_,:;'; |
---|
| 136 | var to = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa------'; |
---|
| 137 | for (var i=0, l=from.length; i<l; i++) { |
---|
| 138 | str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); |
---|
| 139 | } |
---|
| 140 | return str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-'); |
---|
| 141 | }; |
---|
[552] | 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | /* |
---|
| 146 | Remove rounding errors caused by representation of finite binary floating point numbers. |
---|
| 147 | --------------------------------------------------------------------- |
---|
| 148 | > (.1*.2) |
---|
| 149 | 0.020000000000000004 |
---|
| 150 | > (.1*.2).trim() |
---|
| 151 | 0.02 |
---|
| 152 | --------------------------------------------------------------------- |
---|
| 153 | * @access public |
---|
| 154 | * @version 1.0 |
---|
| 155 | * @since 24 Nov 2015 |
---|
| 156 | */ |
---|
| 157 | if (!Number.prototype.trim) { |
---|
| 158 | Number.prototype.trim = function (precision) { |
---|
| 159 | var precision = precision || 11; |
---|
| 160 | return Math.round(this * Math.pow(10, precision)) / Math.pow(10, precision); |
---|
| 161 | }; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /* |
---|
| 165 | Uppercase the first letter of string. |
---|
| 166 | --------------------------------------------------------------------- |
---|
| 167 | > 'hello world'.trim() |
---|
| 168 | Hello world |
---|
| 169 | --------------------------------------------------------------------- |
---|
| 170 | * @access public |
---|
| 171 | * @version 1.0 |
---|
| 172 | * @since 24 Nov 2015 |
---|
| 173 | */ |
---|
| 174 | if (!String.prototype.ucfirst) { |
---|
| 175 | String.prototype.ucfirst = function() { |
---|
| 176 | return this.charAt(0).toUpperCase() + this.slice(1); |
---|
| 177 | }; |
---|
| 178 | } |
---|
| 179 | |
---|