SelectHTML5Time.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2013
  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.SelectHTML5Time = 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.SelectHTML5Time";
  22. this.f_parent( v_oProps ); // call parent's initialize()
  23. //@display indicates which time parts are used for display; secs and millisecs are optional
  24. if ( typeof this["@display"] == K_PRMT_sSTRING ) {
  25. this["@display"] = parseInt( this["@display"], 10 );
  26. }
  27. if ( typeof this["@display"] != "number" || isNaN(this["@display"]) ) {
  28. this["@display"] = ( this["@showMilliseconds"] ? 0 : (this["@showSeconds"] ? 1 : 2) );
  29. }
  30. //@type is used for formatting the time display value
  31. if ( typeof this["@type"] == K_PRMT_sSTRING ) {
  32. this["@type"] = parseInt( this["@type"], 10 );
  33. }
  34. if ( typeof this["@type"] != "number" || isNaN(this["@type"]) ) {
  35. if (typeof g_24HourClock != K_PRMT_sUNDEFINED && g_24HourClock.toString().toLowerCase() == "true") {
  36. this["@type"] = 2;
  37. } else {
  38. this["@type"] = (typeof g_AMPMLocation != K_PRMT_sUNDEFINED && g_AMPMLocation == 'left' ? 1 : 0);
  39. }
  40. }
  41. //@clockMode indicates if the clock is to display live time of static time
  42. if ( this["@clockMode"] == "static" ) {
  43. this["@clockMode"] = 0;
  44. } else if ( this["@clockMode"] == "live" ) {
  45. this["@clockMode"] = 1;
  46. } else if ( typeof this["@clockMode"] == K_PRMT_sSTRING ) {
  47. this["@clockMode"] = parseInt( this["@clockMode"], 10 );
  48. }
  49. if ( typeof this["@clockMode"] != "number" || isNaN(this["@clockMode"]) ) {
  50. this["@clockMode"] = 0;
  51. }
  52. this.m_bSkipAdornments = true;
  53. this.m_bDisabled = false;
  54. this.m_bValid = false;
  55. this.f_initCompleted();
  56. },
  57. checkData: function()
  58. {
  59. // We keep a reference to the parent function
  60. // Calling this.m_oFrom functions seems to have a side effect on this.f_parent().
  61. var v_fnParent = this.f_parent;
  62. //Value in the time form input field
  63. var v_sValue = this.m_elPrompt.value;
  64. if ( ( this.isRequired() || !(this.m_bDisabled) ) && !(this.m_oTimeHelper.bCheckTime(v_sValue, this.m_iDisplay)) ) {
  65. this.m_bValid = false;
  66. } else {
  67. this.m_bValid = true;
  68. }
  69. this.f_parent = v_fnParent; // call parent's checkData using saved reference.
  70. this.f_parent(); // call parent's checkData using saved reference.
  71. return this.m_bValid;
  72. },
  73. clearValues: function()
  74. {
  75. this.f_parent();
  76. this.f_clear();
  77. },
  78. f_drawCompleted: function()
  79. {
  80. // We keep a reference to the parent function
  81. // Calling this.m_oFrom.f_drawCompleted seems to have a side effect on this.f_parent().
  82. var v_fnParent = this.f_parent;
  83. if (!this["@timeZone"]) {
  84. this["@timeZone"] = "GMT";
  85. }
  86. if (!this["@hourFormat"]) {
  87. this["@hourFormat"] = "hh";
  88. }
  89. if (!this["@minuteFormat"]) {
  90. this["@minuteFormat"] = "mm";
  91. }
  92. if (!this["@secondFormat"]) {
  93. this["@secondFormat"] = "ss";
  94. }
  95. this.m_oTimeHelper = new PRMT_TimeHelper(this.isRequired());
  96. this.m_oForm = $(this.f_getId("_Time"));
  97. this.m_elPrompt = $(this.f_getId("selectMobileTime_"));
  98. //Clock Mode
  99. //0 = static
  100. //1 = live
  101. this.m_iLiveClockMode = this["@clockMode"];
  102. if (this.m_iLiveClockMode==1){
  103. this.funcInt = setInterval( this.f_drawLiveTime.bind(this), 1000 );
  104. }
  105. //what is being displayed
  106. //0 = h:m:s:ms
  107. //1 = h:m:s
  108. //2 = h:m
  109. this.m_iDisplay = this["@display"];
  110. this.f_setInputEvents();
  111. if(this["@defaultValue"]){
  112. this.f_setTime(this["@defaultValue"]);
  113. } else {
  114. this.f_setTime("00:00:00.000");
  115. }
  116. this.f_refreshTime();
  117. this.f_parent = v_fnParent; // call parent's f_drawCompleted using saved reference.
  118. this.f_parent(); // call parent's f_drawCompleted using saved reference.
  119. }
  120. });
  121. /**
  122. @private
  123. @param {C_PromptElement} v_el Container.
  124. @return {void}
  125. */
  126. cognos.Prompt.Control.SelectHTML5Time.prototype.f_drawInput = function( v_el )
  127. {
  128. var v_tbl = $CE( "table", { "border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding": 0, "cellSpacing": 0, "role":K_PRMT_ARIA_ROLE_PRESENTATION }, v_el );
  129. var v_tbd = $CE( "tbody", {}, v_tbl );
  130. var v_tr = $CE( "tr", {}, v_tbd );
  131. if ( this["@required"] && !this.isMulti() && !this["@hideAdornments"]) {
  132. this.f_drawAdornments(v_tr);
  133. }
  134. var v_td = $CE( "td", {"id": this.f_getId("timeEditBox"), "noWrap": "noWrap"}, v_tr );
  135. var v_sClass = "clsHTML5Input";
  136. var v_sCustomDir = cssParser( this["@style"], "direction", true );
  137. var v_Container = (this.m_elRangeContainer ? this.m_elRangeContainer : $( this.f_getId(this.m_sDivPrefix) ));
  138. var v_FinalDir = ((v_sCustomDir == "ltr" || v_sCustomDir == "rtl") ? v_sCustomDir : PRMT_BidiUtils.lookupDirection(v_Container));
  139. v_sStyle = "direction:ltr";
  140. if (v_FinalDir == "rtl") {
  141. v_sStyle += ";text-align:right";
  142. v_sClass += "RTL";
  143. }
  144. var v_oControl = null;
  145. //v_fStep is a floating point value representing the number of seconds per increment or "step" in the time control
  146. var v_fStep = 60.0;
  147. if ( this["@display"] == 1) {
  148. v_fStep = 1.0;
  149. }else if ( this["@display"] === 0) {
  150. v_fStep = 0.001;
  151. }
  152. $CE( "input", {"id": this.f_getId("selectMobileTime_"),
  153. "type": "time",
  154. "step": v_fStep,
  155. "class": v_sClass,
  156. "style": v_sStyle}, v_td );
  157. $CE("input", {"type": "hidden", "id": this.f_getId("_Time"), "value":"00:00:00.000"}, v_el );
  158. v_oControl = $(this.f_getId("selectMobileTime_"));
  159. return v_oControl;
  160. };
  161. /**
  162. @private
  163. @return {void}
  164. */
  165. cognos.Prompt.Control.SelectHTML5Time.prototype.f_clear = function( )
  166. {
  167. this.m_dTime = null;
  168. this.m_oForm.value = K_PRMT_sEMPTY;
  169. this.m_elPrompt.value = K_PRMT_sEMPTY;
  170. };
  171. /**
  172. @private
  173. @param {String} sTime
  174. @return {void}
  175. */
  176. cognos.Prompt.Control.SelectHTML5Time.prototype.f_setTime = function( sTime )
  177. {
  178. this.m_dTime = this.m_oTimeHelper.dParseTime (sTime);
  179. this.m_oForm.value = this.m_oTimeHelper.getTimeFormatForSubmit(this.m_dTime);
  180. this.m_elPrompt.value = this.m_oTimeHelper.getTimeFormatForInput(this.m_dTime, this.m_iDisplay);
  181. };
  182. /**
  183. @private
  184. @return {cognos.Value}
  185. */
  186. cognos.Prompt.Control.SelectHTML5Time.prototype.f_getPV = function()
  187. {
  188. var v_bHasValue = this.hasValue();
  189. return ( (v_bHasValue)? {
  190. "use": this.m_oForm.value,
  191. "display": this.m_oTimeHelper.getTimeFormatForDisplay(this.m_dTime, this.m_iDisplay)
  192. } : null);
  193. };
  194. cognos.Prompt.Control.SelectHTML5Time.prototype.getTimeFormatForDisplay = function() {
  195. return this.m_oTimeHelper.getTimeFormatForDisplay(this.m_dTime, this.m_iDisplay);
  196. }
  197. cognos.Prompt.Control.SelectHTML5Time.prototype.getTimeInputValue = function() {
  198. return this.m_oForm.value;
  199. }
  200. /**
  201. @private
  202. @param {cognos.Value} v_oPV
  203. @return {void}
  204. */
  205. cognos.Prompt.Control.SelectHTML5Time.prototype.f_setPV = function( v_oPV )
  206. {
  207. var v_sValue = v_oPV["use"];
  208. if ( v_sValue ) {
  209. var v_aValues = v_sValue.split(new RegExp("[T\\s]"));
  210. if ( v_aValues.length >= 0 ) {
  211. this.f_setTime( v_aValues[ v_aValues.length-1 ] );
  212. }
  213. }
  214. };
  215. /**
  216. Compare values. See {@link cognos.Prompt.Control#f_compare} for details.
  217. @see cognos.Prompt.Control#f_compare
  218. @private
  219. @return {Integer}
  220. */
  221. cognos.Prompt.Control.SelectHTML5Time.prototype.f_compare = function( v_oValue )
  222. {
  223. var v_iRetval = 1;
  224. if ( v_oValue )
  225. {
  226. var v_dThis = this.m_oTimeHelper.dParseTime( this.m_oForm.value );
  227. var v_dCompareTo = v_oValue.m_oTimeHelper.dParseTime( v_oValue.m_oForm.value );
  228. var v_iThis = v_dThis.getTime();
  229. var v_iCompareTo = v_dCompareTo.getTime();
  230. if ( v_iCompareTo > v_iThis ) {
  231. v_iRetval = -1;
  232. } else if ( v_iCompareTo == v_iThis ) {
  233. v_iRetval = 0;
  234. }
  235. }
  236. return v_iRetval;
  237. };
  238. /**
  239. Set up event handlers for time input
  240. @return {void}
  241. */
  242. cognos.Prompt.Control.SelectHTML5Time.prototype.f_setInputEvents = function()
  243. {
  244. //Update the prompt values when user leave the control
  245. PRMTUtils.f_addEvent( this.m_elPrompt, "blur", this.f_updateControl.bind(this) );
  246. //PRMTUtils.f_addEvent( this.m_elPrompt, "input", this.f_updateControl.bind(this) );
  247. if(PRMTUtils.f_isMobileDevice()){
  248. //Stop the live clock when user gives focus to the control
  249. // Limitation of mobile Safari: only fires focus and blur events
  250. PRMTUtils.f_addEvent( this.m_elPrompt, "focus", this.f_stopLiveTime.bind(this) );
  251. }else{
  252. //Stop the live clock on change of input in other browsers
  253. PRMTUtils.f_addEvent( this.m_elPrompt, "change", this.f_stopLiveTime.bind(this) );
  254. PRMTUtils.f_addEvent( this.m_elPrompt, "input", this.f_stopLiveTime.bind(this) );
  255. }
  256. };
  257. cognos.Prompt.Control.SelectHTML5Time.prototype.f_updateControl = function() {
  258. this.f_setTime(this.m_elPrompt.value);
  259. this.f_refreshTime();
  260. };
  261. /**
  262. @private
  263. @return {boolean}
  264. */
  265. cognos.Prompt.Control.SelectHTML5Time.prototype.hasValue = function()
  266. {
  267. return !(this.m_oForm.value == K_PRMT_sEMPTY);
  268. };
  269. //render live time
  270. cognos.Prompt.Control.SelectHTML5Time.prototype.f_drawLiveTime = function()
  271. {
  272. this.m_dTime = new Date();
  273. this.m_oForm.value = this.m_oTimeHelper.getTimeFormatForSubmit(this.m_dTime);
  274. this.m_elPrompt.value = this.m_oTimeHelper.getTimeFormatForInput(this.m_dTime, this.m_iDisplay);
  275. };
  276. //stop live time
  277. cognos.Prompt.Control.SelectHTML5Time.prototype.f_stopLiveTime = function()
  278. {
  279. if (this.m_iLiveClockMode==1) {
  280. if ( this.m_iDisplay > 1 ) {
  281. // Can't change the seconds in the UI, so reset to 0.
  282. this.m_dTime.setSeconds(0);
  283. }
  284. if ( this.m_iDisplay > 0 ) {
  285. // Can't change the milliseconds in the UI, so reset to 0.
  286. this.m_dTime.setMilliseconds(0);
  287. }
  288. this.m_iLiveClockMode=0;
  289. clearInterval(this.funcInt);
  290. }
  291. };
  292. //render the values for the control based on display settings
  293. cognos.Prompt.Control.SelectHTML5Time.prototype.f_refreshTime = function()
  294. {
  295. this.checkData();
  296. if (this.m_bValid) {
  297. this.m_oForm.value = this.m_oTimeHelper.getTimeFormatForSubmit(this.m_dTime);
  298. this.m_elPrompt.value = this.m_oTimeHelper.getTimeFormatForInput(this.m_dTime, this.m_iDisplay);
  299. } else {
  300. this.f_clear();
  301. }
  302. };
  303. /**
  304. @private
  305. @return {void}
  306. */
  307. cognos.Prompt.Control.SelectHTML5Time.prototype.f_setEnabled = function( v_bState )
  308. {
  309. this.m_bDisabled = !v_bState;
  310. if (v_bState) {
  311. this.m_elPrompt.removeAttribute("disabled");
  312. } else {
  313. this.m_elPrompt.setAttribute("disabled",true);
  314. }
  315. };