User:Locke/common.js: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
(add patrol calls)
m (lol int)
Line 35: Line 35:
         var revisions = data.query.pages[pageid].revisions;
         var revisions = data.query.pages[pageid].revisions;
         var promises = [];
         var promises = [];
         for (int i = 0; i < revisions.length; i++) {
         for (var i = 0; i < revisions.length; i++) {
           promises.push(api.postWithToken('patrol', {
           promises.push(api.postWithToken('patrol', {
             formatversion: 2, // no clue what this is or if it's needed
             formatversion: 2, // no clue what this is or if it's needed
Line 48: Line 48:
           }, function(error) {
           }, function(error) {
             $spinner.remove();
             $spinner.remove();
             $this.after($('<span style="color:red">Cannot mark as patrolled</span>'))
             $this.after($('<span style="color:red">Cannot mark as patrolled</span>'));
           });
           });



Revision as of 23:28, March 20, 2017

if (mw.user.tokens.exists('patrolToken')) {
  // add patrol link under diff-multi notice
  var $diffMultiNotice = $('td.diff-multi');
  if ($diffMultiNotice.length > 0) {
    var $patrolAllLink = $('<a href="#">Mark all as patrolled</a>');
    $diffMultiNotice.append('<br>[').append($patrolAllLink).append(']');

    // patrol link click handler
    $patrolAllLink.click(function(e) {
      var $this = $(this);
      var $spinner;
      // replace text with spinner
      mw.loader.using('jquery.spinner').then(function() {
        $spinner = $.createSpinner({ size :'small', type: 'inline' });
        $this.hide().after($spinner);
      });

      // find bounding revision ids
      var oldid = mw.util.getParamValue('oldid');
      var newid = mw.util.getParamValue('diff');
      var pageid = mw.util.getParamValue('curid');

      // call api for all revisions in that span
      var api = new mw.Api();
      api.get({
        action: 'query',
        prop: 'revisions',
        rvprop: 'ids',
        rvstartid: newid,
        rvendid: oldid,
        rvlimit: 'max',
        pageids: pageid
      }).done(function (data) {
        console.log(data);
        var revisions = data.query.pages[pageid].revisions;
        var promises = [];
        for (var i = 0; i < revisions.length; i++) {
          promises.push(api.postWithToken('patrol', {
            formatversion: 2, // no clue what this is or if it's needed
            action: 'patrol',
            revid: revisions[i].revid
          }));
        }
        $.when.apply($, promises).then(
          function(result) {
            $spinner.remove();
            $this.after($('<span style="color:green">Marked as patrolled</span>'));
          }, function(error) {
            $spinner.remove();
            $this.after($('<span style="color:red">Cannot mark as patrolled</span>'));
          });

      });
      e.preventDefault();
    });
  }
}