ajaxwatch.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // dependencies:
  2. // * ajax.js:
  3. /*extern sajax_init_object, sajax_do_call */
  4. // * wikibits.js:
  5. /*extern changeText, akeytt, hookEvent, jsMsg */
  6. // These should have been initialized in the generated js
  7. /*extern wgAjaxWatch, wgPageName */
  8. if(typeof wgAjaxWatch === "undefined" || !wgAjaxWatch) {
  9. var wgAjaxWatch = {
  10. watchMsg: "Watch",
  11. unwatchMsg: "Unwatch",
  12. watchingMsg: "Watching...",
  13. unwatchingMsg: "Unwatching..."
  14. };
  15. }
  16. wgAjaxWatch.supported = true; // supported on current page and by browser
  17. wgAjaxWatch.watching = false; // currently watching page
  18. wgAjaxWatch.inprogress = false; // ajax request in progress
  19. wgAjaxWatch.timeoutID = null; // see wgAjaxWatch.ajaxCall
  20. wgAjaxWatch.watchLinks = []; // "watch"/"unwatch" links
  21. wgAjaxWatch.setLinkText = function(newText) {
  22. for (i = 0; i < wgAjaxWatch.watchLinks.length; i++) {
  23. changeText(wgAjaxWatch.watchLinks[i], newText);
  24. }
  25. };
  26. wgAjaxWatch.setLinkID = function(newId) {
  27. // We can only set the first one
  28. wgAjaxWatch.watchLinks[0].setAttribute( 'id', newId );
  29. akeytt(newId); // update tooltips for Monobook
  30. };
  31. wgAjaxWatch.setHref = function( string ) {
  32. for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
  33. if( string == 'watch' ) {
  34. wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
  35. .replace( /&action=unwatch/, '&action=watch' );
  36. } else if( string == 'unwatch' ) {
  37. wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
  38. .replace( /&action=watch/, '&action=unwatch' );
  39. }
  40. }
  41. }
  42. wgAjaxWatch.ajaxCall = function() {
  43. if(!wgAjaxWatch.supported) {
  44. return true;
  45. } else if (wgAjaxWatch.inprogress) {
  46. return false;
  47. }
  48. if(!wfSupportsAjax()) {
  49. // Lazy initialization so we don't toss up
  50. // ActiveX warnings on initial page load
  51. // for IE 6 users with security settings.
  52. wgAjaxWatch.supported = false;
  53. return true;
  54. }
  55. wgAjaxWatch.inprogress = true;
  56. wgAjaxWatch.setLinkText( wgAjaxWatch.watching
  57. ? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg);
  58. sajax_do_call(
  59. "wfAjaxWatch",
  60. [wgPageName, (wgAjaxWatch.watching ? "u" : "w")],
  61. wgAjaxWatch.processResult
  62. );
  63. // if the request isn't done in 10 seconds, allow user to try again
  64. wgAjaxWatch.timeoutID = window.setTimeout(
  65. function() { wgAjaxWatch.inprogress = false; },
  66. 10000
  67. );
  68. return false;
  69. };
  70. wgAjaxWatch.processResult = function(request) {
  71. if(!wgAjaxWatch.supported) {
  72. return;
  73. }
  74. var response = request.responseText;
  75. if( response.match(/^<w#>/) ) {
  76. wgAjaxWatch.watching = true;
  77. wgAjaxWatch.setLinkText(wgAjaxWatch.unwatchMsg);
  78. wgAjaxWatch.setLinkID("ca-unwatch");
  79. wgAjaxWatch.setHref( 'unwatch' );
  80. } else if( response.match(/^<u#>/) ) {
  81. wgAjaxWatch.watching = false;
  82. wgAjaxWatch.setLinkText(wgAjaxWatch.watchMsg);
  83. wgAjaxWatch.setLinkID("ca-watch");
  84. wgAjaxWatch.setHref( 'watch' );
  85. } else {
  86. // Either we got a <err#> error code or it just plain broke.
  87. window.location.href = wgAjaxWatch.watchLinks[0].href;
  88. return;
  89. }
  90. jsMsg( response.substr(4), 'watch' );
  91. wgAjaxWatch.inprogress = false;
  92. if(wgAjaxWatch.timeoutID) {
  93. window.clearTimeout(wgAjaxWatch.timeoutID);
  94. }
  95. // Bug 12395 - avoid some watch link confusion on edit
  96. var watchthis = document.getElementById("wpWatchthis");
  97. if( watchthis && response.match(/^<[uw]#>/) ) {
  98. watchthis.checked = response.match(/^<w#>/) ? "checked" : "";
  99. }
  100. return;
  101. };
  102. wgAjaxWatch.onLoad = function() {
  103. // This document structure hardcoding sucks. We should make a class and
  104. // toss all this out the window.
  105. var el1 = document.getElementById("ca-unwatch");
  106. var el2 = null;
  107. if (!el1) {
  108. el1 = document.getElementById("mw-unwatch-link1");
  109. el2 = document.getElementById("mw-unwatch-link2");
  110. }
  111. if(el1) {
  112. wgAjaxWatch.watching = true;
  113. } else {
  114. wgAjaxWatch.watching = false;
  115. el1 = document.getElementById("ca-watch");
  116. if (!el1) {
  117. el1 = document.getElementById("mw-watch-link1");
  118. el2 = document.getElementById("mw-watch-link2");
  119. }
  120. if(!el1) {
  121. wgAjaxWatch.supported = false;
  122. return;
  123. }
  124. }
  125. // The id can be either for the parent (Monobook-based) or the element
  126. // itself (non-Monobook)
  127. wgAjaxWatch.watchLinks.push( el1.tagName.toLowerCase() == "a"
  128. ? el1 : el1.firstChild );
  129. if( el2 ) {
  130. wgAjaxWatch.watchLinks.push( el2 );
  131. }
  132. // I couldn't get for (watchLink in wgAjaxWatch.watchLinks) to work, if
  133. // you can be my guest.
  134. for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
  135. wgAjaxWatch.watchLinks[i].onclick = wgAjaxWatch.ajaxCall;
  136. }
  137. return;
  138. };
  139. hookEvent("load", wgAjaxWatch.onLoad);