SelectHTML5Date.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. This script is used to provide interactivity for the textBox prompt control
  14. @private
  15. @class
  16. */
  17. cognos.Prompt.Control.SelectHTML5Date = cognos.Prompt.Control.f_extend({
  18. f_initialize: function( v_oProps ) {
  19. this[K_PRMT_LABEL_RANGE_LOWEST] = PMT_RNG_EARLIEST_DATE;
  20. this[K_PRMT_LABEL_RANGE_HIGHEST] = PMT_RNG_LATEST_DATE;
  21. this._type_ = "cognos.Prompt.Control.SelectHTML5Date";
  22. this.f_parent( v_oProps ); // call parent's initialize()
  23. this.m_bDisabled = false;
  24. this.f_initCompleted();
  25. },
  26. //validate the input into the control
  27. checkData: function()
  28. {
  29. // We keep a reference to the parent function
  30. // Calling this.m_oFrom functions seems to have a side effect on this.f_parent().
  31. var v_fnParent = this.f_parent;
  32. if ( ( this.isRequired() || !(this.m_bDisabled) ) && !(this.m_oDateHelper.f_checkDate( this.m_elPrompt.value )) ) {
  33. this.m_bValid = false;
  34. } else {
  35. this.m_bValid = true;
  36. }
  37. this.f_parent = v_fnParent; // call parent's checkData using saved reference.
  38. this.f_parent(); // call parent's checkData using saved reference.
  39. return this.m_bValid;
  40. },
  41. clearValues: function() {
  42. this.f_parent();
  43. this.f_clear();
  44. },
  45. f_drawCompleted: function() {
  46. var v_fnParent = this.f_parent;
  47. this.m_oInputCopy = $( this.f_getId( "_Date" ) );
  48. var v_iStartOfWeek = 0;
  49. switch( this["@startOfWeek"] )
  50. {
  51. case "Monday": iStartDay = 1; break;
  52. case "Tuesday": iStartDay = 2; break;
  53. case "Wednesday": iStartDay = 3; break;
  54. case "Thursday": iStartDay = 4; break;
  55. case "Friday": iStartDay = 5; break;
  56. case "Saturday": iStartDay = 6; break;
  57. }
  58. //oForm, oEditBox, sRef, sDefaultDate, iType, sInputOrder, iStartOfWeek, iDateTimeType,
  59. // sFirstDate, sLastDate, bRequired, sCVId
  60. var v_oDateHelper = new PRMT_DateHelper (
  61. $( this.f_getId( "_Date" ) ), //oForm,
  62. $( this.f_getId( "txtDate" )), //oEditBox,
  63. this.f_getId(), //sRef,
  64. this["@defaultDate"], //sDefaultDate,
  65. (this["@calendar"] == "Imperial" ? 1 : 0), //iType,
  66. this["@inputOrder"], //sInputOrder,
  67. v_iStartOfWeek,
  68. (this["@DateTime"] ? 0 : 1), //iDateTimeType,
  69. this["@firstDate"], //sFirstDate,
  70. this["@lastDate"], //sLastDate,
  71. this.isRequired() //bRequired
  72. );
  73. this.m_oDateHelper = v_oDateHelper;
  74. var v_oInput = $( this.f_getId("txtDate") );
  75. v_oInput.m_oPrompt = this;
  76. this.m_elPrompt = v_oInput;
  77. this.m_oForm = $(this.f_getId("_Date"));
  78. //indicate whether the control has valid data
  79. this.m_bValid = false;
  80. var v_currentDate = v_oDateHelper.getCurrentDate();
  81. this.f_setObjectDate(v_currentDate);
  82. //PRMTUtils.f_addEvent( v_oInput, "input", this.checkData.bind(this) );
  83. PRMTUtils.f_addEvent( v_oInput, "blur", this.checkData.bind(this) );
  84. this.checkData();
  85. this.f_parent = v_fnParent; // call parent's f_drawCompleted using saved reference.
  86. this.f_parent(); // call parent's f_drawCompleted using saved reference.
  87. }
  88. });
  89. /**
  90. @private
  91. @param {C_PromptElement} v_el Container.
  92. @return {void}
  93. */
  94. cognos.Prompt.Control.SelectHTML5Date.prototype.f_drawInput = function( v_el )
  95. {
  96. $CE("input", {"type": "hidden", "id": this.f_getId("_Date"), "value": this["@defaultDate"]}, v_el );
  97. var v_tbl = $CE( "table", { "border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding": 0, "cellSpacing": 0, "role":K_PRMT_ARIA_ROLE_PRESENTATION}, v_el );
  98. var v_tbd = $CE( "tbody", {}, v_tbl );
  99. var v_tr = $CE( "tr", {}, v_tbd );
  100. var v_td = $CE( "td", {}, v_tr );
  101. // selectDateEditBoxStyle
  102. var v_oP = {
  103. "type": "date",
  104. "class": "clsHTML5Input",
  105. "id": this.f_getId("txtDate"),
  106. "value": K_PRMT_sEMPTY
  107. };
  108. // title for WCAG 2.0
  109. if ( this["@title"] ) {
  110. v_oP.title = this["@title"];
  111. }
  112. if ( this.isRequired() ) {
  113. v_oP["aria-required"] = "true";
  114. }
  115. if ( this["@firstDate"] ) {
  116. v_oP["min"] = this["@firstDate"];
  117. }
  118. if ( this["@lastDate"] ) {
  119. v_oP["max"] = this["@lastDate"];
  120. }
  121. var v_oInput = $CE( "input", v_oP, v_td );
  122. return v_oInput;
  123. };
  124. /**
  125. @private
  126. @return {void}
  127. */
  128. cognos.Prompt.Control.SelectHTML5Date.prototype.f_clear = function( )
  129. {
  130. this.m_elPrompt.value = "";
  131. };
  132. /**
  133. @private
  134. @return {void}
  135. */
  136. cognos.Prompt.Control.SelectHTML5Date.prototype.f_setDate = function(/* String */ v_sDate )
  137. {
  138. this.f_setObjectDate(dParseDate(v_sDate, "YMD"));
  139. };
  140. /**
  141. @private
  142. @return {void}
  143. */
  144. cognos.Prompt.Control.SelectHTML5Date.prototype.f_setObjectDate = function( v_sDate )
  145. {
  146. var v_oInput = this.m_elPrompt;
  147. v_oInput.value = this.m_oDateHelper.f_formatDateYMD(v_sDate);
  148. };
  149. // aux methods for DateTime
  150. cognos.Prompt.Control.SelectHTML5Date.prototype.getCalendarType = function() { return this.m_oDateHelper.m_iDateTimeType; };
  151. cognos.Prompt.Control.SelectHTML5Date.prototype.getInputOrder = function() { return this.m_oDateHelper.m_sInputOrder; };
  152. /**
  153. * Return as Date javascript object
  154. *
  155. * @private
  156. * @return {cognos.Value[]}
  157. */
  158. cognos.Prompt.Control.SelectHTML5Date.prototype.f_getValueAsDate = function()
  159. {
  160. var result = dParseDate(this.m_elPrompt.value,"YMD");
  161. return result;
  162. };
  163. cognos.Prompt.Control.SelectHTML5Date.prototype.f_getUseValue = function()
  164. {
  165. return this.m_elPrompt.value;
  166. };
  167. cognos.Prompt.Control.SelectHTML5Date.prototype.f_getDisplayValue = function()
  168. {
  169. return this.m_oDateHelper.f_formatDateCurrentLocale(this.f_getValueAsDate());
  170. };
  171. /**
  172. @private
  173. @return {cognos.Value[]}
  174. */
  175. cognos.Prompt.Control.SelectHTML5Date.prototype.f_getPV = function()
  176. {
  177. var v_oPV = null;
  178. this.m_oDateHelper.f_checkDate(this.m_elPrompt.value);
  179. var v_oDate = this.f_getValueAsDate();
  180. var v_sUse = this.m_oDateHelper.f_formatSubmitDate(v_oDate);
  181. if ( this.m_bIsRangeEndControl && this["@DateTime"]) {
  182. v_sUse = v_sUse.replace( K_PRMT_reTIMEPART, K_PRMT_sTIME_MAX );
  183. }
  184. return (
  185. this.m_bDisabled || !v_sUse ?
  186. null :
  187. { "use": v_sUse, "display": this.m_oDateHelper.f_formatDateCurrentLocale(v_oDate) }
  188. );
  189. };
  190. /**
  191. @private
  192. @return {cognos.Value[]}
  193. */
  194. cognos.Prompt.Control.SelectHTML5Date.prototype.f_setPV = function(v_oPV)
  195. {
  196. var v_sValue = v_oPV["use"];
  197. if ( v_sValue )
  198. {
  199. var v_aValues = v_sValue.split(new RegExp("[T\\s]"));
  200. if ( v_aValues.length > 2 )
  201. {
  202. for ( var v_idx = 1; v_idx < v_aValues.length - 1; v_idx++ )
  203. {
  204. // add all Date parts to the first element.
  205. v_aValues[0] += K_PRMT_sSP + (v_aValues[v_idx]).f_trim();
  206. }
  207. v_aValues[0] = v_aValues[0].f_trim();
  208. // move Time part (last value) in slot 1
  209. v_aValues[1] = v_aValues[ v_aValues.length - 1];
  210. }
  211. if ( v_aValues.length >= 1 )
  212. {
  213. this.f_setObjectDate( dParseDate(v_aValues[0], "YMD") );
  214. }
  215. }
  216. };
  217. /**
  218. Compare values. See {@link cognos.Prompt.Control#f_compare} for details.
  219. @see cognos.Prompt.Control#f_compare
  220. @private
  221. @return {Integer}
  222. */
  223. cognos.Prompt.Control.SelectHTML5Date.prototype.f_compare = function( v_oValue )
  224. {
  225. var v_iRetval = 1;
  226. if ( v_oValue )
  227. {
  228. // Get Date objects
  229. var v_dThis = this.f_getValueAsDate();
  230. var v_dCompareTo = v_oValue.f_getValueAsDate();
  231. var v_iThis = v_dThis.getTime();
  232. var v_iCompareTo = v_dCompareTo.getTime();
  233. if ( v_iCompareTo > v_iThis )
  234. {
  235. v_iRetval = -1;
  236. }
  237. else if ( v_iCompareTo == v_iThis )
  238. {
  239. v_iRetval = 0;
  240. }
  241. }
  242. return v_iRetval;
  243. };
  244. /**
  245. Update UI when control is losing the focus.
  246. @private
  247. @return {void}
  248. */
  249. cognos.Prompt.Control.SelectHTML5Date.prototype.f_lostFocus = function()
  250. {
  251. this.checkData();
  252. };
  253. /**
  254. @private
  255. @return {void}
  256. */
  257. cognos.Prompt.Control.SelectHTML5Date.prototype.f_setEnabled = function( v_bState )
  258. {
  259. this.m_bDisabled = !v_bState;
  260. if (v_bState) {
  261. this.m_elPrompt.removeAttribute("disabled");
  262. } else {
  263. this.m_elPrompt.setAttribute("disabled",true);
  264. }
  265. };
  266. /**
  267. Update UI when control is losing the focus.
  268. @private
  269. @return {void}
  270. */
  271. cognos.Prompt.Control.SelectHTML5Date.prototype.f_toggleDisable = function()
  272. {
  273. var v_bState = this.m_bDisabled;
  274. this.f_setEnabled( v_bState );
  275. this.m_bDisabled = !v_bState;
  276. };
  277. /**
  278. Check if is a reprompt(this.c exists) and in case of a reprompt if the reprompt has data(a date)
  279. When a date control is optional, has a Calendar UI the user can decide not to submit the date, in
  280. that case the reprompt won't contain data.
  281. @private
  282. @return {boolean}
  283. */
  284. cognos.Prompt.Control.SelectHTML5Date.prototype.isEmptyReprompt = function()
  285. {
  286. var result = false;
  287. if ((typeof this.c) != K_PRMT_sUNDEFINED) {
  288. // it's a reprompt
  289. var v_c = this.c;
  290. // [{n:'selectChoices',c:[{n:'selectOption',"@useValue":"2010-01-14","@displayValue":"Jan 14, 2010"}]}] => not empty(value.n =='selectOption' || [{n:'selectChoices'}] => empty(value.n == 'selectChoices')
  291. if ( v_c.length && v_c[0].n && v_c[0].n == "selectChoices") {
  292. var v_c_value = cognos.Value.getValue( this.c );
  293. if (v_c_value.n && v_c_value.n == "selectChoices") result = true;
  294. }
  295. }
  296. return result;
  297. };
  298. /**
  299. @private
  300. @return {boolean}
  301. */
  302. cognos.Prompt.Control.SelectHTML5Date.prototype.hasValue = function()
  303. {
  304. return (this.m_bValid);
  305. };