2016-08-25 17:47:30 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2016 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
2016-08-25 18:09:56 +02:00
|
|
|
RED.text = {};
|
|
|
|
RED.text.bidi = (function() {
|
2016-08-25 17:47:30 +02:00
|
|
|
var textDir = "";
|
|
|
|
var LRE = "\u202A",
|
|
|
|
RLE = "\u202B",
|
|
|
|
PDF = "\u202C";
|
2016-08-25 18:09:56 +02:00
|
|
|
|
2016-08-25 17:47:30 +02:00
|
|
|
function isRTLValue(stringValue) {
|
2016-08-26 01:28:22 +02:00
|
|
|
var length = stringValue.length;
|
|
|
|
for (var i=0;i<length;i++) {
|
|
|
|
if (isBidiChar(stringValue.charCodeAt(i))) {
|
2016-08-25 17:47:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-26 01:28:22 +02:00
|
|
|
else if(isLatinChar(stringValue.charCodeAt(i))) {
|
2016-08-25 17:47:30 +02:00
|
|
|
return false;
|
2016-08-25 18:09:56 +02:00
|
|
|
}
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isBidiChar(c) {
|
2016-08-26 01:28:22 +02:00
|
|
|
return (c >= 0x05d0 && c <= 0x05ff)||
|
|
|
|
(c >= 0x0600 && c <= 0x065f)||
|
|
|
|
(c >= 0x066a && c <= 0x06ef)||
|
|
|
|
(c >= 0x06fa && c <= 0x07ff)||
|
|
|
|
(c >= 0xfb1d && c <= 0xfdff)||
|
|
|
|
(c >= 0xfe70 && c <= 0xfefc);
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isLatinChar(c){
|
2016-08-26 01:28:22 +02:00
|
|
|
return (c > 64 && c < 91)||(c > 96 && c < 123)
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
2016-08-25 18:09:56 +02:00
|
|
|
|
|
|
|
/**
|
2016-08-25 17:47:30 +02:00
|
|
|
* Determines the text direction of a given string.
|
2016-08-25 18:09:56 +02:00
|
|
|
* @param value - the string
|
2016-08-25 17:47:30 +02:00
|
|
|
*/
|
|
|
|
function resolveBaseTextDir(value) {
|
|
|
|
if (textDir == "auto") {
|
|
|
|
if (isRTLValue(value)) {
|
|
|
|
return "rtl";
|
|
|
|
} else {
|
|
|
|
return "ltr";
|
|
|
|
}
|
2016-08-25 18:09:56 +02:00
|
|
|
}
|
2016-08-25 17:47:30 +02:00
|
|
|
else {
|
|
|
|
return textDir;
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 18:09:56 +02:00
|
|
|
|
2016-08-25 17:47:30 +02:00
|
|
|
function onInputChange() {
|
2016-08-25 18:09:56 +02:00
|
|
|
$(this).attr("dir", resolveBaseTextDir($(this).val()));
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 18:09:56 +02:00
|
|
|
/**
|
2016-08-26 13:50:18 +02:00
|
|
|
* Adds event listeners to the Input to ensure its text-direction attribute
|
|
|
|
* is properly set based on its content.
|
2016-08-25 18:09:56 +02:00
|
|
|
* @param input - the input field
|
2016-08-25 17:47:30 +02:00
|
|
|
*/
|
2016-08-26 13:50:18 +02:00
|
|
|
function prepareInput(input) {
|
2016-08-25 17:47:30 +02:00
|
|
|
input.on("keyup",onInputChange).on("paste",onInputChange).on("cut",onInputChange);
|
2016-08-26 13:50:18 +02:00
|
|
|
// Set the initial text direction
|
|
|
|
onInputChange.call(input);
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
2016-08-25 18:09:56 +02:00
|
|
|
|
|
|
|
/**
|
2016-08-26 01:28:22 +02:00
|
|
|
* Enforces the text direction of a given string by adding
|
|
|
|
* UCC (Unicode Control Characters)
|
2016-08-25 18:09:56 +02:00
|
|
|
* @param value - the string
|
2016-08-25 17:47:30 +02:00
|
|
|
*/
|
|
|
|
function enforceTextDirectionWithUCC(value) {
|
|
|
|
if (value) {
|
|
|
|
var dir = resolveBaseTextDir(value);
|
|
|
|
if (dir == "ltr") {
|
|
|
|
return LRE + value + PDF;
|
|
|
|
}
|
|
|
|
else if (dir == "rtl") {
|
|
|
|
return RLE + value + PDF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2016-08-25 18:09:56 +02:00
|
|
|
|
|
|
|
/**
|
2016-08-26 01:28:22 +02:00
|
|
|
* Enforces the text direction for all the spans with style bidiAware under
|
|
|
|
* workspace or sidebar div
|
2016-08-25 17:47:30 +02:00
|
|
|
*/
|
2016-08-25 18:09:56 +02:00
|
|
|
function enforceTextDirectionOnPage() {
|
|
|
|
$("#workspace").find('span.bidiAware').each(function() {
|
2016-08-25 17:47:30 +02:00
|
|
|
$(this).attr("dir", resolveBaseTextDir($(this).html()));
|
2016-08-25 18:09:56 +02:00
|
|
|
});
|
|
|
|
$("#sidebar").find('span.bidiAware').each(function() {
|
2016-08-25 17:47:30 +02:00
|
|
|
$(this).attr("dir", resolveBaseTextDir($(this).text()));
|
2016-08-25 18:09:56 +02:00
|
|
|
});
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
2016-08-25 18:09:56 +02:00
|
|
|
|
|
|
|
/**
|
2016-08-25 17:47:30 +02:00
|
|
|
* Sets the text direction preference
|
2016-08-25 18:09:56 +02:00
|
|
|
* @param dir - the text direction preference
|
2016-08-25 17:47:30 +02:00
|
|
|
*/
|
|
|
|
function setTextDirection(dir) {
|
|
|
|
textDir = dir;
|
2016-08-26 01:40:01 +02:00
|
|
|
RED.nodes.eachNode(function(n) { n.dirty = true;});
|
|
|
|
RED.view.redraw();
|
|
|
|
RED.palette.refresh();
|
|
|
|
enforceTextDirectionOnPage();
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
2016-08-25 18:09:56 +02:00
|
|
|
|
2016-08-25 17:47:30 +02:00
|
|
|
return {
|
|
|
|
setTextDirection: setTextDirection,
|
|
|
|
enforceTextDirectionWithUCC: enforceTextDirectionWithUCC,
|
|
|
|
resolveBaseTextDir: resolveBaseTextDir,
|
2016-08-26 13:50:18 +02:00
|
|
|
prepareInput: prepareInput
|
2016-08-25 17:47:30 +02:00
|
|
|
}
|
|
|
|
})();
|