مستخدم:Gerges/المستعيد/main.js

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

mw.loader.using(['mediawiki.Title', 'mediawiki.messagePoster'], function() {
  if (mw.config.get('wgAction') != 'history') return;
  var rationaleRecoverInput;
  var dialog;
  var rationaleHideInput = '';
  var ApiRecover = new mw.Api();
  var VarsRecover = {
    'TemplateP': 'مستخدم:Gerges/المستعيد/تنبيهات/قالب:'
  };

  window.restorerSummary = window.restorerSummary ||
    'استعادة نسخة [[User:$USER|$USER]] ([[User talk:$USER|نقاش]]) رقم  $ID ([[ar:مستخدم:Gerges/المستعيد|المُستَعيد]])';

  function SendNotify(_page, _title, _notify) {
    var title = new mw.Title(_page);
    var posterPromise = mw.messagePoster.factory.create(title);
    posterPromise.done(function(poster) {
      poster.post(_title, _notify);
    });
    return posterPromise;
  }


  function Recover(revid, input) {
    return ApiRecover.get({
      action: 'query',
      revids: revid,
      prop: 'revisions',
      rvprop: 'user',
      format: 'json',
      formatversion: '2'
    }).then(function(res) {
      var user = res.query.pages[0].revisions[0].user;
      var summaryRecover;
      if (input === '') {
        summaryRecover = window.restorerSummary.replace(/\$ID/g, revid).replace(/\$USER/g, user);
      } else {
        summaryRecover = window.restorerSummary.replace(/\$ID/g, revid).replace(/\$USER/g, user) + ' : ' + input;
      }

      return ApiRecover.postWithEditToken({
        action: 'edit',
        pageid: mw.config.get('wgArticleId'),
        undo: mw.config.get('wgCurRevisionId'),
        undoafter: revid,
        summary: summaryRecover
      });
    }).then(
      function() {

        mw.notify('استعدت النسخة بنجاح.');
        location.reload();
      },
      function(_, data) {

        mw.notify(ApiRecover.getErrorMessage(data), { type: 'error' });
      }
    );
  }

  function addLink(item) {
    var revid = item.getAttribute('data-mw-revid');
    if (revid == mw.config.get('wgCurRevisionId')) return;

    var links = item.querySelector('.comment + .mw-changeslist-links');
    if (!links) return;

    var parent = document.createElement('span'),
      el = document.createElement('a');

    el.addEventListener('click', function() {
      var RecoverNotify;
      var indexTabs;
      var RecoverOptions;
      var RecoverHideOptions;

      function RecoverDialog(config) {
        RecoverDialog.super.call(this, config);
      }
      OO.inheritClass(RecoverDialog, OO.ui.ProcessDialog);

      RecoverDialog.static.name = 'RecoverDialog';
      RecoverDialog.static.title = 'المُستَعيد';
      RecoverDialog.static.actions = [{
        action: 'save',
        label: 'تنفيذ',
        flags: ['primary', 'progressive']
    }, {
        label: 'إلغاء',
        flags: 'safe'
    }];
      RecoverDialog.prototype.initialize = function() {
        RecoverDialog.super.prototype.initialize.apply(this, arguments);
        this.content = new OO.ui.PanelLayout({
          padded: true,
          expanded: false
        });
        RecoverOptions = new OO.ui.FieldsetLayout({
          label: 'استرجاع التعديلات حتي نسخة ' + revid + ''
        });
        RecoverOptions.addItems([rationaleField = new OO.ui.FieldLayout(rationaleRecoverInput = new OO.ui.MultilineTextInputWidget({
          placeholder: 'سبب الاستعادة',
          indicator: 'required',
          value: '',
        }), {
          label: 'ضع سبب لعملية الاستعادة',
          align: 'inline',
        })]);


        var RecoverNotifyOptions = new OO.ui.FieldsetLayout({
          label: 'تنبيه المستخدم'
        });
        var RecoverNotifyCheckbox = new OO.ui.CheckboxInputWidget({
          selected: false
        });
        var RecoverNotifyDropdown = new OO.ui.DropdownWidget({
          menu: {
            items: [
						new OO.ui.MenuOptionWidget({
                data: 'تنبيه 1',
                label: 'تنبيه 1'

              }),
              new OO.ui.MenuOptionWidget({
                data: 'تنبيه 2',
                label: 'تنبيه 2'
              }),
              new OO.ui.MenuOptionWidget({
                data: 'تنبيه 3',
                label: 'تنبيه 3'
              }),
              new OO.ui.MenuOptionWidget({
                data: 'سبام 1',
                label: 'سبام 1'
              }),
                new OO.ui.MenuOptionWidget({
                data: 'معلومات غير صحيحة 1',
                label: 'معلومات غير صحيحة 1'
              }),
                new OO.ui.MenuOptionWidget({
                data: 'إساءة أو ألفاظ نابية 1',
                label: 'إساءة أو ألفاظ نابية 1'
              }),
                new OO.ui.MenuOptionWidget({
                data: 'وجهة نظر غير حيادية 1',
                label: 'وجهة نظر غير حيادية 1'
              }),
                new OO.ui.MenuOptionWidget({
                data: 'بدون مصدر 1',
                label: 'بدون مصدر 1'
              })

			         ]
          }
        });

        RecoverNotifyOptions.addItems([
new OO.ui.FieldLayout(RecoverNotifyDropdown, { label: 'نوع التنبيه :' })
]);
        RecoverNotifyDropdown.getMenu().on('choose', function(menuOption) {
          RecoverNotify = menuOption.getData();
        });


        RecoverHideOptions = new OO.ui.FieldsetLayout({
          label: 'طلب إخفاء مراجعة'
        });
        RecoverHideOptions.addItems([rationaleField = new OO.ui.FieldLayout(rationaleHideInput = new OO.ui.MultilineTextInputWidget({
          placeholder: 'سبب الإخفاء',
          indicator: 'required',
          value: '',
        }), {
          label: 'ضع سبب لطلب إخفاء مراجعة',
          align: 'inline',
        })]);
        //start tabs
        var ConfigTab1 = {
          label: 'استرجاع',
          $element: RecoverOptions.$element
        };
        var ConfigTab2 = {
          label: 'تنبيه',
          $element: RecoverNotifyOptions.$element
        };
        var ConfigTab3 = {
          label: 'إخفاء',
          $element: RecoverHideOptions.$element
        };
        var tabPanel1 = new OO.ui.TabPanelLayout('recover', ConfigTab1),
          tabPanel2 = new OO.ui.TabPanelLayout('warning', ConfigTab2),
          tabPanel3 = new OO.ui.TabPanelLayout('hide', ConfigTab3);


        indexTabs = new OO.ui.IndexLayout();
        indexTabs.addTabPanels([tabPanel1, tabPanel2, tabPanel3]);

        //end tabs
        this.content.$element = indexTabs.$element;

        this.$body.append(this.content.$element);
      };
      RecoverDialog.prototype.getActionProcess = function(action) {
        dialog = this;
        var _out;
        if (action) {
          switch (indexTabs.currentTabPanelName) {

            case 'recover':
              return RecoverDialog.super.prototype.getActionProcess.call(this, action).next(function() {
                return Recover(revid, rationaleRecoverInput.value).then(function() {
                  dialog.close();
                });
              }, this);

              break;
            case 'warning':
              return RecoverDialog.super.prototype.getActionProcess.call(this, action).next(function() {


                return ApiRecover.get({
                  action: 'query',
                  revids: revid,
                  prop: 'revisions',
                  rvprop: 'user',
                  format: 'json',
                  formatversion: '2'
                }).then(function(res) {
                  var userTalk = 'نقاش المستخدم:' + res.query.pages[0].revisions[0].user;
                  var _title = 'تنبيه في [[' + mw.config.get('wgPageName').replace(/_/g, " ") + ']]';
                  var _linkR = 'https://ar.wikipedia.org/w/index.php?title=' + mw.config.get('wgPageName') + '&diff=' + revid;
                  var _massage = '{{نسخ:' + VarsRecover.TemplateP + RecoverNotify + '|' + mw.config.get('wgPageName').replace(/_/g, " ") + '|2=' + _linkR + '}}<!-- ~~~~ -->';


                  return SendNotify(userTalk, _title, _massage).then(function() {
                    mw.notify('أَرْسَلْتُ تَنْبِيهًا ل ' + res.query.pages[0].revisions[0].user + ' بنجاح');
                    dialog.close();
                  });
                });
              }, this);
              break;
            case 'hide':
              return RecoverDialog.super.prototype.getActionProcess.call(this, action).next(function() {


                _page = mw.config.get('wgPageName').replace(/_/g, " ");
                return SendNotify('ويكيبيديا:إخطار الإداريين/إخفاء/الحالية', '', '\{\{نسخ:طلب إخفاء مراجعة|توقيع=\~\~\~\~|صفحة = ' + _page + '|رقم النسخة = ' + revid + '|سبب = ' + rationaleHideInput.value + '\}\}<!-- ~~~~ -->').then(function() {
                  mw.notify('أرسلت طلب إخفاء مراجعة بنجاح');
                  dialog.close();
                });
              }, this);
          }

          //dialog.close({ action: action });


        }
        return RecoverDialog.super.prototype.getActionProcess.call(this, action);
      };

      var windowManager = new OO.ui.WindowManager();
      $(document.body).append(windowManager.$element);

      var dialog = new RecoverDialog();
      windowManager.addWindows([dialog]);
      windowManager.openWindow(dialog);


    });

    el.textContent = 'المُستَعيد';
    parent.appendChild(el);
    links.appendChild(parent);

  }

  var parents = document.querySelectorAll('li[data-mw-revid]');

  for (var i = 0; i < parents.length; i++) {
    addLink(parents[i]);
  }
});