User:Rutilant/statusChanger.js

/*
    <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();
  });
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.