ผู้ใช้:Jo Shigeru/Script/FA.js

จากวิกิพีเดีย สารานุกรมเสรี

หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง

  • ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
  • กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
  • อินเทอร์เน็ตเอกซ์พลอเรอร์ และ Edge: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
  • โอเปร่า: กด Ctrl-F5
// <syntaxhighlight lang="javascript">
// =========สคริปต์สำหรับจัดการโครงการบทความคัดสรร==========
// ==แจ้งเตือนเมื่อผู้ใช้กดเสนอชื่อบทความ โดยที่ยังไม่ได้กรอกชื่อบทความ==
function alertWhenEmptyTitle() {
  if(wgPageName != "วิกิพีเดีย:บทความคัดสรร") return;
  var span = document.getElementById("fa-nominate-span");
  var forms = span.getElementsByTagName("form");
  var i = 0;
  for(i=0;i<=forms.length-1;i++) {
    var form = forms[i];
    if(form.name != "createbox") continue;
    form.setAttribute("action", "javascript:fa_submitNominate("+i+")");
  }
}
$(alertWhenEmptyTitle);

function fa_submitNominate(i) {
  var span = document.getElementById("fa-nominate-span");
  var forms = span.getElementsByTagName("form");
  var form = forms[i];
  var txtbox = getElementByClass("createboxInput", form);
  if(txtbox.value == "") {
    alert("โปรดกรอกชื่อบทความด้วย!");
  } else {
    form.setAttribute("action", "/index.php")
    form.submit();
  }
}

// ==แสดงความคิดเห็นเกี่ยวกับบทความโดยใช้จาวาสคริปต์==
$(function() {
  if(!(wgPageName.match(/^วิกิพีเดีย:บทความคัดสรร\/.+\/.+$/))) return;
  var setLink = function(element,mode) {
    if(element==null) return;
    var link = document.createElement("a");
    link.setAttribute("id","link");
    link.setAttribute("href","javascript:showForm('"+mode+"')");
    link.appendChild(document.createTextNode("เพิ่มความคิดเห็น"));
    element.appendChild(link);
  };
  var setDisabledLink = function(element) {
    if(element==null) return;
    var link = document.createElement("font");
    link.setAttribute("id","link");
    link.setAttribute("color","gray");
    link.appendChild(document.createTextNode("เพิ่มความคิดเห็น"));
    element.appendChild(link);
  };
  setLink(document.getElementById("add_comment"),"comment");
  setDisabledLink(document.getElementById("add_comment_disabled"));
  setLink(document.getElementById("add_vote"),"vote");
  setDisabledLink(document.getElementById("add_vote_disabled"));
  setLink(document.getElementById("add_object"),"object");
  setDisabledLink(document.getElementById("add_object_disabled"));
});

function showForm(mode) {
  var textbox = document.createElement("input");
  textbox.setAttribute("type","text");
  textbox.setAttribute("id","textbox");
  textbox.style.width = "300px"

  /*
  var moretools = document.createElement("div")
  moretools.style.width = textbox.style.width;
  moretools.style.border = "1px solid black"
  moretools.style.bgColor = "white";
  moretools.style.padding = "1px";
  moretools.style.visibility = "hidden";
  var moretools_table = document.createElement("table");
  moretools.appendChild(moretools_table);

  var moretools_subcommentChk = document.createElement("input");
  moretools_subcommentChk.setAttribute("type","checkbox");
  moretools_subcommentChk.setAttribute("id","subcommentChk");
  moretools_subcommentChk.onmouseover = function() { moretools_subcommentChk.setAttribute("isMouseOver","1"); };
  moretools_subcommentChk.onmouseout = function() { moretools_subcommentChk.setAttribute("isMouseOver","0"); };
  moretools.appendChild(document.createElement("tr")).appendChild(document.createElement("td")).appendChild(moretools_subcommentChk);

  textbox.onclick = function() { moretools.style.visibility = "visible"; };
  textbox.onblur = function() { if(moretools_subcommentChk.getAttribute("isMouseOver")=="0") moretools.style.visibility = "hidden"; };
  */
  var button = document.createElement("input");
  button.setAttribute("type","submit");
  button.setAttribute("id","button");
  button.setAttribute("value","ตกลง");
  button.onclick = function() { saveComment(mode); };

  var element = document.getElementById("add_"+mode);
  removeAll(element);
  element.appendChild(textbox);
  //element.appendChild(document.createElement("br"));
  //element.appendChild(moretools);
  element.appendChild(button);
};

function saveComment(mode) {
  var element = document.getElementById("add_"+mode);
  var textboxValue = getElementById("textbox",element).value;
  if(textboxValue==null) textboxValue = "";
  removeAll(element);
  var loadingText = document.createElement("font");
  loadingText.setAttribute("color","gray");
  loadingText.appendChild(document.createTextNode("กำลังบันทึก..."));
  element.appendChild(loadingText);

  var subPages = new Array();
  subPages['comment'] = "คห";
  subPages['vote'] = "โหวต";
  subPages['object'] = "คัดค้าน";
  
  var commentPageHeader =
    '<noinclude>{{บคคส-คหหัวเรื่อง}}<!--\n'+
    '-->[[ภาพ:Redirectltr.png]] <font size=4px>\'\'\'กลับไปที่ [[{{#titleparts:{{FULLPAGENAME}}|3}}|หน้าโหวตหลักของ "{{#titleparts:{{FULLPAGENAME}}|1|3}}"]]\'\'\'</font>\n'+
    '----\n'+
    '</noinclude>';

  var pattern = new Array();
  pattern['comment'] = "* $ ~~~~"
  pattern['vote'] = "* {{บคคส-เห็นด้วย| $ }} ~~~~"
  pattern['object'] = "* {{บคคส-ไม่เห็นด้วย| $ }} ~~~~"

  var commentPage = wgPageName+"/"+subPages[mode];

  var errorAlert = function() {
    alert("เกิดความผิดพลาดขึ้น โปรดลองอีกครั้ง!");
    window.location.reload();
  }

  try {
    API_isExists(commentPage,function(isExists) {
      API_getProp(commentPage,"tokens",null,function(tokens) {
        var editToken = tokens['token_edit'];
        var text = "";
        if(!isExists) text += commentPageHeader;
        if(textboxValue.match(/^(:+)/)) {
          var commentText = textboxValue.replace(/^(:+)(\s*)/,"");
          var indents = textboxValue.replace(commentText,"");
          text += indents+pattern['comment'].replace("$",commentText);
        } else {
          text += pattern[mode].replace("$",textboxValue);
        }
        API_edit(commentPage,editToken,"new",null,text,function() {
          window.location = wgArticlePath.replace("$1",encodeURI(wgPageName))+"?action=purge";
        },errorAlert);
      },errorAlert);
    },errorAlert);
  } catch(err) { errorAlert(); }
};

// </syntaxhighlight>