YearDateField.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * 选择年份picker
  3. */
  4. Ext.define('erp.view.core.form.YearDateField', {
  5. extend: 'Ext.form.field.Trigger',
  6. alias: 'widget.yeardatefield',
  7. triggerCls: Ext.baseCSSPrefix + "form-date-trigger",
  8. initComponent : function(){
  9. this.callParent(arguments);
  10. if(this.fromnow){
  11. this.minYear = new Date().getFullYear();
  12. this.setMinValue(this.minYear);
  13. }
  14. },
  15. onTriggerClick: function(){
  16. var me = this;
  17. if(this.yearPicker && !this.yearPicker.hidden){
  18. this.yearPicker.hide();
  19. return;
  20. }
  21. this.createyearPicker().show();
  22. this.yearPicker.prevEl.dom.onclick = function(){//翻页时刷新样式
  23. me.setMaxValue(me.maxYear);
  24. me.setMinValue(me.minYear);
  25. };
  26. this.yearPicker.nextEl.dom.onclick = function(){
  27. me.setMaxValue(me.maxYear);
  28. me.setMinValue(me.minYear);
  29. };
  30. },
  31. fromnow: false,
  32. regex: /^[1-9]\d{3}$/,
  33. regexText: '格式不正确!',
  34. createyearPicker: function(){
  35. var b = this, a = b.yearPicker;
  36. if (!a) {
  37. b.yearPicker = a = Ext.create("Ext.picker.Month", {
  38. renderTo : Ext.getBody(),
  39. floating : true,
  40. ownerCt: b,
  41. listeners : {
  42. scope : b,
  43. cancelclick : b.onCancelClick,
  44. okclick : b.onOkClick,
  45. yeardblclick : b.onOkClick,
  46. afterrender: function(p){
  47. if(b.maxValue){
  48. p.setMaxDate(b.maxValue);
  49. }
  50. if(b.minValue){
  51. p.setMinDate(b.minValue);
  52. }
  53. }
  54. },
  55. setMaxDate : function(dt){
  56. this.maxDate = dt;
  57. var years = this.years;
  58. Ext.each(years.elements, function(el){
  59. if(Number(el.innerHTML) > dt.getFullYear()){
  60. el.style.color = '#EEE9E9';
  61. } else {
  62. el.style.color = 'black';
  63. }
  64. });
  65. },
  66. setMinDate : function(dt){
  67. this.minDate = dt;
  68. var years = this.years;
  69. Ext.each(years.elements, function(el){
  70. if(Number(el.innerHTML) < dt.getFullYear()){
  71. el.style.color = '#EEE9E9';
  72. } else {
  73. el.style.color = 'black';
  74. }
  75. });
  76. },
  77. totalYears: 10,
  78. yearOffset: 5,
  79. renderTpl: [
  80. '<div id="{id}-bodyEl" class="{baseCls}-body">',
  81. '<div class="{baseCls}-years">',
  82. '<div class="{baseCls}-yearnav">',
  83. '<button id="{id}-prevEl" class="{baseCls}-yearnav-prev"></button>',
  84. '<button id="{id}-nextEl" class="{baseCls}-yearnav-next"></button>',
  85. '</div>',
  86. '<tpl for="years">',
  87. '<div class="{parent.baseCls}-item {parent.baseCls}-year"><a href="#" hidefocus="on">{.}</a></div>',
  88. '</tpl>',
  89. '</div>',
  90. '<div class="' + Ext.baseCSSPrefix + 'clear"></div>',
  91. '</div>',
  92. '<tpl if="showButtons">',
  93. '<div id="{id}-buttonsEl" class="{baseCls}-buttons"></div>',
  94. '</tpl>'
  95. ]
  96. });
  97. a.alignTo(b.inputEl, 'tl-bl?');
  98. }
  99. return a;
  100. },
  101. onCancelClick: function(){
  102. this.yearPicker.hide();
  103. },
  104. onOkClick: function(){
  105. var vals = this.yearPicker.getValue();
  106. var a = vals[1];
  107. if(vals.length == 2){
  108. a = a == null ? new Date().getFullYear() : a;
  109. if(this.minValue){
  110. if(Number(a) < this.minYear){
  111. return;
  112. }
  113. }
  114. if(this.maxValue){
  115. if(Number(a) > this.maxYear){
  116. return;
  117. }
  118. }
  119. this.setValue(a);
  120. }
  121. this.yearPicker.hide();
  122. },
  123. setMaxValue: function(value){
  124. if(this.regex.test(value)){
  125. var me = this,
  126. picker = me.yearPicker,
  127. maxValue = Ext.Date.parse(value.toString().substring(0, 4), 'Y');
  128. me.maxValue = maxValue;
  129. me.maxYear = Number(value.toString().substring(0, 4));
  130. if (picker) {
  131. picker.setMaxDate(maxValue);
  132. }
  133. }
  134. },
  135. setMinValue : function(value){
  136. if(this.regex.test(value)){
  137. var me = this,
  138. picker = me.yearPicker,
  139. minValue = Ext.Date.parse(value.toString().substring(0, 4), 'Y');
  140. me.minValue = minValue;
  141. me.minYear = Number(value.toString().substring(0, 4));
  142. if (picker) {
  143. picker.setMinDate(minValue);
  144. }
  145. }
  146. },
  147. setValue: function(value){
  148. if(!this.regex.test(value)){
  149. value = new Date().getFullYear();
  150. }
  151. this.callParent(arguments);
  152. }
  153. });