UU跑腿标准版

ligerDateEditor.js 37KB

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