মিডিয়াউইকি:Gadget-subpagesMenu.js: সংশোধিত সংস্করণের মধ্যে পার্থক্য

বিষয়বস্তু বিয়োগ হয়েছে বিষয়বস্তু যোগ হয়েছে
"→‎* * Add a menu for selecting subpages after the page title.: jQuery(function($) { var api, request, localization; /** * Add pages that are direct descendants of the current one. * * A "direct descendant" is a page whose title starts with the * title of the current page, and contain one more slash. I.e. "A/B" is a * direct descendant of "A", but "A/B/C" is not. "Empty" subpages, pages * that don't have co..." দিয়ে পাতা তৈরি
(কোনও পার্থক্য নেই)

১৪:৪১, ১৫ সেপ্টেম্বর ২০২১ তারিখে সংশোধিত সংস্করণ

/**
* Add a menu for selecting subpages after the page title.
*/

jQuery(function($) {
    var api, request, localization;

    /**
     * Add pages that are direct descendants of the current one.
     *
     * A "direct descendant" is a page whose title starts with the
     * title of the current page, and contain one more slash. I.e. "A/B" is a
     * direct descendant of "A", but "A/B/C" is not. "Empty" subpages, pages
     * that don't have content themselves, but have subpages of their own, are
     * also added.
     */

	localization = {
		addButtonTooltip: "একটি নতুন উপপাতা যোগ করুন",
		addTextFieldTooltip: "নতুন উপপাতা তৈরি করুন",
		dropdownTooltip: "একটি উপপাতা নির্বাচন করুন"
	};

    function addSubPagesToDropdown(data) {
        var page, $option, parts, i, subpageTitle;

        console.log("[Subpages] data =", data);
        for(i = 0; i < data.query.allpages.length; i ++) {
        	page = data.query.allpages[i];
            // The part of the title following the title of the
            // current page.
            subpageTitle =
            	page.title.slice(mw.config.get("wgPageName").length + 1);
            parts = subpageTitle.split("/");
            if($("#subpages-menu").children().filter(function() {
            	return this.textContent.slice(1) === parts[0];
            }).length === 0)
            {
            	// Add the subpage if it's not already in the dropdown.
                $("<option></option>").text("/" + parts[0])
                    .appendTo($("#subpages-menu"));
            }
        }
        // Using data.continue gives a syntax error, hence the atypical syntax
        // below.
        if(data["continue"]) {
            request["continue"] = data["continue"]["continue"];
            request.apcontinue = data["continue"].apcontinue;
            api.get(request).done(getSubPages);
        }
    }

    /**
     * Create the dropdown menu.
     */

    function addDropdown() {
        $("<select></select>")
            .attr("id", "subpages-menu")
            .addClass("subpages-controls")
            .prop("title", localization.dropdownTooltip)
            .appendTo($("#firstHeading"));
        // Add a slash as the first item. Without this, it's initially
        // impossible to select the first subpage. It also serves as
        // an visual indication as to what the dropdown does.
        $("<option></option>").text("/").appendTo($("#subpages-menu"));
        $("#subpages-menu").change(function(event) {
            if(this.value !== "/") {
                // Don't do anything if the user manage to select the
                // initial slash
                var subpageUrl = mw.config.get("wgServer") +
                    mw.util.getUrl(mw.config.get("wgPageName") + this.value);
                window.location = subpageUrl.toString();
            }
        });
    }
    
    /**
     * Add button that shows the text field for adding supbage.
     * 
     * The dropdown menu is hidden and the text field takes its place.
     */
    
    function addAddButton() {
    	$("<button></button>")
    		.attr("id", "add-subpages-button")
    		.addClass("subpages-controls")
    		.text("+")
    		.prop("title", localization.addButtonTooltip)
    		.click(function() {
    			$("#subpages-menu").hide();
    			$("#add-subpages-button").hide();
    			$("#add-subpage-field")
    				.show()
    				.focus();
    		})
    		.insertAfter("#subpages-menu");
    }
    
    /**
     * Add text field for adding a new subpage.
     * 
     * Changing the text (and pressing enter) will take the user to the subpage
     * of that name. Intended for easily getting to the create page, but
     * entering an already existing subpage will take the user to that page.
     * 
     * Pressing backspace when the field is empty will hide the field and show
     * the dropdown again.
     */
    
    function addAddField() {
        $("<input></input>")
	    	.prop("id", "add-subpage-field")
	    	.addClass("subpages-controls")
    		.prop("title", localization.addTextFieldTooltip)
	    	.change(function() {
	    		var newPageTitle = mw.config.get("wgPageName") +
	    			"/" + this.value;
	    		var parameters = {};
            	if(!mw.Title.exists(newPageTitle)) {
            		// Skip the this-page-doesn't-exist page and go directly
            		// to the editor.
            		parameters.action = "edit";
            	}
	    		var newSubpageUrl = mw.config.get("wgServer") +
                    mw.util.getUrl(newPageTitle, parameters);
	    		window.location = newSubpageUrl.toString();
	    	})
	    	.keypress(function(event) {
	    		if(event.which === 8 && this.value === "") {
	    			// Key number 8 is backspace.
	    			$("#add-subpage-field").hide();
	    			$("#subpages-menu").show();
	    			$("#add-subpages-button").show();
	    		}
	    	})
	    	.insertAfter("#subpages-menu")
	    	.hide();
    }

	addDropdown();
    mw.loader.using(["mediawiki.api", "mediawiki.util"]).done(function() {
        api = new mw.Api();
        request = {
            "action": "query",
            "format": "json",
            "list": "allpages",
            "apprefix": mw.config.get("wgTitle") + "/",
            "apnamespace": mw.config.get("wgNamespaceNumber"),
            "aplimit": 500,
            "continue": ""
        };
        console.log("[Subpages] request =", request);
        api.get(request).done(addSubPagesToDropdown);
    	addAddButton();
    	addAddField();
    	$(window).on("pageshow", function() {
	        // Select the slash item. If you go back, the page may otherwise have
        	// the last selected item selected, which means you can't change to
        	// that.
        	$("#subpages-menu").get(0).selectedIndex = 0;
		});
    });
});