yvanui.datetimebox.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * YvanUI
  3. *
  4. * Copyright (c) 2018 www.yvanui.com. All rights reserved.
  5. * @author luoyifan@qq.com
  6. * @time 2018-12-18 20:16:00
  7. */
  8. (function ($) {
  9. function createBox(target) {
  10. var state = $.data(target, "datetimebox");
  11. var opts = state.options;
  12. $(target).datebox($.extend({}, opts, {
  13. onShowPanel: function () {
  14. var value = $(this).datetimebox("getValue");
  15. setValue(this, value, true);
  16. opts.onShowPanel.call(this);
  17. }, formatter: $.fn.datebox.defaults.formatter, parser: $.fn.datebox.defaults.parser
  18. }));
  19. $(target).removeClass("datebox-f").addClass("datetimebox-f");
  20. $(target).datebox("calendar").calendar({
  21. showTime: true,
  22. onSelect: function (value) {
  23. opts.onSelect.call(this.target, value);
  24. }
  25. });
  26. if (!state.spinner) {
  27. var panel = $(target).datebox("panel");
  28. var p = $("<div style=\"padding:2px\"><input></div>").insertAfter(panel.children("div.datebox-calendar-inner"));
  29. state.spinner = p.children("input");
  30. }
  31. state.spinner.timespinner({ width: opts.spinnerWidth, showSeconds: opts.showSeconds, separator: opts.timeSeparator });
  32. $(target).datetimebox("initValue", opts.value);
  33. }
  34. function getDate(target) {
  35. var dateControl = $(target).datetimebox("calendar");
  36. var timeControl = $(target).datetimebox("spinner");
  37. var date = dateControl.calendar("options").current;
  38. return new Date(date.getFullYear(), date.getMonth(), date.getDate(), timeControl.timespinner("getHours"), timeControl.timespinner("getMinutes"), timeControl.timespinner("getSeconds"));
  39. }
  40. function doQuery(target, q) {
  41. setValue(target, q, true);
  42. }
  43. function doEnter(target) {
  44. var opts = $.data(target, "datetimebox").options;
  45. var date = getDate(target);
  46. setValue(target, opts.formatter.call(target, date));
  47. $(target).combo("hidePanel");
  48. }
  49. function setValue(target, value, remainText) {
  50. var opts = $.data(target, "datetimebox").options;
  51. $(target).combo("setValue", value);
  52. if (!remainText) {
  53. if (value) {
  54. var parsedValue = opts.parser.call(target, value);
  55. $(target).combo("setText", opts.formatter.call(target, parsedValue));
  56. $(target).combo("setValue", opts.formatter.call(target, parsedValue));
  57. } else {
  58. $(target).combo("setText", value);
  59. }
  60. }
  61. var parsedValue = opts.parser.call(target, value);
  62. $(target).datetimebox("calendar").calendar("moveTo", parsedValue);
  63. $(target).datetimebox("spinner").timespinner("setValue", getTimeString(parsedValue));
  64. function getTimeString(date) {
  65. function padz(value) {
  66. return (value < 10 ? "0" : "") + value;
  67. };
  68. var tt = [padz(date.getHours()), padz(date.getMinutes())];
  69. if (opts.showSeconds) {
  70. tt.push(padz(date.getSeconds()));
  71. }
  72. return tt.join($(target).datetimebox("options").timeSeparator);
  73. }
  74. }
  75. $.fn.datetimebox = function (options, param) {
  76. if (typeof options == "string") {
  77. var method = $.fn.datetimebox.methods[options];
  78. if (method) {
  79. return method(this, param);
  80. } else {
  81. return this.datebox(options, param);
  82. }
  83. }
  84. options = options || {};
  85. return this.each(function () {
  86. var state = $.data(this, "datetimebox");
  87. if (state) {
  88. $.extend(state.options, options);
  89. } else {
  90. $.data(this, "datetimebox", { options: $.extend({}, $.fn.datetimebox.defaults, $.fn.datetimebox.parseOptions(this), options) });
  91. }
  92. createBox(this);
  93. });
  94. };
  95. $.fn.datetimebox.methods = {
  96. options: function (jq) {
  97. var copts = jq.datebox("options");
  98. return $.extend($.data(jq[0], "datetimebox").options, {
  99. originalValue: copts.originalValue,
  100. disabled: copts.disabled,
  101. readonly: copts.readonly
  102. });
  103. },
  104. cloneFrom: function (jq, from) {
  105. return jq.each(function () {
  106. $(this).datebox("cloneFrom", from);
  107. $.data(this, "datetimebox", {
  108. options: $.extend(true, {}, $(from).datetimebox("options")),
  109. spinner: $(from).datetimebox("spinner")
  110. });
  111. $(this).removeClass("datebox-f").addClass("datetimebox-f");
  112. });
  113. },
  114. spinner: function (jq) {
  115. return $.data(jq[0], "datetimebox").spinner;
  116. },
  117. initValue: function (jq, value) {
  118. return jq.each(function () {
  119. var opts = $(this).datetimebox("options");
  120. var value = opts.value;
  121. if (value) {
  122. value = opts.formatter.call(this, opts.parser.call(this, value));
  123. }
  124. $(this).combo("initValue", value).combo("setText", value);
  125. });
  126. },
  127. setValue: function (jq, value) {
  128. return jq.each(function () {
  129. setValue(this, value);
  130. });
  131. },
  132. reset: function (jq) {
  133. return jq.each(function () {
  134. var opts = $(this).datetimebox("options");
  135. $(this).datetimebox("setValue", opts.originalValue);
  136. });
  137. }
  138. };
  139. $.fn.datetimebox.parseOptions = function (target) {
  140. var t = $(target);
  141. return $.extend({},
  142. $.fn.datebox.parseOptions(target),
  143. $.parser.parseOptions(target,
  144. ["timeSeparator", "spinnerWidth", { showSeconds: "boolean" }])
  145. );
  146. };
  147. $.fn.datetimebox.defaults = $.extend({}, $.fn.datebox.defaults,
  148. {
  149. //spinnerWidth: "100%",
  150. panelWidth: 422,
  151. panelHeight: 301,
  152. showSeconds: true,
  153. timeSeparator: ":",
  154. panelEvents: {
  155. mousedown: function (e) {
  156. }
  157. },
  158. keyHandler: {
  159. up: function (e) { },
  160. down: function (e) { },
  161. left: function (e) { },
  162. right: function (e) { },
  163. enter: function (e) { doEnter(this); },
  164. query: function (q, e) { doQuery(this, q); }
  165. },
  166. buttons: [
  167. {
  168. text: function (target) {
  169. return $(target).datetimebox("options").currentText;
  170. },
  171. handler: function (target) {
  172. var opts = $(target).datetimebox("options");
  173. setValue(target, opts.formatter.call(target, new Date()));
  174. $(target).datetimebox("hidePanel");
  175. }
  176. },
  177. {
  178. text: function (target) {
  179. return $(target).datetimebox("options").okText;
  180. },
  181. handler: function (target) {
  182. doEnter(target);
  183. }
  184. },
  185. {
  186. text: function (target) {
  187. return $(target).datetimebox("options").closeText;
  188. },
  189. handler: function (target) {
  190. $(target).datetimebox("hidePanel");
  191. }
  192. }
  193. ],
  194. formatter: function (date) {
  195. var h = date.getHours();
  196. var M = date.getMinutes();
  197. var s = date.getSeconds();
  198. function padz(v) {
  199. return (v < 10 ? "0" : "") + v;
  200. };
  201. var opts = $(this).datetimebox("options");
  202. var sep = opts.timeSeparator;
  203. var r = $.fn.datebox.defaults.formatter(date) + " " + padz(h) + sep + padz(M);
  204. if ($(this).datetimebox("options").showSeconds) {
  205. r += sep + padz(s);
  206. }
  207. return r;
  208. },
  209. parser: function (s) {
  210. if ($.trim(s) == "") {
  211. return new Date();
  212. }
  213. var dt = s.split(" ");
  214. var d = $.fn.datebox.defaults.parser(dt[0]);
  215. if (dt.length < 2) {
  216. return d;
  217. }
  218. var sep = $(this).datetimebox("options").timeSeparator;
  219. var tt = dt[1].split(sep);
  220. var hh = parseInt(tt[0], 10) || 0;
  221. var mm = parseInt(tt[1], 10) || 0;
  222. var ss = parseInt(tt[2], 10) || 0;
  223. return new Date(d.getFullYear(), d.getMonth(), d.getDate(), hh, mm, ss);
  224. }
  225. });
  226. })(jQuery);