মিডিয়াউইকি:Gadget-delNominator.js

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
//<nowiki>
/**
 * Gadget to quickly and easily nominate the current page for deletion on bn.wikivoyage.org.
 * @author: [[User:Yahya]]
 */
var DeletionNomination = function () {
  mw.loader.using([
    'oojs-ui-core',
    'oojs-ui-windows',
    'mediawiki.widgets'
  ]).done(function () {

    // Function to get CSRF token using MediaWiki's built-in method
    function getCsrfToken(callback) {
      var csrfToken;
      var editToken = mw.user.tokens.get('csrfToken');
      if (editToken) {
        csrfToken = editToken;
      } else {
        new mw.Api().get({
          action: 'query',
          meta: 'tokens',
          format: 'json'
        }).done(function(data) {
          csrfToken = data.query.tokens.csrftoken;
        }).fail(function() {
          console.error("Failed to retrieve CSRF token.");
          callback(null);  // Handle missing token
        });
      }
      callback(csrfToken);  // Pass token to the callback function
    }

    function DeletionDialog(config) {
      DeletionDialog.super.call(this, config);
    }

    OO.inheritClass(DeletionDialog, OO.ui.ProcessDialog);

    $.extend(DeletionDialog.static, {
      name: 'DeletionDialog',
      actions: [
        {
          flags: ['primary', 'progressive'],
          label: 'মনোনয়ন দিন',
          action: 'save'
        },
        {
          flags: 'safe',
          label: 'বাতিল'
        }
      ]
    });

    $.extend(DeletionDialog.prototype, {
      initialize: function () {
        DeletionDialog.super.prototype.initialize.call(this);
        this.panel = new OO.ui.PanelLayout({
          padded: true,
          expanded: false
        });
        this.content = new OO.ui.FieldsetLayout();

        // Pre-fill page name with current page name
        var currentPageName = mw.config.get('wgPageName').replace(/_/g, ' ');
        this.pageName = new OO.ui.TextInputWidget({
          value: currentPageName,
          disabled: true
        });
        this.pageNameField = new OO.ui.FieldLayout(this.pageName, {
          label: 'পাতার নাম',
          align: 'top'
        });

        this.deletionRationale = new OO.ui.MultilineTextInputWidget({autosize: true});
        this.deletionRationaleField = new OO.ui.FieldLayout(this.deletionRationale, {
          label: 'কেন পাতাটি অপসারিত হওয়া উচিত তার কারণ লিখুন:',
          align: 'top'
        });

        this.content.addItems([
          this.pageNameField,
          this.deletionRationaleField
        ]);

        this.panel.$element.append(this.content.$element);
        this.$body.append(this.panel.$element);

        // Enable/disable "Nominate" button based on deletion rationale input
        this.deletionRationale.on('change', this.onDeletionRationaleChange.bind(this));
      },
      getSetupProcess: function (data) {
        return DeletionDialog.super.prototype.getSetupProcess.call(this, data)
          .next(function () {
            this.actions.setAbilities({'save': false});
          }, this);
      },

      getActionProcess: function (action) {
        if (action === 'save') {
          var modal = this;
          return new OO.ui.Process(function () {
            var pageName = modal.pageName.getValue();
            var deletionRationale = modal.deletionRationale.getValue();
            var vfdTag = '{{vfd}}\n';

            // Add {{vfd}} tag to the beginning of the current page content
            var content = $('#wpTextbox1').val();
            $('#wpTextbox1').val(vfdTag + content);

            // Get CSRF token and prepend {{vfd}} tag to current page content
            getCsrfToken(function(csrfToken) {
              if (!csrfToken) {
                alert('Failed to retrieve CSRF token.');
                return;
              }

              // Edit the current page to prepend {{vfd}} tag to the content
              var editCurrentPage = new mw.Api()
              .postWithToken('csrf', {
                action: 'edit',
                title: pageName,
                prependtext: vfdTag,
                summary: 'অপসারণ আলোচনার জন্য {{vfd}} ট্যাগ যোগ করা হয়েছে'
              });

              // Append deletion discussion section to Project:Deletion Discussion page using API
              var editDeletionDiscussion = new mw.Api()
              .postWithToken('csrf', {
                action: 'edit',
                title: 'উইকিভ্রমণ:অপসারণ ভোটাভুটি',
                section: 'new',
                sectiontitle: '[[:' + pageName + ']]',
                summary: '[[:' + pageName + ']] পাতাটিকে অপসারণের জন্য মনোনীত করা হলো',
                text: deletionRationale + ' – ~~~~'
              });

              // Perform both edits simultaneously
              $.when(editCurrentPage, editDeletionDiscussion)
              .done(function() {
                // Successfully added {{vfd}} tag to current page and deletion discussion section
                alert('পাতাটিকে সফলভাবে অপসারণের জন্য মনোনীত করা হয়েছে! এটি দুই সপ্তাহ আলোচনার জন্য উন্মুক্ত থাকবে।');
                modal.close();
              })
              .fail(function(error) {
                console.error("Failed to perform edits:", error);
                alert('অপসারণের জন্য মনোনীত করা যায়নি। অনুগ্রহ করে আপনার ইন্টারনেট সংযোগ পরীক্ষা করুন। এতেও সমাধান না হলে একজন স্ক্রিপ্ট ডেভেলপারের সাথে যোগাযোগ করুন।');
              });
            });
          }, this);
        }
        return DeletionDialog.super.prototype.getActionProcess.call(this, action);
      },

      onDeletionRationaleChange: function (value) {
        this.actions.setAbilities({
          save: !!value.length
        });
      }
    });


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

    var deletionDialog = new DeletionDialog();

    windowManager.addWindows([deletionDialog]);
    windowManager.openWindow(deletionDialog, {});
  });
};

$(document).ready(function () {
  var deletionLink = mw.util.addPortletLink('p-tb', '#', 'অপসারণের জন্য মনোনয়ন', 'p-nominate-deletion', 'এই পাতাটিকে অপসারণের জন্য মনোনীত করুন');
  $(deletionLink).click(function (e) {
    e.preventDefault();
    DeletionNomination();
  });
});
//</nowiki>