WordPress.org

Make WordPress Core

Changeset 6546


Ignore:
Timestamp:
01/04/2008 08:46:33 AM (10 years ago)
Author:
ryan
Message:

Port autosave to jquery. Props rmccue. see #3824

Location:
trunk
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/page-new.php

    r6513 r6546  
    44$parent_file = 'post-new.php';
    55$editing = true;
    6 wp_enqueue_script('prototype');
    7 wp_enqueue_script('jquery');
    86wp_enqueue_script('autosave');
    97require_once('admin-header.php');
  • trunk/wp-admin/page.php

    r6513 r6546  
    5353    }
    5454
    55     if($post->post_status == 'draft') {
    56         wp_enqueue_script('prototype');
    57         wp_enqueue_script('jquery');
     55    if ( 'draft' == $post->post_status )
    5856        wp_enqueue_script('autosave');
    59     }
     57
    6058    require_once('admin-header.php');
    6159
  • trunk/wp-admin/post-new.php

    r6542 r6546  
    44$parent_file = 'post-new.php';
    55$editing = true;
    6 wp_enqueue_script('prototype');
    7 wp_enqueue_script('jquery');
    86wp_enqueue_script('autosave');
    97wp_enqueue_script('post');
  • trunk/wp-admin/post.php

    r6542 r6546  
    6161    wp_enqueue_script('post');
    6262
    63     if( 'draft' == $post->post_status ) {
    64         wp_enqueue_script('prototype');
     63    if ( 'draft' == $post->post_status )
    6564        wp_enqueue_script('autosave');
    66     }
    6765
    6866    require_once('admin-header.php');
  • trunk/wp-includes/functions.php

    r6542 r6546  
    969969function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true ) {
    970970    $name = attribute_escape( $name );
    971     echo '<input type="hidden" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
     971    echo '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
    972972    if ( $referer )
    973973        wp_referer_field();
  • trunk/wp-includes/js/autosave.js

    r6373 r6546  
    33
    44function autosave_start_timer() {
    5     var form = $('post');
    6     autosaveLast = form.post_title.value+form.content.value;
     5    autosaveLast = jQuery('#post #title').val()+jQuery('#post #content').val();
    76    // Keep autosave_interval in sync with edit_post().
    8     autosavePeriodical = new PeriodicalExecuter(autosave, autosaveL10n.autosaveInterval);
     7    autosavePeriodical = jQuery.schedule({time: autosaveL10n.autosaveInterval * 1000, func: autosave, repeat: true, protect: true});
     8
    99    //Disable autosave after the form has been submitted
    10     if(form.addEventListener) {
    11         form.addEventListener("submit", function () { autosavePeriodical.currentlyExecuting = true; }, false);
    12     }
    13     if(form.attachEvent) {
    14         form.save ? form.save.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null;
    15         form.submit ? form.submit.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null;
    16         form.publish ? form.publish.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null;
    17         form.deletepost ? form.deletepost.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null;
    18     }
     10    jQuery("#post #submit").submit(function() { jQuery.cancel(autosavePeriodical); });
     11    jQuery("#post #save").click(function() { jQuery.cancel(autosavePeriodical); });
     12    jQuery("#post #submit").click(function() { jQuery.cancel(autosavePeriodical); });
     13    jQuery("#post #publish").click(function() { jQuery.cancel(autosavePeriodical); });
     14    jQuery("#post #deletepost").click(function() { jQuery.cancel(autosavePeriodical); });
    1915}
    2016addLoadEvent(autosave_start_timer)
     
    2723}
    2824
    29 function autosave_update_nonce() {
    30     var response = nonceAjax.response;
    31     document.getElementsByName('_wpnonce')[0].value = response;
    32 }
    33 
    34 function autosave_update_post_ID() {
    35     var response = autosaveAjax.response;
     25function autosave_update_post_ID(response) {
    3626    var res = parseInt(response);
    3727    var message;
     
    4131    } else {
    4232        message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time());
    43         $('post_ID').name = "post_ID";
    44         $('post_ID').value = res;
     33        jQuery('#post_ID').attr({name: "post_ID"});
     34        jQuery('#post_ID').val(res);
    4535        // We need new nonces
    46         nonceAjax = new sack();
    47         nonceAjax.element = null;
    48         nonceAjax.setVar("action", "autosave-generate-nonces");
    49         nonceAjax.setVar("post_ID", res);
    50         nonceAjax.setVar("cookie", document.cookie);
    51         nonceAjax.setVar("post_type", $('post_type').value);
    52         nonceAjax.requestFile = autosaveL10n.requestFile;
    53         nonceAjax.onCompletion = autosave_update_nonce;
    54         nonceAjax.method = "POST";
    55         nonceAjax.runAJAX();
    56         $('hiddenaction').value = 'editpost';
     36        jQuery.post(autosaveL10n.requestFile, {
     37            action: "autosave-generate-nonces",
     38            post_ID: res,
     39            cookie: document.cookie,
     40            post_type: jQuery('#post_type').val()
     41        }, function(html) {
     42            jQuery('#_wpnonce').val(html);
     43        });
     44        jQuery('#hiddenaction').val('editpost');
    5745    }
    58     $('autosave').innerHTML = message;
     46    jQuery('#autosave').html(message);
    5947    autosave_enable_buttons();
    6048}
    6149
    6250function autosave_loading() {
    63     $('autosave').innerHTML = autosaveL10n.savingText;
     51    jQuery('#autosave').html(autosaveL10n.savingText);
    6452}
    6553
    66 function autosave_saved() {
    67     var response = autosaveAjax.response;
     54function autosave_saved(response) {
    6855    var res = parseInt(response);
    6956    var message;
     
    7461        message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time());
    7562    }
    76     $('autosave').innerHTML = message;
     63    jQuery('#autosave').html(message);
    7764    autosave_enable_buttons();
    7865}
    7966
    8067function autosave_disable_buttons() {
    81     var form = $('post');
    82     form.save ? form.save.disabled = 'disabled' : null;
    83     form.submit ? form.submit.disabled = 'disabled' : null;
    84     form.publish ? form.publish.disabled = 'disabled' : null;
    85     form.deletepost ? form.deletepost.disabled = 'disabled' : null;
     68    jQuery("#post #save:enabled").attr('disabled', 'disabled');
     69    jQuery("#post #submit:enabled").attr('disabled', 'disabled');
     70    jQuery("#post #publish:enabled").attr('disabled', 'disabled');
     71    jQuery("#post #deletepost:enabled").attr('disabled', 'disabled');
    8672    setTimeout('autosave_enable_buttons();', 1000); // Re-enable 1 sec later.  Just gives autosave a head start to avoid collisions.
    8773}
    8874
    8975function autosave_enable_buttons() {
    90     var form = $('post');
    91     form.save ? form.save.disabled = '' : null;
    92     form.submit ? form.submit.disabled = '' : null;
    93     form.publish ? form.publish.disabled = '' : null;
    94     form.deletepost ? form.deletepost.disabled = '' : null;
     76    jQuery("#post #save:disabled").attr('disabled', '');
     77    jQuery("#post #submit:disabled").attr('disabled', '');
     78    jQuery("#post #publish:disabled").attr('disabled', '');
     79    jQuery("#post #deletepost:disabled").attr('disabled', '');
    9580}
    9681
    9782function autosave() {
    98     var form = $('post');
    9983    var rich = ((typeof tinyMCE != "undefined") && tinyMCE.getInstanceById('content')) ? true : false;
    100 
    101     autosaveAjax = new sack();
     84    var post_data = {
     85            action: "autosave",
     86            post_ID:  jQuery("#post_ID").val() || 0,
     87            post_title: jQuery("#title").val() || "",
     88            cookie: document.cookie,
     89            tags_input: jQuery("#tags-input").val() || "",
     90            post_type: jQuery('#post_type').val() || ""
     91        };
    10292
    10393    /* Gotta do this up here so we can check the length when tinyMCE is in use */
    10494    if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 || rich == false ) {
    105         autosaveAjax.setVar("content", form.content.value);
     95        post_data["content"] = jQuery("#content").val();
    10696    } else {
    10797        // Don't run while the TinyMCE spellcheck is on.
    10898        if(tinyMCE.selectedInstance.spellcheckerOn) return;
    10999        tinyMCE.wpTriggerSave();
    110         autosaveAjax.setVar("content", form.content.value);
     100        post_data["content"] = jQuery("#content").val();
    111101    }
    112102
    113     if(form.post_title.value.length==0 || form.content.value.length==0 || form.post_title.value+form.content.value == autosaveLast)
     103    if(post_data["post_title"].length==0 || post_data["content"].length==0 || post_data["post_title"] + post_data["content"] == autosaveLast) {
    114104        return;
     105    }
    115106
    116107    autosave_disable_buttons();
    117108
    118     autosaveLast = form.post_title.value+form.content.value;
     109    autosaveLast = jQuery("#title").val()+jQuery("#content").val();
     110    goodcats = ([]);
     111    jQuery("[@name='post_category[]']:checked").each( function(i) {
     112        goodcats.push(this.value);
     113    } );
     114    post_data["catslist"] = goodcats.join(",");
    119115
    120     cats = document.getElementsByName("post_category[]");
    121     goodcats = ([]);
    122     for(i=0;i<cats.length;i++) {
    123         if(cats[i].checked)
    124             goodcats.push(cats[i].value);
    125     }
    126     catslist = goodcats.join(",");
    127 
    128     autosaveAjax.setVar("action", "autosave");
    129     autosaveAjax.setVar("cookie", document.cookie);
    130     autosaveAjax.setVar("catslist", catslist);
    131     autosaveAjax.setVar("post_ID", $("post_ID").value);
    132     autosaveAjax.setVar("post_title", form.post_title.value);
    133     autosaveAjax.setVar("post_type", form.post_type.value);
    134     autosaveAjax.setVar("tags_input", form.tags_input.value);
    135     if ( form.comment_status.checked )
    136         autosaveAjax.setVar("comment_status", 'open');
    137     if ( form.ping_status.checked )
    138         autosaveAjax.setVar("ping_status", 'open');
    139     if(form.excerpt)
    140         autosaveAjax.setVar("excerpt", form.excerpt.value);
     116    if ( jQuery("#comment_status").attr("checked") )
     117        post_data["comment_status"] = 'open';
     118    if ( jQuery("#ping_status").attr("checked") )
     119        post_data["ping_status"] = 'open';
     120    if( jQuery("#excerpt"))
     121        post_data["excerpt"] = jQuery("#excerpt").val();
    141122
    142123    if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 || rich == false ) {
    143         autosaveAjax.setVar("content", form.content.value);
     124        post_data["content"] = jQuery("#content").val();
    144125    } else {
    145126        tinyMCE.wpTriggerSave();
    146         autosaveAjax.setVar("content", form.content.value);
     127        post_data["content"] = jQuery("#content").val();
    147128    }
    148129
    149     autosaveAjax.requestFile = autosaveL10n.requestFile;
    150     autosaveAjax.method = "POST";
    151     autosaveAjax.element = null;
    152     autosaveAjax.onLoading = autosave_loading;
    153     autosaveAjax.onInteractive = autosave_loading;
    154     if(parseInt($("post_ID").value) < 1)
    155         autosaveAjax.onCompletion = autosave_update_post_ID;
    156     else
    157         autosaveAjax.onCompletion = autosave_saved;
    158     autosaveAjax.runAJAX();
     130    if(parseInt(post_data["post_ID"]) < 1) {
     131        post_data["temp_ID"] = post_data["post_ID"];
     132        jQuery.ajaxSetup({
     133            success: function(html) { autosave_update_post_ID(html); }
     134        });
     135    } else {
     136        jQuery.ajaxSetup({
     137            success: function(html) { autosave_saved(html); }
     138        });
     139    }
     140    jQuery.ajax({
     141        data: post_data,
     142        beforeSend: function() { autosave_loading() },
     143        type: "POST",
     144        url: autosaveL10n.requestFile
     145    });
    159146}
  • trunk/wp-includes/post.php

    r6537 r6546  
    10031003        if ( !in_array($post_status, array('draft', 'pending')) )
    10041004            $post_date = current_time('mysql');
     1005        else
     1006            $post_date = '0000-00-00 00:00:00';
    10051007    }
    10061008
     
    10081010        if ( !in_array($post_status, array('draft', 'pending')) )
    10091011            $post_date_gmt = get_gmt_from_date($post_date);
     1012        else
     1013            $post_date_gmt = '0000-00-00 00:00:00';
    10101014    }
    10111015
  • trunk/wp-includes/script-loader.php

    r6544 r6546  
    3737        $this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.6');
    3838
    39         $this->add( 'autosave', '/wp-includes/js/autosave.js', array('prototype', 'sack'), '20070306');
     39        $this->add( 'autosave', '/wp-includes/js/autosave.js', array('jquery', 'schedule'), '20080104');
    4040        $this->localize( 'autosave', 'autosaveL10n', array(
    4141            'autosaveInterval' => apply_filters('autosave_interval', '120'),
     
    8181        $this->add( 'dimensions', '/wp-includes/js/jquery/jquery.dimensions.min.js', array('jquery'), '1.1.2');
    8282        $this->add( 'suggest', '/wp-includes/js/jquery/suggest.js', array('dimensions'), '1.1');
     83        $this->add( 'schedule', '/wp-includes/js/jquery/jquery.schedule.js', array('jquery'), '20');
    8384
    8485        if ( is_admin() ) {
Note: See TracChangeset for help on using the changeset viewer.