پرش به محتوا

مدیاویکی:Gadget-close.js: تفاوت میان نسخه‌ها

از دانشنامه علوم انسانی
imported>Huji
محدود به فضاهای نام بحث و فضای نام پروژه
imported>Huji
چیزهایی مثل {{شد}} و {{نشد}} و «جمع‌بندی:» را به سادگی می‌شود تایپ کرد نیاز به دکمه ندارد؛ ضخیم کردن را می‌تواند دگمه کرد (درخواست Arash.pt)
خط ۸۷: خط ۸۷:
           value: true,
           value: true,
         });
         });
         archiveSectionOptions.switchDone = new OO.ui.ToggleSwitchWidget({
         archiveSectionOptions.switchBold = new OO.ui.ToggleSwitchWidget({
           label: 'قبل از جمع‌بندی برچسب {{شد}} اضافه شود',
           label: 'متن جمع‌بندی ضخیم نمایش داده شود',
           value: false,
           value: false,
         });
         });
خط ۱۰۳: خط ۱۰۳:
           align: 'inline',
           align: 'inline',
         }));
         }));
         fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.switchDone, {
         fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.switchBold, {
           label: 'قبل از جمع‌بندی برچسب {{شد}} اضافه شود',
           label: 'متن جمع‌بندی ضخیم نمایش داده شود',
           align: 'inline',
           align: 'inline',
         }));
         }));
خط ۲۶۶: خط ۲۶۶:
           if (archiveSectionOptions.switchTop.getValue()) {
           if (archiveSectionOptions.switchTop.getValue()) {
             newText += ':';
             newText += ':';
             if (archiveSectionOptions.switchDone.getValue()) {
             if (archiveSectionOptions.switchBold.getValue()) {
               newText += '{{شد}} ';
               newText += "'''";
             }
             }
            newText += "'''جمع‌بندی:''' ";
             newText += archiveSectionOptions.textSummary.getValue();
             newText += archiveSectionOptions.textSummary.getValue();
            if (archiveSectionOptions.switchBold.getValue()) {
              newText += "'''";
            }
             newText += ' ~~' + '~~\n';
             newText += ' ~~' + '~~\n';
             newText += content + '\n{{پایان بسته}}';
             newText += content + '\n{{پایان بسته}}';
           } else {
           } else {
             var summary = '';
             var summary = '';
             if (archiveSectionOptions.switchDone.getValue()) {
             if (archiveSectionOptions.switchBold.getValue()) {
               summary += '{{شد}} ';
               newText += "'''";
            } else {
              summary += "'''جمع‌بندی:''' ";
             }
             }
             summary += archiveSectionOptions.textSummary.getValue();
             summary += archiveSectionOptions.textSummary.getValue();
            if (archiveSectionOptions.switchBold.getValue()) {
              newText += "'''";
            }
             summary += ' ~~' + '~~\n';
             summary += ' ~~' + '~~\n';
             newText = archiveSectionOptions.setIndentation(newText + content, summary);
             newText = archiveSectionOptions.setIndentation(newText + content, summary);

نسخهٔ ۵ نوامبر ۲۰۱۶، ساعت ۲۰:۳۳

/*global jQuery, mediaWiki */
(function($, mw) {
  'use strict';
  var windowManager;
  var archiveSection;
  var commentInSection;
  var archiveSectionOptions = {
    install: function() {
      if ($.inArray(mw.config.get("wgNamespaceNumber"), [1,3,4,5,7,9,11,13,15]) > -1) {
        $('.mw-editsection').each(function() {
          var span = $('<span>', {
            class: 'mw-editsection mw-archivesection'
          }).insertBefore(this);
          span.append($('<span>', {
            class: 'mw-editsection-bracket',
            text: '['
          }));
          span.append($('<a>', {
            href: '#',
            title: 'ثبت نظر (موافق/مخالف/شد/...)',
            style: 'font-size: smaller;',
            text: 'ثبت نظر',
          }).click(function(e) {
            e.preventDefault();
            var section = e.target;
            mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']).done(function() {
              archiveSectionOptions.createCommentWindow(section);
            });
          }));
          span.append($('<span>', {
            class: 'mw-editsection-bracket',
            text: ' / '
          }));
          span.append($('<a>', {
            href: '#',
            title: 'جمع‌بندی این بخش',
            style: 'font-size: smaller;',
            text: 'جمع‌بندی',
          }).click(function(e) {
            e.preventDefault();
            var section = e.target;
            mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']).done(function() {
              archiveSectionOptions.createArchiveWindow(section);
            });
          }));
          span.append($('<span>', {
            class: 'mw-editsection-bracket',
            text: ']'
          }));
          // Late pre-loading
          mw.loader.load(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']);
        });
      }
    },
    createArchiveWindow: function(section) {
      var query = archiveSectionOptions.getQuery(section);
      archiveSectionOptions.currentSectionId = query.section;

      function ArchiveSection(config) {
        ArchiveSection.super.call(this, config);
      }
      OO.inheritClass(ArchiveSection, OO.ui.ProcessDialog);
      ArchiveSection.static.name = 'archive';
      ArchiveSection.static.title = 'جمع‌بندی بخش';
      ArchiveSection.static.actions = [{
        action: 'archive',
        label: 'جمع‌بندی',
        flags: 'primary'
      }, {
        label: 'لغو',
        flags: 'safe'
      }];
      ArchiveSection.prototype.initialize = function() {
        var fieldset = [];
        ArchiveSection.super.prototype.initialize.apply(this, arguments);
        this.panel = new OO.ui.PanelLayout({
          padded: true,
          expanded: false
        });
        this.content = new OO.ui.FieldsetLayout();
        archiveSectionOptions.switchTop = new OO.ui.ToggleSwitchWidget({
          label: 'جمع‌بندی بالای متن باشد',
          value: true,
        });
        archiveSectionOptions.switchSuccess = new OO.ui.ToggleSwitchWidget({
          label: 'بحث در رسیدن به اجماع موفق بود',
          value: true,
        });
        archiveSectionOptions.switchBold = new OO.ui.ToggleSwitchWidget({
          label: 'متن جمع‌بندی ضخیم نمایش داده شود',
          value: false,
        });
        archiveSectionOptions.textSummary = new OO.ui.TextInputWidget({
          placeholder: 'متن جمع‌بندی را وارد کنید',
        });
        archiveSectionOptions.textSummary.setRTL(true);
        fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.switchTop, {
          label: 'جمع‌بندی بالای متن باشد',
          align: 'inline',
        }));
        fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.switchSuccess, {
          label: 'بحث در رسیدن به اجماع موفق بود',
          align: 'inline',
        }));
        fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.switchBold, {
          label: 'متن جمع‌بندی ضخیم نمایش داده شود',
          align: 'inline',
        }));
        fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.textSummary, {
          label: 'جمع‌بندی:',
          align: 'top'
        }));
        this.content.addItems(fieldset);
        this.panel.$element.append(this.content.$element);
        this.$body.append(this.panel.$element);
      };
      ArchiveSection.prototype.getActionProcess = function(action) {
        var dialog = this;
        if (action === 'archive') {
          archiveSectionOptions.doEdit(archiveSectionOptions.currentSectionId, action);
        }
        return ArchiveSection.super.prototype.getActionProcess.call(this, action);
      };
      if (!windowManager) {
        windowManager = new OO.ui.WindowManager();
        $('body').append(windowManager.$element);
      }
      if (!archiveSection) {
        archiveSection = new ArchiveSection({
          size: 'medium'
        });
        windowManager.addWindows([archiveSection]);
      }
      windowManager.openWindow(archiveSection);
    },
    createCommentWindow: function(section) {
      var query = archiveSectionOptions.getQuery(section);
      archiveSectionOptions.currentSectionId = query.section;

      function CommentInSection(config) {
        CommentInSection.super.call(this, config);
      }
      OO.inheritClass(CommentInSection, OO.ui.ProcessDialog);
      CommentInSection.static.title = 'comment';
      CommentInSection.static.title = 'ثبت نظر';
      CommentInSection.static.actions = [{
        action: 'comment',
        label: 'ثبت نظر',
        flags: 'primary'
      }, {
        label: 'لغو',
        flags: 'safe'
      }];
      CommentInSection.prototype.initialize = function() {
        var fieldset = [];
        CommentInSection.super.prototype.initialize.apply(this, arguments);
        this.panel = new OO.ui.PanelLayout({
          padded: true,
          expanded: false
        });
        this.content = new OO.ui.FieldsetLayout();
        archiveSectionOptions.radioDone = new OO.ui.RadioOptionWidget({
          label: 'شد',
        });
        archiveSectionOptions.radioSupport = new OO.ui.RadioOptionWidget({
          label: 'موافق',
        });
        archiveSectionOptions.radioOppose = new OO.ui.RadioOptionWidget({
          label: 'مخالف',
        });
        archiveSectionOptions.radioCommentType = new OO.ui.RadioSelectWidget({
          items: [archiveSectionOptions.radioDone,
            archiveSectionOptions.radioSupport,
            archiveSectionOptions.radioOppose
          ]
        });
        archiveSectionOptions.radioCommentType.selectItem(archiveSectionOptions.radioDone);
        archiveSectionOptions.textComment = new OO.ui.TextInputWidget({
          placeholder: 'متن نظر را وارد کنید',
        });
        archiveSectionOptions.textComment.setRTL(true);
        fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.radioCommentType, {
          label: 'برچسب قبل از نظر',
          align: 'top'
        }));
        fieldset.push(new OO.ui.FieldLayout(archiveSectionOptions.textComment, {
          label: 'متن نظر:',
          align: 'top'
        }));
        this.content.addItems(fieldset);
        this.panel.$element.append(this.content.$element);
        this.$body.append(this.panel.$element);
      };
      CommentInSection.prototype.getActionProcess = function(action) {
        var dialog = this;
        if (action === 'comment') {
          archiveSectionOptions.doEdit(archiveSectionOptions.currentSectionId, action);
        }
        return CommentInSection.super.prototype.getActionProcess.call(this, action);
      };
      if (!windowManager) {
        windowManager = new OO.ui.WindowManager();
        $('body').append(windowManager.$element);
      }
      if (!commentInSection) {
        commentInSection = new CommentInSection({
          size: 'medium'
        });
        windowManager.addWindows([commentInSection]);
      }
      windowManager.openWindow(commentInSection);
    },
    getQuery: function(section) {
      var href = $(section).parent().next().find('a').attr('href');
      var query = new mw.Uri(href).query;
      return query;
    },
    setIndentation: function(text, newText) {
      text = text.replace('\r', '')
      var lrI = text.lastIndexOf('\n');
      var Lline = text.lastIndexOf('\n:');
      if (Lline !== -1) {
        var textE = text.slice(Lline);
        var lrM = textE.match(/\n[:]+ ?/);
        if (lrM) {
          var lrLen = lrM[0].trim().length;
          if (lrLen) {
            text = text + '\n' + lrM[0].trim() + ': ' + newText.trim()
          } else {
            text = text + '\n: ' + newText.trim()
          }
        } else {
          text = text + '\n: ' + newText.trim()
        }
      } else {
        text = text + '\n: ' + newText.trim()
      }
      return text
    },
    doEdit: function(sectionId, action) {
      (new mw.Api()).get({
        format: 'json',
        action: 'query',
        titles: mw.config.get("wgPageName"),
        rvsection: sectionId,
        prop: 'revisions',
        rvprop: 'content',
      }).then(function(data) {
        var page = data.query.pages[Object.keys(data.query.pages)[0]];
        var current = page.revisions[0]['*'];
        var header = current.substr(0, current.indexOf('\n'));
        var content = current.substr(current.indexOf('\n') + 1);
        var newText = '';
        var section_title = (current.match(/^(=+)[^=]*?(\1)$/m) || [''])[0];
        section_title = section_title.replace(/\=/g, '');
        var editSummary = '/' + '*' + section_title + '*' + '/ جمع‌بندی: ';
        if (action == 'archive') {
          editSummary += archiveSectionOptions.textSummary.getValue();
          newText = header + '\n';
          newText += '{{بسته';
          if (archiveSectionOptions.switchSuccess.getValue()) {
            newText += '}}\n';
          } else {
            newText += '|ناموفق=بله}}\n';
          }
          if (archiveSectionOptions.switchTop.getValue()) {
            newText += ':';
            if (archiveSectionOptions.switchBold.getValue()) {
              newText += "'''";
            }
            newText += archiveSectionOptions.textSummary.getValue();
            if (archiveSectionOptions.switchBold.getValue()) {
              newText += "'''";
            }
            newText += ' ~~' + '~~\n';
            newText += content + '\n{{پایان بسته}}';
          } else {
            var summary = '';
            if (archiveSectionOptions.switchBold.getValue()) {
              newText += "'''";
            }
            summary += archiveSectionOptions.textSummary.getValue();
            if (archiveSectionOptions.switchBold.getValue()) {
              newText += "'''";
            }
            summary += ' ~~' + '~~\n';
            newText = archiveSectionOptions.setIndentation(newText + content, summary);
            newText += '\n{{پایان بسته}}';
          }
          return (new mw.Api()).post({
            format: 'json',
            action: 'edit',
            title: mw.config.get("wgPageName"),
            section: sectionId,
            summary: editSummary,
            text: newText,
            token: mw.user.tokens.get('editToken')
          });
        } else {
          editSummary += archiveSectionOptions.textComment.getValue();
          if (archiveSectionOptions.radioDone.isSelected()) {
            newText = '{{شد}} ';
            editSummary += 'انجام شد';
          } else if (archiveSectionOptions.radioSupport.isSelected()) {
            newText = '{{موافق}} ';
            editSummary += 'ثبت نظر موافق';
          } else {
            newText = '{{مخالف}} ';
            editSummary += 'ثبت نظر مخالف';
          }
          newText += archiveSectionOptions.textComment.getValue();
          newText += ' ~~' + '~~';
          newText = archiveSectionOptions.setIndentation(current, newText);
          return (new mw.Api()).post({
            format: 'json',
            action: 'edit',
            title: mw.config.get("wgPageName"),
            section: sectionId,
            summary: editSummary,
            text: newText,
            token: mw.user.tokens.get('editToken')
          });
        }
      }).then(function() {
        if (action == 'archive') {
          mw.notify('جمع‌بندی شما ثبت شد!');
        } else {
          mw.notify('نظر شما ثبت شد!');
        }
        location.hash = $('.mw-headline')[sectionId - 1].id;
        location.reload(true);
      }, function(error) {
        mw.notify('ثبت نظر یا جمع‌بندی ناموفق بود: ' + error, {
          type: 'error'
        });
      })
    },
    currentSectionId: -1,
  };
  if (mw.config.get('wgNamespaceNumber') >= 0) {
    $(archiveSectionOptions.install);
  }
})(jQuery, mediaWiki);