跳转到内容

User:魔琴/gadgets/dykc-edit-type/index.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//<nowiki>
mw.loader.using( ['mediawiki.util', 'mediawiki.api', 'ext.gadget.site-lib'/* ,'ext.gadget.HanAssist'*/ ], function () {
  
function han(s, t) {
  return wgULS(s, t);
  //return HanAssist.localize( { hans: s, hant: t } );
}


var cmt = "";
var dl = null;
var dlp = null;
var cj = {};
var API = new mw.Api();
var dykn = 'Wikipedia:新条目推荐/候选';
var regQue = /^(.|\n)*question *= *(.*?)\n(.|\n)*?$/;
var regSig = /(?<!~)~{4}(?!~)/;
var articlepath = mw.config.get('wgArticlePath');
var script = mw.config.get('wgScript');


if (mw.config.get('wgPageName') === dykn) {
  processNom();
} else {
  return;
}



function opendl(article) {
  cmt = "";
  undwarning = "";
  if (article === "undefined") undwarning = "<p><span style='color:#f00;font-size:large'>" + han("注意:条目名探测结果为undefined,这可能是程序错误导致,请考虑", "注意:條目名探測結果為undefined,這可能是程式錯誤導致,請考慮") + "<a href='/wiki/User_talk:%E9%AD%94%E7%90%B4'>" + han("回报", "回報") + "</a>。</span></p>"
  var html =
    '<div id="dykc-edit-type-dialog" class="dykc-edit-type-dialog">' +
    '<style>.dykc-edit-type-dialog,.dykc-edit-type-preview{font-size:initial;}</style>' +
    '<p>' + han('评选条目:', '評選條目:') + '<span class="dykc-edit-type-dialog-article">' + atl(article) + '</span></p>' + undwarning + 
    '<p>' + han('在文本框中输入意见,单击文字插入模板。', '在文本框中輸入意見,點擊文字插入模板。') + '</p>' +
    '<textarea name="dykc-edit-type-cmt" id="dykc-edit-type-cmt" class="dykc-edit-type-cmt" rows=4 placeholder=' + han('不用签名;留言的首行自动缩进。','不用簽名;留言的首行自動縮進。') + '></textarea>' +
    '</div>';

  if (dl) {
    dl.html(html).closest('.ui-dialog-content').dialog('destroy').remove();
    dl = null;
  }

  dl = $(html).dialog({
    title: han('修改类别', '修改類別'),
    minWidth: 600,
    minHeight: 200,
    buttons: [
      {
        text: '修改',
        click: function () {
          processCmt(article);
          postCmt(article, cmt);
        }
      },
      {
        text: han('关闭', '關閉'),
        click: function () {
          processCmt(article);
          $(this).dialog('close');
        }
      }
    ]
  });

  $('#dykc-edit-type-dialog').parent().addClass('dykc-edit-type-dialog-base')
  if (cj[article] !== undefined) {
    $('#dykc-edit-type-dialog').find('.dykc-edit-type-cmt').val(cj[article]);
  }


  function yikAdd(a) {
    var c = document.getElementById("dykc-edit-type-cmt");
    var p1 = c.selectionStart;
    var p2 = c.selectionEnd;
  
    c.value =
      c.value.substring(0, p1) +
      a +
      c.value.substring(p2, c.value.length);
  
    c.focus();
    c.setSelectionRange(p2 + a.length, p2 + a.length);
  }

}

function processCmt(a) {
  cmt = $('#dykc-edit-type-dialog').find('.dykc-edit-type-cmt').val().replace(/\n*$/,"");
  cj[a] = cmt;
  if (cmt.search(regSig) === -1) {
    cmt = cmt + '&nbsp;~~~~';
  }
}


function previewCmt(a, c) {
  previewPromise = API.post({
    action: 'parse',
    text: '**' + c,
    pst: 'true',    // PST = pre-save transform; this makes substitution work properly
    title: dykn,
    prop: 'text',
    formatversion: '2'
  } ).done(function(data){
    $('<style>.dykc-edit-type-nomarker::marker{content:"";}.dykc-edit-type-preview .ext-discussiontools-init-replylink-buttons{display:none;}</style>').appendTo('head');
    var html =
      '<div id="dykc-edit-type-preview" class="dykc-edit-type-preview mw-body-content">' + data.parse.text.replace(/li/, 'li class=dykc-edit-type-nomarker')	+ '</div>';
    if (dlp) {
      dlp.html(html).closest('.ui-dialog-content').dialog('destroy').remove();
      dlp = null;
    }
    dlp = $(html).dialog({
      title: han("预览投票:", "預覽投票:") + a,
      minWidth: 600,
      minHeight: 100,
      buttons: [
        {
          text: han('确认', '確認'),
          click: function() {
            postCmt(a, c);
            $(this).dialog('close');
          }
        },
        {
          text: '取消',
          click: function() { $(this).dialog('close'); }
        }
      ]
    });
  }).fail(function(e) {
    mw.notify( e );
  });
}

function postCmt(a, c) {
  $('.dykc-edit-type-dialog-base button').each(function () {
    $(this).prop("disabled", true);
  });
  mw.notify(han('正在发布,请稍候。','正在發佈,請稍候。'))
  getQuestion = API.post({
    action: 'parse',
    page: dykn,
    prop: 'wikitext',
    formatversion: '2'
  }).done(function (data) {
    
    regCmtStr = '(\\| *article *= *' + a.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/ /g,'[ _]') + '(.|\n)*?)\n\n?=';
    regCmt = RegExp(regCmtStr, '');

    c0 = data.parse.wikitext;
    c1 = c0.replace(regCmt, '$1\n**' + c + '\n\n=');

    if (c1 === c0) {
      mw.notify('Failed to sent.');
      enableButton();
      return;
    }

    postPromise = API.postWithToken('csrf', {
      action: 'edit',
      title: dykn,
      text: c1,
      summary: '/* ' + a + ' */ Comment on [[' + a + ']] via [[User:魔琴/gadgets/yik|YIK]]'
    }).then(function () {
      mw.notify('修改成功')
    }, function (e) {
      mw.notify(e);
      enableButton();
    });
  }).fail(function (e) {
    mw.notify(e);
    enableButton();
  })
}

function enableButton() {
  $('.dykc-edit-type-dialog-base button').each(function () {
    $(this).prop("disabled", false);
  });
}


// Process nomination page

function processNom() {
  $('<style>.mw-heading4 .mw-editsection-divider { display: inline !important; }</style>').appendTo('head');

  if ($('.mw-editsection').length > 0) {
    var yikBtn = $('<span>').append($('<span>').addClass('mw-editsection-divider').text(' | ')).append($('<a>').addClass('dykc-edit-type-view').attr('href', 'javascript:void(0)').text(han('YIK查看','YIK檢視'))).append($('<span>').addClass('mw-editsection-divider').text(' | ')).append($('<a>').addClass('dykc-edit-type-vote').attr('href', 'javascript:void(0)').text('YIK投票',));
    $( ".mw-heading4 .mw-editsection" ).each(function(){
      var _yikBtn = yikBtn.clone();
      $(this).find('.mw-editsection-bracket').last().before(_yikBtn);
    });
  }

  $('.dykc-edit-type-view').click( function () {
    window.open(atu(findArticle( $(this) )), '_blank');
  })

  $('.dykc-edit-type-vote').click( function () {
    opendl(findArticle( $(this) ));
  })

}

function findArticle ( that ) {
  closesttag = that.closest('.mw-heading4').next().prop("tagName")
  switch (closesttag) {
    case "SPAN":
      return that.closest('.mw-heading4').next().next().next().find('.dykarticle a').html();
      break;
    case "FIGURE":
      return that.closest('.mw-heading4').next().next().find('.dykarticle a').html();
      break;
    case "UL":
    default:
      return that.closest('.mw-heading4').next().find('.dykarticle a').html();
      break;
  }
}

function atu(ar) {
  return articlepath.replace('$1', encodeURI(ar)) + "?yikeditor=1&dykarticle=" + encodeURI(ar);
}

function atl(ar) {
  return  "<a href='" + atu(ar) + "' title='" + ar + "'>" + ar + "</a>";
}

});

//</nowiki>