jquery.DirectionDetector.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * @category Plugin
  17. * @package StatusNet
  18. * @author Behrooz shabani (everplays) - <behrooz@rock.com>
  19. * @copyright 2009-2010 Behrooz shabani
  20. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  21. *
  22. */
  23. (function($){
  24. $.fn.isRTL = function(str){
  25. if(typeof str != typeof "" || str.length<1)
  26. return false;
  27. var cc = str.charCodeAt(0);
  28. if(cc>=1536 && cc<=1791) // arabic, persian, ...
  29. return true;
  30. if(cc>=65136 && cc<=65279) // arabic peresent 2
  31. return true;
  32. if(cc>=64336 && cc<=65023) // arabic peresent 1
  33. return true;
  34. if(cc>=1424 && cc<=1535) // hebrew
  35. return true;
  36. if(cc>=64256 && cc<=64335) // hebrew peresent
  37. return true;
  38. if(cc>=1792 && cc<=1871) // Syriac
  39. return true;
  40. if(cc>=1920 && cc<=1983) // Thaana
  41. return true;
  42. if(cc>=1984 && cc<=2047) // NKo
  43. return true;
  44. if(cc>=11568 && cc<=11647) // Tifinagh
  45. return true;
  46. return false;
  47. };
  48. var origInit = SN.Init.NoticeFormSetup;
  49. SN.Init.NoticeFormSetup = function(form) {
  50. origInit(form);
  51. var tArea = form.find(".notice_data-text:first");
  52. if (tArea.length > 0) {
  53. var tCleaner = new RegExp('@[^ ]+|![^ ]+|#[^ ]+|^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]+', 'g')
  54. var ping = function(){
  55. var cleaned = tArea.val().replace(tCleaner, '').replace(/^[ ]+/, '');
  56. if($().isRTL(cleaned))
  57. tArea.css('direction', 'rtl');
  58. else
  59. tArea.css('direction', 'ltr');
  60. };
  61. tArea.bind('keyup cut paste', function() {
  62. // cut/paste trigger before the change
  63. window.setTimeout(ping, 0);
  64. });
  65. form.bind('reset', function() {
  66. tArea.css('direction', 'ltr');
  67. });
  68. }
  69. };
  70. })(jQuery);