-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor Numeric Field
More file actions
124 lines (108 loc) · 3.71 KB
/
Copy pathEditor Numeric Field
File metadata and controls
124 lines (108 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery', 'datatables', 'datatables-editor'], factory);
}
else if (typeof exports === 'object') {
// Node / CommonJS
module.exports = function ($, dt) {
if (!$) { $ = require('jquery'); }
factory($, dt || $.fn.dataTable || require('datatables'));
};
}
else if (jQuery) {
// Browser standard
factory(jQuery, jQuery.fn.dataTable);
}
}(function ($, DataTable) {
'use strict';
if (!DataTable.ext.editorFields) {
DataTable.ext.editorFields = {};
}
var _fieldTypes = DataTable.Editor ?
DataTable.Editor.fieldTypes :
DataTable.ext.editorFields;
_fieldTypes.numeric = {
create: function (conf) {
conf._input = $('<input/>').attr($.extend({
id: DataTable.Editor.safeId(conf.id),
type: 'text'
}, conf.attr || {}));
conf._input.on("focus", function () { $(this).select(); });
conf._input.on("keydown", function (evtObj) {
return keydownHandler(evtObj);
});
conf._input.on("paste drop", function (evtObj) {
// event handler for drop and paste
// strip the number of all characters that are not part of the actual number
let paste = evtObj.clipboardData || window.clipboardData || evtObj.originalEvent.clipboardData || event.dataTransfer;
let str = paste.getData("text").replace(/[^0-9\.-]+/g, "");
var number = Number(str);
this.value = isNaN(number) ? 0 : number;
evtObj.preventDefault();
})
return conf._input[0];
},
get: function (conf) {
// user is allowed to use commas to make the number easier to read so
// so need to remove them here.
var raw = conf._input.val();
var val = parseFloat(raw.replace(/[^0-9\.-]+/g, ""));
if (isNaN(val) || val === "") return 0;
else return val;
},
set: function (conf, val) {
conf._input
.val(val);
},
canReturnSubmit: function (opts, node) {
return true;
},
enable: function (conf) {
conf._input.prop('disabled', false);
},
disable: function (conf) {
conf._input.prop('disabled', true);
},
destroy: function (conf) {
conf._input.off();
}
};
}));
function keydownHandler(evtObj) {
// length greater than 1 assumed to be
// nonprintable key
if (evtObj.key.length > 1) {
// non printable keys
switch (evtObj.key) {
// does not work with input event.
//case "Escape":
//default behavior is good
//case "Tab":
//case "Enter":
// decimal from keypad.
case "Decimal":
if (evtObj.target.value.indexOf(".") > -1) {
evtObj.preventDefault();
return false;
}
break;
}
}
else if (evtObj.ctrlKey && (evtObj.key == "v" || evtObj.key == "V" || evtObj.key == "c" || evtObj.key == "C")) {
// allow keyboard copy and paste actions
}
else {
if (evtObj.key == ".") {
if (evtObj.target.value.indexOf(".") > -1) {
evtObj.preventDefault();
return false;
}
}
else if ("1234567890,".indexOf(evtObj.key) < 0) {
evtObj.preventDefault();
return false;
}
}
return true;
}