ligerDateEditor.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /**
  2. * jQuery ligerUI 1.1.9
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2012 [ gd_star@163.com ]
  7. *
  8. */
  9. (function ($)
  10. {
  11. $.fn.ligerDateEditor = function ()
  12. {
  13. return $.ligerui.run.call(this, "ligerDateEditor", arguments);
  14. };
  15. $.fn.ligerGetDateEditorManager = function ()
  16. {
  17. return $.ligerui.run.call(this, "ligerGetDateEditorManager", arguments);
  18. };
  19. $.ligerDefaults.DateEditor = {
  20. format: "yyyy-MM-dd hh:mm",
  21. showTime: false,
  22. onChangeDate: false,
  23. absolute: true //选择框是否在附加到body,并绝对定位
  24. };
  25. $.ligerDefaults.DateEditorString = {
  26. dayMessage: ["日", "一", "二", "三", "四", "五", "六"],
  27. monthMessage: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
  28. todayMessage: "今天",
  29. closeMessage: "关闭"
  30. };
  31. $.ligerMethos.DateEditor = {};
  32. $.ligerui.controls.DateEditor = function (element, options)
  33. {
  34. $.ligerui.controls.DateEditor.base.constructor.call(this, element, options);
  35. };
  36. $.ligerui.controls.DateEditor.ligerExtend($.ligerui.controls.Input, {
  37. __getType: function ()
  38. {
  39. return 'DateEditor';
  40. },
  41. __idPrev: function ()
  42. {
  43. return 'DateEditor';
  44. },
  45. _extendMethods: function ()
  46. {
  47. return $.ligerMethos.DateEditor;
  48. },
  49. _render: function ()
  50. {
  51. var g = this, p = this.options;
  52. if (!p.showTime && p.format.indexOf(" hh:mm") > -1)
  53. p.format = p.format.replace(" hh:mm", "");
  54. if (this.element.tagName.toLowerCase() != "input" || this.element.type != "text")
  55. return;
  56. g.inputText = $(this.element);
  57. if (!g.inputText.hasClass("l-text-field"))
  58. g.inputText.addClass("l-text-field");
  59. g.link = $('<div class="l-trigger"><div class="l-trigger-icon"></div></div>');
  60. g.text = g.inputText.wrap('<div class="l-text l-text-date"></div>').parent();
  61. g.text.append('<div class="l-text-l"></div><div class="l-text-r"></div>');
  62. g.text.append(g.link);
  63. g.textwrapper = g.text.wrap('<div class="l-text-wrapper"></div>').parent();
  64. var dateeditorHTML = "";
  65. dateeditorHTML += "<div class='l-box-dateeditor' style='display:none'>";
  66. dateeditorHTML += " <div class='l-box-dateeditor-header'>";
  67. dateeditorHTML += " <div class='l-box-dateeditor-header-btn l-box-dateeditor-header-prevyear'><span></span></div>";
  68. dateeditorHTML += " <div class='l-box-dateeditor-header-btn l-box-dateeditor-header-prevmonth'><span></span></div>";
  69. dateeditorHTML += " <div class='l-box-dateeditor-header-text'><a class='l-box-dateeditor-header-month'></a> , <a class='l-box-dateeditor-header-year'></a></div>";
  70. dateeditorHTML += " <div class='l-box-dateeditor-header-btn l-box-dateeditor-header-nextmonth'><span></span></div>";
  71. dateeditorHTML += " <div class='l-box-dateeditor-header-btn l-box-dateeditor-header-nextyear'><span></span></div>";
  72. dateeditorHTML += " </div>";
  73. dateeditorHTML += " <div class='l-box-dateeditor-body'>";
  74. dateeditorHTML += " <table cellpadding='0' cellspacing='0' border='0' class='l-box-dateeditor-calendar'>";
  75. dateeditorHTML += " <thead>";
  76. dateeditorHTML += " <tr><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td></tr>";
  77. dateeditorHTML += " </thead>";
  78. dateeditorHTML += " <tbody>";
  79. dateeditorHTML += " <tr class='l-first'><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td></tr><tr><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td></tr><tr><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td></tr><tr><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td></tr><tr><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td></tr><tr><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td><td align='center'></td></tr>";
  80. dateeditorHTML += " </tbody>";
  81. dateeditorHTML += " </table>";
  82. dateeditorHTML += " <ul class='l-box-dateeditor-monthselector'><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul>";
  83. dateeditorHTML += " <ul class='l-box-dateeditor-yearselector'><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul>";
  84. dateeditorHTML += " <ul class='l-box-dateeditor-hourselector'><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul>";
  85. dateeditorHTML += " <ul class='l-box-dateeditor-minuteselector'><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul>";
  86. dateeditorHTML += " </div>";
  87. dateeditorHTML += " <div class='l-box-dateeditor-toolbar'>";
  88. dateeditorHTML += " <div class='l-box-dateeditor-time'></div>";
  89. dateeditorHTML += " <div class='l-button l-button-today'></div>";
  90. dateeditorHTML += " <div class='l-button l-button-close'></div>";
  91. dateeditorHTML += " <div class='l-clear'></div>";
  92. dateeditorHTML += " </div>";
  93. dateeditorHTML += "</div>";
  94. g.dateeditor = $(dateeditorHTML);
  95. if (p.absolute)
  96. g.dateeditor.appendTo('body').addClass("l-box-dateeditor-absolute");
  97. else
  98. g.textwrapper.append(g.dateeditor);
  99. g.header = $(".l-box-dateeditor-header", g.dateeditor);
  100. g.body = $(".l-box-dateeditor-body", g.dateeditor);
  101. g.toolbar = $(".l-box-dateeditor-toolbar", g.dateeditor);
  102. g.body.thead = $("thead", g.body);
  103. g.body.tbody = $("tbody", g.body);
  104. g.body.monthselector = $(".l-box-dateeditor-monthselector", g.body);
  105. g.body.yearselector = $(".l-box-dateeditor-yearselector", g.body);
  106. g.body.hourselector = $(".l-box-dateeditor-hourselector", g.body);
  107. g.body.minuteselector = $(".l-box-dateeditor-minuteselector", g.body);
  108. g.toolbar.time = $(".l-box-dateeditor-time", g.toolbar);
  109. g.toolbar.time.hour = $("<a></a>");
  110. g.toolbar.time.minute = $("<a></a>");
  111. g.buttons = {
  112. btnPrevYear: $(".l-box-dateeditor-header-prevyear", g.header),
  113. btnNextYear: $(".l-box-dateeditor-header-nextyear", g.header),
  114. btnPrevMonth: $(".l-box-dateeditor-header-prevmonth", g.header),
  115. btnNextMonth: $(".l-box-dateeditor-header-nextmonth", g.header),
  116. btnYear: $(".l-box-dateeditor-header-year", g.header),
  117. btnMonth: $(".l-box-dateeditor-header-month", g.header),
  118. btnToday: $(".l-button-today", g.toolbar),
  119. btnClose: $(".l-button-close", g.toolbar)
  120. };
  121. var nowDate = new Date();
  122. g.now = {
  123. year: nowDate.getFullYear(),
  124. month: nowDate.getMonth() + 1, //注意这里
  125. day: nowDate.getDay(),
  126. date: nowDate.getDate(),
  127. hour: nowDate.getHours(),
  128. minute: nowDate.getMinutes()
  129. };
  130. //当前的时间
  131. g.currentDate = {
  132. year: nowDate.getFullYear(),
  133. month: nowDate.getMonth() + 1,
  134. day: nowDate.getDay(),
  135. date: nowDate.getDate(),
  136. hour: nowDate.getHours(),
  137. minute: nowDate.getMinutes()
  138. };
  139. //选择的时间
  140. g.selectedDate = null;
  141. //使用的时间
  142. g.usedDate = null;
  143. //初始化数据
  144. //设置周日至周六
  145. $("td", g.body.thead).each(function (i, td)
  146. {
  147. $(td).html(p.dayMessage[i]);
  148. });
  149. //设置一月到十一二月
  150. $("li", g.body.monthselector).each(function (i, li)
  151. {
  152. $(li).html(p.monthMessage[i]);
  153. });
  154. //设置按钮
  155. g.buttons.btnToday.html(p.todayMessage);
  156. g.buttons.btnClose.html(p.closeMessage);
  157. //设置时间
  158. if (p.showTime)
  159. {
  160. g.toolbar.time.show();
  161. g.toolbar.time.append(g.toolbar.time.hour).append(":").append(g.toolbar.time.minute);
  162. $("li", g.body.hourselector).each(function (i, item)
  163. {
  164. var str = i;
  165. if (i < 10) str = "0" + i.toString();
  166. $(this).html(str);
  167. });
  168. $("li", g.body.minuteselector).each(function (i, item)
  169. {
  170. var str = i;
  171. if (i < 10) str = "0" + i.toString();
  172. $(this).html(str);
  173. });
  174. }
  175. //设置主体
  176. g.bulidContent();
  177. //初始化
  178. if (g.inputText.val() != "")
  179. g.onTextChange();
  180. /**************
  181. **bulid evens**
  182. *************/
  183. g.dateeditor.hover(null, function (e)
  184. {
  185. if (g.dateeditor.is(":visible") && !g.editorToggling)
  186. {
  187. g.toggleDateEditor(true);
  188. }
  189. });
  190. //toggle even
  191. g.link.hover(function ()
  192. {
  193. if (p.disabled) return;
  194. this.className = "l-trigger-hover";
  195. }, function ()
  196. {
  197. if (p.disabled) return;
  198. this.className = "l-trigger";
  199. }).mousedown(function ()
  200. {
  201. if (p.disabled) return;
  202. this.className = "l-trigger-pressed";
  203. }).mouseup(function ()
  204. {
  205. if (p.disabled) return;
  206. this.className = "l-trigger-hover";
  207. }).click(function ()
  208. {
  209. if (p.disabled) return;
  210. g.bulidContent();
  211. g.toggleDateEditor(g.dateeditor.is(":visible"));
  212. });
  213. //不可用属性时处理
  214. if (p.disabled)
  215. {
  216. g.inputText.attr("readonly", "readonly");
  217. g.text.addClass('l-text-disabled');
  218. }
  219. //初始值
  220. if (p.initValue)
  221. {
  222. g.inputText.val(p.initValue);
  223. }
  224. g.buttons.btnClose.click(function ()
  225. {
  226. g.toggleDateEditor(true);
  227. });
  228. //日期 点击
  229. $("td", g.body.tbody).hover(function ()
  230. {
  231. if ($(this).hasClass("l-box-dateeditor-today")) return;
  232. $(this).addClass("l-box-dateeditor-over");
  233. }, function ()
  234. {
  235. $(this).removeClass("l-box-dateeditor-over");
  236. }).click(function ()
  237. {
  238. $(".l-box-dateeditor-selected", g.body.tbody).removeClass("l-box-dateeditor-selected");
  239. if (!$(this).hasClass("l-box-dateeditor-today"))
  240. $(this).addClass("l-box-dateeditor-selected");
  241. g.currentDate.date = parseInt($(this).html());
  242. g.currentDate.day = new Date(g.currentDate.year, g.currentDate.month - 1, 1).getDay();
  243. if ($(this).hasClass("l-box-dateeditor-out"))
  244. {
  245. if ($("tr", g.body.tbody).index($(this).parent()) == 0)
  246. {
  247. if (--g.currentDate.month == 0)
  248. {
  249. g.currentDate.month = 12;
  250. g.currentDate.year--;
  251. }
  252. } else
  253. {
  254. if (++g.currentDate.month == 13)
  255. {
  256. g.currentDate.month = 1;
  257. g.currentDate.year++;
  258. }
  259. }
  260. }
  261. g.selectedDate = {
  262. year: g.currentDate.year,
  263. month: g.currentDate.month,
  264. date: g.currentDate.date
  265. };
  266. g.showDate();
  267. g.editorToggling = true;
  268. g.dateeditor.slideToggle('fast', function ()
  269. {
  270. g.editorToggling = false;
  271. });
  272. });
  273. $(".l-box-dateeditor-header-btn", g.header).hover(function ()
  274. {
  275. $(this).addClass("l-box-dateeditor-header-btn-over");
  276. }, function ()
  277. {
  278. $(this).removeClass("l-box-dateeditor-header-btn-over");
  279. });
  280. //选择年份
  281. g.buttons.btnYear.click(function ()
  282. {
  283. //build year list
  284. if (!g.body.yearselector.is(":visible"))
  285. {
  286. $("li", g.body.yearselector).each(function (i, item)
  287. {
  288. var currentYear = g.currentDate.year + (i - 4);
  289. if (currentYear == g.currentDate.year)
  290. $(this).addClass("l-selected");
  291. else
  292. $(this).removeClass("l-selected");
  293. $(this).html(currentYear);
  294. });
  295. }
  296. g.body.yearselector.slideToggle();
  297. });
  298. g.body.yearselector.hover(function () { }, function ()
  299. {
  300. $(this).slideUp();
  301. });
  302. $("li", g.body.yearselector).click(function ()
  303. {
  304. g.currentDate.year = parseInt($(this).html());
  305. g.body.yearselector.slideToggle();
  306. g.bulidContent();
  307. });
  308. //select month
  309. g.buttons.btnMonth.click(function ()
  310. {
  311. $("li", g.body.monthselector).each(function (i, item)
  312. {
  313. //add selected style
  314. if (g.currentDate.month == i + 1)
  315. $(this).addClass("l-selected");
  316. else
  317. $(this).removeClass("l-selected");
  318. });
  319. g.body.monthselector.slideToggle();
  320. });
  321. g.body.monthselector.hover(function () { }, function ()
  322. {
  323. $(this).slideUp("fast");
  324. });
  325. $("li", g.body.monthselector).click(function ()
  326. {
  327. var index = $("li", g.body.monthselector).index(this);
  328. g.currentDate.month = index + 1;
  329. g.body.monthselector.slideToggle();
  330. g.bulidContent();
  331. });
  332. //选择小时
  333. g.toolbar.time.hour.click(function ()
  334. {
  335. $("li", g.body.hourselector).each(function (i, item)
  336. {
  337. //add selected style
  338. if (g.currentDate.hour == i)
  339. $(this).addClass("l-selected");
  340. else
  341. $(this).removeClass("l-selected");
  342. });
  343. g.body.hourselector.slideToggle();
  344. });
  345. g.body.hourselector.hover(function () { }, function ()
  346. {
  347. $(this).slideUp("fast");
  348. });
  349. $("li", g.body.hourselector).click(function ()
  350. {
  351. var index = $("li", g.body.hourselector).index(this);
  352. g.currentDate.hour = index;
  353. g.body.hourselector.slideToggle();
  354. g.bulidContent();
  355. });
  356. //选择分钟
  357. g.toolbar.time.minute.click(function ()
  358. {
  359. $("li", g.body.minuteselector).each(function (i, item)
  360. {
  361. //add selected style
  362. if (g.currentDate.minute == i)
  363. $(this).addClass("l-selected");
  364. else
  365. $(this).removeClass("l-selected");
  366. });
  367. g.body.minuteselector.slideToggle("fast", function ()
  368. {
  369. var index = $("li", this).index($('li.l-selected', this));
  370. if (index > 29)
  371. {
  372. var offSet = ($('li.l-selected', this).offset().top - $(this).offset().top);
  373. $(this).animate({ scrollTop: offSet });
  374. }
  375. });
  376. });
  377. g.body.minuteselector.hover(function () { }, function ()
  378. {
  379. $(this).slideUp("fast");
  380. });
  381. $("li", g.body.minuteselector).click(function ()
  382. {
  383. var index = $("li", g.body.minuteselector).index(this);
  384. g.currentDate.minute = index;
  385. g.body.minuteselector.slideToggle("fast");
  386. g.bulidContent();
  387. });
  388. //上个月
  389. g.buttons.btnPrevMonth.click(function ()
  390. {
  391. if (--g.currentDate.month == 0)
  392. {
  393. g.currentDate.month = 12;
  394. g.currentDate.year--;
  395. }
  396. g.bulidContent();
  397. });
  398. //下个月
  399. g.buttons.btnNextMonth.click(function ()
  400. {
  401. if (++g.currentDate.month == 13)
  402. {
  403. g.currentDate.month = 1;
  404. g.currentDate.year++;
  405. }
  406. g.bulidContent();
  407. });
  408. //上一年
  409. g.buttons.btnPrevYear.click(function ()
  410. {
  411. g.currentDate.year--;
  412. g.bulidContent();
  413. });
  414. //下一年
  415. g.buttons.btnNextYear.click(function ()
  416. {
  417. g.currentDate.year++;
  418. g.bulidContent();
  419. });
  420. //今天
  421. g.buttons.btnToday.click(function ()
  422. {
  423. g.currentDate = {
  424. year: g.now.year,
  425. month: g.now.month,
  426. day: g.now.day,
  427. date: g.now.date
  428. };
  429. g.selectedDate = {
  430. year: g.now.year,
  431. month: g.now.month,
  432. day: g.now.day,
  433. date: g.now.date
  434. };
  435. g.showDate();
  436. g.dateeditor.slideToggle("fast");
  437. });
  438. //文本框
  439. g.inputText.change(function ()
  440. {
  441. g.onTextChange();
  442. }).blur(function ()
  443. {
  444. g.text.removeClass("l-text-focus");
  445. }).focus(function ()
  446. {
  447. g.text.addClass("l-text-focus");
  448. });
  449. g.text.hover(function ()
  450. {
  451. g.text.addClass("l-text-over");
  452. }, function ()
  453. {
  454. g.text.removeClass("l-text-over");
  455. });
  456. //LEABEL 支持
  457. if (p.label)
  458. {
  459. g.labelwrapper = g.textwrapper.wrap('<div class="l-labeltext"></div>').parent();
  460. g.labelwrapper.prepend('<div class="l-text-label" style="float:left;display:inline;">' + p.label + ':&nbsp</div>');
  461. g.textwrapper.css('float', 'left');
  462. if (!p.labelWidth)
  463. {
  464. p.labelWidth = $('.l-text-label', g.labelwrapper).outerWidth();
  465. } else
  466. {
  467. $('.l-text-label', g.labelwrapper).outerWidth(p.labelWidth);
  468. }
  469. $('.l-text-label', g.labelwrapper).width(p.labelWidth);
  470. $('.l-text-label', g.labelwrapper).height(g.text.height());
  471. g.labelwrapper.append('<br style="clear:both;" />');
  472. if (p.labelAlign)
  473. {
  474. $('.l-text-label', g.labelwrapper).css('text-align', p.labelAlign);
  475. }
  476. g.textwrapper.css({ display: 'inline' });
  477. g.labelwrapper.width(g.text.outerWidth() + p.labelWidth + 2);
  478. }
  479. g.set(p);
  480. },
  481. destroy: function ()
  482. {
  483. if (this.textwrapper) this.textwrapper.remove();
  484. if (this.dateeditor) this.dateeditor.remove();
  485. this.options = null;
  486. $.ligerui.remove(this);
  487. },
  488. bulidContent: function ()
  489. {
  490. var g = this, p = this.options;
  491. //当前月第一天星期
  492. var thismonthFirstDay = new Date(g.currentDate.year, g.currentDate.month - 1, 1).getDay();
  493. //当前月天数
  494. var nextMonth = g.currentDate.month;
  495. var nextYear = g.currentDate.year;
  496. if (++nextMonth == 13)
  497. {
  498. nextMonth = 1;
  499. nextYear++;
  500. }
  501. var monthDayNum = new Date(nextYear, nextMonth - 1, 0).getDate();
  502. //当前上个月天数
  503. var prevMonthDayNum = new Date(g.currentDate.year, g.currentDate.month - 1, 0).getDate();
  504. g.buttons.btnMonth.html(p.monthMessage[g.currentDate.month - 1]);
  505. g.buttons.btnYear.html(g.currentDate.year);
  506. g.toolbar.time.hour.html(g.currentDate.hour);
  507. g.toolbar.time.minute.html(g.currentDate.minute);
  508. if (g.toolbar.time.hour.html().length == 1)
  509. g.toolbar.time.hour.html("0" + g.toolbar.time.hour.html());
  510. if (g.toolbar.time.minute.html().length == 1)
  511. g.toolbar.time.minute.html("0" + g.toolbar.time.minute.html());
  512. $("td", this.body.tbody).each(function () { this.className = "" });
  513. $("tr", this.body.tbody).each(function (i, tr)
  514. {
  515. $("td", tr).each(function (j, td)
  516. {
  517. var id = i * 7 + (j - thismonthFirstDay);
  518. var showDay = id + 1;
  519. if (g.selectedDate && g.currentDate.year == g.selectedDate.year &&
  520. g.currentDate.month == g.selectedDate.month &&
  521. id + 1 == g.selectedDate.date)
  522. {
  523. if (j == 0 || j == 6)
  524. {
  525. $(td).addClass("l-box-dateeditor-holiday")
  526. }
  527. $(td).addClass("l-box-dateeditor-selected");
  528. $(td).siblings().removeClass("l-box-dateeditor-selected");
  529. }
  530. else if (g.currentDate.year == g.now.year &&
  531. g.currentDate.month == g.now.month &&
  532. id + 1 == g.now.date)
  533. {
  534. if (j == 0 || j == 6)
  535. {
  536. $(td).addClass("l-box-dateeditor-holiday")
  537. }
  538. $(td).addClass("l-box-dateeditor-today");
  539. }
  540. else if (id < 0)
  541. {
  542. showDay = prevMonthDayNum + showDay;
  543. $(td).addClass("l-box-dateeditor-out")
  544. .removeClass("l-box-dateeditor-selected");
  545. }
  546. else if (id > monthDayNum - 1)
  547. {
  548. showDay = showDay - monthDayNum;
  549. $(td).addClass("l-box-dateeditor-out")
  550. .removeClass("l-box-dateeditor-selected");
  551. }
  552. else if (j == 0 || j == 6)
  553. {
  554. $(td).addClass("l-box-dateeditor-holiday")
  555. .removeClass("l-box-dateeditor-selected");
  556. }
  557. else
  558. {
  559. td.className = "";
  560. }
  561. $(td).html(showDay);
  562. });
  563. });
  564. },
  565. updateSelectBoxPosition: function ()
  566. {
  567. var g = this, p = this.options;
  568. if (p.absolute)
  569. {
  570. g.dateeditor.css({ left: g.text.offset().left, top: g.text.offset().top + 1 + g.text.outerHeight() });
  571. }
  572. else
  573. {
  574. if (g.text.offset().top + 4 > g.dateeditor.height() && g.text.offset().top + g.dateeditor.height() + textHeight + 4 - $(window).scrollTop() > $(window).height())
  575. {
  576. g.dateeditor.css("marginTop", -1 * (g.dateeditor.height() + textHeight + 5));
  577. g.showOnTop = true;
  578. }
  579. else
  580. {
  581. g.showOnTop = false;
  582. }
  583. }
  584. },
  585. toggleDateEditor: function (isHide)
  586. {
  587. var g = this, p = this.options;
  588. var textHeight = g.text.height();
  589. g.editorToggling = true;
  590. if (isHide)
  591. {
  592. g.dateeditor.hide('fast', function ()
  593. {
  594. g.editorToggling = false;
  595. });
  596. }
  597. else
  598. {
  599. g.updateSelectBoxPosition();
  600. g.dateeditor.slideDown('fast', function ()
  601. {
  602. g.editorToggling = false;
  603. });
  604. }
  605. },
  606. showDate: function ()
  607. {
  608. var g = this, p = this.options;
  609. if (!this.selectedDate) return;
  610. var dateStr = g.selectedDate.year + "/" + g.selectedDate.month + "/" + g.selectedDate.date;
  611. this.currentDate.hour = parseInt(g.toolbar.time.hour.html(), 10);
  612. this.currentDate.minute = parseInt(g.toolbar.time.minute.html(), 10);
  613. if (p.showTime)
  614. {
  615. dateStr += " " + this.currentDate.hour + ":" + this.currentDate.minute;
  616. }
  617. this.inputText.val(dateStr);
  618. this.inputText.trigger("change").focus();
  619. },
  620. isDateTime: function (dateStr)
  621. {
  622. var g = this, p = this.options;
  623. var r = dateStr.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  624. if (r == null) return false;
  625. var d = new Date(r[1], r[3] - 1, r[4]);
  626. if (d == "NaN") return false;
  627. return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
  628. },
  629. isLongDateTime: function (dateStr)
  630. {
  631. var g = this, p = this.options;
  632. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2})$/;
  633. var r = dateStr.match(reg);
  634. if (r == null) return false;
  635. var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6]);
  636. if (d == "NaN") return false;
  637. return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6]);
  638. },
  639. getFormatDate: function (date)
  640. {
  641. var g = this, p = this.options;
  642. if (date == "NaN") return null;
  643. var format = p.format;
  644. var o = {
  645. "M+": date.getMonth() + 1,
  646. "d+": date.getDate(),
  647. "h+": date.getHours(),
  648. "m+": date.getMinutes(),
  649. "s+": date.getSeconds(),
  650. "q+": Math.floor((date.getMonth() + 3) / 3),
  651. "S": date.getMilliseconds()
  652. }
  653. if (/(y+)/.test(format))
  654. {
  655. format = format.replace(RegExp.$1, (date.getFullYear() + "")
  656. .substr(4 - RegExp.$1.length));
  657. }
  658. for (var k in o)
  659. {
  660. if (new RegExp("(" + k + ")").test(format))
  661. {
  662. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
  663. : ("00" + o[k]).substr(("" + o[k]).length));
  664. }
  665. }
  666. return format;
  667. },
  668. onTextChange: function ()
  669. {
  670. var g = this, p = this.options;
  671. var val = g.inputText.val();
  672. if (val == "")
  673. {
  674. g.selectedDate = null;
  675. return true;
  676. }
  677. if (!p.showTime && !g.isDateTime(val))
  678. {
  679. //恢复
  680. if (!g.usedDate)
  681. {
  682. g.inputText.val("");
  683. } else
  684. {
  685. g.inputText.val(g.getFormatDate(g.usedDate));
  686. }
  687. }
  688. else if (p.showTime && !g.isLongDateTime(val))
  689. {
  690. //恢复
  691. if (!g.usedDate)
  692. {
  693. g.inputText.val("");
  694. } else
  695. {
  696. g.inputText.val(g.getFormatDate(g.usedDate));
  697. }
  698. }
  699. else
  700. {
  701. val = val.replace(/-/g, "/");
  702. var formatVal = g.getFormatDate(new Date(val));
  703. if (formatVal == null)
  704. {
  705. //恢复
  706. if (!g.usedDate)
  707. {
  708. g.inputText.val("");
  709. } else
  710. {
  711. g.inputText.val(g.getFormatDate(g.usedDate));
  712. }
  713. }
  714. g.usedDate = new Date(val); //记录
  715. g.selectedDate = {
  716. year: g.usedDate.getFullYear(),
  717. month: g.usedDate.getMonth() + 1, //注意这里
  718. day: g.usedDate.getDay(),
  719. date: g.usedDate.getDate(),
  720. hour: g.usedDate.getHours(),
  721. minute: g.usedDate.getMinutes()
  722. };
  723. g.currentDate = {
  724. year: g.usedDate.getFullYear(),
  725. month: g.usedDate.getMonth() + 1, //注意这里
  726. day: g.usedDate.getDay(),
  727. date: g.usedDate.getDate(),
  728. hour: g.usedDate.getHours(),
  729. minute: g.usedDate.getMinutes()
  730. };
  731. g.inputText.val(formatVal);
  732. g.trigger('changeDate', [formatVal]);
  733. if ($(g.dateeditor).is(":visible"))
  734. g.bulidContent();
  735. }
  736. },
  737. _setHeight: function (value)
  738. {
  739. var g = this;
  740. if (value > 4)
  741. {
  742. g.text.css({ height: value });
  743. g.inputText.css({ height: value });
  744. g.textwrapper.css({ height: value });
  745. }
  746. },
  747. _setWidth: function (value)
  748. {
  749. var g = this;
  750. if (value > 20)
  751. {
  752. g.text.css({ width: value });
  753. g.inputText.css({ width: value - 20 });
  754. g.textwrapper.css({ width: value });
  755. }
  756. },
  757. _setValue: function (value)
  758. {
  759. var g = this;
  760. if (!value) g.inputText.val('');
  761. if (typeof value == "string")
  762. {
  763. g.inputText.val(value);
  764. }
  765. else if (typeof value == "object")
  766. {
  767. if (value instanceof Date)
  768. {
  769. g.inputText.val(g.getFormatDate(value));
  770. g.onTextChange();
  771. }
  772. }
  773. },
  774. _getValue: function ()
  775. {
  776. return this.usedDate;
  777. },
  778. setEnabled: function ()
  779. {
  780. var g = this, p = this.options;
  781. this.inputText.removeAttr("readonly");
  782. this.text.removeClass('l-text-disabled');
  783. p.disabled = false;
  784. },
  785. setDisabled: function ()
  786. {
  787. var g = this, p = this.options;
  788. this.inputText.attr("readonly", "readonly");
  789. this.text.addClass('l-text-disabled');
  790. p.disabled = true;
  791. }
  792. });
  793. })(jQuery);