1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.ux.ValidationStatus |
---|
11 | * A {@link Ext.StatusBar} plugin that provides automatic error notification when the |
---|
12 | * associated form contains validation errors. |
---|
13 | * @extends Ext.Component |
---|
14 | * @constructor |
---|
15 | * Creates a new ValiationStatus plugin |
---|
16 | * @param {Object} config A config object |
---|
17 | */ |
---|
18 | Ext.ux.ValidationStatus = Ext.extend(Ext.Component, { |
---|
19 | |
---|
20 | errorIconCls : 'x-status-error', |
---|
21 | |
---|
22 | errorListCls : 'x-status-error-list', |
---|
23 | |
---|
24 | validIconCls : 'x-status-valid', |
---|
25 | |
---|
26 | showText : 'The form has errors (click for details...)', |
---|
27 | |
---|
28 | hideText : 'Click again to hide the error list', |
---|
29 | |
---|
30 | submitText : 'Saving...', |
---|
31 | |
---|
32 | // private |
---|
33 | init : function(sb){ |
---|
34 | sb.on('render', function(){ |
---|
35 | this.statusBar = sb; |
---|
36 | this.monitor = true; |
---|
37 | this.errors = new Ext.util.MixedCollection(); |
---|
38 | this.listAlign = (sb.statusAlign=='right' ? 'br-tr?' : 'bl-tl?'); |
---|
39 | |
---|
40 | if(this.form){ |
---|
41 | this.form = Ext.getCmp(this.form).getForm(); |
---|
42 | this.startMonitoring(); |
---|
43 | |
---|
44 | // Have to give the status bar time to render since it happens in afterRender |
---|
45 | (function(){ |
---|
46 | sb.statusEl.on('click', this.onStatusClick, this, {buffer:200}); |
---|
47 | }).defer(200, this); |
---|
48 | |
---|
49 | this.form.on('beforeaction', function(f, action){ |
---|
50 | if(action.type == 'submit'){ |
---|
51 | // Ignore monitoring while submitting otherwise the field validation |
---|
52 | // events cause the status message to reset too early |
---|
53 | this.monitor = false; |
---|
54 | } |
---|
55 | }, this); |
---|
56 | var startMonitor = function(){ |
---|
57 | this.monitor = true; |
---|
58 | } |
---|
59 | this.form.on('actioncomplete', startMonitor, this); |
---|
60 | this.form.on('actionfailed', startMonitor, this); |
---|
61 | } |
---|
62 | }, this, {single:true}); |
---|
63 | }, |
---|
64 | |
---|
65 | // private |
---|
66 | startMonitoring : function(){ |
---|
67 | this.form.items.each(function(f){ |
---|
68 | f.on('invalid', this.onFieldValidation, this); |
---|
69 | f.on('valid', this.onFieldValidation, this); |
---|
70 | }, this); |
---|
71 | }, |
---|
72 | |
---|
73 | // private |
---|
74 | stopMonitoring : function(){ |
---|
75 | this.form.items.each(function(f){ |
---|
76 | f.un('invalid', this.onFieldValidation, this); |
---|
77 | f.un('valid', this.onFieldValidation, this); |
---|
78 | }, this); |
---|
79 | }, |
---|
80 | |
---|
81 | // private |
---|
82 | onDestroy : function(){ |
---|
83 | this.stopMonitoring(); |
---|
84 | this.statusBar.statusEl.un('click', this.onStatusClick, this); |
---|
85 | Ext.ux.ValidationStatus.superclass.onDestroy.call(this); |
---|
86 | }, |
---|
87 | |
---|
88 | // private |
---|
89 | onFieldValidation : function(f, msg){ |
---|
90 | if(!this.monitor){ |
---|
91 | return false; |
---|
92 | } |
---|
93 | if(msg){ |
---|
94 | this.errors.add(f.id, {field:f, msg:msg}); |
---|
95 | }else{ |
---|
96 | this.errors.removeKey(f.id); |
---|
97 | } |
---|
98 | this.updateErrorList(); |
---|
99 | if(this.errors.getCount() > 0){ |
---|
100 | if(this.statusBar.getText() != this.showText){ |
---|
101 | this.statusBar.setStatus({text:this.showText, iconCls:this.errorIconCls}); |
---|
102 | } |
---|
103 | }else{ |
---|
104 | this.statusBar.clearStatus().setIcon(this.validIconCls); |
---|
105 | } |
---|
106 | }, |
---|
107 | |
---|
108 | // private |
---|
109 | updateErrorList : function(){ |
---|
110 | if(this.errors.getCount() > 0){ |
---|
111 | var msg = '<ul>'; |
---|
112 | this.errors.each(function(err){ |
---|
113 | msg += ('<li id="x-err-'+ err.field.id +'"><a href="#">' + err.msg + '</a></li>'); |
---|
114 | }, this); |
---|
115 | this.getMsgEl().update(msg+'</ul>'); |
---|
116 | }else{ |
---|
117 | this.getMsgEl().update(''); |
---|
118 | } |
---|
119 | }, |
---|
120 | |
---|
121 | // private |
---|
122 | getMsgEl : function(){ |
---|
123 | if(!this.msgEl){ |
---|
124 | this.msgEl = Ext.DomHelper.append(Ext.getBody(), { |
---|
125 | cls: this.errorListCls+' x-hide-offsets' |
---|
126 | }, true); |
---|
127 | |
---|
128 | this.msgEl.on('click', function(e){ |
---|
129 | var t = e.getTarget('li', 10, true); |
---|
130 | if(t){ |
---|
131 | Ext.getCmp(t.id.split('x-err-')[1]).focus(); |
---|
132 | this.hideErrors(); |
---|
133 | } |
---|
134 | }, this, {stopEvent:true}); // prevent anchor click navigation |
---|
135 | } |
---|
136 | return this.msgEl; |
---|
137 | }, |
---|
138 | |
---|
139 | // private |
---|
140 | showErrors : function(){ |
---|
141 | this.updateErrorList(); |
---|
142 | this.getMsgEl().alignTo(this.statusBar.getEl(), this.listAlign).slideIn('b', {duration:.3, easing:'easeOut'}); |
---|
143 | this.statusBar.setText(this.hideText); |
---|
144 | this.form.getEl().on('click', this.hideErrors, this, {single:true}); // hide if the user clicks directly into the form |
---|
145 | }, |
---|
146 | |
---|
147 | // private |
---|
148 | hideErrors : function(){ |
---|
149 | var el = this.getMsgEl(); |
---|
150 | if(el.isVisible()){ |
---|
151 | el.slideOut('b', {duration:.2, easing:'easeIn'}); |
---|
152 | this.statusBar.setText(this.showText); |
---|
153 | } |
---|
154 | this.form.getEl().un('click', this.hideErrors, this); |
---|
155 | }, |
---|
156 | |
---|
157 | // private |
---|
158 | onStatusClick : function(){ |
---|
159 | if(this.getMsgEl().isVisible()){ |
---|
160 | this.hideErrors(); |
---|
161 | }else if(this.errors.getCount() > 0){ |
---|
162 | this.showErrors(); |
---|
163 | } |
---|
164 | } |
---|
165 | }); |
---|