123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /**
- *+------------------------------------------------------------------------+
- *| 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);
- }
- };
|