| | 362 | |
| | 363 | autosave_local_storage = { |
| | 364 | |
| | 365 | lastsaveddata:'', |
| | 366 | modified_data : {}, |
| | 367 | |
| | 368 | prepare_data: function() { |
| | 369 | |
| | 370 | var post_data = { |
| | 371 | action: "autosave", |
| | 372 | post_ID: jQuery("#post_ID").val() || 0, |
| | 373 | post_type: jQuery('#post_type').val() || "" |
| | 374 | }; |
| | 375 | |
| | 376 | if ( fullscreen && fullscreen.settings.visible ) { |
| | 377 | post_data["post_title"] = jQuery('#wp-fullscreen-title').val() || ''; |
| | 378 | post_data["content"] = jQuery("#wp_mce_fullscreen").val() || ''; |
| | 379 | } else { |
| | 380 | post_data["post_title"] = jQuery("#title").val() || ''; |
| | 381 | post_data["content"] = jQuery("#content").val() || ''; |
| | 382 | } |
| | 383 | |
| | 384 | if ( jQuery('#post_name').val() ) |
| | 385 | post_data["post_name"] = jQuery('#post_name').val(); |
| | 386 | |
| | 387 | var cats = ([]); |
| | 388 | |
| | 389 | jQuery("[name='post_category[]']:checked").each( function(i) { |
| | 390 | cats.push(this.value); |
| | 391 | } ); |
| | 392 | post_data["catslist"] = cats.join(","); |
| | 393 | |
| | 394 | if ( jQuery("#comment_status").prop("checked") ) |
| | 395 | post_data["comment_status"] = 'open'; |
| | 396 | if ( jQuery("#ping_status").prop("checked") ) |
| | 397 | post_data["ping_status"] = 'open'; |
| | 398 | if ( jQuery("#excerpt").size() ) |
| | 399 | post_data["excerpt"] = jQuery("#excerpt").val(); |
| | 400 | if ( jQuery("#post_author").size() ) |
| | 401 | post_data["post_author"] = jQuery("#post_author").val(); |
| | 402 | if ( jQuery("#parent_id").val() ) |
| | 403 | post_data["parent_id"] = jQuery("#parent_id").val(); |
| | 404 | post_data["user_ID"] = jQuery("#user-id").val(); |
| | 405 | if ( jQuery('#auto_draft').val() == '1' ) |
| | 406 | post_data["auto_draft"] = '1'; |
| | 407 | |
| | 408 | post_data["save_time"] = new Date().getTime(); |
| | 409 | |
| | 410 | post_data['storage_status'] = 'fresh'; |
| | 411 | |
| | 412 | return post_data; |
| | 413 | |
| | 414 | }, |
| | 415 | |
| | 416 | get_post_id: function() { |
| | 417 | var post_id = jQuery("#post_ID").val() || 0; |
| | 418 | |
| | 419 | if( 0 < post_id ) { |
| | 420 | return post_id; |
| | 421 | } |
| | 422 | }, |
| | 423 | |
| | 424 | get_storage: function() { |
| | 425 | |
| | 426 | if ( window.localStorage ) { |
| | 427 | var store = localStorage.getItem( 'wp_autosave' ); |
| | 428 | |
| | 429 | if( store ) { |
| | 430 | return JSON.parse( store ); |
| | 431 | } else { |
| | 432 | return {}; |
| | 433 | } |
| | 434 | } |
| | 435 | }, |
| | 436 | |
| | 437 | handle_post_data: function( data ) { |
| | 438 | |
| | 439 | var storage_arr = this.get_storage(); |
| | 440 | |
| | 441 | if ( storage_arr ) { |
| | 442 | |
| | 443 | var post_id = this.get_post_id(); |
| | 444 | |
| | 445 | if( post_id ) { |
| | 446 | var post_key = autosaveL10n.blog_id+'_'+post_id; |
| | 447 | |
| | 448 | if( data ) { |
| | 449 | |
| | 450 | if( 'del' === data){ |
| | 451 | |
| | 452 | if( typeof storage_arr[post_key] !== 'undefined' ) { |
| | 453 | delete storage_arr[post_key]; |
| | 454 | } |
| | 455 | |
| | 456 | } else{ |
| | 457 | storage_arr[post_key] = data; |
| | 458 | } |
| | 459 | |
| | 460 | storage_arr = JSON.stringify( storage_arr ); |
| | 461 | |
| | 462 | localStorage.setItem( 'wp_autosave', storage_arr ); |
| | 463 | } else if( typeof storage_arr[post_key] !== 'undefined' ) { |
| | 464 | |
| | 465 | return storage_arr[post_key]; |
| | 466 | } |
| | 467 | } |
| | 468 | |
| | 469 | } |
| | 470 | }, |
| | 471 | |
| | 472 | get_data: function() { |
| | 473 | |
| | 474 | return this.handle_post_data(); |
| | 475 | |
| | 476 | }, |
| | 477 | |
| | 478 | set_data: function() { |
| | 479 | |
| | 480 | var post_data = this.prepare_data(); |
| | 481 | |
| | 482 | var storage_data = this.handle_post_data(); |
| | 483 | |
| | 484 | //if the content and title did not change from last server save,dont keep data in local storage |
| | 485 | if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) |
| | 486 | || post_data["post_title"] + post_data["content"] == autosaveLast |
| | 487 | || post_data["post_title"] + post_data["content"] == this.lastsaveddata |
| | 488 | ) { |
| | 489 | if( storage_data && 5 < storage_data.length ) { |
| | 490 | storage_data.pop(); |
| | 491 | this.handle_post_data( storage_data ) |
| | 492 | } |
| | 493 | |
| | 494 | } else { |
| | 495 | |
| | 496 | if( storage_data ) { |
| | 497 | |
| | 498 | if( 5 < storage_data.unshift( post_data ) ) { |
| | 499 | storage_data.pop(); |
| | 500 | } |
| | 501 | |
| | 502 | } else { |
| | 503 | var storage_data = new Array( post_data ); |
| | 504 | } |
| | 505 | |
| | 506 | this.lastsaveddata = post_data["post_title"] + post_data["content"]; |
| | 507 | |
| | 508 | this.handle_post_data( storage_data ); |
| | 509 | |
| | 510 | } |
| | 511 | }, |
| | 512 | |
| | 513 | delete_data: function() { |
| | 514 | |
| | 515 | this.handle_post_data( 'del' ); |
| | 516 | |
| | 517 | }, |
| | 518 | |
| | 519 | stale_data: function() { |
| | 520 | var storage_data = this.handle_post_data(); |
| | 521 | |
| | 522 | if( storage_data ) { |
| | 523 | for( var i=0; i < storage_data.length; i++ ) { |
| | 524 | storage_data[i]['storage_status'] = 'stale'; |
| | 525 | } |
| | 526 | this.handle_post_data( storage_data ); |
| | 527 | } |
| | 528 | }, |
| | 529 | |
| | 530 | remove_stale_data: function() { |
| | 531 | |
| | 532 | var stored_data = this.get_storage(); |
| | 533 | |
| | 534 | var new_stored_data = {}; |
| | 535 | |
| | 536 | if( stored_data ) { |
| | 537 | |
| | 538 | jQuery.each( stored_data, function( key, value ) { |
| | 539 | |
| | 540 | var current_time = new Date().getTime(); |
| | 541 | |
| | 542 | var new_val_arr = new Array(); |
| | 543 | |
| | 544 | for( var i=0; i < value.length; i++ ) { |
| | 545 | |
| | 546 | var post_store = value[i]; |
| | 547 | |
| | 548 | var time_diff = current_time - post_store['save_time'] |
| | 549 | |
| | 550 | //check for 12 hr difference && stale data |
| | 551 | if( 'stale' != post_store['storage_status'] && 43200000 > time_diff ) { |
| | 552 | new_val_arr.unshift( post_store ); |
| | 553 | } |
| | 554 | |
| | 555 | } |
| | 556 | |
| | 557 | if( new_val_arr && 0 < new_val_arr.length ) { |
| | 558 | autosave_local_storage.modified_data[key] = new_val_arr; |
| | 559 | } |
| | 560 | }); |
| | 561 | |
| | 562 | storage_arr = JSON.stringify( autosave_local_storage.modified_data ); |
| | 563 | autosave_local_storage.modified_data = ''; |
| | 564 | |
| | 565 | localStorage.setItem( 'wp_autosave', storage_arr ); |
| | 566 | |
| | 567 | } |
| | 568 | } |
| | 569 | } |