1/**
2 * Js for plugin catlist
3 * Adding pages
4 *
5 * @license   MIT
6 * @author    Félix Faisant <xcodexif@xif.fr>
7 *
8 */
9
10function catlist_button_add_page (element, ns) {
11	var addPageForm = element.parentNode;
12	addPageForm.innerHTML = "";
13	var addPageLabel = document.createElement('label');
14	addPageLabel.innerHTML = ns;
15	var addPageInput = document.createElement('input');
16	addPageInput.type = 'text';
17	addPageInput.id = 'catlist_addpage_id';
18	addPageInput.onkeyup = function (evt) {
19		var key = evt.keyCode || evt.which;
20		if (key == 13)
21			jQuery('#catlist_addpage_btn').click();
22	};
23	addPageLabel.htmlFor = 'catlist_addpage_id';
24	var addPageValidButton = document.createElement('button');
25	addPageValidButton.className = 'button';
26	addPageValidButton.innerHTML = "Ok";
27	addPageValidButton.id = 'catlist_addpage_btn';
28	jQuery(addPageForm).append(addPageLabel).append(addPageInput).append(addPageValidButton);
29	addPageInput.focus();
30	jQuery(addPageValidButton).click(function(){
31		if (addPageInput.value.length == 0) {
32			addPageInput.focus();
33			return;
34		}
35		var pagename = addPageInput.value;
36		if (catlist_pagename_sanitize) {
37			if (catlist_deaccent == 0) {
38				pagename = encodeURI(pagename)
39				          .replace(/[^a-zA-Z0-9._:%-]+/g, catlist_sepchar)  // transforms characters not allowed as pagename in `catlist_sepchar`
40				          .replace(/%(?![A-Fa-f0-9]{2})/, catlist_sepchar); // replace "%" if it is not the part of an URL encoded character
41			} else {
42				if (typeof String.prototype.normalize === "function")
43				pagename = pagename.normalize('NFD')
44				                   .replace(/[\u0300-\u036f]/g, ""); // eliminates diacritics
45				pagename = pagename.replace(/[^a-zA-Z0-9._:-]+/g, catlist_sepchar);
46			}
47			pagename = pagename.replace(/^[._-]+/, "") // eliminates '.', '_' and '-' at the beginning and end
48			                   .replace(/[._-]+$/, "")
49			                   .replace(new RegExp(catlist_sepchar+'{2,}','g'), catlist_sepchar) // squash multiple sepchars into one
50			                   .toLowerCase();
51		}
52		var newPageID = ns + pagename;
53		if (catlist_useslash && catlist_userewrite != 0) {
54			newPageID = newPageID.replace(/:/g, '/');
55		}
56		switch (catlist_userewrite) {
57			case 0:
58				newPageURL = catlist_baseurl + catlist_basescript + '?id=' + newPageID + '&do=edit'; break;
59			case 1:
60				newPageURL = catlist_baseurl + newPageID + '?do=edit'; break;
61			case 2:
62				newPageURL = catlist_baseurl + catlist_basescript + '/' + newPageID + '?do=edit'; break;
63		}
64		window.location.href = newPageURL;
65	});
66}
67