PageSourceSearch

https://sigrh.sp.gov.br/public/js/jquery.numeric.js

js sigrh.sp.gov.br collected 2026-09-27 20:57:41 UTC 11,441 bytes, 351 lines download raw bytes

vendor: 11,441 bytes, lines 1-351
1/*
2 *
3 * Copyright (c) 2006-2014 Sam Collett (http://www.texotela.co.uk)
4 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
5 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
6 *
7 * Version 1.4.1
8 * Demo: http://www.texotela.co.uk/code/jquery/numeric/
9 *
10 */
11(function(factory){
12	if(typeof define === 'function' && define.amd){
13		define(['jquery'], factory);
14	}else{
15        factory(window.jQuery);
16    }
17}(function($) {
18/*
19 * Allows only valid characters to be entered into input boxes.
20 * Note: fixes value when pasting via Ctrl+V, but not when using the mouse to paste
21  *      side-effect: Ctrl+A does not work, though you can still use the mouse to select (or double-click to select all)
22 *
23 * @name     numeric
24 * @param    config      { decimal : "." , negative : true }
25 * @param    callback     A function that runs if the number is not valid (fires onblur)
26 * @author   Sam Collett (http://www.texotela.co.uk)
27 * @example  $(".numeric").numeric();
28 * @example  $(".numeric").numeric(","); // use , as separator
29 * @example  $(".numeric").numeric({ decimal : "," }); // use , as separator
30 * @example  $(".numeric").numeric({ negative : false }); // do not allow negative values
31 * @example  $(".numeric").numeric({ decimalPlaces : 2 }); // only allow 2 decimal places
32 * @example  $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function
33 *
34 */
35$.fn.numeric = function(config, callback)
36{
37	if(typeof config === 'boolean')
38	{
39		config = { decimal: config, negative: true, decimalPlaces: -1 };
40	}
41	config = config || {};
42	// if config.negative undefined, set to true (default is to allow negative numbers)
43	if(typeof config.negative == "undefined") { config.negative = true; }
44	// set decimal point
45	var decimal = (config.decimal === false) ? "" : config.decimal || ".";
46	// allow negatives
47	var negative = (config.negative === true) ? true : false;
48    // set decimal places
49	var decimalPlaces = (typeof config.decimalPlaces == "undefined") ? -1 : config.decimalPlaces;
50	// callback function
51	callback = (typeof(callback) == "function" ? callback : function() {});
52	// set data and methods
53	return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).data("numeric.decimalPlaces", decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
54};
55
56$.fn.numeric.keypress = function(e)
57{
58	// get decimal character and determine if negatives are allowed
59	var decimal = $.data(this, "numeric.decimal");
60	var negative = $.data(this, "numeric.negative");
61    var decimalPlaces = $.data(this, "numeric.decimalPlaces");
62	// get the key that was pressed
63	var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
64	// allow enter/return key (only when in an input box)
65	if(key == 13 && this.nodeName.toLowerCase() == "input")
66	{
67		return true;
68	}
69	else if(key == 13)
70	{
71		return false;
72	}
73	//dont allow #, $, %
74	else if(key == 35 || key == 36 || key == 37){
75		return false;
76	}
77	var allow = false;
78	// allow Ctrl+A
79	if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) { return true; }
80	// allow Ctrl+X (cut)
81	if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) { return true; }
82	// allow Ctrl+C (copy)
83	if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) { return true; }
84	// allow Ctrl+Z (undo)
85	if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) { return true; }
86	// allow or deny Ctrl+V (paste), Shift+Ins
87	if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */ ||
88	  (e.shiftKey && key == 45)) { return true; }
89	// if a number was not pressed
90	if(key < 48 || key > 57)
91	{
92	  var value = $(this).val();
93		/* '-' only allowed at start and if negative numbers allowed */
94		if($.inArray('-', value.split('')) !== 0 && negative && key == 45 && (value.length === 0 || parseInt($.fn.getSelectionStart(this), 10) === 0)) { return true; }
95		/* only one decimal separator allowed */
96		if(decimal && key == decimal.charCodeAt(0) && $.inArray(decimal, value.split('')) != -1)
97		{
98			allow = false;
99		}
100		// check for other keys that have special purposes
101		if(
102			key != 8 /* backspace */ &&
103			key != 9 /* tab */ &&
104			key != 13 /* enter */ &&
105			key != 35 /* end */ &&
106			key != 36 /* home */ &&
107			key != 37 /* left */ &&
108			key != 39 /* right */ &&
109			key != 46 /* del */
110		)
111		{
112			allow = false;
113		}
114		else
115		{
116			// for detecting special keys (listed above)
117			// IE does not support 'charCode' and ignores them in keypress anyway
118			if(typeof e.charCode != "undefined")
119			{
120				// special keys have 'keyCode' and 'which' the same (e.g. backspace)
121				if(e.keyCode == e.which && e.which !== 0)
122				{
123					allow = true;
124					// . and delete share the same code, don't allow . (will be set to true later if it is the decimal point)
125					if(e.which == 46) { allow = false; }
126				}
127				// or keyCode != 0 and 'charCode'/'which' = 0
128				else if(e.keyCode !== 0 && e.charCode === 0 && e.which === 0)
129				{
130					allow = true;
131				}
132			}
133		}
134		// if key pressed is the decimal and it is not already in the field
135		if(decimal && key == decimal.charCodeAt(0))
136		{
137			if($.inArray(decimal, value.split('')) == -1)
138			{
139				allow = true;
140			}
141			else
142			{
143				allow = false;
144			}
145		}
146	}
147	else
148	{
149		allow = true;
150        // remove extra decimal places
151 		if(decimal && decimalPlaces > 0)
152 		{
153            var selectionStart = $.fn.getSelectionStart(this);
154            var selectionEnd = $.fn.getSelectionEnd(this);
155            var dot = $.inArray(decimal, $(this).val().split(''));
156            if (selectionStart === selectionEnd && dot >= 0 && selectionStart > dot && $(this).val().length > dot + decimalPlaces) {
157                allow = false;
158            }
159        }
160
161	}
162	return allow;
163};
164
165$.fn.numeric.keyup = function(e)
166{
167	var val = $(this).val();
168	if(val && val.length > 0)
169	{
170		// get carat (cursor) position
171		var carat = $.fn.getSelectionStart(this);
172		var selectionEnd = $.fn.getSelectionEnd(this);
173		// get decimal character and determine if negatives are allowed
174		var decimal = $.data(this, "numeric.decimal");
175		var negative = $.data(this, "numeric.negative");
176        var decimalPlaces = $.data(this, "numeric.decimalPlaces");
177
178		// prepend a 0 if necessary
179		if(decimal !== "" && decimal !== null)
180		{
181			// find decimal point
182			var dot = $.inArray(decimal, val.split(''));
183			// if dot at start, add 0 before
184			if(dot === 0)
185			{
186				this.value = "0" + val;
187				carat++;
188            			selectionEnd++;
189			}
190			// if dot at position 1, check if there is a - symbol before it
191			if(dot == 1 && val.charAt(0) == "-")
192			{
193				this.value = "-0" + val.substring(1);
194				carat++;
195            			selectionEnd++;
196			}
197			val = this.value;
198		}
199
200		// if pasted in, only allow the following characters
201		var validChars = [0,1,2,3,4,5,6,7,8,9,'-',decimal];
202		// get length of the value (to loop through)
203		var length = val.length;
204		// loop backwards (to prevent going out of bounds)
205		for(var i = length - 1; i >= 0; i--)
206		{
207			var ch = val.charAt(i);
208			// remove '-' if it is in the wrong place
209			if(i !== 0 && ch == "-")
210			{
211				val = val.substring(0, i) + val.substring(i + 1);
212			}
213			// remove character if it is at the start, a '-' and negatives aren't allowed
214			else if(i === 0 && !negative && ch == "-")
215			{
216				val = val.substring(1);
217			}
218			var validChar = false;
219			// loop through validChars
220			for(var j = 0; j < validChars.length; j++)
221			{
222				// if it is valid, break out the loop
223				if(ch == validChars[j])
224				{
225					validChar = true;
226					break;
227				}
228			}
229			// if not a valid character, or a space, remove
230			if(!validChar || ch == " ")
231			{
232				val = val.substring(0, i) + val.substring(i + 1);
233			}
234		}
235		// remove extra decimal characters
236		var firstDecimal = $.inArray(decimal, val.split(''));
237		if(firstDecimal > 0)
238		{
239			for(var k = length - 1; k > firstDecimal; k--)
240			{
241				var chch = val.charAt(k);
242				// remove decimal character
243				if(chch == decimal)
244				{
245					val = val.substring(0, k) + val.substring(k + 1);
246				}
247			}
248		}
249
250        // remove extra decimal places
251        if(decimal && decimalPlaces > 0)
252        {
253            var dot = $.inArray(decimal, val.split(''));
254            if (dot >= 0)
255            {
256                val = val.substring(0, dot + decimalPlaces + 1);
257                selectionEnd = Math.min(val.length, selectionEnd);
258            }
259        }
260		// set the value and prevent the cursor moving to the end
261		this.value = val;
262		$.fn.setSelection(this, [carat, selectionEnd]);
263	}
264};
265
266$.fn.numeric.blur = function()
267{
268	var decimal = $.data(this, "numeric.decimal");
269	var callback = $.data(this, "numeric.callback");
270	var negative = $.data(this, "numeric.negative");
271	var val = this.value;
272	if(val !== "")
273	{
274		var re = new RegExp("^" + (negative?"-?":"") + "\\d+$|^" + (negative?"-?":"") + "\\d*" + decimal + "\\d+$");
275		if(!re.exec(val))
276		{
277			callback.apply(this);
278		}
279	}
280};
281
282$.fn.removeNumeric = function()
283{
284	return this.data("numeric.decimal", null).data("numeric.negative", null).data("numeric.callback", null).data("numeric.decimalPlaces", null).unbind("keypress", $.fn.numeric.keypress).unbind("keyup", $.fn.numeric.keyup).unbind("blur", $.fn.numeric.blur);
285};
286
287// Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini <[email protected]>)
288$.fn.getSelectionStart = function(o)
289{
290	if(o.type === "number"){
291		return undefined;
292	}
293	else if (o.createTextRange && document.selection)
294	{
295        var r = document.selection.createRange().duplicate();
296        r.moveEnd('character', o.value.length);
297		if (r.text == '') return o.value.length;
298
299		return Math.max(0, o.value.lastIndexOf(r.text));
300	} else {
301        try { return o.selectionStart; }
302        catch(e) { return 0; }
303    }
304};
305
306// Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini <[email protected]>)
307$.fn.getSelectionEnd = function(o)
308{
309	if(o.type === "number"){
310		return undefined;
311	}
312	else if (o.createTextRange && document.selection) {
313		var r = document.selection.createRange().duplicate()
314		r.moveStart('character', -o.value.length)
315		return r.text.length
316	} else return o.selectionEnd
317}
318
319// set the selection, o is the object (input), p is the position ([start, end] or just start)
320$.fn.setSelection = function(o, p)
321{
322	// if p is number, start and end are the same
323	if(typeof p == "number") { p = [p, p]; }
324	// only set if p is an array of length 2
325	if(p && p.constructor == Array && p.length == 2)
326	{
327		if(o.type === "number") {
328			o.focus();
329		}
330		else if (o.createTextRange)
331		{
332			var r = o.createTextRange();
333			r.collapse(true);
334			r.moveStart('character', p[0]);
335			r.moveEnd('character', p[1] - p[0]);
336			r.select();
337		}
338		else {
339            o.focus();
340            try{
341                if(o.setSelectionRange)
342                {
343                    o.setSelectionRange(p[0], p[1]);
344                }
345            } catch(e) {
346            }
347        }
348	}
349};
350
351}));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.