author | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
commit | 541bb378ddece2eab135a8066a16994e94436dea (patch) (unidiff) | |
tree | ff160ea3e26f7fe07fcfd401387c5a0232ca715e /frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js | |
parent | 1bf431fd3d45cbdf4afa3e12afefe5d24f4d3bc7 (diff) | |
parent | ecad5e895831337216544e81f1a467e0c68c4a6a (diff) | |
download | clipperz-541bb378ddece2eab135a8066a16994e94436dea.zip clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.gz clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.bz2 |
Merge pull request #1 from gcsolaroli/master
First version of the restructured repository
Diffstat (limited to 'frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js b/frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js new file mode 100644 index 0000000..f74d3d9 --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js | |||
@@ -0,0 +1,166 @@ | |||
1 | /** | ||
2 | * @class YAHOO.ext.grid.NumberEditor | ||
3 | * @extends YAHOO.ext.grid.CellEditor | ||
4 | Provides a masked editor for numeric values. Invalid keys are ignored. It supports the following configuration options: | ||
5 | <ul class="list"> | ||
6 | <li><i>allowDecimals</i> - True if the cell can have decimal values.</li> | ||
7 | <li><i>decimalSeparator</i> - Character(s) to allow as the decimal separator.</li> | ||
8 | <li><i>decimalPrecision</i> - Set the maximum decimal precision.</li> | ||
9 | <li><i>decimalPrecisionFcn</i> - Define the function to call to remove extra precision (ie. Math.floor, Math.round, Math.ceil or your own function).</li> | ||
10 | <li><i>allowNegative</i> - True if the cell allows negative values.</li> | ||
11 | <li><i>selectOnFocus</i> - True to select the text when the editor is activated.</li> | ||
12 | <li><i>minValue</i> - The minimum value the cell will allow.</li> | ||
13 | <li><i>maxValue</i> - The maximum value the cell will allow.</li> | ||
14 | <li><i>minText</i> - The tooltip to display when the value in the cell is below the minimum.</li> | ||
15 | <li><i>maxText</i> - The tooltip to display when the value in the cell is above the maximum.</li> | ||
16 | <li><i>nanText</i> - The tooltip to display when the value in the cell is not a valid number (for example, negatives are allowed and the value in the cell is just "-" with no numbers).</li> | ||
17 | <li><i>allowBlank</i> - True if the cell is allowed to be empty.</li> | ||
18 | <li><i>blankText</i> - The tooltip (error message) to display when the cell is empty and is not allowed to be.</li> | ||
19 | <li><i>validator</i> - Any custom validation function you want called. The function must return true if the data is valid or an error message otherwise.</li> | ||
20 | <li><i>validationDelay</i> - The delay in milliseconds for validation. Each time the user types something the field is validated after a specified delay, setting this value allows you to customize that delay (for example, if your custom validation routine is slow).</li> | ||
21 | </ul> | ||
22 | For more information on using this editor, see <a href="http://www.jackslocum.com/yui/2006/09/10/adding-built-in-editing-support-to-the-yahoo-ui-extensions-grid/">this blog post</a>. | ||
23 | * @constructor | ||
24 | * Create a new NumberEditor | ||
25 | * @param {Object} config | ||
26 | */ | ||
27 | YAHOO.ext.grid.NumberEditor = function(config){ | ||
28 | var element = document.createElement('input'); | ||
29 | element.type = 'text'; | ||
30 | element.className = 'ygrid-editor ygrid-num-editor'; | ||
31 | element.setAttribute('autocomplete', 'off'); | ||
32 | document.body.appendChild(element); | ||
33 | YAHOO.ext.grid.NumberEditor.superclass.constructor.call(this, element); | ||
34 | YAHOO.ext.util.Config.apply(this, config); | ||
35 | }; | ||
36 | YAHOO.extendX(YAHOO.ext.grid.NumberEditor, YAHOO.ext.grid.CellEditor); | ||
37 | |||
38 | YAHOO.ext.grid.NumberEditor.prototype.initEvents = function(){ | ||
39 | var stopOnEnter = function(e){ | ||
40 | if(e.browserEvent.keyCode == e.RETURN){ | ||
41 | this.stopEditing(true); | ||
42 | }else if(e.browserEvent.keyCode == e.ESC){ | ||
43 | this.setValue(this.originalValue); | ||
44 | this.stopEditing(true); | ||
45 | } | ||
46 | }; | ||
47 | |||
48 | var allowed = "0123456789"; | ||
49 | if(this.allowDecimals){ | ||
50 | allowed += this.decimalSeparator; | ||
51 | } | ||
52 | if(this.allowNegative){ | ||
53 | allowed += '-'; | ||
54 | } | ||
55 | var keyPress = function(e){ | ||
56 | var c = e.getCharCode(); | ||
57 | if(c != e.BACKSPACE && allowed.indexOf(String.fromCharCode(c)) === -1){ | ||
58 | e.stopEvent(); | ||
59 | } | ||
60 | }; | ||
61 | this.element.mon('keydown', stopOnEnter, this, true); | ||
62 | var vtask = new YAHOO.ext.util.DelayedTask(this.validate, this); | ||
63 | this.element.mon('keyup', vtask.delay.createDelegate(vtask, [this.validationDelay])); | ||
64 | this.element.mon('keypress', keyPress, this, true); | ||
65 | this.element.on('blur', this.stopEditing, this, true); | ||
66 | }; | ||
67 | |||
68 | YAHOO.ext.grid.NumberEditor.prototype.validate = function(){ | ||
69 | var dom = this.element.dom; | ||
70 | var value = dom.value; | ||
71 | if(value.length < 1){ // if it's blank | ||
72 | if(this.allowBlank){ | ||
73 | dom.title = ''; | ||
74 | this.element.removeClass('ygrid-editor-invalid'); | ||
75 | return true; | ||
76 | }else{ | ||
77 | dom.title = this.blankText; | ||
78 | this.element.addClass('ygrid-editor-invalid'); | ||
79 | return false; | ||
80 | } | ||
81 | } | ||
82 | if(value.search(/\d+/) === -1){ | ||
83 | dom.title = this.nanText.replace('%0', value); | ||
84 | this.element.addClass('ygrid-editor-invalid'); | ||
85 | return false; | ||
86 | } | ||
87 | var num = this.parseValue(value); | ||
88 | if(num < this.minValue){ | ||
89 | dom.title = this.minText.replace('%0', this.minValue); | ||
90 | this.element.addClass('ygrid-editor-invalid'); | ||
91 | return false; | ||
92 | } | ||
93 | if(num > this.maxValue){ | ||
94 | dom.title = this.maxText.replace('%0', this.maxValue); | ||
95 | this.element.addClass('ygrid-editor-invalid'); | ||
96 | return false; | ||
97 | } | ||
98 | var msg = this.validator(value); | ||
99 | if(msg !== true){ | ||
100 | dom.title = msg; | ||
101 | this.element.addClass('ygrid-editor-invalid'); | ||
102 | return false; | ||
103 | } | ||
104 | dom.title = ''; | ||
105 | this.element.removeClass('ygrid-editor-invalid'); | ||
106 | return true; | ||
107 | }; | ||
108 | |||
109 | YAHOO.ext.grid.NumberEditor.prototype.show = function(){ | ||
110 | this.element.dom.title = ''; | ||
111 | YAHOO.ext.grid.NumberEditor.superclass.show.call(this); | ||
112 | if(this.selectOnFocus){ | ||
113 | try{ | ||
114 | this.element.dom.select(); | ||
115 | }catch(e){} | ||
116 | } | ||
117 | this.validate(this.element.dom.value); | ||
118 | }; | ||
119 | |||
120 | YAHOO.ext.grid.NumberEditor.prototype.getValue = function(){ | ||
121 | if(!this.validate()){ | ||
122 | return this.originalValue; | ||
123 | }else{ | ||
124 | var value = this.element.dom.value; | ||
125 | if(value.length < 1){ | ||
126 | return value; | ||
127 | } else{ | ||
128 | return this.fixPrecision(this.parseValue(value)); | ||
129 | } | ||
130 | } | ||
131 | }; | ||
132 | YAHOO.ext.grid.NumberEditor.prototype.parseValue = function(value){ | ||
133 | return parseFloat(new String(value).replace(this.decimalSeparator, '.')); | ||
134 | }; | ||
135 | |||
136 | YAHOO.ext.grid.NumberEditor.prototype.fixPrecision = function(value){ | ||
137 | if(!this.allowDecimals || this.decimalPrecision == -1 || isNaN(value) || value == 0 || !value){ | ||
138 | return value; | ||
139 | } | ||
140 | // this should work but doesn't due to precision error in JS | ||
141 | // var scale = Math.pow(10, this.decimalPrecision); | ||
142 | // var fixed = this.decimalPrecisionFcn(value * scale); | ||
143 | // return fixed / scale; | ||
144 | // | ||
145 | // so here's our workaround: | ||
146 | var scale = Math.pow(10, this.decimalPrecision+1); | ||
147 | var fixed = this.decimalPrecisionFcn(value * scale); | ||
148 | fixed = this.decimalPrecisionFcn(fixed/10); | ||
149 | return fixed / (scale/10); | ||
150 | }; | ||
151 | |||
152 | YAHOO.ext.grid.NumberEditor.prototype.allowBlank = true; | ||
153 | YAHOO.ext.grid.NumberEditor.prototype.allowDecimals = true; | ||
154 | YAHOO.ext.grid.NumberEditor.prototype.decimalSeparator = '.'; | ||
155 | YAHOO.ext.grid.NumberEditor.prototype.decimalPrecision = 2; | ||
156 | YAHOO.ext.grid.NumberEditor.prototype.decimalPrecisionFcn = Math.floor; | ||
157 | YAHOO.ext.grid.NumberEditor.prototype.allowNegative = true; | ||
158 | YAHOO.ext.grid.NumberEditor.prototype.selectOnFocus = true; | ||
159 | YAHOO.ext.grid.NumberEditor.prototype.minValue = Number.NEGATIVE_INFINITY; | ||
160 | YAHOO.ext.grid.NumberEditor.prototype.maxValue = Number.MAX_VALUE; | ||
161 | YAHOO.ext.grid.NumberEditor.prototype.minText = 'The minimum value for this field is %0'; | ||
162 | YAHOO.ext.grid.NumberEditor.prototype.maxText = 'The maximum value for this field is %0'; | ||
163 | YAHOO.ext.grid.NumberEditor.prototype.blankText = 'This field cannot be blank'; | ||
164 | YAHOO.ext.grid.NumberEditor.prototype.nanText = '%0 is not a valid number'; | ||
165 | YAHOO.ext.grid.NumberEditor.prototype.validationDelay = 100; | ||
166 | YAHOO.ext.grid.NumberEditor.prototype.validator = function(){return true;}; | ||