3fe3caf22b927be80f9791542661bcf9b134939d
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',
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

61) 			bindButtonEvents: 'click tap touch touchstart focus',
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) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

85) 			if (options.type === 'number')
86) 				$(container).val(parseInt(value));
87) 			else
88) 				$(container).val(parseFloat(value));
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

89) 
90) 			if (options.onAfterSetNewvalue !== undefined)
91) 				options.onAfterSetNewvalue(this, event, container, value);
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

92) 		}
93) 
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

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

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

136) 			if (options.onBeforeInitialized !== undefined)
137) 				options.onBeforeInitialized(this);
138) 
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

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

jdarwood007 authored 2 years ago

143) 			var $percision = precision($step) + 1;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

144) 			var $minValue = findMinMaxValue(options.min, $base.attr('min'), Number.MIN_VALUE);
145) 			var $maxValue = findMinMaxValue(options.max, $base.attr('max'), Number.MAX_VALUE);
146) 
147) 			// Build the parent up. 
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

150) 			}
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

153) 
154) 			// Set the base.
155) 			$base.attr('type', options.type);
156) 			$base.addClass(options.inputCss);
157) 
158) 			// Wrap the buttons around.			
159) 			$base.before(options.prependHtml);
160) 			$base.after(options.appendHtml);
jdarwood007 Destroy the plugin better....

jdarwood007 authored 3 years ago

161) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

173) 				event.preventDefault();
174) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

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

jdarwood007 authored 2 years ago

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

jdarwood007 authored 3 years ago

179) 				if (options.onAfterClickDecrease !== undefined)
180) 					options.onAfterClickDecrease(this, event);
181) 				if (options.debug)
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

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

jdarwood007 authored 3 years ago

183) 			});
184) 
185) 			// The increase event.
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

188) 				event.preventDefault();
189) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

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

jdarwood007 authored 2 years ago

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

jdarwood007 authored 3 years ago

194) 				if (options.onAfterClickIncrease !== undefined)
195) 					options.onAfterClickIncrease(this, event);
196) 				if (options.debug)
jdarwood007 Fixed a issue with precisio...

jdarwood007 authored 2 years ago

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

jdarwood007 authored 3 years ago

198) 			});
199) 
200) 			// The Popup Numberpad
201) 			if (!options.disableVirtualKeyboard)
202) 			{
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

203) 				var $KeyboardLayout;
204) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

205) 				if (options.onBeforeVirtualKeyboardInitalized !== undefined)
206) 					options.onBeforeVirtualKeyboardInitalized(this);
207) 
208) 				$base.on(options.bindButtonEvents, function (event) {
209) 					event.stopPropagation();
210) 
211) 					if (options.onBeforeVirtualKeyboardOpen !== undefined)
212) 						options.onBeforeVirtualKeyboardOpen(this);
213) 
214) 					var $location = options.virtualKeyboardAttachSelector ? $(options.virtualKeyboardAttachSelector) : $base;
215) 
216) 					if (options.keyboardLayout)
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

219) 						$KeyboardLayout = 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

272) 						event.preventDefault();
273) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

285) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

286) 							
287) 							case 'CLEAR':
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

288) 								$VirtualKeyboardInput.val('');
289) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

290) 							
291) 							case 'DONE':
292) 								if ($VirtualKeyboardInput.val() > $maxValue)
293) 									setNewValue($base, $maxValue);
294) 								else if ($VirtualKeyboardInput.val() < $minValue)
295) 									setNewValue($base, $minValue);
296) 								else
297) 									setNewValue($base, $VirtualKeyboardInput.val());
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

298) 								
299) 								$VirtualKeyboard.remove();
300) 								break;
301) 
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

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

jdarwood007 authored 3 years ago

304) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

305) 							
306) 							case 'UP':
307) 								if ($VirtualKeyboardInput.val() < $maxValue)
308) 									setNewValue($VirtualKeyboardInput, parseFloat($VirtualKeyboardInput.val()) + parseFloat($step));
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

309) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

310) 							
311) 							case 'DOWN':
312) 								if ($VirtualKeyboardInput.val() > $minValue)
313) 									setNewValue($VirtualKeyboardInput, parseFloat($VirtualKeyboardInput.val()) - parseFloat($step));
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

314) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

315) 
316) 							case 'SEP':
317) 								if ($VirtualKeyboardInput.val().toString().indexOf(options.separator) === -1)
318) 									$VirtualKeyboardInput.val($VirtualKeyboardInput.val().toString() + options.separator);
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

319) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

320) 
321) 							case 'INVERSE':
322) 								setNewValue($VirtualKeyboardInput, parseFloat($VirtualKeyboardInput.val()) * -1);
jdarwood007 Cleanup, enforce style and...

jdarwood007 authored 3 years ago

323) 								break;
jdarwood007 Initial commit

jdarwood007 authored 3 years ago

324) 														
325) 							// Default to assume its numbers.
326) 							default:
327) 								if ($(this).attr('data-custom-function'))
328) 									$(this).attr('data-custom-function')(this, event, thisFunction);
329) 								else
330) 									setNewValue($VirtualKeyboardInput, $VirtualKeyboardInput.val().toString() + $(this).attr('data-function'));
331) 						}
332) 
333) 						if (options.onAfterVirtualKeyboardButton !== undefined)
334) 							options.onAfterVirtualKeyboardButton(this, event, thisFunction);
335) 					});
336) 
337) 					if (options.onAfterVirtualKeyboardOpen !== undefined)
338) 						options.onAfterVirtualKeyboardOpen(this);
339) 				});
340) 
341) 				if (options.onAfterVirtualKeyboardInitalized !== undefined)
342) 					options.onAfterVirtualKeyboardInitalized(this);
343) 			}
344) 
345) 			if (options.onAfterInitialized !== undefined)
346) 				options.onAfterInitialized(this);
347) 			if (options.debug) console.log($base.parent());
348) 		});