From ea72fe3ff5c73eeedc9fe9e50426823a0cc28570 Mon Sep 17 00:00:00 2001 From: samarsultan Date: Sun, 5 Mar 2017 10:43:29 +0200 Subject: [PATCH] Creating bidi interface --- notebook/static/bidi/bidi.js | 114 ++++++++++++++++++++++ notebook/static/edit/js/savewidget.js | 6 +- notebook/static/notebook/js/savewidget.js | 4 +- notebook/static/tree/js/notebooklist.js | 14 ++- 4 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 notebook/static/bidi/bidi.js diff --git a/notebook/static/bidi/bidi.js b/notebook/static/bidi/bidi.js new file mode 100644 index 000000000..bee1e0342 --- /dev/null +++ b/notebook/static/bidi/bidi.js @@ -0,0 +1,114 @@ +// Copyright (c) Jupyter Development Team. +// Distributed under the terms of the Modified BSD License. + +define([ + 'bidi/numericshaping', + 'moment-hijri', +], function (numericshaping, moment){ + "use strict"; + var calendarType=""; + var textDir=""; + var shaperType=""; + + var _setCalendarType = function (val){ + calendarType=val; + }; + + var _getcalendarType = function (){ + return calendarType; + }; + + var _setTextDir = function (dir){ + textDir=dir; + }; + + var _setNumericShaping = function (val){ + shaperType=val; + }; + + var _flags= { + NS: 1, + CALENDAR : 2 + }; + + /* + * Test the UI Language + */ + var _uiLang= function (){ + return navigator.language.toLowerCase(); + }; + /* + * TODO: this function cannot be invoked once ,as moment has different formats for the date . + * It needs improvements. + */ + var _getGlobalizedDate = function (date) { + var lang = _uiLang(); + if (calendarType === "hijri" || lang ==='ar-sa') { + + } else if (calendarType === "hebrew") { + } + return; + }; + + /* + * check if the locale exists provided by moment + */ + var _hasLocale = function (lang) { + if (lang == 'af'||'ar-ma'||'ar-sa'||'ar'||'az'||'be'||'bg' ||'bn'||'bo'||'br'||'bs'||'ca'||'cs'||'cv'||'cy'||'da'||'de-at'||'de'||'el'||'en-au'||'en-ca'||'en-gb'||'eo'||'es'||'et'||'eu'||'fa'||'fi'||'fo'||'fr-ca'||'fr'||'gl'||'he'||'hi'||'hr'||'hu'||'hy-am'||'id'||'is'||'it'||'ja'||'ka'||'km'||'ko'||'lb'||'lt'||'lv'||'mk'||'ml'||'mr'||'ms-my'||'my'||'nb'||'ne'||'nl'||'nn'||'pl'||'pt-br'||'pt'||'ro'||'ru'||'sk'||'sl'||'sq'||'sr-cyrl'||'sr'||'sv'||'ta'||'th'||'tl-ph'||'tr'||'tzm-latn'||'tzm'||'uk'||'uz'||'vi'||'zh-cn'||'zh-tw' ) { + return true; + } + else{ + return false; + } + }; + + /* + * to load the required locale + */ + var _loadLocale = function (lang) { + if ( _hasLocale(lang)){ + return requirejs(['components/moment/locale/'+lang], function (){}); + } + else{ + return requirejs(['components/moment'], function (){}); + } + }; + + /* + * TODO: to add the other RTL languages + */ + var _isMirroringEnabled= function() { + if(new RegExp("^(ar|he)").test(_uiLang())){ + $("body").attr("dir","rtl"); + } + return; + }; + + var _applyBidi = function (value , flag) { + if(flag & _flags.NS == _flags.NS) { + value = numericshaping.shapeNumerals(value, shaperType, textDir); + return value; + } + if(flag & _flags.CALENDAR == _flags.CALENDAR) { + value = _getGlobalizedDate(value); + console.log("Calendar "); + return value; + } + return value; + }; + + var bidi = { + isMirroringEnabled : _isMirroringEnabled, + setNumericShaping : _setNumericShaping, + setCalendarType: _setCalendarType, + getcalendarType : _getcalendarType, + setTextDir : _setTextDir, + getGlobalizedDate: _getGlobalizedDate, + flags : _flags, + applyBidi : _applyBidi, + uiLang : _uiLang, + loadLocale : _loadLocale, + }; + + return bidi; +}); diff --git a/notebook/static/edit/js/savewidget.js b/notebook/static/edit/js/savewidget.js index 31d7cc65d..7d0cd3a31 100644 --- a/notebook/static/edit/js/savewidget.js +++ b/notebook/static/edit/js/savewidget.js @@ -7,9 +7,10 @@ define([ 'base/js/dialog', 'base/js/keyboard', 'moment', -], function($, utils, dialog, keyboard, moment) { + 'bidi/bidi', +], function($, utils, dialog, keyboard, moment ,bidi) { "use strict"; - + bidi.loadLocale(bidi.uiLang()); var SaveWidget = function (selector, options) { this.editor = undefined; this.selector = selector; @@ -108,6 +109,7 @@ define([ SaveWidget.prototype.update_filename = function (filename) { + filename = bidi.applyBidi(filename , bidi.flags.NS); this.element.find('span.filename').text(filename); }; diff --git a/notebook/static/notebook/js/savewidget.js b/notebook/static/notebook/js/savewidget.js index c78067f98..a17d0b67a 100644 --- a/notebook/static/notebook/js/savewidget.js +++ b/notebook/static/notebook/js/savewidget.js @@ -7,7 +7,8 @@ define([ 'base/js/dialog', 'base/js/keyboard', 'moment', -], function($, utils, dialog, keyboard, moment) { + 'bidi/bidi', +], function($, utils, dialog, keyboard, moment ,bidi) { "use strict"; var SaveWidget = function (selector, options) { @@ -130,6 +131,7 @@ define([ SaveWidget.prototype.update_notebook_name = function () { var nbname = this.notebook.get_notebook_name(); + nbname = bidi.applyBidi(nbname , bidi.flags.NS); this.element.find('span.filename').text(nbname); }; diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index f9724c0a7..ff1e6b609 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -8,8 +8,9 @@ define([ 'base/js/dialog', 'base/js/events', 'base/js/keyboard', - 'moment' -], function($, IPython, utils, dialog, events, keyboard, moment) { + 'moment', + 'bidi/bidi', +], function($, IPython, utils, dialog, events, keyboard, moment , bidi) { "use strict"; var NotebookList = function (selector, options) { @@ -643,6 +644,7 @@ define([ select_all.data('indeterminate', true); } // Update total counter + checked = bidi.applyBidi(checked , bidi.flags.NS); $('#counter-select-all').html(checked===0 ? ' ' : checked); // If at aleast on item is selected, hide the selection instructions. @@ -658,7 +660,8 @@ define([ name = model.name, modified = model.last_modified; var running = (model.type === 'notebook' && this.sessions[path] !== undefined); - + + name = bidi.applyBidi(name , bidi.flags.NS); item.data('name', name); item.data('path', path); item.data('modified', modified); @@ -697,6 +700,11 @@ define([ // Add in the date that the file was last modified item.find(".item_modified").text(utils.format_datetime(modified)); + if (bidi.getcalendarType()==="hijri"){ + require(['moment-hijri'],function (moment_hijri){ + item.find(".item_modified").attr("title", moment_hijri(modified).format("iYYYY-iMM-iDD HH:mm")); + }); + } item.find(".item_modified").attr("title", moment(modified).format("YYYY-MM-DD HH:mm")); };