locktimer.js 4.3 KB

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