diaspora/app/assets/javascripts/widgets/direction-detector.js
Steven Hancock 1aa0b15c8c Move Javascript to the asset pipeline
* Move all Diaspora-specific javascripts to app/assets/javascripts
* Move all vendored javascripts to vendor/assets/javascripts
* Add the appropriate Sprockets require directives to make sure
  everything gets included in the right order
* Remove Jammit dependencies
* Fix all templates that were using Jammit's include_javascripts helper
* Add handlebars_assets gem for compiling Handlebars templates
* Move all Handlebars templates to app/assets/templates and rename
  from .handlebars to .jst.hbs (this is to keep them in the same
  global JST namespace that they were in under Jammit)
* Add public/assets to .gitignore since these files can and should
  be re-generated by Heroku or Capistrano during each deploy
* Fix a few Handlebars templates that were looking for images in the
  wrong location (I'm sure there are others, but it's late)
* Configure application.rb to precompile all javascript and css assets
  that were compiled by Jammit in the Rails 3.0 code
2012-03-27 18:07:44 -07:00

86 lines
2.3 KiB
JavaScript

/* Copyright (c) 2010-2011, Diaspora Inc. This file is
* licensed under the Affero General Public License version 3 or later. See
* the COPYRIGHT file.
*/
/* Modified version of https://gitorious.org/statusnet/mainline/blobs/master/plugins/DirectionDetector/jquery.DirectionDetector.js */
(function() {
var DirectionDetector = function() {
var self = this;
this.binds = [];
this.cleaner = new RegExp("@[^ ]+|^RT[: ]{1}| RT | RT: |[♺♻:]+", "g");
this.subscribe("widget/ready", function() {
self.updateBinds();
self.globalSubscribe("stream/scrolled", function() {
self.updateBinds();
});
});
this.isRTL = function(str) {
if(typeof str !== "string" || str.length < 1) {
return false;
}
var charCode = str.charCodeAt(0);
if(charCode >= 1536 && charCode <= 1791) // Sarabic, Persian, ...
return true;
else if(charCode >= 65136 && charCode <= 65279) // Arabic present 1
return true;
else if(charCode >= 64336 && charCode <= 65023) // Arabic present 2
return true;
else if(charCode>=1424 && charCode<=1535) // Hebrew
return true;
else if(charCode>=64256 && charCode<=64335) // Hebrew present
return true;
else if(charCode>=1792 && charCode<=1871) // Syriac
return true;
else if(charCode>=1920 && charCode<=1983) // Thaana
return true;
else if(charCode>=1984 && charCode<=2047) // NKo
return true;
else if(charCode>=11568 && charCode<=11647) // Tifinagh
return true;
return false;
};
this.updateBinds = function() {
$.each(self.binds, function(index, bind) {
bind.unbind("keyup", self.updateDirection);
});
self.binds = [];
$("textarea, input[type='text'], input[type='search']").each(self.bind);
};
this.bind = function() {
self.binds.push(
$(this).bind("keyup", self.updateDirection)
);
};
this.updateDirection = function() {
var textArea = $(this),
cleaned = textArea.val().replace(self.cleaner, "").replace(/^[ ]+/, "");
if(self.isRTL(cleaned)) {
textArea.css("direction", "rtl");
}
else {
textArea.css("direction", "ltr");
}
};
};
Diaspora.Widgets.DirectionDetector = DirectionDetector;
})();