/easyui/src/jquery.datebox.js

https://bitbucket.org/chanthorn/easyace · JavaScript · 187 lines · 144 code · 16 blank · 27 comment · 10 complexity · 5ee5c836e5424e50440c782814b1d0f1 MD5 · raw file

  1. /**
  2. * datebox - jQuery EasyUI
  3. *
  4. * Copyright (c) 2009-2013 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the GPL or commercial licenses
  7. * To use it on other terms please contact us: info@jeasyui.com
  8. * http://www.gnu.org/licenses/gpl.txt
  9. * http://www.jeasyui.com/license_commercial.php
  10. *
  11. * Dependencies:
  12. * calendar
  13. * combo
  14. *
  15. */
  16. (function($){
  17. /**
  18. * create date box
  19. */
  20. function createBox(target){
  21. var state = $.data(target, 'datebox');
  22. var opts = state.options;
  23. $(target).addClass('datebox-f');
  24. $(target).combo($.extend({}, opts, {
  25. onShowPanel:function(){
  26. state.calendar.calendar('resize');
  27. opts.onShowPanel.call(target);
  28. }
  29. }));
  30. $(target).combo('textbox').parent().addClass('datebox');
  31. /**
  32. * if the calendar isn't created, create it.
  33. */
  34. if (!state.calendar){
  35. createCalendar();
  36. }
  37. function createCalendar(){
  38. var panel = $(target).combo('panel');
  39. state.calendar = $('<div></div>').appendTo(panel).wrap('<div class="datebox-calendar-inner"></div>');
  40. state.calendar.calendar({
  41. fit:true,
  42. border:false,
  43. onSelect:function(date){
  44. var value = opts.formatter(date);
  45. setValue(target, value);
  46. $(target).combo('hidePanel');
  47. opts.onSelect.call(target, date);
  48. }
  49. });
  50. setValue(target, opts.value);
  51. var button = $('<div class="datebox-button"></div>').appendTo(panel);
  52. $('<a href="javascript:void(0)" class="datebox-current"></a>').html(opts.currentText).appendTo(button);
  53. $('<a href="javascript:void(0)" class="datebox-close"></a>').html(opts.closeText).appendTo(button);
  54. button.find('.datebox-current,.datebox-close').hover(
  55. function(){$(this).addClass('datebox-button-hover');},
  56. function(){$(this).removeClass('datebox-button-hover');}
  57. );
  58. button.find('.datebox-current').click(function(){
  59. state.calendar.calendar({
  60. year:new Date().getFullYear(),
  61. month:new Date().getMonth()+1,
  62. current:new Date()
  63. });
  64. });
  65. button.find('.datebox-close').click(function(){
  66. $(target).combo('hidePanel');
  67. });
  68. }
  69. }
  70. /**
  71. * called when user inputs some value in text box
  72. */
  73. function doQuery(target, q){
  74. setValue(target, q);
  75. }
  76. /**
  77. * called when user press enter key
  78. */
  79. function doEnter(target){
  80. var opts = $.data(target, 'datebox').options;
  81. var c = $.data(target, 'datebox').calendar;
  82. var value = opts.formatter(c.calendar('options').current);
  83. setValue(target, value);
  84. $(target).combo('hidePanel');
  85. }
  86. function setValue(target, value){
  87. var state = $.data(target, 'datebox');
  88. var opts = state.options;
  89. $(target).combo('setValue', value).combo('setText', value);
  90. state.calendar.calendar('moveTo', opts.parser(value));
  91. }
  92. $.fn.datebox = function(options, param){
  93. if (typeof options == 'string'){
  94. var method = $.fn.datebox.methods[options];
  95. if (method){
  96. return method(this, param);
  97. } else {
  98. return this.combo(options, param);
  99. }
  100. }
  101. options = options || {};
  102. return this.each(function(){
  103. var state = $.data(this, 'datebox');
  104. if (state){
  105. $.extend(state.options, options);
  106. } else {
  107. $.data(this, 'datebox', {
  108. options: $.extend({}, $.fn.datebox.defaults, $.fn.datebox.parseOptions(this), options)
  109. });
  110. }
  111. createBox(this);
  112. });
  113. };
  114. $.fn.datebox.methods = {
  115. options: function(jq){
  116. var copts = jq.combo('options');
  117. return $.extend($.data(jq[0], 'datebox').options, {
  118. originalValue: copts.originalValue,
  119. disabled: copts.disabled,
  120. readonly: copts.readonly
  121. });
  122. },
  123. calendar: function(jq){ // get the calendar object
  124. return $.data(jq[0], 'datebox').calendar;
  125. },
  126. setValue: function(jq, value){
  127. return jq.each(function(){
  128. setValue(this, value);
  129. });
  130. },
  131. reset: function(jq){
  132. return jq.each(function(){
  133. var opts = $(this).datebox('options');
  134. $(this).datebox('setValue', opts.originalValue);
  135. });
  136. }
  137. };
  138. $.fn.datebox.parseOptions = function(target){
  139. var t = $(target);
  140. return $.extend({}, $.fn.combo.parseOptions(target), {
  141. });
  142. };
  143. $.fn.datebox.defaults = $.extend({}, $.fn.combo.defaults, {
  144. panelWidth:180,
  145. panelHeight:'auto',
  146. keyHandler: {
  147. up:function(){},
  148. down:function(){},
  149. enter:function(){doEnter(this);},
  150. query:function(q){doQuery(this, q);}
  151. },
  152. currentText:'Today',
  153. closeText:'Close',
  154. okText:'Ok',
  155. formatter:function(date){
  156. var y = date.getFullYear();
  157. var m = date.getMonth()+1;
  158. var d = date.getDate();
  159. return m+'/'+d+'/'+y;
  160. },
  161. parser:function(s){
  162. var t = Date.parse(s);
  163. if (!isNaN(t)){
  164. return new Date(t);
  165. } else {
  166. return new Date();
  167. }
  168. },
  169. onSelect:function(date){}
  170. });
  171. })(jQuery);