ValidationStatus.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.ux.statusbar.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.define('Ext.ux.statusbar.ValidationStatus', {
  19. extend: 'Ext.Component',
  20. requires: ['Ext.util.MixedCollection'],
  21. /**
  22. * @cfg {String} errorIconCls
  23. * The {@link #iconCls} value to be applied to the status message when there is a
  24. * validation error. Defaults to <tt>'x-status-error'</tt>.
  25. */
  26. errorIconCls : 'x-status-error',
  27. /**
  28. * @cfg {String} errorListCls
  29. * The css class to be used for the error list when there are validation errors.
  30. * Defaults to <tt>'x-status-error-list'</tt>.
  31. */
  32. errorListCls : 'x-status-error-list',
  33. /**
  34. * @cfg {String} validIconCls
  35. * The {@link #iconCls} value to be applied to the status message when the form
  36. * validates. Defaults to <tt>'x-status-valid'</tt>.
  37. */
  38. validIconCls : 'x-status-valid',
  39. /**
  40. * @cfg {String} showText
  41. * The {@link #text} value to be applied when there is a form validation error.
  42. * Defaults to <tt>'The form has errors (click for details...)'</tt>.
  43. */
  44. showText : 'The form has errors (click for details...)',
  45. /**
  46. * @cfg {String} showText
  47. * The {@link #text} value to display when the error list is displayed.
  48. * Defaults to <tt>'Click again to hide the error list'</tt>.
  49. */
  50. hideText : 'Click again to hide the error list',
  51. /**
  52. * @cfg {String} submitText
  53. * The {@link #text} value to be applied when the form is being submitted.
  54. * Defaults to <tt>'Saving...'</tt>.
  55. */
  56. submitText : 'Saving...',
  57. // private
  58. init : function(sb){
  59. sb.on('render', function(){
  60. this.statusBar = sb;
  61. this.monitor = true;
  62. this.errors = Ext.create('Ext.util.MixedCollection');
  63. this.listAlign = (sb.statusAlign === 'right' ? 'br-tr?' : 'bl-tl?');
  64. if (this.form) {
  65. this.formPanel = Ext.getCmp(this.form);
  66. this.basicForm = this.formPanel.getForm();
  67. this.startMonitoring();
  68. this.basicForm.on('beforeaction', function(f, action){
  69. if(action.type === 'submit'){
  70. // Ignore monitoring while submitting otherwise the field validation
  71. // events cause the status message to reset too early
  72. this.monitor = false;
  73. }
  74. }, this);
  75. var startMonitor = function(){
  76. this.monitor = true;
  77. };
  78. this.basicForm.on('actioncomplete', startMonitor, this);
  79. this.basicForm.on('actionfailed', startMonitor, this);
  80. }
  81. }, this, {single:true});
  82. sb.on({
  83. scope: this,
  84. afterlayout:{
  85. single: true,
  86. fn: function(){
  87. // Grab the statusEl after the first layout.
  88. sb.statusEl.getEl().on('click', this.onStatusClick, this, {buffer:200});
  89. }
  90. },
  91. beforedestroy:{
  92. single: true,
  93. fn: this.onDestroy
  94. }
  95. });
  96. },
  97. // private
  98. startMonitoring : function() {
  99. this.basicForm.getFields().each(function(f){
  100. f.on('validitychange', this.onFieldValidation, this);
  101. }, this);
  102. },
  103. // private
  104. stopMonitoring : function(){
  105. this.basicForm.getFields().each(function(f){
  106. f.un('validitychange', this.onFieldValidation, this);
  107. }, this);
  108. },
  109. // private
  110. onDestroy : function(){
  111. this.stopMonitoring();
  112. this.statusBar.statusEl.un('click', this.onStatusClick, this);
  113. this.callParent(arguments);
  114. },
  115. // private
  116. onFieldValidation : function(f, isValid){
  117. if (!this.monitor) {
  118. return false;
  119. }
  120. var msg = f.getErrors()[0];
  121. if (msg) {
  122. this.errors.add(f.id, {field:f, msg:msg});
  123. } else {
  124. this.errors.removeAtKey(f.id);
  125. }
  126. this.updateErrorList();
  127. if(this.errors.getCount() > 0) {
  128. if(this.statusBar.getText() !== this.showText){
  129. this.statusBar.setStatus({text:this.showText, iconCls:this.errorIconCls});
  130. }
  131. }else{
  132. this.statusBar.clearStatus().setIcon(this.validIconCls);
  133. }
  134. },
  135. // private
  136. updateErrorList : function(){
  137. if(this.errors.getCount() > 0){
  138. var msg = '<ul>';
  139. this.errors.each(function(err){
  140. msg += ('<li id="x-err-'+ err.field.id +'"><a href="#">' + err.msg + '</a></li>');
  141. }, this);
  142. this.getMsgEl().update(msg+'</ul>');
  143. }else{
  144. this.getMsgEl().update('');
  145. }
  146. // reset msgEl size
  147. this.getMsgEl().setSize('auto', 'auto');
  148. },
  149. // private
  150. getMsgEl : function(){
  151. if(!this.msgEl){
  152. this.msgEl = Ext.DomHelper.append(Ext.getBody(), {
  153. cls: this.errorListCls
  154. }, true);
  155. this.msgEl.hide();
  156. this.msgEl.on('click', function(e){
  157. var t = e.getTarget('li', 10, true);
  158. if(t){
  159. Ext.getCmp(t.id.split('x-err-')[1]).focus();
  160. this.hideErrors();
  161. }
  162. }, this, {stopEvent:true}); // prevent anchor click navigation
  163. }
  164. return this.msgEl;
  165. },
  166. // private
  167. showErrors : function(){
  168. this.updateErrorList();
  169. this.getMsgEl().alignTo(this.statusBar.getEl(), this.listAlign).slideIn('b', {duration: 300, easing:'easeOut'});
  170. this.statusBar.setText(this.hideText);
  171. this.formPanel.el.on('click', this.hideErrors, this, {single:true}); // hide if the user clicks directly into the form
  172. },
  173. // private
  174. hideErrors : function(){
  175. var el = this.getMsgEl();
  176. if(el.isVisible()){
  177. el.slideOut('b', {duration: 300, easing:'easeIn'});
  178. this.statusBar.setText(this.showText);
  179. }
  180. this.formPanel.el.un('click', this.hideErrors, this);
  181. },
  182. // private
  183. onStatusClick : function(){
  184. if(this.getMsgEl().isVisible()){
  185. this.hideErrors();
  186. }else if(this.errors.getCount() > 0){
  187. this.showErrors();
  188. }
  189. }
  190. });