User:Rutilant/statusChanger.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
    <section begin=last_update/>February 7, 2019<section end=last_update/>
*/

if (s_appname === undefined) {
  var s_appname = "statusChanger";
}

$(function() {
  var get_token = $.get("/w/api.php?action=query&meta=tokens&format=json").then(response => {
    try {
      return response.query.tokens.csrftoken;
    } catch (e) {
      mw.notify(s_appname + ': unable to parse the token; check console log for more detail.');
      console.log(e);
      return false;
    }
  });

  function change_status(new_status) {
    var wgUserName = mw.config.values.wgUserName;
    var edit_summary = `Status: ${new_status} ([[User:RainFall/statusChanger|${s_appname}]])`;
    get_token.then(token => {
      if (token) {
        $(".s-btns").attr("disabled", "disabled");
        $.ajax({
          type: 'POST',
          url: '/w/api.php',
          data: {
            title: 'User:' + wgUserName + '/Status',
            action: 'edit',
            token: token,
            summary: edit_summary,
            minor: 1,
            recreate: 1,
            text: new_status,
            format: 'json'
          },
          success: function(e) {
            hide_options();
            mw.notify(s_appname + ': updated status to \'' + new_status + '\'.');
          },
          error: function(e) {
            console.log(e);
            mw.notify(s_appname + ': unable to perform the edit; check console log for more detail.');
            return 'Unable to perform the edit.';
          },
          complete: () => {
            $(".s-btns").prop("disabled", false);
          }
        });
      }
    });
  }


  function show_options() {
    var wgUserName = mw.config.values.wgUserName;
    var overlay = "";
    var style = `
      position: fixed;
      top:50%;
      left: 50%;
      background-color:#f3f3f3;
      padding: 10px;
      font-family: BlinkMacSystemFont, 'Segoe UI', Constantia, Calibri;
      transform: translate(-50%, -50%);
      width: 350px;
      z-index: 1; 
    `;
    var header = "User: " + wgUserName + "<br>Current status: <span id='s-current-status'>unknown</span>";
    var available = ['online', 'away', 'around', 'busy', 'offline', 'sleeping'];
    var cancelbutton = `<button id='cancel_status_change'>Cancel</button>`;

    var available_options = "";
    available.forEach(function(e, i) {
      available_options += `<button id="s-btn-${i}" class="s-btns" data-status="${e}">${e}</button>`;
    });
    overlay += `
  <div style="${style}" id="status_changer_overlay_11">
  ${header}<br><br>
  <div style="text-align:center">
  ${available_options}
  </div><br>
  ${cancelbutton}
  </div>`;
    $("body").prepend(overlay);
    $(".s-btns").click(function(elem) {
      change_status(elem.currentTarget.dataset.status);
    });
    $("#cancel_status_change").click(function() {
      hide_options();
    });
    /* get the current status */
    $.get('/w/api.php?action=parse&prop=wikitext&format=json&page=User:' + wgUserName + '/Status')
      .then(returndata => {
        try {
          returndata = returndata.parse.wikitext["*"];
        } catch (e) {
          returndata = "parse error";
          console.log('Parse error.');
          console.log(e);
        }
        $("#s-current-status").html(returndata);
      });
  }

  function hide_options() {
    $("#status_changer_overlay_11").animate({
      opacity: 0,
      top: 0
    }, 500, function() {
      $("#status_changer_overlay_11").remove();
    });
  }

  mw.util.addPortletLink("p-cactions", "#", "Change status", "sts_change");
  $("#sts_change").click(function(ev) {
    ev.preventDefault();
    show_options();
  });
});