ls.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function SaveLocal(){
  2. if( !ifvisible.now() ) return false;
  3. var date = new Date();
  4. var options = {
  5. year: 'numeric',
  6. month: 'numeric',
  7. day: 'numeric',
  8. hour: 'numeric',
  9. minute: 'numeric',
  10. };
  11. var obj = {
  12. date: new Date(),
  13. header: $('#news-title').val()+' ('+date.toLocaleString("ru", options)+')',
  14. };
  15. var arr = [ "News[title]", "News[lid]", "News[meta_title]", "News[photo_title]", "News[text]", "News[dt_pub]" ];
  16. console.log( "save" );
  17. // console.log( $('#news-form') );
  18. $.each( $( "form" ).serializeArray() , function( i, field ) {
  19. if( $.inArray( field.name, arr ) >-1 ){
  20. value = field.value;
  21. if( field.name == 'News[text]'){
  22. // value = LZW.compress( CKEDITOR.instances.editor1.getData() );
  23. //console.log(CKEDITOR.instances);
  24. //x = 'js-news-content';
  25. value = CKEDITOR.instances.js_news_content.getData();
  26. }
  27. // console.log( value );
  28. key = field.name;
  29. obj[key]=value;
  30. }
  31. });
  32. if( localStorage.getItem('articles') ){
  33. a = JSON.parse( localStorage.articles );
  34. a.push( obj );
  35. }else{
  36. a = [ obj ];
  37. }
  38. // óäàëßåì ñòàðûå çàïèñè
  39. if( a.length > 15 ){
  40. a.shift();
  41. }
  42. console.log(a);
  43. localStorage.setItem("articles", JSON.stringify( a ) );
  44. return false;
  45. }
  46. function LoadLocal(){
  47. if( isset( localStorage.articles ) ){
  48. obj = JSON.parse( localStorage.articles );
  49. var sel=0;
  50. var opt='';
  51. $.each( obj , function( i, field ) {
  52. opt += '<option value="'+i+'">'+field.header+'</option>';
  53. });
  54. $( '<div id="dialog" title="save copy"><select id="his" name="his" class="input-xxlarge">'+opt+'</select></div>' ).dialog({
  55. modal: true,
  56. resizable: true,
  57. height: "auto",
  58. width: "60%",
  59. buttons: {
  60. Ok: function() {
  61. //sel = $('#his option:selected').attr('value');
  62. sel = $('#his option:selected').val();
  63. $( this ).dialog( "close" ).detach();
  64. // console.log(sel);
  65. tmp = obj[sel];
  66. var arr = [ "News[title]", "News[lid]", "News[meta_title]", "News[photo_title]", "News[text]", "News[dt_pub]" ];
  67. $.each( tmp , function( i, field ) {
  68. if( $.inArray( i, arr ) >-1 ){
  69. value = field;
  70. if( i == 'News[text]'){
  71. // value = LZW.decompress( value );
  72. key = "[name='"+i+"']";
  73. $(key).val(value);
  74. console.log(value);
  75. CKEDITOR.instances['js_news_content'].setData(value);
  76. }else{
  77. key = "[name='"+i+"']";
  78. // console.log(key);
  79. // console.log(value);
  80. $(key).val(value);
  81. }
  82. }
  83. });
  84. }
  85. }
  86. });
  87. }
  88. /// localStorage.clear();
  89. return false;
  90. }
  91. window.isset = function(v_var) {
  92. if(typeof(v_var) == 'number'){ if(isNaN(v_var)){return false;}}
  93. if(typeof(v_var) == 'undefined' || v_var === null){return false;}
  94. return true;
  95. };
  96. $(function() {
  97. setInterval(SaveLocal, 1000*30);
  98. ifvisible.setIdleDuration(30);
  99. ifvisible.on("blur", function() { SaveLocal(); });
  100. })