-
Notifications
You must be signed in to change notification settings - Fork 0
/
jss.editor.textpattern.js
98 lines (79 loc) · 2.82 KB
/
jss.editor.textpattern.js
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
/**
* Custom editor for textPattern
*
* @version 2.0.0
* @author Guillaume Bonnaire <contact@gbonnaire.fr>
* @website https://www.gbonnaire.fr
*
* @license This plugin is distribute under MIT License
*/
jspreadsheet.editors.textpattern = function() {
var methods = {};
var editor = null;
methods.createCell = function(cell, value, x, y, instance, options) {
if(value!=null) {
value = methods.parseValue(x, y, value, instance, options);
cell.innerHTML = value;
}
return value; // Save new value verified
}
methods.updateCell = function(cell, value, x, y, instance, options) {
if(value!=null) {
value = methods.parseValue(x, y, value, instance, options);
cell.innerHTML = value;
}
return value; // Save new value verified
}
methods.openEditor = function(cell, value, x, y, instance, options) {
options = defaultOptions(options);
editor = jspreadsheet.editors.createEditor('input', cell, value, instance);
editor.setAttribute('pattern', options.pattern);
editor.focus();
if(options.uppercase) {
value = value.toUpperCase();
}
editor.value = value;
editor.addEventListener("keyup", handleEvent);
editor.addEventListener("change", handleEvent);
editor.addEventListener("input", handleEvent);
}
methods.closeEditor = function(cell, save, x, y, instance, options) {
if (save) {
var value = editor.value;
} else {
var value = '';
}
return value;
}
methods.parseValue = function(x, y, value, instance, options) {
options = defaultOptions(options);
var regReplace = new RegExp(options.pattern.replace("[","[^"), 'ig');
value = value.replace(regReplace,'');
if(options.uppercase) {
value = value.toUpperCase();
}
return value;
}
function defaultOptions(options) {
var defaultOptions = {
uppercase: false,
pattern: '[0-9a-zA-Z]*'
}
for(var ite_option in defaultOptions) {
if(options[ite_option]==null) {
options[ite_option] = defaultOptions[ite_option];
}
}
return options;
}
function handleEvent(event) {
if(event.target.getAttribute("pattern")) {
var value = event.target.value,
regReplace,
filter = event.target.getAttribute("pattern");
regReplace = RegExp(filter.replace("[","[^"), 'ig');
event.target.value = value.replace(regReplace, '');
}
}
return methods;
}();