Time.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2020
  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. @private
  14. @class
  15. */
  16. cognos.Prompt.Control.Time = cognos.Prompt.Control.f_extend({
  17. f_initialize: function( v_oProps )
  18. {
  19. this[K_PRMT_LABEL_RANGE_LOWEST] = PMT_RNG_EARLIEST_TIME;
  20. this[K_PRMT_LABEL_RANGE_HIGHEST] = PMT_RNG_LATEST_TIME;
  21. this._type_ = "cognos.Prompt.Control.Time";
  22. this.f_parent( v_oProps ); // call parent's initialize()
  23. if ( this["@selectTimeUI"] )
  24. {
  25. this["@selectUI"] = this["@selectTimeUI"];
  26. }
  27. if ( !this["@selectUI"] )
  28. {
  29. this["@selectUI"] = "clock";
  30. }
  31. if ( typeof this["@display"] == K_PRMT_sSTRING )
  32. {
  33. this["@display"] = parseInt( this["@display"], 10 );
  34. }
  35. if ( typeof this["@display"] != "number" || isNaN(this["@display"]) )
  36. {
  37. this["@display"] = ( this["@showMilliseconds"] ? 0 : (this["@showSeconds"] ? 1 : 2) );
  38. }
  39. if ( typeof this["@type"] == K_PRMT_sSTRING )
  40. {
  41. this["@type"] = parseInt( this["@type"], 10 );
  42. }
  43. if ( typeof this["@type"] != "number" || isNaN(this["@type"]) )
  44. {
  45. if (typeof g_24HourClock != K_PRMT_sUNDEFINED && g_24HourClock.toString().toLowerCase() == "true")
  46. {
  47. this["@type"] = 2;
  48. }
  49. else
  50. {
  51. this["@type"] = (typeof g_AMPMLocation != K_PRMT_sUNDEFINED && g_AMPMLocation == 'left' ? 1 : 0);
  52. }
  53. }
  54. if ( this["@clockMode"] == "static" )
  55. {
  56. this["@clockMode"] = 0;
  57. }
  58. else if ( this["@clockMode"] == "live" )
  59. {
  60. this["@clockMode"] = 1;
  61. }
  62. else if ( typeof this["@clockMode"] == K_PRMT_sSTRING )
  63. {
  64. this["@clockMode"] = parseInt( this["@clockMode"], 10 );
  65. }
  66. if ( typeof this["@clockMode"] != "number" || isNaN(this["@clockMode"]) )
  67. {
  68. this["@clockMode"] = 0;
  69. }
  70. this.m_bSkipAdornments = true;
  71. this.f_initCompleted();
  72. },
  73. checkData: function()
  74. {
  75. // We keep a reference to the parent function
  76. // Calling this.m_oFrom functions seems to have a side effect on this.f_parent().
  77. var v_fnParent = this.f_parent;
  78. this.m_bValid = ( this.m_oControl.m_bValid );
  79. this.f_parent = v_fnParent; // call parent's checkData using saved reference.
  80. this.f_parent(); // call parent's checkData using saved reference.
  81. return this.m_bValid;
  82. },
  83. clearValues: function()
  84. {
  85. this.f_parent();
  86. this.f_setTime( "00:00:00.000" );
  87. },
  88. f_drawCompleted: function()
  89. {
  90. // We keep a reference to the parent function
  91. // Calling this.m_oFrom.f_drawCompleted seems to have a side effect on this.f_parent().
  92. var v_fnParent = this.f_parent;
  93. var v_divClock = $( this.f_getId("clock") );
  94. if ( v_divClock )
  95. {
  96. this.m_clockDisplay = new CClock( v_divClock, null, this.f_getId("clockDisplay"), this["@clockMode"], this["@showSeconds"] );
  97. }
  98. if (!this["@timeZone"]) {
  99. this["@timeZone"] = "GMT";
  100. }
  101. if (!this["@hourFormat"]) {
  102. this["@hourFormat"] = "hh";
  103. }
  104. if (!this["@minuteFormat"]) {
  105. this["@minuteFormat"] = "mm";
  106. }
  107. if (!this["@secondFormat"]) {
  108. this["@secondFormat"] = "ss";
  109. }
  110. var v_oControl = new CTimePicker(
  111. null, //oSubmit,
  112. $(this.f_getId("_Time")), //oForm,
  113. $(this.f_getId("hours")), //oHours,
  114. $(this.f_getId("minutes")), //oMinutes,
  115. $(this.f_getId("seconds")), //oSeconds,
  116. $(this.f_getId("milliseconds")), //oMilliseconds,
  117. $(this.f_getId("AMPM")), //oAMPM,
  118. null, //oImgTest,
  119. $(this.f_getId("clock")), //oClockBox,
  120. $(this.f_getId("editBox")), //oEditBox,
  121. this.f_getId(), //sRef,
  122. this["@type"], //iType,
  123. this["@display"], //iDisplay,
  124. (this["@selectUI"] != "editBox"), //bInline,
  125. this["@timeZone"], //sTimeZone,
  126. this["@hourFormat"], //sHourFormat,
  127. this["@minuteFormat"], //sMinuteFormat,
  128. this["@secondFormat"], //sSecondFormat,
  129. this["@clockMode"], //iMode,
  130. this.m_clockDisplay, //sClock,
  131. this["@defaultValue"], //sTime,
  132. this["@required"], //bRequired,
  133. null, //sSubmitType,
  134. this["CVId"], // sCVId
  135. this.f_getControlId(), // sbaseId
  136. this
  137. );
  138. v_oControl.m_oParent = this;
  139. var v_td = $(this.f_getId("btnTimeUp"));
  140. if ( v_td )
  141. {
  142. PRMTUtils.f_addEvent( v_td, "click", v_oControl.timeUp.bind(v_oControl) );
  143. if (window.ie)
  144. {
  145. PRMTUtils.f_addEvent( v_td, "dblclick", v_oControl.timeUp.bind(v_oControl) );
  146. }
  147. }
  148. v_td = $(this.f_getId("btnTimeDown"));
  149. if ( v_td )
  150. {
  151. PRMTUtils.f_addEvent( v_td, "click", v_oControl.timeDown.bind(v_oControl) );
  152. if (window.ie)
  153. {
  154. PRMTUtils.f_addEvent( v_td, "dblclick", v_oControl.timeDown.bind(v_oControl) );
  155. }
  156. }
  157. var v_cb = $( this.f_getId( "chkAnyValue" ) );
  158. if ( v_cb )
  159. {
  160. PRMTUtils.f_addEvent( v_cb, "click", this.f_toggleDisable.bind(this) );
  161. }
  162. this.m_oControl = v_oControl;
  163. this.f_setFocusEvents( v_oControl.m_oHours ? v_oControl.m_oHours.id : this.f_getId("hours"), v_oControl.bCheckHour );
  164. this.f_setFocusEvents( v_oControl.m_oMinutes ? v_oControl.m_oMinutes.id : this.f_getId("minutes"), v_oControl.bCheckMinute );
  165. this.f_setFocusEvents( v_oControl.m_oSeconds ? v_oControl.m_oSeconds.id : this.f_getId("seconds"), v_oControl.bCheckSecond );
  166. this.f_setFocusEvents( v_oControl.m_oMilliseconds ? v_oControl.m_oMilliseconds.id : this.f_getId("milliseconds"), v_oControl.bCheckMillisecond );
  167. this.f_setFocusEvents( v_oControl.m_oAMPM ? v_oControl.m_oAMPM.id : this.f_getId("AMPM"), v_oControl.bCheckAMPM );
  168. // link a11y label if it exists using FOR
  169. var v_a11yLabel = $( this.f_getId("PRMT_LBL_") );
  170. if ( v_a11yLabel && !v_a11yLabel.getAttribute("for")) {
  171. v_a11yLabel.setAttribute("for", this.f_getId("hours"));
  172. }
  173. this.f_parent = v_fnParent; // call parent's f_drawCompleted using saved reference.
  174. this.f_parent(); // call parent's f_drawCompleted using saved reference.
  175. }
  176. });
  177. /**
  178. Compare values. See {@link cognos.Prompt.Control#f_compare} for details.
  179. @see cognos.Prompt.Control#f_compare
  180. @private
  181. @return {Integer}
  182. */
  183. cognos.Prompt.Control.Time.prototype.f_compare = function( v_oValue )
  184. {
  185. var v_iRetval = 1;
  186. if ( v_oValue )
  187. {
  188. var v_dThis = dParseTime( this.m_oControl.sGetValue() );
  189. var v_dCompareTo = dParseTime( v_oValue.m_oControl.sGetValue() );
  190. var v_iThis = v_dThis.getTime();
  191. var v_iCompareTo = v_dCompareTo.getTime();
  192. if ( v_iCompareTo > v_iThis )
  193. {
  194. v_iRetval = -1;
  195. }
  196. else if ( v_iCompareTo == v_iThis )
  197. {
  198. v_iRetval = 0;
  199. }
  200. }
  201. return v_iRetval;
  202. };
  203. /**
  204. Sets the JavaScript references used by custom scripts based on generated code from Blaring and before.
  205. (Custom Scripts Workaround)
  206. @private
  207. @return {void}
  208. */
  209. cognos.Prompt.Control.Time.prototype.f_CSW_init = function()
  210. {
  211. this.f_CSW_createJSObject( "timePicker", this.m_oControl );
  212. if (this["@selectUI"] == "clock")
  213. {
  214. this.f_CSW_createJSObject( "clockDisplay", this.m_clockDisplay );
  215. }
  216. };
  217. /**
  218. @private
  219. @param {C_PromptElement} v_el Container.
  220. @param {String} v_sId
  221. @param {String} v_sValue
  222. @param {String} v_sSeparator
  223. @param {Integer} v_iSize
  224. @return {void}
  225. */
  226. cognos.Prompt.Control.Time.prototype.f_drawField = function( v_el, v_sId, v_sValue, v_sSeparator, v_iSize, inputLabel )
  227. {
  228. if ( v_sSeparator )
  229. {
  230. $CE( "span", {"class": "clsColon pcl"}, v_el ).f_appendText( v_sSeparator );
  231. }
  232. var newInput = $CE( "input", {
  233. "id": this.f_getId( v_sId ),
  234. "name": this.f_getId( v_sId ),
  235. "class": "clsSelectTimeControl",
  236. "style": "border:none;",
  237. "value": v_sValue,
  238. "size": ( v_iSize ? v_iSize : 2 ),
  239. "maxLength": ( v_iSize ? v_iSize : 2 ) }, $CE( "span", {}, v_el ) );
  240. if (inputLabel != null){
  241. newInput.f_setProperty("aria-label", inputLabel);
  242. }
  243. if (this.isRequired()) {
  244. newInput.f_setProperty("aria-required", "true");
  245. }
  246. };
  247. /**
  248. @private
  249. @param {C_PromptElement} v_el Container.
  250. @return {void}
  251. */
  252. cognos.Prompt.Control.Time.prototype.f_drawInput = function( v_el )
  253. {
  254. var v_oInput = null;
  255. var v_tbl = $CE( "table", { "border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding": 0, "cellSpacing": 0, "role":K_PRMT_ARIA_ROLE_PRESENTATION }, v_el );
  256. var v_tbd = $CE( "tbody", {}, v_tbl );
  257. var v_tr = $CE( "tr", {}, v_tbd );
  258. var v_td = null;
  259. var v_ipt = null;
  260. if ( this["@required"] == false && !this["@suppressDisabled"] && !this.m_bIsAComponent )
  261. {
  262. v_td = $CE( "td", {"vAlign": "top"}, v_tr );
  263. v_ipt = $CE( "input", {
  264. "type": "checkbox",
  265. "value": "anyValue",
  266. "id": this.f_getId( "chkAnyValue" ),
  267. "checked": "checked" }, v_td );
  268. }
  269. v_td = $CE( "td", {"vAlign": "top"}, v_tr );
  270. v_tbl = $CE( "table", { "border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding": 0, "cellSpacing": 0, "class": "clsSelectTime", "role":K_PRMT_ARIA_ROLE_PRESENTATION }, v_td );
  271. v_tbd = $CE( "tbody", {}, v_tbl );
  272. var v_div = null;
  273. if ( this["@selectUI"] == "clock" )
  274. {
  275. v_tr = $CE( "tr", {"class": "clsClockPosition"}, v_tbd );
  276. v_td = $CE( "td", { "height": "100%", "colSpan": 3 }, v_tr );
  277. v_div = $CE( "div", {"class": "clsClockBlock", "align": "center"}, v_td );
  278. v_div = $CE( "div", {"id": this.f_getId("clock"), "class": "clsClock"}, v_div );
  279. v_div.f_appendText(K_PRMT_sEMPTY);
  280. }
  281. v_tr = $CE( "tr", {}, v_tbd );
  282. if ( this["@required"] && !this.isMulti() && !this["@hideAdornments"])
  283. {
  284. this.f_drawAdornments(v_tr);
  285. }
  286. v_td = $CE( "td", {"id": this.f_getId("timeEditBox"), "noWrap": "noWrap"}, v_tr );
  287. var v_sClass = "clsSelectTimeEditBox" +
  288. ( this["@display"] === 0 && (this["@type"] === 0 || this["@type"] == 1) ? "Extend" : K_PRMT_sEMPTY );
  289. var v_sCustomDir = cssParser( this["@style"], "direction", true );
  290. var v_Container = (this.m_elRangeContainer ? this.m_elRangeContainer : $( this.f_getId(this.m_sDivPrefix) ));
  291. var v_FinalDir = ((v_sCustomDir == "ltr" || v_sCustomDir == "rtl") ? v_sCustomDir : PRMT_BidiUtils.lookupDirection(v_Container));
  292. v_sStyle = "direction:ltr";
  293. if (v_FinalDir == "rtl")
  294. {
  295. v_sStyle += ";text-align:right";
  296. v_sClass += "RTL";
  297. }
  298. v_div = $CE( "div", {"id": this.f_getId("editBox"), "class": v_sClass, "style": v_sStyle}, v_td );
  299. if ( this["@type"] == 1)
  300. {
  301. this.f_drawField( v_div, "AMPM", "PM", null, null, PMT_DTP_TIME_AMPM_ENTRY_FIELD );
  302. }
  303. this.f_drawField( v_div, "hours", 12, null, null, PMT_DTP_TIME_HOURS_ENTRY_FIELD );
  304. this.f_drawField( v_div, "minutes", 10, K_PRMT_sCOLON , null, PMT_DTP_TIME_MINUTES_ENTRY_FIELD);
  305. if ( this["@display"] == 1 || this["@display"] === 0)
  306. {
  307. this.f_drawField( v_div, "seconds", 59, K_PRMT_sCOLON, null, PMT_DTP_TIME_SECONDS_ENTRY_FIELD );
  308. }
  309. if ( this["@display"] === 0)
  310. {
  311. this.f_drawField( v_div, "milliseconds", "000", K_PRMT_sDOT, 3, PMT_DTP_TIME_MILLISECONDS_ENTRY_FIELD );
  312. }
  313. if ( this["@type"] === 0 )
  314. {
  315. this.f_drawField( v_div, "AMPM", "PM" , null, null, PMT_DTP_TIME_AMPM_ENTRY_FIELD);
  316. }
  317. v_td = $CE( "td", {}, v_tr );
  318. v_tbl = $CE( "table", { "border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding": 0, "cellSpacing": 0, "role":K_PRMT_ARIA_ROLE_PRESENTATION }, v_td );
  319. v_tbd = $CE( "tbody", {}, v_tbl );
  320. v_tr = $CE( "tr", {}, v_tbd );
  321. v_td = $CE( "td", {
  322. "id": this.f_getId("btnTimeUp"),
  323. "width": 15,
  324. "align": "right",
  325. "class": "clsToolbarButton",
  326. "onmouseover": "buttonOver(this)",
  327. "onmouseout": "buttonOut(this)",
  328. "onmousedown": "buttonOver(this)",
  329. "aria-label": PMT_DTP_TIME_BUTTON_INCREMENT,
  330. "role":K_PRMT_TREE_ROLE_BUTTON}, v_tr );
  331. var v_img = $CE( "div", {
  332. "class" : "clsBtnTimeUp",
  333. "border": 0,
  334. "align": "top",
  335. "alt": ""}, v_td );
  336. v_tr = $CE( "tr", {}, v_tbd );
  337. v_td = $CE( "td", {
  338. "id": this.f_getId("btnTimeDown"),
  339. "width": 15,
  340. "align": "right",
  341. "class": "clsToolbarButton",
  342. "onmouseover": "buttonOver(this)",
  343. "onmouseout": "buttonOut(this)",
  344. "onmousedown": "buttonOver(this)",
  345. "aria-label": PMT_DTP_TIME_BUTTON_DECREMENT,
  346. "role":K_PRMT_TREE_ROLE_BUTTON}, v_tr );
  347. var v_img = $CE( "div", {
  348. "class" : "clsBtnTimeDown",
  349. "border": 0,
  350. "align": "top",
  351. "alt": ""}, v_td );
  352. $CE("input", {"type": "hidden", "id": this.f_getId("_Time"), "value":"00:00:00.000"}, v_el );
  353. return v_oInput;
  354. };
  355. /**
  356. @private
  357. @return {cognos.Value}
  358. */
  359. cognos.Prompt.Control.Time.prototype.f_getPV = function()
  360. {
  361. var v_cb = $( this.f_getId( "chkAnyValue" ) );
  362. var v_bEnabled = ( v_cb && !v_cb.checked ? false : true);
  363. return (v_bEnabled? {
  364. "use": this.m_oControl.sGetSQLTime(),
  365. "display": this.m_oControl.sGetFormatTime()
  366. }: null);
  367. };
  368. /**
  369. Event handler for a keypress event which actually will be keyup for IE and keydown for FF
  370. @private
  371. @return {void}
  372. */
  373. cognos.Prompt.Control.Time.prototype.f_keyPress = function(event)
  374. {
  375. /*
  376. Hack to fix RTC 11990: Time prompt does not retain typed in value in FireFox.
  377. extensions.js overrides Function.prototype.bind; the implementation of which preserves
  378. function arguments from previous calls to the same function...that behaviour cannot be fixed
  379. without affecting other prompt controls.
  380. */
  381. var eventArg = event;
  382. if(arguments.length != 0)
  383. {
  384. eventArg = arguments[arguments.length - 1];
  385. }
  386. this.m_oControl.keyPress(eventArg, this);
  387. this.m_fctKeyUp();
  388. };
  389. /**
  390. @private
  391. @return {void}
  392. */
  393. cognos.Prompt.Control.Time.prototype.f_setCurrentFocus = function()
  394. {
  395. this.m_oControl.setCurrentFocus(this);
  396. };
  397. /**
  398. Change control state.
  399. @private
  400. @param {boolean} v_bState
  401. @return {void}
  402. */
  403. cognos.Prompt.Control.Time.prototype.f_setEnabled = function( v_bState )
  404. {
  405. if ( v_bState ) {
  406. this.m_oControl.enable();
  407. }
  408. else {
  409. this.m_oControl.disable();
  410. }
  411. };
  412. /**
  413. Map a KeyUp/Keydown(IE/FF) event handler.
  414. @private
  415. @param {String} v_sId
  416. @param {String} v_fKeyUpHandler
  417. @return {void}
  418. */
  419. cognos.Prompt.Control.Time.prototype.f_setFocusEvents = function( v_sId, v_fKeyUpHandler )
  420. {
  421. var v_el = $( v_sId );
  422. if ( v_el )
  423. {
  424. v_el.m_oControl = this.m_oControl;
  425. if ( v_fKeyUpHandler )
  426. {
  427. v_el.m_fctKeyUp = v_fKeyUpHandler.bind(this.m_oControl);
  428. }
  429. PRMTUtils.f_addEvent( v_el, "keyup", this.f_keyPress.bind(v_el) );
  430. PRMTUtils.f_addEvent( v_el, "blur", this.m_oControl.refreshTime.bind(this.m_oControl) );
  431. PRMTUtils.f_addEvent( v_el, "focus", this.f_setCurrentFocus.bind(v_el) );
  432. }
  433. };
  434. /**
  435. @private
  436. @param {cognos.Value} v_oPV
  437. @return {void}
  438. */
  439. cognos.Prompt.Control.Time.prototype.f_setPV = function( v_oPV )
  440. {
  441. var v_sValue = v_oPV["use"];
  442. if ( v_sValue )
  443. {
  444. var v_oTime = this.m_oControl;
  445. var v_aValues = v_sValue.split(new RegExp("[T\\s]"));
  446. if ( v_aValues.length >= 0 )
  447. {
  448. this.f_setTime( v_aValues[ v_aValues.length-1 ] );
  449. }
  450. }
  451. };
  452. /**
  453. @private
  454. @param {String} v_sTime
  455. @return {void}
  456. */
  457. cognos.Prompt.Control.Time.prototype.f_setTime = function( v_sTime )
  458. {
  459. this.m_oControl.setValue( v_sTime );
  460. };
  461. /**
  462. @private
  463. @return {void}
  464. */
  465. cognos.Prompt.Control.Time.prototype.f_toggleDisable = function()
  466. {
  467. var v_cb = $( this.f_getId( "chkAnyValue" ) );
  468. if ( v_cb )
  469. {
  470. this.m_oControl.toggleDisable( v_cb.checked );
  471. }
  472. };
  473. /**
  474. @private
  475. @return {boolean}
  476. */
  477. cognos.Prompt.Control.Time.prototype.hasValue = function()
  478. {
  479. return (this.m_bValid);
  480. };
  481. var C_Time = cognos.Prompt.Control.Time; // Keep old reference for backward compatibility with custom scripts.