[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/scripts/ -> locktimer.js (source)

   1  /**
   2   * Class managing the timer to display a warning on a expiring lock
   3   */
   4  var dw_locktimer = {
   5      timeout: 0,
   6      draft: false,
   7      timerID: null,
   8      lasttime: null,
   9      msg: LANG.willexpire,
  10      pageid: '',
  11      fieldsToSaveAsDraft: [
  12          'input[name=prefix]',
  13          'textarea[name=wikitext]',
  14          'input[name=suffix]',
  15          'input[name=date]',
  16      ],
  17      callbacks: [],
  18  
  19      /**
  20       * Initialize the lock timer
  21       *
  22       * @param {int}    timeout Length of timeout in seconds
  23       * @param {bool}   draft   Whether to save drafts
  24       * @param {string} edid    Optional; ID of an edit object which has to be present
  25       */
  26      init: function(timeout,draft,edid){
  27          var $edit;
  28  
  29          edid = edid || 'wiki__text';
  30  
  31          $edit = jQuery('#' + edid);
  32          if($edit.length === 0 || $edit.attr('readonly')) {
  33              return;
  34          }
  35  
  36          // init values
  37          dw_locktimer.timeout  = timeout*1000;
  38          dw_locktimer.draft    = draft;
  39          dw_locktimer.lasttime = new Date();
  40  
  41          dw_locktimer.pageid   = jQuery('#dw__editform').find('input[name=id]').val();
  42          if(!dw_locktimer.pageid) {
  43              return;
  44          }
  45  
  46          // register refresh event
  47          $edit.keypress(dw_locktimer.refresh);
  48          // start timer
  49          dw_locktimer.reset();
  50      },
  51  
  52      /**
  53       * Add another field of the editform to be posted to the server when a draft is saved
  54       */
  55      addField: function(selector) {
  56          dw_locktimer.fieldsToSaveAsDraft.push(selector);
  57      },
  58  
  59      /**
  60       * Add a callback that is executed when the post request to renew the lock and save the draft returns successfully
  61       *
  62       * If the user types into the edit-area, then dw_locktimer will regularly send a post request to the DokuWiki server
  63       * to extend the page's lock and update the draft. When this request returns successfully, then the draft__status
  64       * is updated. This method can be used to add further callbacks to be executed at that moment.
  65       *
  66       * @param {function} callback the only param is the data returned by the server
  67       */
  68      addRefreshCallback: function(callback) {
  69          dw_locktimer.callbacks.push(callback);
  70      },
  71  
  72      /**
  73       * (Re)start the warning timer
  74       */
  75      reset: function(){
  76          dw_locktimer.clear();
  77          dw_locktimer.timerID = window.setTimeout(dw_locktimer.warning, dw_locktimer.timeout);
  78      },
  79  
  80      /**
  81       * Display the warning about the expiring lock
  82       */
  83      warning: function(){
  84          dw_locktimer.clear();
  85          alert(fixtxt(dw_locktimer.msg));
  86      },
  87  
  88      /**
  89       * Remove the current warning timer
  90       */
  91      clear: function(){
  92          if(dw_locktimer.timerID !== null){
  93              window.clearTimeout(dw_locktimer.timerID);
  94              dw_locktimer.timerID = null;
  95          }
  96      },
  97  
  98      /**
  99       * Refresh the lock via AJAX
 100       *
 101       * Called on keypresses in the edit area
 102       */
 103      refresh: function(){
 104          var now = new Date(),
 105              params = 'call=lock&id=' + dw_locktimer.pageid + '&';
 106  
 107          // refresh every half minute only
 108          if(now.getTime() - dw_locktimer.lasttime.getTime() <= 30*1000) {
 109              return;
 110          }
 111  
 112          // POST everything necessary for draft saving
 113          if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){
 114              params += jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize();
 115          }
 116  
 117          jQuery.post(
 118              DOKU_BASE + 'lib/exe/ajax.php',
 119              params,
 120              null,
 121              'json'
 122          ).done(function dwLocktimerRefreshDoneHandler(data) {
 123              dw_locktimer.callbacks.forEach(
 124                  function (callback) {
 125                      callback(data);
 126                  }
 127              );
 128          });
 129          dw_locktimer.lasttime = now;
 130      },
 131  
 132      /**
 133       * Callback. Resets the warning timer
 134       */
 135      refreshed: function(data){
 136          if (data.errors.length) {
 137              data.errors.forEach(function(error) {
 138                  jQuery('#draft__status').after(
 139                      jQuery('<div class="error"></div>').text(error)
 140                  );
 141              })
 142          }
 143  
 144          jQuery('#draft__status').html(data.draft);
 145          if(data.lock !== '1') {
 146              return; // locking failed
 147          }
 148          dw_locktimer.reset();
 149      }
 150  };
 151  dw_locktimer.callbacks.push(dw_locktimer.refreshed);