d72d40236f6309a4fc5a9653401d3ce5460f6619
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

1) (function (factory) {
2) 	if (typeof define === 'function' && define.amd) {
3) 		// AMD. Register as an anonymous module.
4) 		define(['jquery'], factory);
5) 	} else if (typeof module === 'object' && module.exports) {
6) 		// Node/CommonJS
7) 		module.exports = function( root, jQuery ) {
8) 			if ( jQuery === undefined ) {
9) 				// require('jQuery') returns a factory that requires window to
10) 				// build a jQuery instance, we normalize how we use modules
11) 				// that require this pattern but the window provided is a noop
12) 				// if it's defined (how jquery works)
13) 				if ( typeof window !== 'undefined' ) {
14) 					jQuery = require('jquery');
15) 				}
16) 				else {
17) 					jQuery = require('jquery')(root);
18) 				}
19) 			}
20) 			factory(jQuery);
21) 			return jQuery;
22) 		};
23) 	} else {
24) 		// Browser globals
25) 		factory(jQuery);
26) 	}
27) }(function ($) {
28) 	'use strict'
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

29) 	$.fn.numbercontrol = function (methodOrProps) {
jdarwood007 Fixes issues with destroy....

jdarwood007 authored 2 years ago

30) 		if (methodOrProps === 'destory' || (typeof methodOrProps === 'object' && methodOrProps.destroy !== undefined)) {
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

31) 			this.each(function () {
32) 				$(this).parent().children().each(function (index, value) {
33) 					var thisSelector = $(value);
34) 
jdarwood007 Fixes issues with destroy....

jdarwood007 authored 2 years ago

35) 					if (typeof options === 'object' && options.onBeforeDestoryInitialize !== undefined)
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

36) 						options.onBeforeDestoryInitialize(this);
37) 
38) 					if (!thisSelector.is('input'))
39) 						thisSelector.remove();
40) 
jdarwood007 Fixes issues with destroy....

jdarwood007 authored 2 years ago

41) 					if (typeof options === 'object' && options.onAfterDestoryInitialize !== undefined)
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

42) 						options.onAfterDestoryInitialize(this);
43) 				});
44) 
45) 				$(this).parent().removeClass().addClass('numberControlDestoryed');
jdarwood007 Fixes issues with destroy....

jdarwood007 authored 2 years ago

46) 			});
47) 
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

48) 			return this;
49) 		}
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

50) 
51) 		// Allow customizing the options.
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

52) 		var options = {
53) 			debug: false,
54) 			separator: '.',
55) 			type: 'number',
56) 			prependHtml: '<div class="input-group-prepend"><button class="btn btn-decrease btn-outline-secondary px-1"><span class="oi oi-minus" /></button></div>',
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

57) 			appendHtml: '<div class="input-group-append"><button class="btn btn-increase btn-outline-secondary px-1"><span class="oi oi-plus" /></button></div>',
58) 			inputParentCss: 'input-group numberControl',
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

59) 			inputParent: 'div',
60) 			inputCss: 'numberControlInput form-control px-1',
Jeremy D Make a good demo page to sh...

Jeremy D authored 2 years ago

61) 			bindButtonEvents: 'click tap touch touchstart',
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

62) 			keyboardLanguage: {
63) 				'UP' : '<span class="oi oi-chevron-top" />',
64) 				'DOWN' : '<span class="oi oi-chevron-bottom" />',
65) 				'INVERSE' : '<span class="oi oi-transfer" />',
66) 				'SEP' : '<span class="oi oi-media-record" />',
67) 			},
68) 			keyboardControl: {
69) 			},
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

70) 			buttons: [...Array(10).keys(), 'DELETE', 'CLEAR', 'DONE', 'CANCEL', 'UP', 'DOWN', 'SEP', 'INVERSE']
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

71) 		}
72) 		for (var option in methodOrProps) {
73) 			options[option] = methodOrProps[option]
74) 		}
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

75) 
76) 		function setNewValue(container, value)
77) 		{
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

78) 			if (options.onBeforeSetNewvalue !== undefined)
79) 				options.onBeforeSetNewvalue(this, event, container, value);
80) 
jdarwood007 Fixes issues with destroy....

jdarwood007 authored 2 years ago

81) 			// The Min value was hit with nothing in the container, which could most likely be a blank string that was converted to a int.
82) 			if (Number.MIN_VALUE == parseFloat(value) && $(container).val() == '')
83) 				return false;
84) 
Jeremy D Make a good demo page to sh...

Jeremy D authored 2 years ago

85) 			// No Nans.
86) 			if (options.type !== 'text' && isNaN(value))
87) 				value = 0;
88) 
89) 			if (options.type === 'text')
90) 				$(container).val(value);
91) 			else if (options.type === 'number')
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

92) 				$(container).val(parseInt(value));
93) 			else
94) 				$(container).val(parseFloat(value));
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

95) 
96) 			if (options.onAfterSetNewvalue !== undefined)
97) 				options.onAfterSetNewvalue(this, event, container, value);
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

98) 		}
99) 
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

100) 		// https://stackoverflow.com/questions/9553354/how-do-i-get-the-decimal-places-of-a-floating-point-number-in-javascript
101) 		function precision(a)
102) 		{
103) 			if (!isFinite(a))
104) 				return 0;
105) 
106) 			var e = 1, p = 0;
107) 			while (Math.round(a * e) / e !== a)
108) 			{
109) 				e *= 10;
110) 				p++;
111) 			}
112) 
113) 			return parseInt(p);
114) 		}
115) 
116) 		function FindPercision(v, p)
117) 		{
118) 			if (!isFinite(v))
119) 				return 0;
120) 			return parseInt(v).toString().length + p;
121) 		}
122) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

123) 		function findMinMaxValue()
124) 		{
125) 			var testValue;
126) 			for (var i=0; i < arguments.length; i++) {
127) 				testValue = arguments[i];
128) 				if (typeof testValue !== 'undefined' && !isNaN(testValue))
129) 				{
130) 					if (options.type === 'number' && parseInt(testValue) !== null)
131) 						return parseInt(testValue);
132) 					else if (parseFloat(testValue) !== null)
133) 						return parseFloat(testValue);
134) 					continue;
135) 				}
136) 			}
137) 			return 0;
138) 		}
139) 
140) 		// Bind to each input selector
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

141) 		this.each(function () {
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

142) 			if (options.onBeforeInitialized !== undefined)
143) 				options.onBeforeInitialized(this);
144) 
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

145) 			var $base = $(this);
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

146) 
147) 			// Some settings we either can pull in from a options, from standard attributes or defaults.
148) 			var $step = findMinMaxValue(parseFloat(options.step), parseFloat($base.attr('step')), 1);
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

149) 			var $percision = precision($step) + 1;
Jeremy D Make a good demo page to sh...

Jeremy D authored 2 years ago

150) 			var $minValue = findMinMaxValue(options.min, $base.attr('min'), Number.MIN_SAFE_INTEGER);
151) 			var $maxValue = findMinMaxValue(options.max, $base.attr('max'), Number.MAX_SAFE_INTEGER);
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

152) 
153) 			// Build the parent up. 
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

154) 			if (!$($base).parent().is('div') || !$($base).parent().hasClass('numberControlDestoryed')) {
155) 				$base.wrap('<' + options.inputParent + '></' + options.inputParent + '>');
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

156) 			}
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

157) 			var $parent = $base.parent(options.parentSelector);
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

158) 			$parent.removeClass().addClass(options.inputParentCss);
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

159) 
160) 			// Set the base.
161) 			$base.attr('type', options.type);
162) 			$base.addClass(options.inputCss);
163) 
164) 			// Wrap the buttons around.			
165) 			$base.before(options.prependHtml);
166) 			$base.after(options.appendHtml);
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

167) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

168) 			// Add the style to the body to cleanup input controls for number.
169) 			if (options.type == 'number' && !options.DisableNumberSpinStyleFix)
170) 				$('body').append('<style>' +
171) 							'.numberControlInput::-webkit-outer-spin-button,.numberControlInput::-webkit-inner-spin-button {' + 
172) 								'-webkit-appearance: none;' +
173) 							'}</style>'
174) 				);
175) 
176) 			// The decrease event.
177) 			var $decreaseButton = $parent.find('.btn-decrease');
178) 			$decreaseButton.on(options.bindButtonEvents, function (event) {
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

179) 				event.preventDefault();
180) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

181) 				if (options.onBeforeClickDecrease !== undefined)
182) 					options.onBeforeClickDecrease(this, event);
183) 				if ($base.val() > $minValue)
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

184) 					setNewValue($base, parseFloat(parseFloat($base.val()) - parseFloat($step)).toPrecision(FindPercision($base.val(), $percision)));
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

185) 				if (options.onAfterClickDecrease !== undefined)
186) 					options.onAfterClickDecrease(this, event);
187) 				if (options.debug)
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

188) 					console.log('numbercontrl: decreaseButton: Click', event, parseFloat($base.val()), $minValue, parseFloat($step));
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

189) 			});
190) 
191) 			// The increase event.
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

192) 			var $increaseButton = $parent.find('.btn-increase');
193) 			$increaseButton.on(options.bindButtonEvents, function (event) {
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

194) 				event.preventDefault();
195) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

196) 				if (options.onBeforeClickIncrease !== undefined)
197) 					options.onBeforeClickIncrease(this, event);
198) 				if ($base.val() < $maxValue)
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

199) 					setNewValue($base, parseFloat(parseFloat($base.val()) + parseFloat($step)).toPrecision(FindPercision($base.val(), $percision)));
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

200) 				if (options.onAfterClickIncrease !== undefined)
201) 					options.onAfterClickIncrease(this, event);
202) 				if (options.debug)
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

203) 					console.log('numbercontrl: increaseButton: Click', event, parseFloat($base.val()), $maxValue, parseFloat($step));
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

204) 			});
205) 
206) 			// The Popup Numberpad
207) 			if (!options.disableVirtualKeyboard)
208) 			{
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

209) 				var $KeyboardLayout;
210) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

211) 				if (options.onBeforeVirtualKeyboardInitalized !== undefined)
212) 					options.onBeforeVirtualKeyboardInitalized(this);
213) 
214) 				$base.on(options.bindButtonEvents, function (event) {
215) 					event.stopPropagation();
216) 
217) 					if (options.onBeforeVirtualKeyboardOpen !== undefined)
218) 						options.onBeforeVirtualKeyboardOpen(this);
219) 
220) 					var $location = options.virtualKeyboardAttachSelector ? $(options.virtualKeyboardAttachSelector) : $base;
221) 
222) 					if (options.keyboardLayout)
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

223) 						$KeyboardLayout = options.keyboardLayout;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

224) 					else
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

225) 						$KeyboardLayout = 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

226) 							'<div class="modal-dialog modal-dialog-centered" style="width: 250px;">' +
227) 								'<div class="modal-content">' +
228) 									'<table>' +
229) 										'<tr>' +
230) 											'<td colspan="4">{INPUTBOX}</td>' +
231) 										'</tr><tr>' +
232) 											'<td>{7}</td>' +
233) 											'<td>{8}</td>' +
234) 											'<td>{9}</td>' +
235) 											'<td>{DELETE}</td>' +
236) 										'</tr><tr>' +
237) 											'<td>{4}</td>' +
238) 											'<td>{5}</td>' +
239) 											'<td>{6}</td>' +
240) 											'<td>{CLEAR}</td>' +
241) 										'</tr><tr>' +
242) 											'<td>{1}</td>' +
243) 											'<td>{2}</td>' +
244) 											'<td>{3}</td>' +
245) 											'<td>{DONE}</td>' +
246) 										'</tr><tr>' +
247) 											'<td>{UP}</td>' +
248) 											'<td>{0}</td>' +
249) 											'<td>{DOWN}</td>' +
250) 											'<td>{CANCEL}</td>' +
251) 										'</tr>' +
252) 									'</table>' +
253) 								'</div>' +
254) 							'</div>'
255) 						;
256) 
257) 					// Fill out the input.
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

258) 					if (typeof options.keyboardControl['INPUTBOX'] === 'undefined')
259) 						options.keyboardControl['INPUTBOX'] = '<input class="numberControlVirtualNumPad numberControlVirtualNumPadINPUT form-control" type="text" readonly value="{VAL}"/>';
260) 					$KeyboardLayout = $KeyboardLayout.replace('{INPUTBOX}', options.keyboardControl['INPUTBOX'].replace('{VAL}', $base.val()).toString());
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

261) 
262) 					// Fill out all buttons.
263) 					$.each(options.buttons, function(i, v){
264) 						var LanguageBox = options.keyboardLanguage[v] ? options.keyboardLanguage[v] : v;
265) 
266) 						if (typeof options.keyboardControl[v] === 'undefined')
267) 							options.keyboardControl[v] = '<button class="numberControlVirtualNumPad numberControlVirtualNumPad' + v + ' btn btn-outline-secondary w-100" data-function="' + v + '">{' + v + '_LANG}</button>';
268) 						$KeyboardLayout = $KeyboardLayout.replace('{' + v + '}', options.keyboardControl[v].replace('{' + v + '_LANG}', LanguageBox));
269) 					});
270) 
271) 					// Attach the keyboard to the container.
272) 					$location.after('<div class="numberControlVirtualNumPad modal d-block">' + $KeyboardLayout + '</div>');
273) 					var $VirtualKeyboard = $parent.find('.numberControlVirtualNumPad');
274) 					var $VirtualKeyboardInput = $VirtualKeyboard.find('.numberControlVirtualNumPadINPUT');
275) 
276) 					// Bind the virtual Keyboard action.
277) 					$VirtualKeyboard.find('.numberControlVirtualNumPad').on(options.bindButtonEvents, function(event){						
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

278) 						event.preventDefault();
279) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

280) 						if (options.debug) console.log('numbercontrl: numberControlVirtualNumPad: Click', event, $base.val(), $VirtualKeyboardInput.val().toString(), $(this).attr('data-function'));
281) 
282) 						var thisFunction = $(this).attr('data-function');
283) 
284) 						if (options.onBeforeVirtualKeyboardButton !== undefined)
285) 							options.onBeforeVirtualKeyboardButton(this, event, thisFunction);
286) 
287) 						switch (thisFunction)
288) 						{
289) 							case 'DELETE':
290) 								$VirtualKeyboardInput.val($VirtualKeyboardInput.val().toString().slice(0, -1));
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

291) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

292) 							
293) 							case 'CLEAR':
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

294) 								$VirtualKeyboardInput.val('');
295) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

296) 							
297) 							case 'DONE':
298) 								if ($VirtualKeyboardInput.val() > $maxValue)
299) 									setNewValue($base, $maxValue);
300) 								else if ($VirtualKeyboardInput.val() < $minValue)
301) 									setNewValue($base, $minValue);
302) 								else
303) 									setNewValue($base, $VirtualKeyboardInput.val());
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

304) 								
305) 								$VirtualKeyboard.remove();
306) 								break;
307) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

308) 							case 'CANCEL':
309) 								$VirtualKeyboard.remove();
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

310) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

311) 							
312) 							case 'UP':
313) 								if ($VirtualKeyboardInput.val() < $maxValue)
314) 									setNewValue($VirtualKeyboardInput, parseFloat($VirtualKeyboardInput.val()) + parseFloat($step));
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

315) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

316) 							
317) 							case 'DOWN':
318) 								if ($VirtualKeyboardInput.val() > $minValue)
319) 									setNewValue($VirtualKeyboardInput, parseFloat($VirtualKeyboardInput.val()) - parseFloat($step));
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

320) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

321) 
322) 							case 'SEP':
323) 								if ($VirtualKeyboardInput.val().toString().indexOf(options.separator) === -1)
324) 									$VirtualKeyboardInput.val($VirtualKeyboardInput.val().toString() + options.separator);
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

325) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

326) 
327) 							case 'INVERSE':
328) 								setNewValue($VirtualKeyboardInput, parseFloat($VirtualKeyboardInput.val()) * -1);
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

329) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

330) 														
331) 							// Default to assume its numbers.
332) 							default:
333) 								if ($(this).attr('data-custom-function'))
334) 									$(this).attr('data-custom-function')(this, event, thisFunction);
Jeremy D Make a good demo page to sh...

Jeremy D authored 2 years ago

335) 								else if ($VirtualKeyboardInput.val() == '0' || $VirtualKeyboardInput.val() == '0.000')
336) 									setNewValue($VirtualKeyboardInput, $(this).attr('data-function'));
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

337) 								else
338) 									setNewValue($VirtualKeyboardInput, $VirtualKeyboardInput.val().toString() + $(this).attr('data-function'));
339) 						}
340) 
341) 						if (options.onAfterVirtualKeyboardButton !== undefined)
342) 							options.onAfterVirtualKeyboardButton(this, event, thisFunction);
343) 					});
344) 
345) 					if (options.onAfterVirtualKeyboardOpen !== undefined)
346) 						options.onAfterVirtualKeyboardOpen(this);
347) 				});
348) 
349) 				if (options.onAfterVirtualKeyboardInitalized !== undefined)
350) 					options.onAfterVirtualKeyboardInitalized(this);
351) 			}
352) 
353) 			if (options.onAfterInitialized !== undefined)
354) 				options.onAfterInitialized(this);
355) 			if (options.debug) console.log($base.parent());
356) 		});