PageSourceSearch

https://www.socialmotus.com/js/wl_Number.js

js socialmotus.com collected 2026-10-04 21:14:44 UTC 3,927 bytes, 144 lines download raw bytes

1/*----------------------------------------------------------------------*/
2/* wl_Number v 1.0 by revaxarts.com
3/* description: Make a Number field out of an input field
4/* dependency: mousewheel plugin 
5/*----------------------------------------------------------------------*/
6
7
8$.fn.wl_Number = function (method) {
9
10	var args = arguments;
11	return this.each(function () {
12
13		var $this = $(this);
14
15
16		if ($.fn.wl_Number.methods[method]) {
17			return $.fn.wl_Number.methods[method].apply(this, Array.prototype.slice.call(args, 1));
18		} else if (typeof method === 'object' || !method) {
19			if ($this.data('wl_Number')) {
20				var opts = $.extend({}, $this.data('wl_Number'), method);
21			} else {
22				var opts = $.extend({}, $.fn.wl_Number.defaults, method, $this.data());
23			}
24		} else {
25			$.error('Method "' + method + '" does not exist');
26		}
27
28
29		if (!$this.data('wl_Number')) {
30
31			$this.data('wl_Number', {});
32			
33			//fetch the nativ attributes
34			opts.min = $this.attr('min') || opts.min;
35			opts.max = $this.attr('max') || opts.max;
36			opts.step = $this.attr('step') || opts.step;
37
38			$this.bind({
39
40				//bind the mouswheel event
41				'mousewheel.wl_Number': function (event, delta) {
42					var opts = $this.data('wl_Number') || opts;
43					if (opts.mousewheel) {
44						event.preventDefault();
45						//delta must be 1 or -1 (different on macs and with shiftkey pressed)
46						delta = (delta < 0) ? -1 : 1;
47						//multiply with 10 if shift key is pressed
48						if (event.shiftKey) delta *= 10;
49						$.fn.wl_Number.methods.change.call($this[0], delta);
50					}
51				},
52
53
54				'change.wl_Number': function () {
55					var _this = $(this);
56					//correct the input
57					$.fn.wl_Number.methods.correct.call($this[0]);
58					//callback
59					_this.data('wl_Number').onChange.call($this[0], _this.val());
60				}
61
62			});
63
64		} else {
65
66		}
67
68		if (opts) $.extend($this.data('wl_Number'), opts);
69	});
70
71};
72
73$.fn.wl_Number.defaults = {
74	step: 1,
75	decimals: 0,
76	start: 0,
77	min: null,
78	max: null,
79	mousewheel: true,
80	onChange: function (value) {},
81	onError: function (value) {}
82};
83$.fn.wl_Number.version = '1.0';
84
85
86$.fn.wl_Number.methods = {
87	correct: function () {
88		var $this = $(this),
89			//replace ',' with '.' because in some countries comma is the separator
90			val = $this.val().replace(/,/g, '.');
91		if (val) $.fn.wl_Number.methods.printValue.call(this, parseFloat(val));
92	},
93	change: function (delta) {
94		var $this = $(this),
95			//the current value
96			_current = $this.val() || $this.data('wl_Number').start,
97			//calculate the new value
98			_new = parseFloat(_current, 10) + (delta * $this.data('wl_Number').step);
99		$.fn.wl_Number.methods.printValue.call(this, _new);
100		$this.trigger('change.wl_Number');
101	},
102	printValue: function (value) {
103		var $this = $(this),
104			opts = $this.data('wl_Number') || opts;
105
106		//is not a number
107		if (isNaN(value) && value != '') {
108			
109			//callback
110			opts.onError.call(this, $this.val());
111			//write '0', focus and select it
112			$this.val(0).focus().select();
113			$this.trigger('change.wl_Number');
114			return;
115		}
116
117		//don't go over min and max values
118		if (opts.min != null) value = Math.max(opts.min, value);
119		if (opts.max != null) value = Math.min(opts.max, value);
120		
121		//decimals? use parseFloat if yes or round the value if it is an integer (no decimals)
122		(opts.decimals) ? value = parseFloat(value, opts.decimals).toFixed(opts.decimals) : value = Math.round(value);
123		
124		//write value
125		$this.val(value);
126	},
127	set: function () {
128		var $this = $(this),
129			options = {};
130		if (typeof arguments[0] === 'object') {
131			options = arguments[0];
132		} else if (arguments[0] && arguments[1] !== undefined) {
133			options[arguments[0]] = arguments[1];
134		}
135		$.each(options, function (key, value) {
136			if ($.fn.wl_Number.defaults[key] !== undefined || $.fn.wl_Number.defaults[key] == null) {
137				$this.data('wl_Number')[key] = value;
138			} else {
139				$.error('Key "' + key + '" is not defined');
140			}
141		});
142
143	}
144};

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.