MediaWiki:Script/Element.js : Différence entre versions

 
(9 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
 
function camelToKebab(str) {
 
function camelToKebab(str) {
   return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
+
   return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
 
}
 
}
 
  
 
function copyProperties(source, target, allowedProps) {
 
function copyProperties(source, target, allowedProps) {
 
   var sourceKeys = Object.keys(source);
 
   var sourceKeys = Object.keys(source);
  
   for (var i = 0; i < sourceKeys.length; i++) {
+
   for (var keyIndex = 0; keyIndex < sourceKeys.length; keyIndex++) {
     var prop = sourceKeys[i];
+
     var property = sourceKeys[keyIndex];
     var propValue = source[prop];
+
     var propValue = source[property];
  
     if (prop === 'viewBox') {
+
     if (property === "viewBox") {
       target.setAttribute(prop, propValue);
+
       target.setAttribute(property, propValue);
 
+
     } else if (
     } else if (prop.indexOf("data") === 0 || allowedProps.indexOf(prop) !== -1) {
+
      property.indexOf("data") === 0 ||
       target.setAttribute(camelToKebab(prop), propValue);
+
      allowedProps.indexOf(property) !== -1
 +
    ) {
 +
       target.setAttribute(camelToKebab(property), propValue);
 
     }
 
     }
 
   }
 
   }
 
}
 
}
  
 +
function createElement(
 +
  elementsToReplace,
 +
  elementsNS,
 +
  allowedProperties,
 +
  elementDataset,
 +
  elementType,
 +
  skillValues
 +
) {
 +
  if (elementsNS.indexOf(elementType) !== -1) {
 +
    var newElement = document.createElementNS(
 +
      "http://www.w3.org/2000/svg",
 +
      elementType
 +
    );
 +
  } else {
 +
    var newElement = document.createElement(elementType);
 +
  }
  
 +
  var childdNodes = elementsToReplace.childNodes;
  
function createLabelInput(element, allowedProperties, dataset) {
+
   copyProperties(elementDataset, newElement, allowedProperties);
 
 
  var label = document.createElement("label");
 
  var input = document.createElement("input");
 
     
 
   copyProperties(dataset, input, allowedProperties);
 
 
 
  label.setAttribute("for", input.id);
 
  label.appendChild(input);
 
  label.appendChild(document.createTextNode(element.textContent));
 
 
 
  return label;
 
}
 
  
 +
  for (var childIndex = 0; childIndex < childdNodes.length; childIndex++) {
 +
    newElement.appendChild(childdNodes[childIndex].cloneNode(true));
 +
  }
  
function createSvgPath(element, allowedProperties, dataset) {
+
  if (newElement.dataset.add === "skill") {
 +
    for (var index = 0; index < skillValues.length; index++) {
 +
      var option = document.createElement("option");
 +
      option.value = index;
 +
      option.textContent = skillValues[index];
 +
      newElement.appendChild(option);
 +
    }
 +
  }
  
  var svg = document.createElementNS(dataset["xmlns"], "svg");
+
   return newElement;
  var path = document.createElementNS(dataset["xmlns"], "path");
 
  var title = document.createElement("title");
 
 
 
  var dAttribute = dataset["d"];
 
  var titleContent = dataset["title"];
 
 
 
  delete dataset["d"];
 
  delete dataset["title"];
 
 
 
  copyProperties(dataset, svg, allowedProperties);
 
 
 
  path.setAttribute("d", dAttribute);
 
  title.textContent = titleContent;
 
 
 
  svg.appendChild(title);
 
  svg.appendChild(path);
 
 
 
   return svg;
 
 
}
 
}
  
 +
function replaceElement(
 +
  elementsToReplace,
 +
  allowedElements,
 +
  elementsNS,
 +
  allowedProperties,
 +
  skillValues
 +
) {
 +
  var elementDataset = elementsToReplace.dataset;
 +
  var elementType = elementDataset.element;
 +
 +
  if (allowedElements.indexOf(elementType) !== -1) {
 +
    var newElement = createElement(
 +
      elementsToReplace,
 +
      elementsNS,
 +
      allowedProperties,
 +
      elementDataset,
 +
      elementType,
 +
      skillValues
 +
    );
  
function createElement(element, allowedProperties, dataset, elementType) {
+
    elementsToReplace.parentNode.replaceChild(newElement, elementsToReplace);
 
+
   } else {
  var newElement = document.createElement(elementType);
+
     elementsToReplace.remove();
  var children = element.childNodes;
 
     
 
  copyProperties(dataset, newElement, allowedProperties);
 
     
 
   for (var i = 0; i < children.length; i++) {
 
     var child = children[i];
 
    newElement.appendChild(child.cloneNode(true));
 
 
   }
 
   }
 
 
  return newElement;
 
 
}
 
}
  
 
+
function addCollapsible(collapsibleContainer) {
function replaceElement(element, allowedElements, allowedProperties) {
+
  var toggleButton = collapsibleContainer.querySelector(".mw-collapsible-toggle");
 +
  var collapsibleContent = collapsibleContainer.querySelector(".mw-collapsible-content");
 +
  var collapsibleContentChild = collapsibleContent.firstElementChild;
 +
  var expandedClass = "mw-collapsible-toggle-expanded";
 +
  var isCollapsed = false;
 
    
 
    
   var dataset = element.dataset;
+
   toggleButton.setAttribute("tabindex", 0);
  var elementType = dataset.element;
 
 
    
 
    
   if (allowedElements.indexOf(elementType) !== -1) {
+
   toggleButton.addEventListener("click", function (event) {
      
+
    if (isCollapsed) {
     if (elementType === "label-input") {
+
      this.classList.remove(expandedClass);
       var newElement = createLabelInput(element, allowedProperties, dataset);
+
      collapsibleContentChild.style.overflow = "hidden";
     } else if (elementType === "svg-path") {
+
      collapsibleContent.style.gridTemplateRows = "0fr";
       var newElement = createSvgPath(element, allowedProperties, dataset);
+
    } else {
 +
      collapsibleContentChild.classList.remove("tabber-noactive");
 +
      this.classList.add(expandedClass);
 +
      collapsibleContent.style.gridTemplateRows = "1fr";
 +
     }
 +
    isCollapsed = !isCollapsed;
 +
  });
 +
  toggleButton.addEventListener("keyup", function (event) {
 +
     if (event.keyCode === 13) {
 +
       this.click();
 +
     }
 +
  });
 +
  collapsibleContent.addEventListener("transitionend", function (event) {
 +
    if (isCollapsed) {
 +
       collapsibleContentChild.style.overflow = "visible";
 
     } else {
 
     } else {
       var newElement = createElement(element, allowedProperties, dataset, elementType);
+
       collapsibleContentChild.classList.add("tabber-noactive");
 
     }
 
     }
   
+
  });
    element.parentNode.replaceChild(newElement, element);
 
  }
 
 
}
 
}
  
 +
(function () {
 +
  var allowedElements = [
 +
    "input",
 +
    "button",
 +
    "select",
 +
    "option",
 +
    "label",
 +
    "form",
 +
    "svg",
 +
    "path",
 +
    "title",
 +
  ];
 +
  var elementsNS = ["svg", "path", "title"];
 +
  var allowedProperties = [
 +
    "id",
 +
    "class",
 +
    "type",
 +
    "name",
 +
    "ariaHaspopup",
 +
    "ariaExpanded",
 +
    "ariaControls",
 +
    "for",
 +
    "min",
 +
    "max",
 +
    "value",
 +
    "style",
 +
    "pattern",
 +
    "required",
 +
    "selected",
 +
    "disabled",
 +
    "checked",
 +
    "tabindex",
 +
    "placeholder",
 +
    "xmlns",
 +
    "height",
 +
    "viewBox",
 +
    "width",
 +
    "fill",
 +
    "d",
 +
    "title",
 +
  ];
 +
 +
  var skillValues = [
 +
    0,
 +
    1,
 +
    2,
 +
    3,
 +
    4,
 +
    5,
 +
    6,
 +
    7,
 +
    8,
 +
    9,
 +
    10,
 +
    11,
 +
    12,
 +
    13,
 +
    14,
 +
    15,
 +
    16,
 +
    17,
 +
    18,
 +
    19,
 +
    "M1",
 +
    "M2",
 +
    "M3",
 +
    "M4",
 +
    "M5",
 +
    "M6",
 +
    "M7",
 +
    "M8",
 +
    "M9",
 +
    "M10",
 +
    "G1",
 +
    "G2",
 +
    "G3",
 +
    "G4",
 +
    "G5",
 +
    "G6",
 +
    "G7",
 +
    "G8",
 +
    "G9",
 +
    "G10",
 +
    "P",
 +
  ];
 +
 +
  var elementsToReplace = document.querySelector("div[data-element]");
 +
 +
  while (elementsToReplace) {
 +
    replaceElement(
 +
      elementsToReplace,
 +
      allowedElements,
 +
      elementsNS,
 +
      allowedProperties,
 +
      skillValues
 +
    );
 +
    elementsToReplace = document.querySelector("div[data-element]");
 +
  }
  
(function(){
+
   var collapsibleContainers = document.querySelectorAll(
 
+
    ".improved-collapsible.custom-js"
   var elementsToReplace = document.querySelectorAll("div[data-element]");
+
   );
  var allowedElements = ["input", "button", "select", "option", "label", "form", "label-input", "svg-path"];
 
   var allowedProperties = ["id", "class", "type", "name", "ariaHaspopup", "ariaExpanded", "ariaControls", "for", "min", "max", "value", "style", "pattern", "required", "selected", "xmlns", "height", "viewBox", "width", "fill", "d", "title"];
 
  
   for (var i = elementsToReplace.length - 1; i >= 0; i--) {
+
   for (var collapsibleContainer of collapsibleContainers) {
      
+
     addCollapsible(collapsibleContainer);
    var element = elementsToReplace[i];
 
   
 
    var children = element.children;
 
   
 
    for (var j = children.length - 1; j >= 0; j--) {
 
      var child = children[j];
 
      replaceElement(child, allowedElements, allowedProperties);
 
    }
 
   
 
    replaceElement(element, allowedElements, allowedProperties);
 
 
   }
 
   }
 
})();
 
})();

Version actuelle datée du 22 mars 2024 à 19:58

function camelToKebab(str) {
  return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
}

function copyProperties(source, target, allowedProps) {
  var sourceKeys = Object.keys(source);

  for (var keyIndex = 0; keyIndex < sourceKeys.length; keyIndex++) {
    var property = sourceKeys[keyIndex];
    var propValue = source[property];

    if (property === "viewBox") {
      target.setAttribute(property, propValue);
    } else if (
      property.indexOf("data") === 0 ||
      allowedProps.indexOf(property) !== -1
    ) {
      target.setAttribute(camelToKebab(property), propValue);
    }
  }
}

function createElement(
  elementsToReplace,
  elementsNS,
  allowedProperties,
  elementDataset,
  elementType,
  skillValues
) {
  if (elementsNS.indexOf(elementType) !== -1) {
    var newElement = document.createElementNS(
      "http://www.w3.org/2000/svg",
      elementType
    );
  } else {
    var newElement = document.createElement(elementType);
  }

  var childdNodes = elementsToReplace.childNodes;

  copyProperties(elementDataset, newElement, allowedProperties);

  for (var childIndex = 0; childIndex < childdNodes.length; childIndex++) {
    newElement.appendChild(childdNodes[childIndex].cloneNode(true));
  }

  if (newElement.dataset.add === "skill") {
    for (var index = 0; index < skillValues.length; index++) {
      var option = document.createElement("option");
      option.value = index;
      option.textContent = skillValues[index];
      newElement.appendChild(option);
    }
  }

  return newElement;
}

function replaceElement(
  elementsToReplace,
  allowedElements,
  elementsNS,
  allowedProperties,
  skillValues
) {
  var elementDataset = elementsToReplace.dataset;
  var elementType = elementDataset.element;

  if (allowedElements.indexOf(elementType) !== -1) {
    var newElement = createElement(
      elementsToReplace,
      elementsNS,
      allowedProperties,
      elementDataset,
      elementType,
      skillValues
    );

    elementsToReplace.parentNode.replaceChild(newElement, elementsToReplace);
  } else {
    elementsToReplace.remove();
  }
}

function addCollapsible(collapsibleContainer) {
  var toggleButton = collapsibleContainer.querySelector(".mw-collapsible-toggle");
  var collapsibleContent = collapsibleContainer.querySelector(".mw-collapsible-content");
  var collapsibleContentChild = collapsibleContent.firstElementChild;
  var expandedClass = "mw-collapsible-toggle-expanded";
  var isCollapsed = false;
  
  toggleButton.setAttribute("tabindex", 0);
  
  toggleButton.addEventListener("click", function (event) {
    if (isCollapsed) {
      this.classList.remove(expandedClass);
      collapsibleContentChild.style.overflow = "hidden";
      collapsibleContent.style.gridTemplateRows = "0fr";
    } else {
      collapsibleContentChild.classList.remove("tabber-noactive");
      this.classList.add(expandedClass);
      collapsibleContent.style.gridTemplateRows = "1fr";
    }
    isCollapsed = !isCollapsed;
  });
  toggleButton.addEventListener("keyup", function (event) {
    if (event.keyCode === 13) {
      this.click();
    }
  });
  collapsibleContent.addEventListener("transitionend", function (event) {
    if (isCollapsed) {
      collapsibleContentChild.style.overflow = "visible";
    } else {
      collapsibleContentChild.classList.add("tabber-noactive");
    }
  });
}

(function () {
  var allowedElements = [
    "input",
    "button",
    "select",
    "option",
    "label",
    "form",
    "svg",
    "path",
    "title",
  ];
  var elementsNS = ["svg", "path", "title"];
  var allowedProperties = [
    "id",
    "class",
    "type",
    "name",
    "ariaHaspopup",
    "ariaExpanded",
    "ariaControls",
    "for",
    "min",
    "max",
    "value",
    "style",
    "pattern",
    "required",
    "selected",
    "disabled",
    "checked",
    "tabindex",
    "placeholder",
    "xmlns",
    "height",
    "viewBox",
    "width",
    "fill",
    "d",
    "title",
  ];

  var skillValues = [
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19,
    "M1",
    "M2",
    "M3",
    "M4",
    "M5",
    "M6",
    "M7",
    "M8",
    "M9",
    "M10",
    "G1",
    "G2",
    "G3",
    "G4",
    "G5",
    "G6",
    "G7",
    "G8",
    "G9",
    "G10",
    "P",
  ];

  var elementsToReplace = document.querySelector("div[data-element]");

  while (elementsToReplace) {
    replaceElement(
      elementsToReplace,
      allowedElements,
      elementsNS,
      allowedProperties,
      skillValues
    );
    elementsToReplace = document.querySelector("div[data-element]");
  }

  var collapsibleContainers = document.querySelectorAll(
    ".improved-collapsible.custom-js"
  );

  for (var collapsibleContainer of collapsibleContainers) {
    addCollapsible(collapsibleContainer);
  }
})();