mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-12-01 02:03 am

[dw-free] User tags slow to show up in RTE when switching between HTML and RTE

[commit: http://hg.dwscoalition.org/dw-free/rev/5d69dee7c529]

http://bugs.dwscoalition.org/show_bug.cgi?id=2084

Add caching for user tags in the RTE. Makes switching back and forth from
HTML mode much faster if you have many user tags.

Patch by [personal profile] afuna.

Files modified:
  • htdocs/js/rte.js
  • htdocs/stc/fck/editor/plugins/livejournal/fckplugin.js
--------------------------------------------------------------------------------
diff -r fbecdd90f36a -r 5d69dee7c529 htdocs/js/rte.js
--- a/htdocs/js/rte.js	Tue Dec 01 01:50:03 2009 +0000
+++ b/htdocs/js/rte.js	Tue Dec 01 02:03:44 2009 +0000
@@ -1,3 +1,5 @@ function LJUser(textArea) {
+var UserTagCache = {};
+
 function LJUser(textArea) {
     var editor_frame = $(textArea + '___Frame');
     if (!editor_frame) return;
@@ -16,7 +18,7 @@ function LJUser(textArea) {
 
     while ((users = regexp.exec(html))) {
         var attrs = [];
-        var postData = {};
+        var postData = { 'username': '', 'site': '' };
 
         while (( attrs=attrs_regexp.exec(users[1]) )) {
             if (attrs[1] == 'user' || attrs[1] == 'name')
@@ -31,11 +33,11 @@ function LJUser(textArea) {
             return;
         }
 
+        // trim any trailing spaces from the userstr
+        // so that we don't get rid of them when we do the replace below
+        var userStrToReplace = users[0].replace(/\s\s*$/, '');
+
         var gotInfo = (function (userstr, username, site) { return function(data) {
-            // trim any trailing spaces from the userstr
-            // so that we don't get rid of them when we do the replace below
-            userstr = userstr.replace(/\s\s*$/, '');
-
             if (data.error) {
                 alert(data.error+' '+username);
                 return;
@@ -48,20 +50,28 @@ function LJUser(textArea) {
                 data.ljuser = data.ljuser.replace(/<span.+?class=['"]?ljuser['"]?.+?>/,'<div class="ljuser">');
 
             data.ljuser = data.ljuser.replace(/<\/span>\s?/,'</div>');
+            UserTagCache[username + "_" + site] = data.ljuser;
+
             html = html.replace(userstr,data.ljuser);
             oEditor.SetData(html);
             oEditor.Focus();
-        }})(users[0], postData.username, postData.site);
+        }})(userStrToReplace, postData.username, postData.site);
 
-        var opts = {
-            "data": window.parent.HTTPReq.formEncoded(postData),
-            "method": "POST",
-            "url": url,
-            "onError": gotError,
-            "onData": gotInfo
-        };
-
-        window.parent.HTTPReq.getJSON(opts);
+        if ( UserTagCache[postData.username+"_"+postData.site] ) {
+            html = html.replace( userStrToReplace, UserTagCache[postData.username+"_"+postData.site] );
+            oEditor.SetData(html);
+            oEditor.Focus();            
+        } else {
+            var opts = {
+                "data": window.parent.HTTPReq.formEncoded(postData),
+                "method": "POST",
+                "url": url,
+                "onError": gotError,
+                "onData": gotInfo
+            };
+    
+            window.parent.HTTPReq.getJSON(opts);
+        }
     }
 }
 
diff -r fbecdd90f36a -r 5d69dee7c529 htdocs/stc/fck/editor/plugins/livejournal/fckplugin.js
--- a/htdocs/stc/fck/editor/plugins/livejournal/fckplugin.js	Tue Dec 01 01:50:03 2009 +0000
+++ b/htdocs/stc/fck/editor/plugins/livejournal/fckplugin.js	Tue Dec 01 02:03:44 2009 +0000
@@ -34,10 +34,10 @@ LJUserCommand.Execute=function() {
 
     function do_insert( username, site ) {
         var postData = {
-            "username" : username,
-            "site"     : site
+            "username" : username || '',
+            "site"     : site || ''
         };
-    
+
         if (username == null) return;
     
         var url = window.parent.Site.siteroot + "/tools/endpoints/ljuser";
@@ -60,21 +60,30 @@ LJUserCommand.Execute=function() {
                 data.ljuser = data.ljuser.replace(/<span.+?class=['"]?ljuser['"]?.+?>/,'<div class="ljuser">');
 
             data.ljuser = data.ljuser.replace(/<\/span>/,'</div>');
+            UserTagCache[username + "_" + site] = data.ljuser;
+
             FCK.InsertHtml(data.ljuser);
             FCK.InsertHtml('&nbsp;')
             if (selection != '') FCKSelection.Collapse();
             FCK.Focus();
         }
     
-        var opts = {
-            "data": window.parent.HTTPReq.formEncoded(postData),
-            "method": "POST",
-            "url": url,
-            "onError": gotError,
-            "onData": gotInfo
-        };
-    
-        window.parent.HTTPReq.getJSON(opts);
+        if ( UserTagCache[postData.username+"_"+postData.site] ) {
+            FCK.InsertHtml(UserTagCache[postData.username+"_"+postData.site]);
+            FCK.InsertHtml('&nbsp;')
+            if (selection != '') FCKSelection.Collapse();
+            FCK.Focus();
+        } else {
+            var opts = {
+                "data": window.parent.HTTPReq.formEncoded(postData),
+                "method": "POST",
+                "url": url,
+                "onError": gotError,
+                "onData": gotInfo
+            };
+        
+            window.parent.HTTPReq.getJSON(opts);
+        }
     }
 
     if (selection != '') {
@@ -82,6 +91,7 @@ LJUserCommand.Execute=function() {
         do_insert( username );
     } else {
         var DOM = window.parent.DOM;
+        var UserTagCache = window.parent.UserTagCache;
         var _textDiv = window.parent._textDiv;
         var userPopup = new window.parent.LJ_IPPU( window.parent.FCKLang.UserPrompt );
         userPopup.hide =   function() {
--------------------------------------------------------------------------------