/** *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| BI and PM: prmt *| (C) Copyright IBM Corp. 2002, 2011 *| *| US Government Users Restricted Rights - Use, duplication or *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *| *+------------------------------------------------------------------------+ */ /** * This file provides methods setting text direction for various HTML control * (dynamic input fields, static labels ...) * */ var PRMT_BidiUtils = { // Bidi constants BIDI_TXTDIR_CONTEXTUAL : "auto", BIDI_TXTDIR_RTL : "rtl", BIDI_TXTDIR_LTR : "ltr", BIDI_CHAR_LRE : "\u202A", BIDI_CHAR_RLE : "\u202B", BIDI_CHAR_PDF : "\u202C", BIDI_CHAR_LRM : "\u200E", BIDI_CHAR_RLM : "\u200F", VK_SHIFT : 0x10, VK_END : 0x23, VK_HOME : 0x24, VK_LEFT : 0x25, VK_RIGHT : 0x27, /** * It fixes the direction (using the CSS style direction) of the INPUT element depending on the setting and the * input value. If input.value is a right to left language (like Arabic, Hebrew), then change the input style to right to left. * txtAlignPref is for future use in case the user prefers a fixed alignnment (not depending on text direction) */ fixInputDirection : function(inputElement, txtDir, txtAlignPref){ if (inputElement !== null && (inputElement.tagName == 'INPUT' || inputElement.tagName == 'TEXTAREA')) { inputElement.style.direction = PRMT_BidiUtils.getTextDirection(inputElement.value, txtDir); } }, /** * This function enforces the text direction of a given string by adding LRE/RLE PDF header and footer */ enforceBidiDirection : function(stringValue, txtDir){ var finalDir = PRMT_BidiUtils.getTextDirection(stringValue, txtDir); var finalValue = stringValue; if (finalDir == "ltr") { finalValue = PRMT_BidiUtils.BIDI_CHAR_LRE + finalValue + PRMT_BidiUtils.BIDI_CHAR_PDF; } else if (finalDir == "rtl") { finalValue = PRMT_BidiUtils.BIDI_CHAR_RLE + finalValue + PRMT_BidiUtils.BIDI_CHAR_PDF; } return finalValue; }, /** * Returns the text direction of a given string depending on the setting and the string text */ getTextDirection : function(val, txtDir){ var finalDir = txtDir; if (txtDir == PRMT_BidiUtils.BIDI_TXTDIR_CONTEXTUAL){ finalDir =((PRMT_BidiUtils.isRTLString(val))? PRMT_BidiUtils.BIDI_TXTDIR_RTL : PRMT_BidiUtils.BIDI_TXTDIR_LTR); } return finalDir; }, /** * Detect the direction (RTL || LTR ) of a string * The first strong character of the string determines the direction of the string * */ isRTLString : function(stringValue){ var result = false; for (var i = 0; i < stringValue.length; i++) { if (PRMT_BidiUtils.isRTLChar(stringValue.charCodeAt(i))) { result = true; break } else if(PRMT_BidiUtils.isLatinChar(stringValue.charCodeAt(i))) { break; } } return result; }, /** * Determines if the passed character c is a RTL (Hebrew or Arabic) char */ isRTLChar : function(c){ var result = false; if ((c >= 0x05d0 && c <= 0x05ff) || (c >= 0x0600 && c <= 0x065f) || (c >= 0x066a && c <= 0x06ef) || (c >= 0x06fa && c <= 0x07ff) || (c >= 0xfb1d && c <= 0xfdff) || (c >= 0xfe70 && c <= 0xfefc)) { result = true; } return result; }, /** * Determines if the passed character c is a Latin char */ isLatinChar : function(c){ return (((c > 64 && c < 91)||(c > 96 && c < 123))? true : false); }, /** * Search for the first ancestor element(including the element itself) to have a defined direction */ lookupDirection : function(elem){ var lookupDir = null; while ( (lookupDir != PRMT_BidiUtils.BIDI_TXTDIR_LTR) && (lookupDir != PRMT_BidiUtils.BIDI_TXTDIR_RTL) && elem && elem.parentNode ) { elem = elem.parentNode; lookupDir = ( (elem.style && elem.style.direction) ? elem.style.direction : elem.dir); } return lookupDir; }, /** * Determines if a string contains a RTL char */ containsRTLChar : function(stringValue){ var result = false; for (var i = 0; i < stringValue.length; i++) { if (PRMT_BidiUtils.isRTLChar(stringValue.charCodeAt(i))) { result = true; break } } return result; }, /** * This function determines if there is a RTL char between the index i and previous in the buffer */ isCharBeforeRTLChar : function(buffer, i, previous) { var result = false; var prevChar; while (i > 0 && i != previous) { prevChar = buffer.charCodeAt(i-1); if(PRMT_BidiUtils.isRTLChar(prevChar)) { result = true; break; } else if(PRMT_BidiUtils.isLatinChar(prevChar)) { break; } i--; } return result; }, /** * This function determines the positions where we should insert the LRM marker for correct display * of the structured text (STT) */ parseSTT : function(str, ce_type){ var i,i1; var delimiters; var previous = -1; var segmentsPointers = []; var sp_len = 0; if(ce_type == "FILEPATH"){ delimiters = "/\\:."; for (i = 0; i < str.length; i++) { if ((delimiters.indexOf(str.charAt(i)) >= 0) && PRMT_BidiUtils.isCharBeforeRTLChar(str,i,previous)){ previous = i; segmentsPointers[sp_len] = i; sp_len++; } } } return segmentsPointers; }, /** * This function inserts the LRM char at the positions returned by the previous method */ insertMarkers : function(str, ce_type) { var segmentsPointers = PRMT_BidiUtils.parseSTT(str, ce_type); var buf = str; shift = 0; var n; var marker = PRMT_BidiUtils.BIDI_CHAR_LRM; for (var i = 0; i< segmentsPointers.length; i++) { n = segmentsPointers[i]; if(n != null){ preStr = buf.substring(0, n + shift); postStr = buf.substring(n + shift, buf.length); buf = preStr + marker + postStr; shift++; } } return buf; }, /** * This function inserts the LRM markers in the fake input when the focus is on the true input */ onFileFocus : function(inputFile, id, ce_type) { var fakeObj = document.getElementById(id); fakeObj.value = PRMT_BidiUtils.insertMarkers(inputFile.value, ce_type); }, /** * This function inserts the LRM markers in the fake input when the true input has changed */ onFileChange : function(inputFile, id, ce_type) { var fakeObj = document.getElementById(id); fakeObj.value = PRMT_BidiUtils.insertMarkers(inputFile.value, ce_type); fakeObj.focus(); }, /** * This function reacts on inserting chars in the fake input */ onFakeKeyDown : function(event, id) { event = (event) ? event : ((window.event) ? window.event : ""); var kCode = event.keyCode; if (event.altKey || event.ctrlKey || kCode == PRMT_BidiUtils.VK_SHIFT) { return true; } else if (!(kCode == PRMT_BidiUtils.VK_HOME || kCode == PRMT_BidiUtils.VK_END || kCode == PRMT_BidiUtils.VK_LEFT || kCode == PRMT_BidiUtils.VK_RIGHT)) { PRMT_BidiUtils.focusElement(id); return false; } else { return true; } }, /** * This function focuses on the element whose id is given in parameter */ focusElement : function(id) { var elem = document.getElementById(id); elem.focus(); return false; }, /** * This function handles the copy operation * Basically we should remove the LRM markers from the text contained in the clipboard */ processCopy : function (obj){ var text = ""; try{ if ( window.navigator.appName == "Microsoft Internet Explorer" ) { var w = obj.document.parentWindow; var e = w.event; var range = obj.document.selection.createRange(); text = range.text; } else { text = obj.document.getSelection(); } var textToClipboard = PRMT_BidiUtils.removeMarkers(text); if (window.clipboardData) { window.clipboardData.setData("Text", textToClipboard); e.returnValue = false; } } catch(e) { var v_sMsg = ""; if ( e && e.name ) { v_sMsg += e.name + "\n"; } if ( e && e.message ) { v_sMsg += e.message; } if ( v_sMsg ) { alert( v_sMsg ); } } }, /** * This function removes all the Bidi markers in a string */ removeMarkers : function (str){ var result = str.replace(/\u200E/g,""); result = result.replace(/\u200F/g,""); result = result.replace(/\u202A/g,""); result = result.replace(/\u202B/g,""); return result.replace(/\u202C/g,""); }, /** * This function formats a structured text of type FILEPATH */ formatFilePath : function (str){ var result = PRMT_BidiUtils.insertMarkers(str, 'FILEPATH'); return PRMT_BidiUtils.enforceBidiDirection(result, PRMT_BidiUtils.BIDI_TXTDIR_LTR); } };