summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js
Unidiff
Diffstat (limited to 'frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js') (more/less context) (show whitespace changes)
-rw-r--r--frontend/beta/js/YUI-extensions/grid/editor/NumberEditor.js166
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
4Provides 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>
22For 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 */
27YAHOO.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};
36YAHOO.extendX(YAHOO.ext.grid.NumberEditor, YAHOO.ext.grid.CellEditor);
37
38YAHOO.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
68YAHOO.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
109YAHOO.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
120YAHOO.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};
132YAHOO.ext.grid.NumberEditor.prototype.parseValue = function(value){
133 return parseFloat(new String(value).replace(this.decimalSeparator, '.'));
134};
135
136YAHOO.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
152YAHOO.ext.grid.NumberEditor.prototype.allowBlank = true;
153YAHOO.ext.grid.NumberEditor.prototype.allowDecimals = true;
154YAHOO.ext.grid.NumberEditor.prototype.decimalSeparator = '.';
155YAHOO.ext.grid.NumberEditor.prototype.decimalPrecision = 2;
156YAHOO.ext.grid.NumberEditor.prototype.decimalPrecisionFcn = Math.floor;
157YAHOO.ext.grid.NumberEditor.prototype.allowNegative = true;
158YAHOO.ext.grid.NumberEditor.prototype.selectOnFocus = true;
159YAHOO.ext.grid.NumberEditor.prototype.minValue = Number.NEGATIVE_INFINITY;
160YAHOO.ext.grid.NumberEditor.prototype.maxValue = Number.MAX_VALUE;
161YAHOO.ext.grid.NumberEditor.prototype.minText = 'The minimum value for this field is %0';
162YAHOO.ext.grid.NumberEditor.prototype.maxText = 'The maximum value for this field is %0';
163YAHOO.ext.grid.NumberEditor.prototype.blankText = 'This field cannot be blank';
164YAHOO.ext.grid.NumberEditor.prototype.nanText = '%0 is not a valid number';
165YAHOO.ext.grid.NumberEditor.prototype.validationDelay = 100;
166YAHOO.ext.grid.NumberEditor.prototype.validator = function(){return true;};