MediaWiki:EditControl.js

From WikiLectures

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* ============== Edit Control ========================================
   author:      [[User:Slepi]]
   description: edit-based functions
   ====================================================================
*/

/* ================== BUTTONS ====================================== */
// Citation tool to all people
importScript("MediaWiki:Gadget-cite.js");

$(function() {

  // editorial tools for editors
  if (mwGetUserRight("editor")) importScript("MediaWiki:Gadget-button-editorial.js");
 
  // check tools for teachers
  if (mwGetUserRight("pedagogue")) importScript("MediaWiki:Gadget-button-checked.js");

});

// THIS FUNCTION CHECKS FILLING IN THE SUMMARY FIELD
// when saving edit
$(function() {
  // check the summary field
  if (wgAction=="edit") {
    document.forms["editform"].elements["wpSave"].onclick = function () {return checkSummaryFill()};
  } else if (document.forms["editform"]) {
    document.forms["editform"].elements["wpSave"].onclick = function () {return checkSummaryFill()};
  }

  return;
});

// edit buttons
if ( typeof $ != 'undefined' && typeof $.fn.wikiEditor != 'undefined' ) {
  // start at onload page
  $( function() {
    // Links at the bottom of the article
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
      'section': 'main',
      'group': 'insert',
      'tools': {
        'literaturebutton': {
          label: 'Links at the bottom of the article',
          type: 'button',
          icon: 'http://upload.wikimedia.org/wikipedia/commons/8/8d/Wikieditor-references.png',
          action: {
            type: 'encapsulate',
            options: {
              pre: "<noinclude>\n== Links ==\n=== Related Articles ===\n=== Bibliography ===\n=== References ===\n<references />\n</noinclude>\n" // text to be inserted
            }
          }
        }
      }
    } );

    // insert template
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
      'section': 'advanced',
      'group': 'insert',
      'tools': {
        'templatebutton': {
          label: 'Insert template',
          type: 'button',
          icon: 'http://upload.wikimedia.org/wikipedia/commons/4/4a/Norwegian_template_sign.png',
          action: {
            type: 'encapsulate',
            options: {
              pre: "{{",
              peri: "Template",
              post: "}}"
            }
          }
        }
      }
    } );

    // inserted article
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
      'section': 'advanced',
      'group': 'insert',
      'tools': {
        'insertbutton': {
          label: 'This article is a part of another article',
          type: 'button',
          icon: 'http://upload.wikimedia.org/wikipedia/commons/a/a1/Toolbaricon_insert_reference_plus.png',
          action: {
            type: 'encapsulate',
            options: {
              pre: "{{subst:Inserted" + " article}}"
            }
          }
        }
      }
    } );
  } );
}

/* ===================== EDIT SUMMARY ============================== */
 
// FUNCTION CHECKS EDIT SUMMARY
// when saving changes
$(function() {
  // check edit summary
  if (wgAction=="edit") {
    document.forms["editform"].elements["wpSave"].onclick = function () {return checkSummaryFill()};
  } else if (document.forms["editform"]) {
    document.forms["editform"].elements["wpSave"].onclick = function () {return checkSummaryFill()};
  }
 
  return;
});
 
function checkSummaryFill() {
 
  // check summary
  if(document.getElementById("wpSummary")) {
    wpsummary = document.getElementById("wpSummary").value;
    if (wpsummary == "") wpsummary = false;
  } else { wpsummary = true; }
 
  if (!wpsummary) {
    window.alert("Your changes cannot be saved as you did not filled in the Summary field. Please, describe there shortly what changes you did.\n\nIf you created a new article, write simply \?new article\?. If you did only small changes, describe them (e.g. \?correction of the pathology\? or \?added clinical notes\?).\n\nAfter filling in the Summary field you can save the article.\n\nThank you!");
    return false;
  } else return true;
}

/* ======================= OTHER FUNCTIONS ========================= */
// FUNCTION ADDING "SAVE AS DRAFT" BUTTON
$(function() {
  // check edit
  if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) == -1) return;
  if (mw.config.get("wgUserName") == null) return;
  if (!document.getElementById("wpTextbox1")) return;

  // create button
  draftButton = document.createElement("input");
  draftButton.type = "submit";
  draftButton.value = "Save as draft";

  // button will add "Under construction" template and save changes
  draftButton.onclick = function() {
    article = document.getElementById("wpTextbox1").value;

    if (article.indexOf("{{Under construction}}") == -1) {
      article = "{{Under construction}}\n\n" + article;
      document.getElementById("wpTextbox1").value = article;
    }

    window.alert("This article will be saved as a draft under construction.\n\nNobody will change your work for one month. If you forget this article, our editorial board will contact you.\n\nIn case of any problem or question, do not hesitate to contact us at info@wikilectures.eu.");

    document.forms["editform"].elements["wpSave"].click();
  };

  // insert button
  document.getElementById("wpDiff").parentNode.insertBefore(draftButton, document.getElementById("wpDiff").nextSibling);

  // button SHOW CHANGES
  // prepare
  buttonShowChanges = document.createElement("span");
  editButtonsRow = getElementsByClass("editButtons")[0];
  editButtonsRow.appendChild(buttonShowChanges);

  // button Show changes
  bscChanges = document.createElement("a");
  bscChanges.href = "#";
  bscChanges.onclick = function() {
    document.forms["editform"].elements["wpDiff"].click();
    return false;
  }
 
  bscChanges.appendChild(document.createTextNode("Show changes"));
  buttonShowChanges.appendChild(document.createTextNode(" | "));
  buttonShowChanges.appendChild(bscChanges);
});

// SIMPLIFY EDIT FIELD WITH REASONS TO DELETE THE ARTICLE
$(function () {
  // without deleting it make no sense to do this
  if (mw.config.get("wgAction") != "delete") return;

  // reasons
  wpReason = document.getElementById("wpReason");
  if (!wpReason) return;

  // usual reasons
  usualReasons = /(content was|page was empty|content before blanking was)/i;

  // help string
  wpReasonSmall = wpReason.value.toLowerCase();

  // reasons edit
  if (usualReasons.test(wpReasonSmall)){
    if((wpReasonSmall.indexOf("{{delete|") != -1) && (wpReasonSmall.indexOf("}}") != -1)) {
      indexStart = wpReasonSmall.indexOf("{{delete|") + 9;
      indexEnd = wpReasonSmall.indexOf("}}");
      wpReason.value = wpReason.value.substring(indexStart, indexEnd);
    } else {wpReason.value = "";}
  }
});