You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.0 KiB

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