Diffstat (limited to 'frontend/beta/js/YUI-extensions/grid/editor/TextEditor.js') (more/less context) (show whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/grid/editor/TextEditor.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/grid/editor/TextEditor.js b/frontend/beta/js/YUI-extensions/grid/editor/TextEditor.js new file mode 100644 index 0000000..3c97acd --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/grid/editor/TextEditor.js | |||
@@ -0,0 +1,110 @@ | |||
1 | /** | ||
2 | * @class YAHOO.ext.grid.TextEditor | ||
3 | * @extends YAHOO.ext.grid.CellEditor | ||
4 | Provides basic text editing for a cells and supports the following configuration options: | ||
5 | <ul class="list"> | ||
6 | <li><i>allowBlank</i> - True if the cell is allowed to be empty.</li> | ||
7 | <li><i>minLength</i> - The minimum length the cell will accept.</li> | ||
8 | <li><i>maxLength</i> - The maximum length the cell will allow.</li> | ||
9 | <li><i>minText</i> - The tooltip to display when the length of the value in the cell is below the minimum.</li> | ||
10 | <li><i>maxText</i> - The tooltip to display when the length of the value in the cell is above the maximum.</li> | ||
11 | <li><i>selectOnFocus</i> - True to select the text when the editor is activated.</li> | ||
12 | <li><i>blankText</i> - The tooltip (error message) to display when the cell is empty and is not allowed to be.</li> | ||
13 | <li><i>regex</i> - A regular expression to match if the value is valid. If the regex.test(value) returns false, the value is considered invalid.</li> | ||
14 | <li><i>regexText</i> - The tooltip (error message) to display when regex does not match.</li> | ||
15 | <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> | ||
16 | <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> | ||
17 | </ul> | ||
18 | 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>. | ||
19 | * @constructor | ||
20 | * Create a new TextEditor | ||
21 | * @param {Object} config | ||
22 | */ | ||
23 | YAHOO.ext.grid.TextEditor = function(config){ | ||
24 | var element = document.createElement('input'); | ||
25 | element.type = 'text'; | ||
26 | element.className = 'ygrid-editor ygrid-text-editor'; | ||
27 | element.setAttribute('autocomplete', 'off'); | ||
28 | document.body.appendChild(element); | ||
29 | YAHOO.ext.grid.TextEditor.superclass.constructor.call(this, element); | ||
30 | YAHOO.ext.util.Config.apply(this, config); | ||
31 | }; | ||
32 | YAHOO.extendX(YAHOO.ext.grid.TextEditor, YAHOO.ext.grid.CellEditor); | ||
33 | |||
34 | YAHOO.ext.grid.TextEditor.prototype.validate = function(){ | ||
35 | var dom = this.element.dom; | ||
36 | var value = dom.value; | ||
37 | if(value.length < 1){ // if it's blank | ||
38 | if(this.allowBlank){ | ||
39 | dom.title = ''; | ||
40 | this.element.removeClass('ygrid-editor-invalid'); | ||
41 | return true; | ||
42 | }else{ | ||
43 | dom.title = this.blankText; | ||
44 | this.element.addClass('ygrid-editor-invalid'); | ||
45 | return false; | ||
46 | } | ||
47 | } | ||
48 | if(value.length < this.minLength){ | ||
49 | dom.title = this.minText.replace('%0', this.minLength); | ||
50 | this.element.addClass('ygrid-editor-invalid'); | ||
51 | return false; | ||
52 | } | ||
53 | if(value.length > this.maxLength){ | ||
54 | dom.title = this.maxText.replace('%0', this.maxLength); | ||
55 | this.element.addClass('ygrid-editor-invalid'); | ||
56 | return false; | ||
57 | } | ||
58 | var msg = this.validator(value); | ||
59 | if(msg !== true){ | ||
60 | dom.title = msg; | ||
61 | this.element.addClass('ygrid-editor-invalid'); | ||
62 | return false; | ||
63 | } | ||
64 | if(this.regex && !this.regex.test(value)){ | ||
65 | dom.title = this.regexText; | ||
66 | this.element.addClass('ygrid-editor-invalid'); | ||
67 | return false; | ||
68 | } | ||
69 | dom.title = ''; | ||
70 | this.element.removeClass('ygrid-editor-invalid'); | ||
71 | return true; | ||
72 | }; | ||
73 | |||
74 | YAHOO.ext.grid.TextEditor.prototype.initEvents = function(){ | ||
75 | YAHOO.ext.grid.TextEditor.superclass.initEvents.call(this); | ||
76 | var vtask = new YAHOO.ext.util.DelayedTask(this.validate, this); | ||
77 | this.element.mon('keyup', vtask.delay.createDelegate(vtask, [this.validationDelay])); | ||
78 | }; | ||
79 | |||
80 | YAHOO.ext.grid.TextEditor.prototype.show = function(){ | ||
81 | this.element.dom.title = ''; | ||
82 | YAHOO.ext.grid.TextEditor.superclass.show.call(this); | ||
83 | this.element.focus(); | ||
84 | if(this.selectOnFocus){ | ||
85 | try{ | ||
86 | this.element.dom.select(); | ||
87 | }catch(e){} | ||
88 | } | ||
89 | this.validate(this.element.dom.value); | ||
90 | }; | ||
91 | |||
92 | YAHOO.ext.grid.TextEditor.prototype.getValue = function(){ | ||
93 | if(!this.validate()){ | ||
94 | return this.originalValue; | ||
95 | }else{ | ||
96 | return this.element.dom.value; | ||
97 | } | ||
98 | }; | ||
99 | |||
100 | YAHOO.ext.grid.TextEditor.prototype.allowBlank = true; | ||
101 | YAHOO.ext.grid.TextEditor.prototype.minLength = 0; | ||
102 | YAHOO.ext.grid.TextEditor.prototype.maxLength = Number.MAX_VALUE; | ||
103 | YAHOO.ext.grid.TextEditor.prototype.minText = 'The minimum length for this field is %0'; | ||
104 | YAHOO.ext.grid.TextEditor.prototype.maxText = 'The maximum length for this field is %0'; | ||
105 | YAHOO.ext.grid.TextEditor.prototype.selectOnFocus = true; | ||
106 | YAHOO.ext.grid.TextEditor.prototype.blankText = 'This field cannot be blank'; | ||
107 | YAHOO.ext.grid.TextEditor.prototype.validator = function(){return true;}; | ||
108 | YAHOO.ext.grid.TextEditor.prototype.validationDelay = 200; | ||
109 | YAHOO.ext.grid.TextEditor.prototype.regex = null; | ||
110 | YAHOO.ext.grid.TextEditor.prototype.regexText = ''; | ||