From 662ad0f7bb3abb576d4ec188c11e235bf777a99b Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 9 Mar 2011 18:11:08 -0800 Subject: [PATCH] Update DirectionDetector for reusable notice forms. Also tweaked it to trigger on cut/paste, and to reset when the form resets. --- .../jquery.DirectionDetector.js | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/plugins/DirectionDetector/jquery.DirectionDetector.js b/plugins/DirectionDetector/jquery.DirectionDetector.js index 6808bcabd8..840cd69097 100644 --- a/plugins/DirectionDetector/jquery.DirectionDetector.js +++ b/plugins/DirectionDetector/jquery.DirectionDetector.js @@ -47,15 +47,26 @@ return true; return false; }; - $(document).ready(function(){ - var tArea = $("#notice_data-text"); - var tCleaner = new RegExp('@[^ ]+|![^ ]+|#[^ ]+|^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]+', 'g') - tArea.keyup(function(){ - var cleaned = tArea.val().replace(tCleaner, '').replace(/^[ ]+/, ''); - if($().isRTL(cleaned)) - tArea.css('direction', 'rtl'); - else - tArea.css('direction', 'ltr'); - }); - }); + var origInit = SN.Init.NoticeFormSetup; + SN.Init.NoticeFormSetup = function(form) { + origInit(form); + var tArea = form.find(".notice_data-text:first"); + if (tArea.length > 0) { + var tCleaner = new RegExp('@[^ ]+|![^ ]+|#[^ ]+|^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]+', 'g') + var ping = function(){ + var cleaned = tArea.val().replace(tCleaner, '').replace(/^[ ]+/, ''); + if($().isRTL(cleaned)) + tArea.css('direction', 'rtl'); + else + tArea.css('direction', 'ltr'); + }; + tArea.bind('keyup cut paste', function() { + // cut/paste trigger before the change + window.setTimeout(ping, 0); + }); + form.bind('reset', function() { + tArea.css('direction', 'ltr'); + }); + } + }; })(jQuery); -- 2.39.5