0f6bcc9e71d16c63e07be2320b00742145f5d165.svn-base 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * jQuery EasyUI 1.3.6
  3. *
  4. * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
  7. * To use it on other terms please contact us at info@jeasyui.com
  8. *
  9. */
  10. /**
  11. * datebox - jQuery EasyUI
  12. *
  13. * Dependencies:
  14. * calendar
  15. * combo
  16. *
  17. */
  18. (function($){
  19. /**
  20. * create date box
  21. */
  22. function createBox(target){
  23. var state = $.data(target, 'datebox');
  24. var opts = state.options;
  25. $(target).addClass('datebox-f').combo($.extend({}, opts, {
  26. onShowPanel:function(){
  27. setCalendar();
  28. setValue(target, $(target).datebox('getText'), true);
  29. // setValue(target, $(target).datebox('getText'));
  30. opts.onShowPanel.call(target);
  31. }
  32. }));
  33. $(target).combo('textbox').parent().addClass('datebox');
  34. /**
  35. * if the calendar isn't created, create it.
  36. */
  37. if (!state.calendar){
  38. createCalendar();
  39. }
  40. setValue(target, opts.value);
  41. function createCalendar(){
  42. var panel = $(target).combo('panel').css('overflow','hidden');
  43. panel.panel('options').onBeforeDestroy = function(){
  44. var sc = $(this).find('.calendar-shared');
  45. if (sc.length){
  46. sc.insertBefore(sc[0].pholder);
  47. }
  48. };
  49. var cc = $('<div class="datebox-calendar-inner"></div>').appendTo(panel);
  50. if (opts.sharedCalendar){
  51. var sc = $(opts.sharedCalendar);
  52. if (!sc[0].pholder){
  53. sc[0].pholder = $('<div class="calendar-pholder" style="display:none"></div>').insertAfter(sc);
  54. }
  55. sc.addClass('calendar-shared').appendTo(cc);
  56. if (!sc.hasClass('calendar')){
  57. sc.calendar();
  58. }
  59. state.calendar = sc;
  60. // state.calendar = $(opts.sharedCalendar).appendTo(cc);
  61. // if (!state.calendar.hasClass('calendar')){
  62. // state.calendar.calendar();
  63. // }
  64. } else {
  65. state.calendar = $('<div></div>').appendTo(cc).calendar();
  66. }
  67. $.extend(state.calendar.calendar('options'), {
  68. fit:true,
  69. border:false,
  70. onSelect:function(date){
  71. var opts = $(this.target).datebox('options');
  72. setValue(this.target, opts.formatter.call(this.target, date));
  73. $(this.target).combo('hidePanel');
  74. opts.onSelect.call(target, date);
  75. }
  76. });
  77. // setValue(target, opts.value);
  78. var button = $('<div class="datebox-button"><table cellspacing="0" cellpadding="0" style="width:100%"><tr></tr></table></div>').appendTo(panel);
  79. var tr = button.find('tr');
  80. for(var i=0; i<opts.buttons.length; i++){
  81. var td = $('<td></td>').appendTo(tr);
  82. var btn = opts.buttons[i];
  83. var t = $('<a href="javascript:void(0)"></a>').html($.isFunction(btn.text) ? btn.text(target) : btn.text).appendTo(td);
  84. t.bind('click', {target: target, handler: btn.handler}, function(e){
  85. e.data.handler.call(this, e.data.target);
  86. });
  87. }
  88. tr.find('td').css('width', (100/opts.buttons.length)+'%');
  89. }
  90. function setCalendar(){
  91. var panel = $(target).combo('panel');
  92. var cc = panel.children('div.datebox-calendar-inner');
  93. panel.children()._outerWidth(panel.width());
  94. state.calendar.appendTo(cc);
  95. state.calendar[0].target = target;
  96. if (opts.panelHeight != 'auto'){
  97. var height = panel.height();
  98. panel.children().not(cc).each(function(){
  99. height -= $(this).outerHeight();
  100. });
  101. cc._outerHeight(height);
  102. }
  103. state.calendar.calendar('resize');
  104. }
  105. }
  106. /**
  107. * called when user inputs some value in text box
  108. */
  109. function doQuery(target, q){
  110. setValue(target, q, true);
  111. }
  112. /**
  113. * called when user press enter key
  114. */
  115. function doEnter(target){
  116. var state = $.data(target, 'datebox');
  117. var opts = state.options;
  118. var current = state.calendar.calendar('options').current;
  119. if (current){
  120. setValue(target, opts.formatter.call(target, current));
  121. $(target).combo('hidePanel');
  122. }
  123. }
  124. function setValue(target, value, remainText){
  125. var state = $.data(target, 'datebox');
  126. var opts = state.options;
  127. var calendar = state.calendar;
  128. $(target).combo('setValue', value);
  129. calendar.calendar('moveTo', opts.parser.call(target, value));
  130. if (!remainText){
  131. if (value){
  132. value = opts.formatter.call(target, calendar.calendar('options').current);
  133. $(target).combo('setValue', value).combo('setText', value);
  134. } else {
  135. $(target).combo('setText', value);
  136. }
  137. }
  138. }
  139. $.fn.datebox = function(options, param){
  140. if (typeof options == 'string'){
  141. var method = $.fn.datebox.methods[options];
  142. if (method){
  143. return method(this, param);
  144. } else {
  145. return this.combo(options, param);
  146. }
  147. }
  148. options = options || {};
  149. return this.each(function(){
  150. var state = $.data(this, 'datebox');
  151. if (state){
  152. $.extend(state.options, options);
  153. } else {
  154. $.data(this, 'datebox', {
  155. options: $.extend({}, $.fn.datebox.defaults, $.fn.datebox.parseOptions(this), options)
  156. });
  157. }
  158. createBox(this);
  159. });
  160. };
  161. $.fn.datebox.methods = {
  162. options: function(jq){
  163. var copts = jq.combo('options');
  164. return $.extend($.data(jq[0], 'datebox').options, {
  165. originalValue: copts.originalValue,
  166. disabled: copts.disabled,
  167. readonly: copts.readonly
  168. });
  169. },
  170. calendar: function(jq){ // get the calendar object
  171. return $.data(jq[0], 'datebox').calendar;
  172. },
  173. setValue: function(jq, value){
  174. return jq.each(function(){
  175. setValue(this, value);
  176. });
  177. },
  178. reset: function(jq){
  179. return jq.each(function(){
  180. var opts = $(this).datebox('options');
  181. $(this).datebox('setValue', opts.originalValue);
  182. });
  183. }
  184. };
  185. $.fn.datebox.parseOptions = function(target){
  186. return $.extend({}, $.fn.combo.parseOptions(target), $.parser.parseOptions(target, ['sharedCalendar']));
  187. };
  188. $.fn.datebox.defaults = $.extend({}, $.fn.combo.defaults, {
  189. panelWidth:180,
  190. panelHeight:'auto',
  191. sharedCalendar:null,
  192. keyHandler: {
  193. up:function(e){},
  194. down:function(e){},
  195. left: function(e){},
  196. right: function(e){},
  197. enter:function(e){doEnter(this)},
  198. query:function(q,e){doQuery(this, q)}
  199. },
  200. currentText:'Today',
  201. closeText:'Close',
  202. okText:'Ok',
  203. buttons:[{
  204. text: function(target){return $(target).datebox('options').currentText;},
  205. handler: function(target){
  206. $(target).datebox('calendar').calendar({
  207. year:allGetServerTime().getFullYear(),
  208. month:allGetServerTime().getMonth()+1,
  209. current:allGetServerTime()
  210. });
  211. doEnter(target);
  212. }
  213. },{
  214. text: function(target){return $(target).datebox('options').closeText;},
  215. handler: function(target){
  216. $(this).closest('div.combo-panel').panel('close');
  217. }
  218. }],
  219. formatter:function(date){
  220. var y = date.getFullYear();
  221. var m = date.getMonth()+1;
  222. var d = date.getDate();
  223. return m+'/'+d+'/'+y;
  224. },
  225. parser:function(s){
  226. var t = Date.parse(s);
  227. if (!isNaN(t)){
  228. return new Date(t);
  229. } else {
  230. return allGetServerTime();
  231. }
  232. },
  233. onSelect:function(date){}
  234. });
  235. })(jQuery);