TimePicker.js
上传用户:kimgenplus
上传日期:2016-06-05
资源大小:20877k
文件大小:15k
源码类别:

OA系统

开发平台:

Java

  1. /*
  2. Copyright (c) 2004-2006, The Dojo Foundation
  3. All Rights Reserved.
  4. Licensed under the Academic Free License version 2.1 or above OR the
  5. modified BSD license. For more information on Dojo licensing, see:
  6. http://dojotoolkit.org/community/licensing.shtml
  7. */
  8. dojo.provide("dojo.widget.TimePicker");
  9. dojo.require("dojo.widget.*");
  10. dojo.require("dojo.widget.HtmlWidget");
  11. dojo.require("dojo.event.*");
  12. dojo.require("dojo.date.serialize");
  13. dojo.require("dojo.date.format");
  14. dojo.require("dojo.dom");
  15. dojo.require("dojo.html.style");
  16. dojo.requireLocalization("dojo.i18n.calendar", "gregorian", null, "ko,zh-cn,zh,sv,ja,en,zh-tw,it,hu,nl,fi,zh-hk,fr,pt,ROOT,es,de,pt-br");
  17. dojo.requireLocalization("dojo.widget", "TimePicker", null, "ROOT");
  18. dojo.widget.defineWidget("dojo.widget.TimePicker", dojo.widget.HtmlWidget, function () {
  19. this.time = "";
  20. this.useDefaultTime = false;
  21. this.useDefaultMinutes = false;
  22. this.storedTime = "";
  23. this.currentTime = {};
  24. this.classNames = {selectedTime:"selectedItem"};
  25. this.any = "any";
  26. this.selectedTime = {hour:"", minute:"", amPm:"", anyTime:false};
  27. this.hourIndexMap = ["", 2, 4, 6, 8, 10, 1, 3, 5, 7, 9, 11, 0];
  28. this.minuteIndexMap = [0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9, 11];
  29. }, {isContainer:false, templateString:"<div class="timePickerContainer" dojoAttachPoint="timePickerContainerNode">nt<table class="timeContainer" cellspacing="0" >ntt<thead>nttt<tr>ntttt<td class="timeCorner cornerTopLeft" valign="top">&nbsp;</td>ntttt<td class="timeLabelContainer hourSelector">${this.calendar.field-hour}</td>ntttt<td class="timeLabelContainer minutesHeading">${this.calendar.field-minute}</td>ntttt<td class="timeCorner cornerTopRight" valign="top">&nbsp;</td>nttt</tr>ntt</thead>ntt<tbody>nttt<tr>ntttt<td valign="top" colspan="2" class="hours">nttttt<table align="center">ntttttt<tbody dojoAttachPoint="hourContainerNode"  ntttttttdojoAttachEvent="onClick: onSetSelectedHour;">nttttttt<tr>ntttttttt<td>12</td>ntttttttt<td>6</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>1</td>ntttttttt<td>7</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>2</td>ntttttttt<td>8</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>3</td>ntttttttt<td>9</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>4</td>ntttttttt<td>10</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>5</td>ntttttttt<td>11</td>nttttttt</tr>ntttttt</tbody>nttttt</table>ntttt</td>ntttt<td valign="top" class="minutes" colspan="2">nttttt<table align="center">ntttttt<tbody dojoAttachPoint="minuteContainerNode" ntttttttdojoAttachEvent="onClick: onSetSelectedMinute;">nttttttt<tr>ntttttttt<td>00</td>ntttttttt<td>30</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>05</td>ntttttttt<td>35</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>10</td>ntttttttt<td>40</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>15</td>ntttttttt<td>45</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>20</td>ntttttttt<td>50</td>nttttttt</tr>nttttttt<tr>ntttttttt<td>25</td>ntttttttt<td>55</td>nttttttt</tr>ntttttt</tbody>nttttt</table>ntttt</td>nttt</tr>nttt<tr>ntttt<td class="cornerBottomLeft">&nbsp;</td>ntttt<td valign="top" class="timeOptions">nttttt<table class="amPmContainer">ntttttt<tbody dojoAttachPoint="amPmContainerNode" ntttttttdojoAttachEvent="onClick: onSetSelectedAmPm;">nttttttt<tr>ntttttttt<td id="am">${this.calendar.am}</td>ntttttttt<td id="pm">${this.calendar.pm}</td>nttttttt</tr>ntttttt</tbody>nttttt</table>ntttt</td>ntttt<td class="timeOptions">nttttt<div dojoAttachPoint="anyTimeContainerNode" nttttttdojoAttachEvent="onClick: onSetSelectedAnyTime;" nttttttclass="anyTimeContainer">${this.widgetStrings.any}</div>ntttt</td>ntttt<td class="cornerBottomRight">&nbsp;</td>nttt</tr>ntt</tbody>nt</table>n</div>n", templateCssString:"/*Time Picker */n.timePickerContainer {ntwidth:122px;ntfont-family:Tahoma, Myriad, Helvetica, Arial, Verdana, sans-serif;ntfont-size:16px;n}nn.timeContainer {ntborder-collapse:collapse;ntborder-spacing:0;n}nn.timeContainer thead {ntcolor:#293a4b;ntfont-size:0.9em;ntfont-weight:700;n}nn.timeContainer thead td {ntpadding:0.25em;ntfont-size:0.80em;ntborder-bottom:1px solid #6782A8;n}nn.timeCorner {ntwidth:10px;n}nn.cornerTopLeft {ntbackground: url("images/dpCurveTL.png") top left no-repeat;n}nn.cornerTopRight {ntbackground: url("images/dpCurveTR.png") top right no-repeat;n}nn.timeLabelContainer {ntbackground: url("images/dpMonthBg.png") top left repeat-x;n}nn.hours, .minutes, .timeBorder {ntbackground: #7591bc url("images/dpBg.gif") top left repeat-x;nn}nn.hours td, .minutes td {ntpadding:0.2em;nttext-align:center;ntfont-size:0.7em;ntfont-weight:bold;ntcursor:pointer;ntcursor:hand;ntcolor:#fff;n}nn.minutes {ntborder-left:1px solid #f5d1db;n}nn.hours {ntborder-right:1px solid #6782A8;n}nn.hourSelector {ntborder-right:1px solid #6782A8;ntpadding:5px;ntpadding-right:10px;n}nn.minutesSelector {ntpadding:5px;ntborder-left:1px solid #f5c7d4;nttext-align:center;n}nn.minutesHeading {ntpadding-left:9px !important;n}nn.timeOptions {ntbackground-color:#F9C9D7;n}nn.timeContainer .cornerBottomLeft, .timeContainer .cornerBottomRight, .timeContainer .timeOptions {ntborder-top:1px solid #6782A8;n}nn.timeContainer .cornerBottomLeft {ntbackground: url("images/dpCurveBL.png") bottom left no-repeat !important;ntwidth:9px !important;ntpadding:0;ntmargin:0;n}nn.timeContainer .cornerBottomRight {ntbackground: url("images/dpCurveBR.png") bottom right no-repeat !important;ntwidth:9px !important;ntpadding:0;ntmargin:0;n}nn.timeOptions {ntcolor:#fff;ntbackground:url("images/dpYearBg.png") top left repeat-x;nn}nn.selectedItem {ntbackground-color:#fff;ntcolor:#6782a8 !important;n}nn.timeOptions .selectedItem {ntcolor:#fff !important;ntbackground-color:#9ec3fb !important;n}nn.anyTimeContainer {nttext-align:center;ntfont-weight:bold;ntfont-size:0.7em;ntpadding:0.1em;ntcursor:pointer;ntcursor:hand;ntcolor:#fff !important;n}nn.amPmContainer {ntwidth:100%;n}nn.amPmContainer td {nttext-align:center;ntfont-size:0.7em;ntfont-weight:bold;ntcursor:pointer;ntcursor:hand;ntcolor:#fff;n}nnnn/*.timePickerContainer {ntmargin:1.75em 0 0.5em 0;ntwidth:10em;ntfloat:left;n}nn.timeContainer {ntborder-collapse:collapse;ntborder-spacing:0;n}nn.timeContainer thead td{ntborder-bottom:1px solid #e6e6e6;ntpadding:0 0.4em 0.2em 0.4em;n}nn.timeContainer td {ntfont-size:0.9em;ntpadding:0 0.25em 0 0.25em;nttext-align:left;ntcursor:pointer;cursor:hand;n}nn.timeContainer td.minutesHeading {ntborder-left:1px solid #e6e6e6;ntborder-right:1px solid #e6e6e6;tn}nn.timeContainer .minutes {ntborder-left:1px solid #e6e6e6;ntborder-right:1px solid #e6e6e6;n}nn.selectedItem {ntbackground-color:#3a3a3a;ntcolor:#ffffff;n}*/n", templateCssPath:dojo.uri.moduleUri("dojo.widget", "templates/TimePicker.css"), postMixInProperties:function (localProperties, frag) {
  30. dojo.widget.TimePicker.superclass.postMixInProperties.apply(this, arguments);
  31. this.calendar = dojo.i18n.getLocalization("dojo.i18n.calendar", "gregorian", this.lang);
  32. this.widgetStrings = dojo.i18n.getLocalization("dojo.widget", "TimePicker", this.lang);
  33. }, fillInTemplate:function (args, frag) {
  34. var source = this.getFragNodeRef(frag);
  35. dojo.html.copyStyle(this.domNode, source);
  36. if (args.value) {
  37. if (args.value instanceof Date) {
  38. this.storedTime = dojo.date.toRfc3339(args.value);
  39. } else {
  40. this.storedTime = args.value;
  41. }
  42. }
  43. this.initData();
  44. this.initUI();
  45. }, initData:function () {
  46. if (this.storedTime.indexOf("T") != -1 && this.storedTime.split("T")[1] && this.storedTime != " " && this.storedTime.split("T")[1] != "any") {
  47. this.time = dojo.widget.TimePicker.util.fromRfcDateTime(this.storedTime, this.useDefaultMinutes, this.selectedTime.anyTime);
  48. } else {
  49. if (this.useDefaultTime) {
  50. this.time = dojo.widget.TimePicker.util.fromRfcDateTime("", this.useDefaultMinutes, this.selectedTime.anyTime);
  51. } else {
  52. this.selectedTime.anyTime = true;
  53. this.time = dojo.widget.TimePicker.util.fromRfcDateTime("", 0, 1);
  54. }
  55. }
  56. }, initUI:function () {
  57. if (!this.selectedTime.anyTime && this.time) {
  58. var amPmHour = dojo.widget.TimePicker.util.toAmPmHour(this.time.getHours());
  59. var hour = amPmHour[0];
  60. var isAm = amPmHour[1];
  61. var minute = this.time.getMinutes();
  62. var minuteIndex = parseInt(minute / 5);
  63. this.onSetSelectedHour(this.hourIndexMap[hour]);
  64. this.onSetSelectedMinute(this.minuteIndexMap[minuteIndex]);
  65. this.onSetSelectedAmPm(isAm);
  66. } else {
  67. this.onSetSelectedAnyTime();
  68. }
  69. }, setTime:function (date) {
  70. if (date) {
  71. this.selectedTime.anyTime = false;
  72. this.setDateTime(dojo.date.toRfc3339(date));
  73. } else {
  74. this.selectedTime.anyTime = true;
  75. }
  76. this.initData();
  77. this.initUI();
  78. }, setDateTime:function (rfcDate) {
  79. this.storedTime = rfcDate;
  80. }, onClearSelectedHour:function (evt) {
  81. this.clearSelectedHour();
  82. }, onClearSelectedMinute:function (evt) {
  83. this.clearSelectedMinute();
  84. }, onClearSelectedAmPm:function (evt) {
  85. this.clearSelectedAmPm();
  86. }, onClearSelectedAnyTime:function (evt) {
  87. this.clearSelectedAnyTime();
  88. if (this.selectedTime.anyTime) {
  89. this.selectedTime.anyTime = false;
  90. this.time = dojo.widget.TimePicker.util.fromRfcDateTime("", this.useDefaultMinutes);
  91. this.initUI();
  92. }
  93. }, clearSelectedHour:function () {
  94. var hourNodes = this.hourContainerNode.getElementsByTagName("td");
  95. for (var i = 0; i < hourNodes.length; i++) {
  96. dojo.html.setClass(hourNodes.item(i), "");
  97. }
  98. }, clearSelectedMinute:function () {
  99. var minuteNodes = this.minuteContainerNode.getElementsByTagName("td");
  100. for (var i = 0; i < minuteNodes.length; i++) {
  101. dojo.html.setClass(minuteNodes.item(i), "");
  102. }
  103. }, clearSelectedAmPm:function () {
  104. var amPmNodes = this.amPmContainerNode.getElementsByTagName("td");
  105. for (var i = 0; i < amPmNodes.length; i++) {
  106. dojo.html.setClass(amPmNodes.item(i), "");
  107. }
  108. }, clearSelectedAnyTime:function () {
  109. dojo.html.setClass(this.anyTimeContainerNode, "anyTimeContainer");
  110. }, onSetSelectedHour:function (evt) {
  111. this.onClearSelectedAnyTime();
  112. this.onClearSelectedHour();
  113. this.setSelectedHour(evt);
  114. this.onSetTime();
  115. }, setSelectedHour:function (evt) {
  116. if (evt && evt.target) {
  117. if (evt.target.nodeType == dojo.dom.ELEMENT_NODE) {
  118. var eventTarget = evt.target;
  119. } else {
  120. var eventTarget = evt.target.parentNode;
  121. }
  122. dojo.event.browser.stopEvent(evt);
  123. dojo.html.setClass(eventTarget, this.classNames.selectedTime);
  124. this.selectedTime["hour"] = eventTarget.innerHTML;
  125. } else {
  126. if (!isNaN(evt)) {
  127. var hourNodes = this.hourContainerNode.getElementsByTagName("td");
  128. if (hourNodes.item(evt)) {
  129. dojo.html.setClass(hourNodes.item(evt), this.classNames.selectedTime);
  130. this.selectedTime["hour"] = hourNodes.item(evt).innerHTML;
  131. }
  132. }
  133. }
  134. this.selectedTime.anyTime = false;
  135. }, onSetSelectedMinute:function (evt) {
  136. this.onClearSelectedAnyTime();
  137. this.onClearSelectedMinute();
  138. this.setSelectedMinute(evt);
  139. this.selectedTime.anyTime = false;
  140. this.onSetTime();
  141. }, setSelectedMinute:function (evt) {
  142. if (evt && evt.target) {
  143. if (evt.target.nodeType == dojo.dom.ELEMENT_NODE) {
  144. var eventTarget = evt.target;
  145. } else {
  146. var eventTarget = evt.target.parentNode;
  147. }
  148. dojo.event.browser.stopEvent(evt);
  149. dojo.html.setClass(eventTarget, this.classNames.selectedTime);
  150. this.selectedTime["minute"] = eventTarget.innerHTML;
  151. } else {
  152. if (!isNaN(evt)) {
  153. var minuteNodes = this.minuteContainerNode.getElementsByTagName("td");
  154. if (minuteNodes.item(evt)) {
  155. dojo.html.setClass(minuteNodes.item(evt), this.classNames.selectedTime);
  156. this.selectedTime["minute"] = minuteNodes.item(evt).innerHTML;
  157. }
  158. }
  159. }
  160. }, onSetSelectedAmPm:function (evt) {
  161. this.onClearSelectedAnyTime();
  162. this.onClearSelectedAmPm();
  163. this.setSelectedAmPm(evt);
  164. this.selectedTime.anyTime = false;
  165. this.onSetTime();
  166. }, setSelectedAmPm:function (evt) {
  167. var eventTarget = evt.target;
  168. if (evt && eventTarget) {
  169. if (eventTarget.nodeType != dojo.dom.ELEMENT_NODE) {
  170. eventTarget = eventTarget.parentNode;
  171. }
  172. dojo.event.browser.stopEvent(evt);
  173. this.selectedTime.amPm = eventTarget.id;
  174. dojo.html.setClass(eventTarget, this.classNames.selectedTime);
  175. } else {
  176. evt = evt ? 0 : 1;
  177. var amPmNodes = this.amPmContainerNode.getElementsByTagName("td");
  178. if (amPmNodes.item(evt)) {
  179. this.selectedTime.amPm = amPmNodes.item(evt).id;
  180. dojo.html.setClass(amPmNodes.item(evt), this.classNames.selectedTime);
  181. }
  182. }
  183. }, onSetSelectedAnyTime:function (evt) {
  184. this.onClearSelectedHour();
  185. this.onClearSelectedMinute();
  186. this.onClearSelectedAmPm();
  187. this.setSelectedAnyTime();
  188. this.onSetTime();
  189. }, setSelectedAnyTime:function (evt) {
  190. this.selectedTime.anyTime = true;
  191. dojo.html.setClass(this.anyTimeContainerNode, this.classNames.selectedTime + " " + "anyTimeContainer");
  192. }, onClick:function (evt) {
  193. dojo.event.browser.stopEvent(evt);
  194. }, onSetTime:function () {
  195. if (this.selectedTime.anyTime) {
  196. this.time = new Date();
  197. var tempDateTime = dojo.widget.TimePicker.util.toRfcDateTime(this.time);
  198. this.setDateTime(tempDateTime.split("T")[0]);
  199. } else {
  200. var hour = 12;
  201. var minute = 0;
  202. var isAm = false;
  203. if (this.selectedTime["hour"]) {
  204. hour = parseInt(this.selectedTime["hour"], 10);
  205. }
  206. if (this.selectedTime["minute"]) {
  207. minute = parseInt(this.selectedTime["minute"], 10);
  208. }
  209. if (this.selectedTime["amPm"]) {
  210. isAm = (this.selectedTime["amPm"].toLowerCase() == "am");
  211. }
  212. this.time = new Date();
  213. this.time.setHours(dojo.widget.TimePicker.util.fromAmPmHour(hour, isAm));
  214. this.time.setMinutes(minute);
  215. this.setDateTime(dojo.widget.TimePicker.util.toRfcDateTime(this.time));
  216. }
  217. this.onValueChanged(this.time);
  218. }, onValueChanged:function (date) {
  219. }});
  220. dojo.widget.TimePicker.util = new function () {
  221. this.toRfcDateTime = function (jsDate) {
  222. if (!jsDate) {
  223. jsDate = new Date();
  224. }
  225. jsDate.setSeconds(0);
  226. return dojo.date.strftime(jsDate, "%Y-%m-%dT%H:%M:00%z");
  227. };
  228. this.fromRfcDateTime = function (rfcDate, useDefaultMinutes, isAnyTime) {
  229. var tempDate = new Date();
  230. if (!rfcDate || rfcDate.indexOf("T") == -1) {
  231. if (useDefaultMinutes) {
  232. tempDate.setMinutes(Math.floor(tempDate.getMinutes() / 5) * 5);
  233. } else {
  234. tempDate.setMinutes(0);
  235. }
  236. } else {
  237. var tempTime = rfcDate.split("T")[1].split(":");
  238. var tempDate = new Date();
  239. tempDate.setHours(tempTime[0]);
  240. tempDate.setMinutes(tempTime[1]);
  241. }
  242. return tempDate;
  243. };
  244. this.toAmPmHour = function (hour) {
  245. var amPmHour = hour;
  246. var isAm = true;
  247. if (amPmHour == 0) {
  248. amPmHour = 12;
  249. } else {
  250. if (amPmHour > 12) {
  251. amPmHour = amPmHour - 12;
  252. isAm = false;
  253. } else {
  254. if (amPmHour == 12) {
  255. isAm = false;
  256. }
  257. }
  258. }
  259. return [amPmHour, isAm];
  260. };
  261. this.fromAmPmHour = function (amPmHour, isAm) {
  262. var hour = parseInt(amPmHour, 10);
  263. if (isAm && hour == 12) {
  264. hour = 0;
  265. } else {
  266. if (!isAm && hour < 12) {
  267. hour = hour + 12;
  268. }
  269. }
  270. return hour;
  271. };
  272. };