extensions.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. var Class = function( v_oProperties ) {
  13. var v_class = function() {
  14. if (this.f_initialize && arguments[0] != 'noinit')
  15. {
  16. return this.f_initialize.apply(this, arguments);
  17. }
  18. else
  19. {
  20. return this;
  21. }
  22. };
  23. for (var v_oProperty in this)
  24. {
  25. v_class[v_oProperty] = this[v_oProperty];
  26. }
  27. v_class.prototype = v_oProperties;
  28. return v_class;
  29. };
  30. Class.prototype = {
  31. f_extend: function( v_oProperties ) {
  32. var v_oProto = new this('noinit');
  33. var f_parentize = function( v_oPrevious, v_oCurrent )
  34. {
  35. if ( !v_oPrevious.apply || !v_oCurrent.apply )
  36. {
  37. return false;
  38. }
  39. return function()
  40. {
  41. this.f_parent = v_oPrevious;
  42. return v_oCurrent.apply( this, arguments );
  43. };
  44. };
  45. for (var v_oProperty in v_oProperties){
  46. var v_oPrevious = v_oProto[ v_oProperty ];
  47. var v_oCurrent = v_oProperties[ v_oProperty ];
  48. if (v_oPrevious && v_oPrevious != v_oCurrent)
  49. {
  50. v_oCurrent = f_parentize(v_oPrevious, v_oCurrent) || v_oCurrent;
  51. }
  52. v_oProto[ v_oProperty ] = v_oCurrent;
  53. }
  54. return new Class( v_oProto );
  55. }
  56. };
  57. /**
  58. Copies all properties from v_oSource into v_oTarget.
  59. if v_oSource is null, we copy v_oTarget into 'this'.
  60. @param v_oTarget
  61. @param v_oSouce
  62. */
  63. Object.f_extend = function( v_oTarget, v_oSource )
  64. {
  65. if ( !v_oSource )
  66. {
  67. v_oTarget = this;
  68. v_oSource = v_oTarget;
  69. }
  70. for ( var v_oProperty in v_oSource )
  71. {
  72. v_oTarget[v_oProperty] = v_oSource[v_oProperty];
  73. }
  74. return v_oTarget;
  75. };
  76. /**
  77. Short for document.getElementById()
  78. */
  79. function $()
  80. {
  81. if (arguments.length == 1)
  82. {
  83. var element = arguments[0];
  84. if (typeof element == 'string')
  85. {
  86. return document.getElementById(element);
  87. }
  88. return element;
  89. }
  90. else
  91. {
  92. var elements = new Array();
  93. var i, l = arguments.length;
  94. for (i = 0; i < l; i++)
  95. {
  96. var element = arguments[i];
  97. if (typeof element == 'string')
  98. {
  99. element = document.getElementById(element);
  100. }
  101. elements.push(element);
  102. }
  103. return elements;
  104. }
  105. }
  106. /**
  107. Short for new C_PromptElement().
  108. */
  109. function $CE( v_sTag, v_oProps, v_elParent )
  110. {
  111. return ( new C_PromptElement(v_sTag, v_oProps, v_elParent) );
  112. }
  113. var PRMTUtils =
  114. {
  115. f_addClass: function( v_oElement, v_sName ) {
  116. if ( v_oElement )
  117. {
  118. v_sName = v_sName.f_trim();
  119. var v_sClass = v_oElement.className;
  120. if ( !v_sClass )
  121. {
  122. v_sClass = K_PRMT_sEMPTY;
  123. }
  124. if ( !v_sClass.match(new RegExp("\\b" + v_sName + "\\b")) )
  125. {
  126. v_sClass = (v_sClass + K_PRMT_sSP + v_sName);
  127. v_oElement.className = v_sClass.f_trim();
  128. }
  129. }
  130. return this;
  131. },
  132. f_addEvent: function( v_oElement, v_sType, v_oFct ) {
  133. if ( v_oElement )
  134. {
  135. if ( v_oElement.addEventListener )
  136. {
  137. v_oElement.addEventListener( v_sType, v_oFct, false );
  138. }
  139. else
  140. {
  141. v_oElement.attachEvent( 'on' + v_sType, v_oFct );
  142. }
  143. }
  144. },
  145. f_addText: function( v_oElement, v_sText ) {
  146. v_oElement.appendChild( document.createTextNode( K_PRMT_sEMPTY + v_sText ) );
  147. },
  148. f_createElement: function( v_sTag, v_oProps, v_elParent ) {
  149. var v_el = document.createElement( v_sTag );
  150. if ( v_oProps )
  151. {
  152. for (var p in v_oProps)
  153. {
  154. v_el.setAttribute( p, v_oProps[p] );
  155. }
  156. }
  157. if ( v_elParent )
  158. {
  159. v_elParent.appendChild( v_el );
  160. }
  161. return v_el;
  162. },
  163. f_parseNumber:function( v_sValue ) //CText::sParseOutCommon(sNumber)
  164. {
  165. var v_sRetval = v_sValue;
  166. // strip out the following
  167. // currency unit
  168. v_sRetval = v_sRetval.replace( new RegExp(sEscapeRegularExpression(g_currencySymbol), K_PRMT_sG), K_PRMT_sEMPTY);
  169. // group separator
  170. v_sRetval = v_sRetval.replace(new RegExp(sEscapeRegularExpression(g_groupingSeparator), K_PRMT_sG), K_PRMT_sEMPTY);
  171. // remove whitespace
  172. v_sRetval = v_sRetval.replace(new RegExp("\\s", K_PRMT_sG), K_PRMT_sEMPTY);
  173. // is there a decimal point, replace with SQL format
  174. v_sRetval = v_sRetval.replace(new RegExp(sEscapeRegularExpression(g_decimalSeparator), K_PRMT_sG), K_PRMT_sDOT);
  175. // remove the percent symbol
  176. v_sRetval = v_sRetval.replace(new RegExp(sEscapeRegularExpression(g_percentSymbol), K_PRMT_sG), K_PRMT_sEMPTY);
  177. return(v_sRetval);
  178. },
  179. f_isNumberOnly: function( v_sValue ) {
  180. var v_re = new RegExp("^[\\s\\+\\-]*[0-9\\s,\\.]+$");
  181. v_sValue = this.f_parseNumber(K_PRMT_sEMPTY + v_sValue);
  182. return ( v_re.test(v_sValue) ? true : false );
  183. },
  184. f_removeClass: function( v_oElement, v_sName ) {
  185. if ( v_oElement )
  186. {
  187. v_sName = v_sName.f_trim();
  188. var v_sClass = v_oElement.className;
  189. if ( !v_sClass )
  190. {
  191. v_sClass = K_PRMT_sEMPTY;
  192. }
  193. v_sClass = v_sClass.replace( new RegExp( "\\s*\\b" + v_sName + "\\b" ), K_PRMT_sEMPTY );
  194. v_oElement.className = v_sClass.f_trim();
  195. }
  196. },
  197. f_removeElement: function( v_oElement ) {
  198. if (v_oElement && v_oElement.parentNode)
  199. {
  200. v_oElement.parentNode.removeChild( v_oElement );
  201. }
  202. },
  203. F_PreventSubmit: function(evt) {
  204. var v_oEvt = (evt ? evt : (typeof event != K_PRMT_sUNDEFINED ? event : null ));
  205. if ( v_oEvt )
  206. {
  207. if (v_oEvt.keyCode == 13)
  208. {
  209. return this.F_StopEvent( v_oEvt );
  210. }
  211. }
  212. },
  213. F_StopEvent: function( v_oEvt ) {
  214. if (typeof v_oEvt.stopPropagation == K_PRMT_sFUNCTION)
  215. {
  216. v_oEvt.stopPropagation();
  217. }
  218. if (typeof v_oEvt.preventDefault == K_PRMT_sFUNCTION)
  219. {
  220. v_oEvt.preventDefault();
  221. }
  222. v_oEvt.cancelBubble = true;
  223. return false;
  224. },
  225. f_log: function( v_sCode, v_sMsg, v_bError ) {
  226. var s = v_sCode + "\n" + v_sMsg;
  227. if ( typeof console != K_PRMT_sUNDEFINED )
  228. {
  229. // Use Firebug's console
  230. if ( v_bError ) {
  231. console.warn( s );
  232. }
  233. else {
  234. console.log( s );
  235. }
  236. }
  237. else if ( K_PRMT_DEBUG )
  238. {
  239. s = '<div style="color:' + (v_bError ? "red" : "blue") + K_PRMT_sQU + K_PRMT_sGT + s + "</div>";
  240. if ( this._WIN_LOG_ )
  241. {
  242. this._WIN_LOG_.document.write( s );
  243. }
  244. else
  245. {
  246. var w = window.open( K_PRMT_sEMPTY, "PRMTUtil_LOG" + (new Date()).getTime() );
  247. w.document.write( s );
  248. w.document.close();
  249. this._WIN_LOG_ = w;
  250. }
  251. }
  252. },
  253. f_parseStyle: function ( sStyle ) {
  254. var v_aRetVal = [];
  255. if ( !sStyle || typeof sStyle != K_PRMT_sSTRING )
  256. {
  257. return v_aRetVal;
  258. }
  259. var v_sStyleParsed = sStyle.replace(/\s*;\s*$/g, K_PRMT_sEMPTY);
  260. v_sStyleParsed = v_sStyleParsed.replace(/\s*;\s*/g,';');
  261. v_sStyleParsed = v_sStyleParsed.replace(/\s*:\s*/g,K_PRMT_sCOLON);
  262. var aStyle = v_sStyleParsed.split(";");
  263. for ( var v_iStyle = 0; v_iStyle < aStyle.length; v_iStyle++ )
  264. {
  265. var v_aParsedStyled = aStyle[v_iStyle].split(K_PRMT_sCOLON);
  266. if ( v_aParsedStyled.length > 0 && v_aParsedStyled[0] )
  267. {
  268. v_aRetVal.push( v_aParsedStyled );
  269. }
  270. }
  271. return v_aRetVal;
  272. },
  273. f_camelCase: function( v_str ) {
  274. return v_str.replace(
  275. /-\D/g,
  276. function( v_str ) {
  277. return v_str.charAt(1).toUpperCase();
  278. }
  279. );
  280. },
  281. f_error: function( v_sCode, v_sMsg ) {
  282. this.f_log( v_sCode, v_sMsg, true );
  283. },
  284. F_OnChange: function( v_eEvent, v_el ) {
  285. if (v_el.type == "checkbox") {
  286. var v_div = v_el.parentNode;
  287. if (v_el.checked) {
  288. v_div.className = K_PRMT_CSS_CHECKBOX_CHECKED;
  289. } else
  290. {
  291. v_div.className = K_PRMT_CSS_CHECKBOX;
  292. }
  293. } else if (v_el.type == "radio") {
  294. // must update all radio button classes
  295. var v_valueObject = v_el.parentNode.parentNode.parentNode.m_oPrompt;
  296. if (v_valueObject && v_valueObject.f_getOptions) {
  297. var v_aOptions = v_valueObject.f_getOptions();
  298. var v_iLength = v_aOptions.length;
  299. for (var i = 0; i < v_iLength; i++) {
  300. var v_cb = v_aOptions[i];
  301. var v_div = v_cb.parentNode;
  302. v_div.className = (v_cb.checked ? K_PRMT_CSS_RADIOBUTTON_CHECKED : K_PRMT_CSS_RADIOBUTTON);
  303. }
  304. }
  305. }
  306. if ( window.ie )
  307. {
  308. while( v_el && !v_el.m_oPrompt )
  309. {
  310. v_el = v_el.parentNode;
  311. }
  312. if ( v_el && v_el.m_oPrompt && v_el.m_oPrompt.f_onChange )
  313. {
  314. v_el.m_oPrompt.f_onChange();
  315. }
  316. return true;
  317. }
  318. },
  319. f_updateRadioButtons : function(v_el) {
  320. if (v_el && v_el.name) {
  321. var v_sName = v_el.name;
  322. var aRadios = document.getElementsByName(v_sName);
  323. var v_iLength = aRadios.length;
  324. for (var i = 0; i < v_iLength; i++) {
  325. var v_cb = aRadios[i];
  326. var v_div = v_cb.parentNode;
  327. v_div.className = (v_cb.checked ? K_PRMT_CSS_RADIOBUTTON_CHECKED : K_PRMT_CSS_RADIOBUTTON);
  328. }
  329. }
  330. },
  331. f_CheckboxOnFocus: function(v_el) {
  332. v_el.parentNode.nextSibling.style.border = "1px dotted";
  333. },
  334. f_CheckboxOnBlur: function(v_el) {
  335. v_el.parentNode.nextSibling.style.border = "0 none";
  336. },
  337. f_RangeRadioOnFocus: function(v_el) {
  338. v_el.parentNode.style.border = "1px dotted";
  339. },
  340. f_RangeRadioOnBlur: function(v_el) {
  341. v_el.parentNode.style.border = "0 none";
  342. },
  343. f_SearchOptionOnFocus: function(v_el) {
  344. v_el.parentNode.parentNode.nextSibling.firstChild.style.border = "1px dotted";
  345. },
  346. f_SearchOptionOnBlur: function(v_el) {
  347. v_el.parentNode.parentNode.nextSibling.firstChild.style.border = "0 none";
  348. },
  349. f_isDOMElem: function (v_obj) {
  350. var v_result = false;
  351. if(typeof v_obj == "object" && "nodeType" in v_obj && v_obj.nodeType === 1 && v_obj.cloneNode){
  352. // most probably this is a DOM node
  353. v_result = true;
  354. }
  355. return v_result;
  356. },
  357. f_alertId: function (inputId, alertSuffix)
  358. {
  359. var v_elAlertId = "alert_" + inputId;
  360. if (typeof alertSuffix != K_PRMT_sUNDEFINED && alertSuffix != null && alertSuffix.length && alertSuffix.length > 0)
  361. {
  362. v_elAlertId += "_" + alertSuffix;
  363. }
  364. return v_elAlertId;
  365. },
  366. f_isMobileDevice: function (){
  367. return (/iPad|iPhone/.test(navigator.platform)) || (/Linux/.test(navigator.platform) && /Android/.test(navigator.userAgent) );
  368. },
  369. f_isHighContrast: function () {
  370. if (window.prmt_bHighContrast === false || window.prmt_bHighContrast === true) {
  371. return window.prmt_bHighContrast;
  372. }
  373. var tempDiv = document.createElement("div");
  374. tempDiv.id = this.m_sId + "hc";
  375. tempDiv.style.border = "1px solid";
  376. tempDiv.style.borderColor = "red green";
  377. tempDiv.style.height = "10px";
  378. tempDiv.style.top = "-999px";
  379. tempDiv.style.position = "absolute";
  380. document.body.appendChild(tempDiv);
  381. var computedStyle = null;
  382. if (window.ie && !window.edge) {
  383. computedStyle = tempDiv.currentStyle;
  384. }
  385. else {
  386. computedStyle = tempDiv.ownerDocument.defaultView.getComputedStyle(tempDiv, null);
  387. }
  388. window.prmt_bHighContrast = computedStyle.borderTopColor == computedStyle.borderRightColor;
  389. document.body.removeChild(tempDiv);
  390. return window.prmt_bHighContrast;
  391. },
  392. f_updateElementAriaChecked: function (v_el, ariaChecked)
  393. {
  394. if (v_el) {
  395. if (v_el[K_PRMT_ARIA_CHECKED]) {
  396. v_el[K_PRMT_ARIA_CHECKED] = ariaChecked;
  397. }
  398. else {
  399. v_el.setAttribute(K_PRMT_ARIA_CHECKED, ariaChecked);
  400. }
  401. }
  402. },
  403. f_removeARIAMessageAlert: function (v_inputElem, v_inputId, alertSuffix)
  404. {
  405. try
  406. {
  407. if ( this.f_isDOMElem(v_inputElem) )
  408. {
  409. // remove ARIA alert div if it exists
  410. var v_elAlertId = this.f_alertId(v_inputId, alertSuffix);
  411. var v_alertDiv = $(v_elAlertId);
  412. if ( v_alertDiv && this.f_isDOMElem(v_alertDiv) )
  413. {
  414. this.f_removeElement(v_alertDiv);
  415. }
  416. }
  417. }
  418. catch(err)
  419. {
  420. }
  421. },
  422. f_showARIAMessageAlert: function (v_inputElem, v_inputId, skinPath, v_sMsg, alertSuffix)
  423. { // render ARIA "alert" message
  424. try {
  425. // add ARIA alert unless it exists
  426. var v_elAlertId = this.f_alertId(v_inputId, alertSuffix);
  427. var v_alertDiv = $(v_elAlertId);
  428. if (v_alertDiv && this.f_isDOMElem(v_alertDiv))
  429. {
  430. // if errorMessage is different
  431. var v_alertImg = v_alertDiv.firstChild;
  432. if (v_alertImg.getAttribute("alt") != v_sMsg)
  433. {
  434. this.f_removeElement(v_alertDiv);
  435. v_alertDiv = K_PRMT_sUNDEFINED;
  436. }
  437. }
  438. if (!(v_alertDiv && this.f_isDOMElem(v_alertDiv)) )
  439. {
  440. var v_newAlert = document.createElement("div");
  441. v_newAlert.setAttribute("role", "alert");
  442. if (v_elAlertId != null)
  443. {
  444. v_newAlert.setAttribute("id", v_elAlertId);
  445. }
  446. var v_container = v_inputElem.parentNode;
  447. if (v_container)
  448. {
  449. v_container.appendChild(v_newAlert);
  450. }
  451. }
  452. }
  453. catch(err)
  454. {
  455. }
  456. },
  457. f_showARIAFail: function (v_inputElem, v_inputId, skinPath, v_sErrorMsg, alertSuffix)
  458. { // render ARIA test failure
  459. try
  460. {
  461. v_inputElem.setAttribute("aria-invalid", true);
  462. this.f_showARIAMessageAlert(v_inputElem, v_inputId, skinPath, v_sErrorMsg, alertSuffix);
  463. }
  464. catch(err)
  465. {
  466. }
  467. },
  468. f_showARIAPass: function (v_inputElem, v_inputId, alertSuffix)
  469. { // render ARIA test pass
  470. try
  471. {
  472. if ( this.f_isDOMElem(v_inputElem) )
  473. {
  474. v_inputElem.setAttribute("aria-invalid", false);
  475. this.f_removeARIAMessageAlert(v_inputElem, v_inputId, alertSuffix);
  476. }
  477. }
  478. catch(err)
  479. {
  480. }
  481. }
  482. };
  483. String.prototype.f_trim = function()
  484. {
  485. return ( this.replace(/^(\s|\n)*|(\s|\n)*$/g, K_PRMT_sEMPTY) );
  486. };
  487. String.prototype.f_xmlSafe = function()
  488. {
  489. return ( this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(K_PRMT_reQU, "&quot;") );
  490. };
  491. Function.prototype.bind = function(v_oBindFct)
  492. {
  493. var v_fct = this;
  494. var v_object = v_oBindFct;
  495. var v_args = [];
  496. for (var v_idx =1; v_idx < arguments.length; v_idx++)
  497. {
  498. v_args.push( arguments[v_idx] );
  499. }
  500. return (
  501. function(e) {
  502. if (e) {
  503. v_args.push(e);
  504. }
  505. return v_fct.apply(v_object, v_args);
  506. }
  507. );
  508. };
  509. /*
  510. Class: window
  511. Some properties are attached to the window object by the browser detection.
  512. Properties:
  513. window.ie - will be set to true if the current browser is internet explorer (any).
  514. window.ie6 - will be set to true if the current browser is internet explorer 6.
  515. window.ie7 - will be set to true if the current browser is internet explorer 7.
  516. window.khtml - will be set to true if the current browser is Safari/Konqueror.
  517. window.gecko - will be set to true if the current browser is Mozilla/Gecko.
  518. */
  519. if (!!window.MSInputMethodContext && !!document.documentMode && (document.documentMode == 11)) { window.ie = window.ie11 = true; }
  520. else if (document.all && !!window.atob) { window.ie = window['ie10'] = true; }
  521. else if (document.all && !!document.addEventListener) { window.ie = window['ie9'] = true; }
  522. else if (document.all && !!document.querySelector) { window.ie = window['ie8'] = true; }
  523. else if (document.all && !!window.XMLHttpRequest) { window.ie = window['ie7'] = true; }
  524. else if (document.all && !!document.compatMode) { window.ie = window['ie6'] = true; }
  525. else if (document.all) { window.ie = window['ie5'] = true; }
  526. else if (document.childNodes && !document.all && !navigator.taintEnabled) { if (!!window.StyleMedia) { window.ie = window.ie11 = window.edge = true; } else window.khtml = true; }
  527. else if (document.getBoxObjectFor != null) window.gecko = true;