ligerDateEditor.js 33 KB

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