TimePicker.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. Ext.define('erp.view.core.picker.TimePicker', {
  2. extend: 'Ext.Component',
  3. requires: ['Ext.XTemplate', 'Ext.Date', 'Ext.button.Button'],
  4. alias: 'widget.timepicker',
  5. alternateClassName: 'Ext.TimePicker',
  6. renderTpl: [
  7. '<div id="{id}-bodyEl" class="{baseCls}-body">',
  8. '<div class="{baseCls}-hours">',
  9. '<div style="font-size:14px;text-align:center;font-weight:600">— 小时 —</div>',
  10. '<tpl for="hours">',
  11. '<div class="{parent.baseCls}-item {parent.baseCls}-hour"><a href="#" hidefocus="on">{.}</a></div>',
  12. '</tpl>',
  13. '</div>',
  14. '<div class="{baseCls}-minutes">',
  15. '<div style="font-size:14px;text-align:center;font-weight:600">— 分钟 —</div>',
  16. '<tpl for="minutes">',
  17. '<div class="{parent.baseCls}-item {parent.baseCls}-minute"><a href="#" hidefocus="on">{.}</a></div>',
  18. '</tpl>',
  19. '</div>',
  20. '<div class="' + Ext.baseCSSPrefix + 'clear"></div>',
  21. '</div>',
  22. '<tpl if="showButtons">',
  23. '<div id="{id}-buttonsEl" class="{baseCls}-buttons"></div>',
  24. '</tpl>'
  25. ],
  26. okText: '确认',
  27. cancelText: '取消',
  28. baseCls: Ext.baseCSSPrefix + 'timepicker',
  29. showButtons: true,
  30. smallCls: Ext.baseCSSPrefix + 'monthpicker-small',
  31. totalYears: 10,
  32. yearOffset: 5,
  33. monthOffset: 6,
  34. width: 178,
  35. hours:['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23'],
  36. minutes:['0','5','10','15','20','25','30','35','40','45','50','55'],
  37. initComponent: function(){
  38. var me = this;
  39. me.selectedCls = me.baseCls + '-selected';
  40. me.addEvents(
  41. 'cancelclick',
  42. 'hourclick',
  43. 'hourdblclick',
  44. 'okclick',
  45. 'select',
  46. 'minuteclick',
  47. 'minutedblclick'
  48. );
  49. if (me.small) {
  50. me.addCls(me.smallCls);
  51. }
  52. me.setValue(me.value);
  53. this.callParent();
  54. },
  55. // private, inherit docs
  56. onRender: function(ct, position){
  57. var me = this,
  58. i = 0,
  59. months = [],
  60. shortName = Ext.Date.getShortMonthName,
  61. monthLen = me.monthOffset;
  62. for (; i < monthLen; ++i) {
  63. months.push(shortName(i), shortName(i + monthLen));
  64. }
  65. Ext.apply(me.renderData, {
  66. hours: me.hours,
  67. minutes: me.minutes,
  68. showButtons: me.showButtons
  69. });
  70. me.addChildEls('bodyEl', 'buttonsEl');
  71. me.callParent(arguments);
  72. },
  73. // private, inherit docs
  74. afterRender: function(){
  75. var me = this,
  76. body = me.bodyEl,
  77. buttonsEl = me.buttonsEl;
  78. me.callParent();
  79. me.mon(body, 'click', me.onBodyClick, me);
  80. me.mon(body, 'dblclick', me.onBodyClick, me);
  81. // keep a reference to the year/month elements since we'll be re-using them
  82. me.minutes = body.select('.' + me.baseCls + '-minute a');
  83. me.hours = body.select('.' + me.baseCls + '-hour a');
  84. if (me.showButtons) {
  85. me.okBtn = Ext.create('Ext.button.Button', {
  86. text: me.okText,
  87. renderTo: buttonsEl,
  88. handler: me.onOkClick,
  89. scope: me
  90. });
  91. me.cancelBtn = Ext.create('Ext.button.Button', {
  92. text: me.cancelText,
  93. renderTo: buttonsEl,
  94. handler: me.onCancelClick,
  95. scope: me
  96. });
  97. }
  98. },
  99. setValue: function(value){
  100. var me = this;
  101. if (!value) {
  102. me.value = [null, null];
  103. }else {
  104. var arrs=me.value.split(":"),
  105. hour=arrs[0],
  106. minute=arrs[1];
  107. hour =hour.charAt(0)=='0'? Number(hour.charAt(1)):Number(hour);
  108. minute =minute.charAt(0)=='0'? Number(minute.charAt(1)):Number(minute);
  109. me.value=[hour,minute];
  110. }
  111. if (me.rendered) {
  112. me.hours.removeCls(cls);
  113. me.minutes.removeCls(cls);
  114. me.minutes.each(function(el, all, index){
  115. el.dom.innerHTML = minute;
  116. if (el.dom.innerHTML == minute) {
  117. el.dom.className = me.selectedCls;
  118. }
  119. });
  120. if (hour !== null) {
  121. me.hours.item(hour).addCls(me.selectedCls);
  122. }
  123. }
  124. return me;
  125. },
  126. getValue: function(){
  127. return this.value;
  128. },
  129. hasSelection: function(){
  130. var value = this.value;
  131. return value[0] !== null && value[1] !== null;
  132. },
  133. updateBody: function(){
  134. var me = this,
  135. hours = me.hours,
  136. minutes = me.minutes,
  137. cls = me.selectedCls,
  138. hour = me.value[0],
  139. minute = me.value[1];
  140. if (me.rendered) {
  141. hours.removeCls(cls);
  142. minutes.removeCls(cls);
  143. minutes.each(function(el, all, index){
  144. el.dom.innerHTML = minute;
  145. if (el.dom.innerHTML == minute) {
  146. el.dom.className = cls;
  147. }
  148. });
  149. if (hour !== null) {
  150. hours.item(hour).addCls(cls);
  151. }
  152. }
  153. },
  154. getYear: function(defaultValue, offset) {
  155. var year = this.value[1];
  156. offset = offset || 0;
  157. return year === null ? defaultValue : year + offset;
  158. },
  159. onBodyClick: function(e, t) {
  160. var me = this,
  161. isDouble = e.type == 'dblclick';
  162. if (e.getTarget('.' + me.baseCls + '-hour')) {
  163. e.stopEvent();
  164. me.onHourClick(t, isDouble);
  165. } else if (e.getTarget('.' + me.baseCls + '-minute')) {
  166. e.stopEvent();
  167. me.onMinuteClick(t, isDouble);
  168. }
  169. },
  170. adjustYear: function(offset){
  171. if (typeof offset != 'number') {
  172. offset = this.totalYears;
  173. }
  174. this.activeYear += offset;
  175. this.updateBody();
  176. },
  177. onOkClick: function(){
  178. this.fireEvent('okclick', this, this.value);
  179. },
  180. onCancelClick: function(){
  181. this.fireEvent('cancelclick', this);
  182. this.hide();
  183. },
  184. onHourClick: function(target, isDouble){
  185. var me = this;
  186. me.value[0] = target.innerText;
  187. me.hours.removeCls(me.selectedCls);
  188. me.hours.each(function(el, all, index){
  189. if (el.dom.innerText == me.value[0] ) {
  190. el.dom.className = me.selectedCls;
  191. }
  192. });
  193. me.fireEvent('hour' + (isDouble ? 'dbl' : '') + 'click', me, me.value);
  194. me.fireEvent('select', me, me.value);
  195. },
  196. onMinuteClick: function(target, isDouble){
  197. var me = this;
  198. me.value[1]=target.innerText;
  199. me.minutes.removeCls(me.selectedCls);
  200. me.minutes.each(function(el, all, index){
  201. if (el.dom.innerText == me.value[1] ) {
  202. el.dom.className = me.selectedCls;
  203. }
  204. });
  205. me.fireEvent('minute' + (isDouble ? 'dbl' : '') + 'click', me, me.value);
  206. me.fireEvent('select', me, me.value);
  207. },
  208. resolveOffset: function(index, offset){
  209. if (index % 2 === 0) {
  210. return (index / 2);
  211. } else {
  212. return offset + Math.floor(index / 2);
  213. }
  214. },
  215. beforeDestroy: function(){
  216. var me = this;
  217. me.hours = me.minutes = null;
  218. Ext.destroyMembers(me, 'backRepeater', 'nextRepeater', 'okBtn', 'cancelBtn');
  219. me.callParent();
  220. }
  221. });