CClockIE5.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2018
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /*
  13. CClockIE5.js
  14. This script is used to provide an analog clock for the time picker
  15. prompt component.
  16. */
  17. //Constructor to create a clock component
  18. // oParent: the time picker control
  19. // sDate: string, date used to set the clock to the correct time
  20. // sRef: string, name of the instance of the object
  21. // iMode: 0 = static clock
  22. // 1 = live clock
  23. // bShowSecondHand: true/false
  24. function CClock(oParent, sDate, sRef, iMode, bShowSecondHand, sCVId)
  25. {
  26. this.setCVId(sCVId);
  27. //Clock face height
  28. this.m_iFaceHeight=70;
  29. //Clock face width
  30. this.m_iFaceWidth=60;
  31. var iXPadding = 10;
  32. var iYPadding = 10;
  33. //instantiate the hands on
  34. //the clock face
  35. var sHourHand ='................';
  36. this.m_arHourHand = sHourHand.split(K_PRMT_sEMPTY);
  37. var sMinuteHand ='......................';
  38. this.m_arMinuteHand = sMinuteHand.split(K_PRMT_sEMPTY);
  39. var sSecondHand ='..........................';
  40. this.m_arSecondHand = sSecondHand.split(K_PRMT_sEMPTY);
  41. this.m_bShowSecondHand = bShowSecondHand;
  42. this.m_Y = this.m_iFaceHeight + iXPadding;
  43. this.m_X = this.m_iFaceWidth + iYPadding;
  44. this.m_iClockTicks=12;
  45. this.m_iClockTickDegrees=360/this.m_iClockTicks;
  46. //Date object used to control the clock
  47. if (sDate)
  48. {
  49. //use the given date/time
  50. this.m_dDate = new Date(sDate);
  51. }
  52. else
  53. {
  54. //initialize clock
  55. this.m_dDate = new Date();
  56. this.m_dDate.setHours(0);
  57. this.m_dDate.setMinutes(0);
  58. this.m_dDate.setSeconds(0);
  59. this.m_dDate.setMilliseconds(0);
  60. }
  61. this.m_oParent = oParent;
  62. this.m_sRef=sRef;
  63. //determine browser for DHTML type
  64. this.m_isNS7 = browserIsNS7();
  65. this.init();
  66. this.draw();
  67. this.m_iMode = iMode;
  68. if (this.m_iMode == 1)
  69. {
  70. this.funcInterval = setInterval( this.drawLiveTime.bind(this), 100 );
  71. }
  72. }
  73. CClock.prototype = new CPromptControl();
  74. //initialize the clock
  75. function CClock_init()
  76. {
  77. //initialize the clock
  78. var oclsDivClock = document.createElement("DIV");
  79. oclsDivClock.className = "clsDivClock";
  80. this.m_oParent.appendChild(oclsDivClock);
  81. var oclsDivRelative = document.createElement("DIV");
  82. oclsDivRelative.className = "clsDivRelative";
  83. oclsDivClock.appendChild(oclsDivRelative);
  84. var i = 0;
  85. for (i=1; i < this.m_iClockTicks +1; i++)
  86. {
  87. var oClockDigitElement = document.createElement("DIV");
  88. oClockDigitElement.className = "clsClockDigits";
  89. oClockDigitElement.id = 'clockDigits'+this.m_sRef+ i;
  90. var oClockDigitElementLabel = document.createTextNode(i);
  91. oClockDigitElement.appendChild(oClockDigitElementLabel);
  92. oclsDivRelative.appendChild(oClockDigitElement);
  93. }
  94. for (i=0; i < this.m_arMinuteHand.length; i++)
  95. {
  96. var oClockMinuteElement = document.createElement("DIV");
  97. oClockMinuteElement.className = "clsMinuteHand";
  98. oClockMinuteElement.id = 'minuteHand'+ this.m_sRef+ i;
  99. oclsDivRelative.appendChild(oClockMinuteElement);
  100. }
  101. for (i=0; i < this.m_arHourHand.length; i++)
  102. {
  103. var oClockHourElement = document.createElement("DIV");
  104. oClockHourElement.className = "clsHourHand";
  105. oClockHourElement.id = 'hourHand' + this.m_sRef+ i;
  106. oclsDivRelative.appendChild(oClockHourElement);
  107. }
  108. if (this.m_bShowSecondHand==true)
  109. {
  110. for (i=0; i < this.m_arSecondHand.length; i++)
  111. {
  112. var oClockSecondElement = document.createElement("DIV");
  113. oClockSecondElement.className = "clsSecondHand";
  114. oClockSecondElement.id = 'secondHand' + this.m_sRef+ i;
  115. oclsDivRelative.appendChild(oClockSecondElement);
  116. }
  117. }
  118. }
  119. //render the clock on screen
  120. function CClock_draw()
  121. {
  122. // check if a valid instance is being used
  123. var oId = 'clockDigits' + this.m_sRef + 1;
  124. var o = document.getElementById(oId);
  125. if (o == null) {
  126. return;
  127. }
  128. //draw the clock
  129. //this will draw the whole clock
  130. this.drawClockFace();
  131. this.drawHour();
  132. this.drawMinute();
  133. if (this.m_bShowSecondHand==true)
  134. {
  135. this.drawSecond();
  136. }
  137. }
  138. //render the clock face
  139. function CClock_drawClockFace()
  140. {
  141. //draw the face of the clock
  142. for (var i=0; i < this.m_iClockTicks; ++i)
  143. {
  144. var oId = 'clockDigits' + this.m_sRef + (i+1);
  145. var o = document.getElementById(oId);
  146. if (o != null)
  147. {
  148. if (this.m_isNS7 == true || window.edge == true)
  149. {
  150. o.style.top = this.m_Y +this.m_iFaceHeight*Math.sin(-1.045 +i *this.m_iClockTickDegrees*Math.PI/180) + 'px';
  151. o.style.left = this.m_X +this.m_iFaceWidth*Math.cos(-1.045 +i *this.m_iClockTickDegrees*Math.PI/180) + 'px';
  152. }
  153. else
  154. {
  155. o.style.pixelTop = this.m_Y +this.m_iFaceHeight*Math.sin(-1.045 +i *this.m_iClockTickDegrees*Math.PI/180);
  156. o.style.pixelLeft = this.m_X +this.m_iFaceWidth*Math.cos(-1.045 +i *this.m_iClockTickDegrees*Math.PI/180);
  157. }
  158. }
  159. }
  160. }
  161. //render the second hand
  162. function CClock_drawSecond()
  163. {
  164. var iSeconds = this.m_dDate.getSeconds();
  165. var iSecond = -1.57 + Math.PI * iSeconds/30;
  166. for (var i=0; i < this.m_arSecondHand.length; i++)
  167. {
  168. var oId = 'secondHand' + this.m_sRef + (i);
  169. var o = document.getElementById(oId);
  170. if (o != null)
  171. {
  172. if (this.m_isNS7 == true || window.edge == true)
  173. {
  174. o.style.top = (this.m_Y +i*2*Math.sin(iSecond)) + 'px';
  175. o.style.left = (this.m_X +i*2*Math.cos(iSecond)+5) + 'px';
  176. }
  177. else
  178. {
  179. o.style.pixelTop = this.m_Y +i*2*Math.sin(iSecond);
  180. o.style.pixelLeft = this.m_X +i*2*Math.cos(iSecond)+5;
  181. }
  182. }
  183. }
  184. }
  185. //render the minute hand
  186. function CClock_drawMinute()
  187. {
  188. var iMinutes = this.m_dDate.getMinutes();
  189. var iMinute = -1.57 + Math.PI * iMinutes/30;
  190. for (var i=0; i < this.m_arMinuteHand.length; i++)
  191. {
  192. var oId = 'minuteHand' + this.m_sRef + (i);
  193. var o = document.getElementById(oId);
  194. if (o != null)
  195. {
  196. if (this.m_isNS7 == true || window.edge == true)
  197. {
  198. o.style.top = (this.m_Y +i*2*Math.sin(iMinute))+ 'px';
  199. o.style.left = (this.m_X +i*2*Math.cos(iMinute) + 5) + 'px';
  200. }
  201. else
  202. {
  203. o.style.pixelTop =this.m_Y +i*2*Math.sin(iMinute);
  204. o.style.pixelLeft=this.m_X +i*2*Math.cos(iMinute)+5;
  205. }
  206. }
  207. }
  208. }
  209. //render the hour hand
  210. function CClock_drawHour()
  211. {
  212. var iHours = this.m_dDate.getHours();
  213. var iHour = -1.57 + Math.PI * iHours/6 + Math.PI*parseInt(this.m_dDate.getMinutes(),10)/360;
  214. for (var i=0; i < this.m_arHourHand.length; i++)
  215. {
  216. var oId = 'hourHand' + this.m_sRef + (i);
  217. var o = document.getElementById(oId);
  218. if (o != null)
  219. {
  220. if (this.m_isNS7 == true || window.edge == true)
  221. {
  222. o.style.top = (this.m_Y +i*2*Math.sin(iHour)) + 'px';
  223. o.style.left = (this.m_X +i*2*Math.cos(iHour)+ 5) + 'px';
  224. }
  225. else
  226. {
  227. o.style.pixelTop = this.m_Y +i*2*Math.sin(iHour);
  228. o.style.pixelLeft = this.m_X +i*2*Math.cos(iHour)+5;
  229. }
  230. }
  231. }
  232. }
  233. //set the clock time hours, minutes and seconds
  234. function CClock_clockSetTime(iHour, iMinute, iSecond)
  235. {
  236. var bUpdateHour = false;
  237. if (iHour != this.m_dDate.getHours())
  238. {
  239. this.m_dDate.setHours(iHour);
  240. bUpdateHour = true;
  241. }
  242. if (iMinute != this.m_dDate.getMinutes())
  243. {
  244. this.m_dDate.setMinutes(iMinute);
  245. this.drawMinute();
  246. bUpdateHour = true;
  247. }
  248. if (iSecond != this.m_dDate.getSeconds())
  249. {
  250. this.m_dDate.setSeconds(iSecond);
  251. if (this.m_bShowSecondHand == true)
  252. {
  253. this.drawSecond();
  254. }
  255. }
  256. if (bUpdateHour == true)
  257. {
  258. this.drawHour();
  259. }
  260. }
  261. //draw the live clock
  262. function CClock_drawLiveTime()
  263. {
  264. var dLiveDate = new Date();
  265. var thisTime = dLiveDate.getHours() + K_PRMT_sCOLON + dLiveDate.getMinutes() + K_PRMT_sCOLON + dLiveDate.getSeconds();
  266. if (thisTime == this.lastLiveTimeDraw)
  267. {
  268. // do not redraw the clock if it is the same thing as the last draw.
  269. return;
  270. }
  271. this.lastLiveTimeDraw = thisTime;
  272. this.m_dDate.setHours(dLiveDate.getHours());
  273. this.m_dDate.setMinutes(dLiveDate.getMinutes());
  274. this.m_dDate.setSeconds(dLiveDate.getSeconds());
  275. this.draw();
  276. }
  277. //stop the live clock
  278. function CClock_stopLiveTime()
  279. {
  280. if (this.m_iMode==1)
  281. {
  282. this.m_iMode = 0;
  283. clearInterval(this.funcInterval);
  284. }
  285. }
  286. //Prototypes to assign methods to new instances of the object
  287. CClock.prototype.init = CClock_init;
  288. CClock.prototype.draw = CClock_draw;
  289. CClock.prototype.drawClockFace = CClock_drawClockFace;
  290. CClock.prototype.drawHour = CClock_drawHour;
  291. CClock.prototype.drawMinute = CClock_drawMinute;
  292. CClock.prototype.drawSecond = CClock_drawSecond;
  293. CClock.prototype.clockSetTime = CClock_clockSetTime;
  294. CClock.prototype.drawLiveTime = CClock_drawLiveTime;
  295. CClock.prototype.stopLiveTime = CClock_stopLiveTime;