[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/js/ -> autosave.js (source)

   1  var autosave = {
   2  
   3      bool         : {},
   4      oldmessage  : '',
   5      periodical    : null,
   6  
   7      tinyMCE_triggerSave : function() {
   8          if ( (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden() )     
   9          {
  10              if ( 'mce_fullscreen' == tinyMCE.activeEditor.id ) 
  11                  tinyMCE.get('content').setContent(tinyMCE.activeEditor.getContent({format : 'raw'}), {format : 'raw'});
  12              tinyMCE.triggerSave();
  13              return true;
  14          }
  15          return false;
  16      },
  17  
  18      tinyMCE_spellcheckerOn : function() {
  19          return ( autosave.tinyMCE_triggerSave() && tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active );
  20      },
  21  
  22      init : function() {
  23          if (jQuery('#autosavenonce').val())
  24          {
  25              autosave.tinyMCE_triggerSave();
  26  
  27              autosave.periodical     = jQuery.schedule({    time: autosaveL10n.autosaveInterval * 1000, 
  28                                              func: function() { autosave.show_preview = false; autosave.main(); }, 
  29                                              repeat: true, 
  30                                              protect: true
  31              });
  32  
  33              jQuery("#mail_newform").submit(function() { jQuery.cancel(autosave.periodical); });    // Disable autosave after the form has been submitted
  34  
  35              autosave.init_html2txt();
  36              autosave.click_html2txt();
  37              autosave.click_preview();                                        // Autosave when the preview button is clicked.
  38              autosave.click_attachments_loader();                                // Autosave when the change loader link is clicked.
  39  
  40              autosave.olddata = autosave.retrieve_data();
  41              autosave.newdata = autosave.olddata;
  42          }
  43          else
  44          {
  45              tb_init('span#preview-button a.preview');
  46          }
  47      },
  48  
  49  // buttons
  50      enable_buttons : function() {
  51          jQuery(':button, :submit', '#submitpost').removeAttr('disabled');
  52      },
  53  
  54      disable_buttons : function() {
  55          jQuery(':button, :submit', '#submitpost').prop('disabled', true);
  56          setTimeout(autosave.enable_buttons, 5000); // Re-enable 5 sec later.  Just gives autosave a head start to avoid collisions.
  57      },
  58  
  59  // data
  60      olddata     : {},
  61      newdata     : {},
  62      data         : {     toemail     :     'toemail',
  63                  toname    :    'toname',
  64                  fromemail    :    'fromemail',
  65                  fromname    :    'fromname',
  66                  to_list    :    'to_list',
  67                  Theme        :    'Theme',
  68                  subject    :    'title',
  69                  html        :    'content',
  70                  plaintext    :    'plaintext',
  71                  mail_format:    'mail_format'
  72                },
  73  
  74      retrieve_data : function() {
  75          var x = new Array();
  76          for (key in autosave.data) { x[key] = jQuery("#"+autosave.data[key]).val() || ""; }
  77          return x;
  78      },
  79  
  80      compare_data : function (a,b) {
  81          for (key in a) if (a[key] != b[key]) return false;
  82          return true;
  83      },
  84  
  85  // preview
  86      show_preview : false,
  87  
  88      click_preview : function() {
  89          jQuery('#preview-button a').click(function(e) {
  90              autosave.show_preview = true;
  91              autosave.main();
  92              return false;
  93          });
  94      },
  95  
  96      callback_preview : function (previewlink) {
  97          var previewText = autosaveL10n.previewMailText;
  98          jQuery('#preview-button').html('<a class="button preview" target="_blank" href="'+previewlink+'" tabindex="4">'+previewText+'</a>');
  99          if (autosave.show_preview)
 100          {
 101              autosave.display_preview();
 102              autosave.show_preview = false;
 103          }
 104          autosave.click_preview();                                            // Autosave when the preview button is clicked. 
 105      },
 106  
 107      display_preview : function () {
 108          var href = jQuery('#preview-button a').attr("href");
 109  
 110          var selectedtheme = jQuery('#theme').val();
 111  
 112          if (selectedtheme != '')
 113          {
 114              var TB_iframe = href.indexOf('&TB_iframe');
 115              hrefnew = href.substring(0,TB_iframe)+'&theme='+selectedtheme+href.substring(TB_iframe);
 116              href = hrefnew;
 117          }
 118  
 119          var thickboxW = jQuery('#TB_window');
 120          var H = jQuery(window).height();
 121          var W = jQuery(window).width();
 122  
 123          var nW = parseInt(W *.8);
 124          var nH = parseInt(H *.9);
 125  
 126          if ( thickboxW.size() ) 
 127          {
 128              thickboxW.width( nW ).height( nH );
 129              jQuery('#TB_iframeContent').width('100%').height('100%');
 130              thickboxW.css({'margin-left': '-' + parseInt( nW / 2 ) + 'px'});
 131              if ( typeof document.body.style.maxWidth != 'undefined' )
 132                  thickboxW.css({'top':'30px','margin-top':'0'});
 133          };
 134  
 135          href = href.replace(/&width=[0-9]+/g, '');
 136          href = href.replace(/&height=[0-9]+/g, '');
 137          hrefnew = href+'&width=' + ( nW ) + '&height=' + ( nH );
 138          href = hrefnew;
 139  
 140          tb_show(null,href,null);
 141      },
 142  
 143  // attachments
 144      click_attachments_loader : function() {
 145          jQuery('#mp_loader_link').click(function(e) {
 146              autosave.clean_attachments();
 147              autosave.main();
 148              return true;
 149          });
 150      },
 151  
 152      enable_attachments : function(item_id) {
 153          draft_id = item_id;
 154  
 155          var href = jQuery('#mp_loader_link').attr('href');
 156          var oldhref = href;
 157          href.replace(/\&id=0/, '&id=' + draft_id);
 158          if (href == oldhref) href += '&id=' + draft_id;
 159          jQuery('#mp_loader_link').attr({href : href});
 160  
 161          mp_fileupload.add();
 162          jQuery('#attachmentsdiv').show();
 163      },
 164  
 165      clean_attachments : function() {
 166          jQuery('.mp_fileupload_cb').not(':checked').each(function(){
 167              var meta_id = jQuery(this).val();
 168              var cb_data = {    action:         "mp_ajax",
 169                      mp_action:         "delete_attachment",
 170                      meta_id:          meta_id
 171              };
 172  // ajax
 173              jQuery.ajax({
 174                  data: cb_data,
 175                  type: "POST",
 176                  url: ajaxurl,
 177                  success: function() {jQuery('#attachment-item-'+ meta_id).remove();jQuery('#attachment-item-u-'+ meta_id).remove();}
 178              });
 179          });
 180      },
 181  
 182  // html2txt
 183      init_html2txt : function() {
 184          var dest ; 
 185          var html2txt = jQuery('div#div_html2txt');
 186  
 187          dest = jQuery('div#wp-content-media-buttons');
 188          if (!dest.length) 
 189          {
 190              jQuery('div#wp-content-editor-tools').append("<div class='hide-if-no-js wp-media-buttons' id='wp-content-media-buttons'></div>");
 191              dest = jQuery('div#wp-content-media-buttons');
 192          }
 193          dest.append(html2txt.html());
 194          html2txt.remove();
 195      },
 196  
 197      click_html2txt : function() {
 198          jQuery('a#html2txt').click(function() {
 199  
 200              autosave.tinyMCE_triggerSave();
 201  
 202              if (jQuery("#content").val()   == '') return;
 203              if (jQuery("#plaintext").val() != '' && !confirm(MP_AdminPageL10n.html2txt) ) return;
 204  
 205              var html2txt_data = {    action:         "mp_ajax",
 206                          mp_action: "html2txt",
 207                          html  : jQuery("#content").val()
 208              };
 209  // ajax
 210              jQuery.ajax({
 211                  beforeSend: function() { jQuery('#html2txt_loading').toggle(); },
 212                  data: html2txt_data,
 213                  type: 'POST',
 214                  url: ajaxurl,
 215                  success: function(response) { jQuery('#plaintext').val(response); jQuery('#html2txt_loading').toggle(); }
 216              });
 217              return false;
 218          });
 219      },
 220  
 221  
 222  // ajax
 223      loading_ajax : function() {
 224          jQuery('#autosave').html(autosaveL10n.savingText);
 225      },
 226  
 227      callback_ajax : function(response) {
 228          var message = '';
 229          var res = wpAjax.parseAjaxResponse(response, 'autosave');                         // parse the ajax response
 230          if ( res && res.responses && res.responses.length ) 
 231          {
 232              message = res.responses[0].data;
 233              if ( res.responses[0].supplemental ) 
 234              {
 235                  jQuery.each(res.responses[0].supplemental, function(selector, value) {
 236                      if ( selector.match(/^replace-/) ) 
 237                      {
 238                          jQuery('#'+selector.replace('replace-', '')).val(value);
 239                      }
 240                  });
 241  
 242                  if ( 'disable' == res.responses[0].supplemental['disable_autosave'] ) 
 243                  {
 244                      autosave = function() {};
 245                  }
 246                  if ( '' != res.responses[0].supplemental['tipe'] ) 
 247                  {
 248                      var type     = res.responses[0].supplemental['tipe'] ;
 249                      var item_id = parseInt( res.responses[0].id );
 250                      var item_main_id = parseInt( res.responses[0].oldId );
 251                      if (item_id > 0)
 252                      {
 253                          autosave.update_id(item_id, type); 
 254                                                                      // ajax to get preview link
 255                          jQuery.post( ajaxurl, 
 256                                  {action: "mp_ajax", mp_action : "get_previewlink", id: item_id, main_id: item_main_id, getpreviewlinknonce: jQuery('#getpreviewlinknonce').val(), height : parseInt(jQuery(window).height() * .8), width : parseInt(jQuery(window).width()  * .9) }, 
 257                                  autosave.callback_preview);
 258                      }
 259                      else
 260                      {
 261                          if (autosave.show_preview)
 262                          {
 263                              autosave.display_preview();
 264                              autosave.show_preview = false;
 265                          }
 266                      }
 267                  }
 268                  else
 269                  {
 270                      if (autosave.show_preview)
 271                      {
 272                          autosave.display_preview();
 273                          autosave.show_preview = false;
 274                      }
 275                  }
 276                  jQuery.each(    res.responses[0].supplemental, 
 277                              function(selector, value) 
 278                              {
 279                                  if ( (selector != 'tipe') && (selector.match(/^replace-/)) ) 
 280                                  {
 281                                      jQuery('#'+selector.replace('replace-', '')).val(value);
 282                                  }
 283                              }
 284                          );
 285              }
 286              if ( message ) { jQuery('#autosave').html(message); }                     // update autosave message
 287              else if ( autosave.oldmessage && res ) { jQuery('#autosave').html( autosave.oldmessage ); }
 288          }
 289          autosave.clean_attachments();
 290          if (autosave.show_preview)
 291          {
 292              autosave.display_preview();
 293              autosave.show_preview = false;
 294          }
 295          autosave.enable_buttons(); 
 296      },
 297  
 298      update_id : function(item_id, type) {
 299          if ( isNaN(item_id))                         return;
 300          if ( item_id <= 0 )                        return;
 301          var attr = (type == 'mail') ? 'id' : type;
 302          if ( item_id == parseInt(jQuery('#mail_'+attr).val()) )     return;                // no need to do this more than once
 303  
 304          jQuery('#mail_'+attr).val(item_id);
 305  
 306          if (type != 'mail') return;
 307  
 308          autosave.enable_attachments(item_id);
 309      },
 310  
 311  
 312      main : function() {
 313  
 314          autosave.bool['spell'] = autosave.tinyMCE_spellcheckerOn();
 315  
 316          var mail_data =     {    action:         "mp_ajax",
 317                          mp_action:         "autosave",
 318                          autosavenonce:         jQuery('#autosavenonce').val(),
 319                          id:              jQuery("#mail_id").val() || 0,
 320                          revision:          jQuery("#mail_revision").val() || -1
 321                      };
 322          var x = 0;
 323          for (key in autosave.data)
 324          {
 325              mail_data[key] = jQuery("#"+autosave.data[key]).val() || "";
 326              x = x + mail_data[key].length;
 327          }
 328  
 329          autosave.newdata = autosave.retrieve_data();
 330  
 331          autosave.bool['empty'] = (x == 0);
 332          autosave.bool['nochg'] = autosave.compare_data(autosave.olddata,autosave.newdata);
 333          autosave.bool['thick'] = ( jQuery("#TB_window").css('display') == 'block' );
 334                                                                      // We always send the ajax request in order to keep the post lock fresh.
 335          mail_data['autosave'] = 1;                                            // This (bool) tells whether or not to write the mail to the DB during the ajax request.
 336          for (key in autosave.bool) if (autosave.bool[key]) mail_data['autosave'] = 0;
 337  
 338          autosave.oldmessage     = jQuery('#autosave').html();
 339          autosave.olddata         = autosave.newdata;
 340          autosave.disable_buttons();
 341  // ajax
 342          jQuery.ajax({
 343              data: mail_data,
 344              beforeSend: (mail_data['autosave'] == 1) ? autosave.loading_ajax : null,
 345              type: "POST",
 346              url: ajaxurl,
 347              success: autosave.callback_ajax
 348          });
 349      }
 350  };


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1