Save select draft properties: subject, userpic, tags, mood id, mood, 
location, music, adult reason, adult content status, comment settings, comment 
screening settings. Also fix a bug that would cause the "Do you want to 
restore?" alert to pop up unnecessarily.

        Patch by grimsniffer. 

diffs (372 lines):

diff -r 09ce6d0bca17 -r 496bd3ba7db2 bin/upgrading/proplists.dat
--- a/bin/upgrading/proplists.dat       Wed Feb 17 04:57:01 2010 +0000
+++ b/bin/upgrading/proplists.dat       Sun Feb 21 16:37:03 2010 +0000
@@ -277,6 +277,14 @@ userproplist.entry_draft:
   indexed: 0
   multihomed: 0
   prettyname: New Entry Draft
+
+userproplist.draft_properties:
+  cldversion: 4
+  datatype: blobchar
+  des: Storable hashref of a user's most recent draft properties
+  indexed: 0
+  multihomed: 0
+  prettyname: Backup of an Entry Draft's properties (subject line, etc.)
 
 userproplist.entry_editor:
   cldversion: 4
diff -r 09ce6d0bca17 -r 496bd3ba7db2 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm        Wed Feb 17 04:57:01 2010 +0000
+++ b/cgi-bin/LJ/User.pm        Sun Feb 21 16:37:03 2010 +0000
@@ -31,6 +31,7 @@ no warnings 'uninitialized';
 
 package LJ::User;
 use Carp;
+use Storable;
 use lib "$LJ::HOME/cgi-bin";
 use List::Util ();
 use LJ::Constants;
@@ -4099,6 +4100,7 @@ sub security_group_display {
 }
 
 
+
 sub set_draft_text {
     my ($u, $draft) = @_;
     my $old = $u->draft_text;
@@ -4144,7 +4146,6 @@ sub set_draft_text {
     }
     return 0;
 }
-
 
 sub third_party_notify_list {
     my $u = shift;
diff -r 09ce6d0bca17 -r 496bd3ba7db2 htdocs/js/entry.js
--- a/htdocs/js/entry.js        Wed Feb 17 04:57:01 2010 +0000
+++ b/htdocs/js/entry.js        Sun Feb 21 16:37:03 2010 +0000
@@ -905,18 +905,114 @@ LJDraft.save = function (drafttext, cb) 
       onError: function () { LJDraft.saveInProg = false; },
       data: HTTPReq.formEncoded({"saveDraft": drafttext})
     });
+
 };
 
+
 LJDraft.startTimer = function () {
+    var draftProperties = new Object();
+
+    // Get all the properties, excluding the draft body, of the user's draft so
+    // that we can pass them to LJDraft.checkProperties. We do this here to
+    // avoid querying draft.bml every second, and thus spamming MySQL to death.
+    HTTPReq.getJSON({
+      method: "GET",
+      url: "/tools/endpoints/draft",
+      onData: function (resObj) {
+              draftProperties = resObj;
+              }, 
+      data: HTTPReq.formEncoded({"getProperties": 1})
+      });
+
     setInterval(LJDraft.checkIfDirty, 1000);  // check every second
+    setInterval(function () {LJDraft.checkProperties(draftProperties)}, 1000);
     LJDraft.epoch = 0;
+};
+
+LJDraft.clearProperties = function () { //Clear all the draft's properties
+    HTTPReq.getJSON({                   //Excluding the Body.
+      method: "POST",
+      url: "/tools/endpoints/draft",
+      data: HTTPReq.formEncoded({"clearProperties": 1})
+      });
+};
+
+//Check and see if one of the draft's properties was changed.
+//If so, save them all through draft.bml.
+LJDraft.checkProperties = function (properties) {
+    if ( $("prop_picture_keyword") ) { //In case the user has no userpics
+        var currentUserpic = $("prop_picture_keyword").selectedIndex;
+    };
+    var currentSubject = $("subject").value;
+    var currentTaglist = $("prop_taglist").value;
+    var currentMoodID = $("prop_current_moodid").selectedIndex;
+    var currentMood = $("prop_current_mood").value;
+    var currentLocation = $("prop_current_location").value;
+    var currentMusic = $("prop_current_music").value;
+    var currentAdultReason = $("prop_adult_content_reason").value;
+    var currentCommentSet = $("comment_settings").selectedIndex;
+    var currentCommentScr = $("prop_opt_screening").selectedIndex;
+    var currentAdultCnt = $("prop_adult_content").selectedIndex;
+
+
+    currentAdultReason = convert_to_draft(currentAdultReason);
+    currentMusic = convert_to_draft(currentMusic);
+    currentLocation = convert_to_draft(currentLocation);
+    currentSubject = convert_to_draft(currentSubject);
+    currentTaglist = convert_to_draft(currentTaglist);
+    currentMood = convert_to_draft(currentMood);
+
+    if ( currentUserpic     != properties.userpic     ||
+         currentSubject     != properties.subject     ||
+         currentTaglist     != properties.taglist     ||
+         currentMoodID      != properties.moodid      ||
+         currentMood        != properties.mood        ||
+         currentLocation    != properties.location1   || //avoiding saved JS 
term
+         currentMusic       != properties.music       ||
+         currentAdultReason != properties.adultreason ||
+         currentCommentSet  != properties.commentset  ||
+         currentCommentScr  != properties.commentscr  ||
+         currentAdultCnt    != properties.adultcnt       ) 
+       {
+         
+         properties.userpic = currentUserpic;
+         properties.subject = currentSubject;
+         properties.taglist = currentTaglist;
+         properties.moodid  = currentMoodID;
+         properties.mood    = currentMood;
+         properties.location1 = currentLocation;
+         properties.music     = currentMusic;
+         properties.adultreason = currentAdultReason;
+         properties.commentset  = currentCommentSet;
+         properties.commentscr  = currentCommentScr;
+         properties.adultcnt    = currentAdultCnt;
+
+         HTTPReq.getJSON({
+           method: "POST",
+           url: "/tools/endpoints/draft",
+           data: HTTPReq.formEncoded({"saveUserpic":     currentUserpic,
+                                      "saveSubject":     currentSubject,
+                                      "saveTaglist":     currentTaglist,
+                                      "saveMoodID":      currentMoodID,
+                                      "saveMood":        currentMood,
+                                      "saveLocation":    currentLocation,
+                                      "saveMusic":       currentMusic,
+                                      "saveAdultReason": currentAdultReason,
+                                      "saveCommentSet":  currentCommentSet,
+                                      "saveCommentScr":  currentCommentScr,
+                                      "saveAdultCnt":    currentAdultCnt    })
+           });
+    };
 };
 
 LJDraft.checkIfDirty = function () {
     LJDraft.epoch++;
     var curBody;
 
-    if (!$("draft")) return false;
+    // If the draft is empty, delete it.
+    if ( !$( "draft" ) ) {
+        LJDraft.save("");
+    };
     if ($("draft").style.display == 'none') { // Need to check this to deal 
with hitting the back button
         // Since they may start using the RTE in the middle of writing their
         // entry, we should just get the editor each time.
diff -r 09ce6d0bca17 -r 496bd3ba7db2 htdocs/tools/endpoints/draft.bml
--- a/htdocs/tools/endpoints/draft.bml  Wed Feb 17 04:57:01 2010 +0000
+++ b/htdocs/tools/endpoints/draft.bml  Sun Feb 21 16:37:03 2010 +0000
@@ -2,6 +2,7 @@
 {
     use strict;
     use vars qw(%POST);
+    use Storable;
 
     my $err = sub {
         my $msg = shift;
@@ -20,9 +21,80 @@
 
     my $ret = {};
 
+
+    # This property thaws the contents of the userprop 'draft_properties' and
+    # sends them back as a JS object.
+    if ( defined $GET{getProperties} ) {
+        my $rv = Storable::thaw( $u->prop( 'draft_properties' ) || '' );
+
+        BML::set_content_type('text/javascript; charset=utf-8');
+        BML::finish();
+
+        return LJ::js_dumper( $rv );
+    }
+        
+
+    # This property clears out all the fields of the user's draft, except the
+    # draft body itself.
+    if ( defined $POST{clearProperties} ) {
+        $u->clear_prop( 'draft_properties' );
+    }
+
+    # If even one property of the draft was changed, this property saves them
+    # all into a new draft (in order to avoid multiple HTTP posts which would
+    # decrease performance considerably).
+    # This is set up as a long if statement to avoid tying draft property 
saving to
+    # the draft body save logic, so that users won't have to change their
+    # draft body every time they want to get their properties saved.
+    if ( ( defined $POST{saveSubject}     ) || 
+         ( defined $POST{saveUserpic}     ) ||
+         ( defined $POST{saveTaglist}     ) ||
+         ( defined $POST{saveMoodID}      ) ||
+         ( defined $POST{saveMood}        ) ||
+         ( defined $POST{saveLocation}    ) ||
+         ( defined $POST{saveMusic}       ) ||
+         ( defined $POST{saveAdultReason} ) ||
+         ( defined $POST{saveCommentSet}  ) ||
+         ( defined $POST{saveCommentScr}  ) ||
+         ( defined $POST{saveAdultCnt}    )  ) 
+        {
+        my %properties = ( subject     => $POST{saveSubject}, 
+                           userpic     => $POST{saveUserpic},
+                           taglist     => $POST{saveTaglist},
+                           moodid      => $POST{saveMoodID},
+                           mood        => $POST{saveMood},
+                           location1   => $POST{saveLocation},
+                           music       => $POST{saveMusic},
+                           adultreason => $POST{saveAdultReason},
+                           commentset  => $POST{saveCommentSet},
+                           commentscr  => $POST{saveCommentScr},
+                           adultcnt    => $POST{saveAdultCnt}   );
+        
+        # If the property is null, a default menu selection or a JS undefined
+        # value, we don't want to save it.
+        foreach my $key ( keys ( %properties ) ) {
+            if ( ( $properties{$key} =~ /^$/          ) ||
+                 ( $properties{$key} =~ /^0$/         ) ||
+                 ( $properties{$key} =~ /^undefined$/ ) ) {
+                delete $properties{$key};
+            };
+        };
+
+        # Freeze the hash into a frozen storable string. If the hash is not 
empty
+        # save it to the userprop. If it is, delete it. 
+        my $frozen_properties =  Storable::nfreeze( \%properties );
+        if ( $frozen_properties =~ /\w/ ) {
+            $u->set_prop('draft_properties', $frozen_properties);
+        } else {
+            $u->clear_prop('draft_properties');
+        };
+    }
+
+    # This property saves the main body of the draft.
     if (defined $POST{'saveDraft'}) {
         $u->set_draft_text($POST{'saveDraft'});
 
+    # This property clears out the main body of the draft.
     } elsif ($POST{'clearDraft'}) {
         $u->set_draft_text('');
 
diff -r 09ce6d0bca17 -r 496bd3ba7db2 htdocs/update.bml
--- a/htdocs/update.bml Wed Feb 17 04:57:01 2010 +0000
+++ b/htdocs/update.bml Sun Feb 21 16:37:03 2010 +0000
@@ -533,6 +533,7 @@ head<=
 <?_code
 {
     use strict;
+    use Storable;
 
     my $ret = $_[1]->{'head'};
 
@@ -548,14 +549,24 @@ head<=
                     ));
 
     # draft autosave and restore
+    # The $draft variable contains the draft itself. The hash contains the 
various
+    # fields of the user's draft properties (subject, tags, music, etc.)
     my $remote = LJ::get_remote();
     my $draft = '""';
+    my %draft_properties;
+
     if ($remote) {
+        # Here we get the value of the userprop 'draft_properties', containing
+        # a frozen Storable string, which we then thaw into a hash by the same
+        # name.
         $draft = LJ::ejs_string($remote->prop('entry_draft'));
+        %draft_properties = $remote->prop( 'draft_properties' ) ? 
+            %{Storable::thaw( $remote->prop( 'draft_properties' ) )} : ' ';
     }
 
     my $eMLautosave = LJ::ejs(BML::ml('.draft.autosave', { 'time' => 
'[[time]]' }));
-    my $eMLconfirm = LJ::ejs($ML{'.draft.confirm'});
+    my $eMLconfirm = LJ::ejs( BML::ml( '.draft.confirm2', 
+                { 'subjectline' => LJ::ejs_string( $draft_properties{subject} 
) } ) );
     my $eMLrestored = LJ::ejs($ML{'.draft.restored'});
 
     # Setup draft saving and try to restore from a draft
@@ -582,7 +593,20 @@ head<=
     $ret .= qq^
     <script type="text/javascript">
     //<![CDATA[
-        var restoredDraft = $draft;
+        
+        // These JS variables contain the contents of the various draft fields.
+        var restoredDraft       = $draft;
+        var restoredSubject     = '$draft_properties{subject}';
+        var restoredUserpic     = '$draft_properties{userpic}';
+        var restoredTaglist     = '$draft_properties{taglist}';
+        var restoredMoodID      = '$draft_properties{moodid}';
+        var restoredMood        = '$draft_properties{mood}';
+        var restoredLocation    = '$draft_properties{location1}';
+        var restoredMusic       = '$draft_properties{music}';
+        var restoredAdultReason = '$draft_properties{adultreason}';
+        var restoredCommentSet  = '$draft_properties{commentset}';
+        var restoredCommentScr  = '$draft_properties{commentscr}';
+        var restoredAdultCnt    = '$draft_properties{adultcnt}';
 
         function init_update_bml() {
             $initDraft
@@ -594,18 +618,34 @@ head<=
         LJDraft.autoSaveInterval = $LJ::AUTOSAVE_DRAFT_INTERVAL;
         LJDraft.savedMsg = '$eMLautosave';
 
-            function initDraft(askToRestore) {
-                if (askToRestore && restoredDraft) {
-                    if (confirm("$eMLconfirm")) {
-                        \$("draft").value = restoredDraft;
-                        \$("draftstatus").value = '$eMLrestored ';
-                    } else {
-                        // Clear out their current draft
-                        LJDraft.save('');
-                    }
+        function initDraft(askToRestore) {
+            if (askToRestore && restoredDraft) {
+                if (confirm("$eMLconfirm")) {
+                  // If the user wants to restore the draft, we place the
+                  // values of their saved draft into the form.
+                  \$("draft").value = restoredDraft;
+                  \$("draftstatus").value = '$eMLrestored ';
+                  \$("subject").value = restoredSubject;
+                  if ( \$("prop_picture_keyword") ) {
+                      \$("prop_picture_keyword").selectedIndex = 
restoredUserpic;
+                  }
+                  \$("prop_taglist").value                 = restoredTaglist;
+                  \$("prop_current_moodid").selectedIndex  = restoredMoodID;
+                  \$("prop_current_mood").value            = restoredMood;
+                  \$("prop_current_location").value        = restoredLocation;
+                  \$("prop_current_music").value           = restoredMusic;
+                  \$("prop_adult_content_reason").value    = 
restoredAdultReason;
+                  \$("comment_settings").selectedIndex     = 
restoredCommentSet;
+                  \$("prop_opt_screening").selectedIndex   = 
restoredCommentScr;
+                  \$("prop_adult_content").selectedIndex   = restoredAdultCnt;
+                } else {
+                    // Clear out their current draft
+                    LJDraft.save('');
+                    LJDraft.clearProperties();
                 }
-                LJDraft.startTimer();
            }
+            LJDraft.startTimer();
+       }
     //]]>
     </script>^;
 
diff -r 09ce6d0bca17 -r 496bd3ba7db2 htdocs/update.bml.text
--- a/htdocs/update.bml.text    Wed Feb 17 04:57:01 2010 +0000
+++ b/htdocs/update.bml.text    Sun Feb 21 16:37:03 2010 +0000
@@ -18,6 +18,8 @@
 .draft.autosave=Autosaved draft at [[time]]
 
 .draft.confirm=Restore from saved draft?
+
+.draft.confirm2=Restore from saved draft entitled [[subjectline]]?
 
 .draft.restored=Restored last saved draft
 
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog

Reply via email to