Skip to content

Reorder Patterns, Folders in Pattern Lab Nav Based On Order Config #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/PatternLab/PatternData.php
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class PatternData {
protected static $patternSubtypeDash = "";
protected static $patternSubtypeSet = false;
protected static $patternType = "";
protected static $defaultPatternOrder = 0;
protected static $patternTypeClean = "";
protected static $patternTypeDash = "";
protected static $rules = array();
@@ -240,7 +241,7 @@ public static function getDirSep() {
}

/**
* GEt the front meta bits (hidden and noviewall)
* Get the front meta bits (hidden and noviewall)
*/
public static function getFrontMeta() {
return self::$frontMeta;
@@ -329,6 +330,18 @@ public static function getPatternSubtypeSet() {

}


/**
* Get the pattern order if it exists, otherwise return default
*/
public static function getPatternOrder() {
if (isset(self::$patternOrder)) {
return self::$patternOrder;
}
return self::$defaultPatternOrder;
}


/**
* Get the pattern type
*/
@@ -544,6 +557,16 @@ public static function setPatternType($optionValue) {

}


/**
* Set the pattern order value
* @param {String} the order value
*/
public static function setPatternOrder($optionValue) {
self::$patternOrder = $optionValue;
}


/**
* Set the pattern type clean
* @param {String} the option value
44 changes: 36 additions & 8 deletions src/PatternLab/PatternData/Exporters/NavItemsExporter.php
Original file line number Diff line number Diff line change
@@ -27,8 +27,21 @@ public function __construct($options = array()) {

$this->store = PatternData::get();
$this->styleGuideExcludes = Config::getOption("styleGuideExcludes");

}


// Sort navigation level based on the `order` key (if it exists)
private function sortNavByOrder($a, $b) {
if (!isset($a['order'])){
return 0;
} else if (!isset($b['order'])){
return 0;
} else if ($a['order'] == $b['order']){
return 0;
}
return ($a['order'] < $b['order']) ? -1 : 1;
}


public function run() {

@@ -46,14 +59,15 @@ public function run() {
foreach ($this->store as $patternStoreKey => $patternStoreData) {

if ($patternStoreData["category"] == "patternType") {

$bi = (count($navItems["patternTypes"]) == 0) ? 0 : $bi + 1;

// add a new patternType to the nav
$navItems["patternTypes"][$bi] = array("patternTypeLC" => strtolower($patternStoreData["nameClean"]),
"patternTypeUC" => ucwords($patternStoreData["nameClean"]),
"patternType" => $patternStoreData["name"],
"patternTypeDash" => $patternStoreData["nameDash"],
"order" => $patternStoreData["order"],
"patternTypeItems" => array(),
"patternItems" => array());

@@ -71,6 +85,7 @@ public function run() {
"patternSubtypeUC" => ucwords($patternStoreData["nameClean"]),
"patternSubtype" => $patternStoreData["name"],
"patternSubtypeDash" => $patternStoreData["nameDash"],
"order" => $patternStoreData["order"],
"patternSubtypeItems" => array());

// starting a new set of pattern types. it might not have any pattern subtypes
@@ -87,6 +102,7 @@ public function run() {
"patternSrcPath" => $patternStoreData["pathName"],
"patternName" => ucwords($patternStoreData["nameClean"]),
"patternState" => $patternStoreData["state"],
"order" => $patternStoreData["order"],
"patternPartial" => $patternStoreData["partial"]);

// add to the nav
@@ -95,11 +111,8 @@ public function run() {
} else {
$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][] = $patternInfo;
}

}

}

}

// review each subtype. add a view all link or remove the subtype as necessary
@@ -154,11 +167,26 @@ public function run() {
"patternPartial" => "viewall-".$patternTypeDash."-all");

}
}


// Sort the navItems by order property before returning final navigation (@TODO: look into possibly moving the sortNavByOrder function to a more global (ie. reusable) place)
foreach ($navItems as $navItem) {
// Sort top level patternTypes (ex. 01-atoms)
usort($navItems['patternTypes'], array( $this, 'sortNavByOrder' ) );

foreach ($navItem as $patternTypeKey => $patternTypeValue) {
// Then sort patternTypeItems and/or patternItems depending on what exists (ex. Homepage or Buttons)
usort($navItems['patternTypes'][$patternTypeKey]['patternTypeItems'], array( $this, 'sortNavByOrder' ) );
usort($navItems['patternTypes'][$patternTypeKey]['patternItems'], array( $this, 'sortNavByOrder' ) );

// Finallly, finish sorting out the nested patternSubtypeItems (Primary Button, etc)
for($i = 0, $c = count($navItems['patternTypes'][$patternTypeKey]['patternTypeItems']); $i < $c; $i++){
usort($navItems['patternTypes'][$patternTypeKey]['patternTypeItems'][$i]['patternSubtypeItems'], array( $this, 'sortNavByOrder' ) );
}
}
}

return $navItems;

}

}
36 changes: 28 additions & 8 deletions src/PatternLab/PatternData/Rules/DocumentationRule.php
Original file line number Diff line number Diff line change
@@ -33,7 +33,6 @@ public function __construct($options) {
}

public function run($depth, $ext, $path, $pathName, $name) {

// load default vars
$patternType = PatternData::getPatternType();
$patternTypeDash = PatternData::getPatternTypeDash();
@@ -57,23 +56,42 @@ public function run($depth, $ext, $path, $pathName, $name) {
unset($yaml["title"]);
}

// figure out if this is a pattern subtype
// figure out if this is a top level pattern type or pattern subtype
$patternSubtypeDoc = false;
if ($depth == 1) {
// go through all of the directories to see if this one matches our doc
$patternTypeDoc = false;
if ($depth == 0) {
foreach (glob($patternSourceDir.DIRECTORY_SEPARATOR.$patternType,GLOB_ONLYDIR) as $dir) {
$dir = str_replace($patternSourceDir.DIRECTORY_SEPARATOR,"",$dir);
if ($dir == $doc) {
$patternTypeDoc = true;
break;
}
}
} else if ($depth == 1){
// go through all of the directories to see if this one matches our doc
foreach (glob($patternSourceDir.DIRECTORY_SEPARATOR.$patternType.DIRECTORY_SEPARATOR."*",GLOB_ONLYDIR) as $dir) {
$dir = str_replace($patternSourceDir.DIRECTORY_SEPARATOR.$patternType.DIRECTORY_SEPARATOR,"",$dir);
if ($dir == $doc) {
$patternSubtypeDoc = true;
break;
}
}

}

$category = ($patternSubtypeDoc) ? "patternSubtype" : "pattern";
$patternStoreKey = ($patternSubtypeDoc) ? $docPartial."-plsubtype" : $docPartial;

$category = "pattern"; // By default, make the pattern type a "pattern"
$patternStoreKey = $docPartial;

// Update if patternType or subtype
if ($patternTypeDoc) {
$category = "patternType";
$patternStoreKey = $patternTypeDash."-pltype";

// organisms-pltype
} else if ($patternSubtypeDoc){
$category = "patternSubtype";
$patternStoreKey = $docPartial."-plsubtype";
}

$patternStoreData = array("category" => $category,
"desc" => trim($markdown),
"descExists" => true,
@@ -100,6 +118,8 @@ public function run($depth, $ext, $path, $pathName, $name) {
}
}



// if the pattern data store already exists make sure this data overwrites it
$patternStoreData = (PatternData::checkOption($patternStoreKey)) ? array_replace_recursive(PatternData::getOption($patternStoreKey),$patternStoreData) : $patternStoreData;
PatternData::setOption($patternStoreKey, $patternStoreData);
2 changes: 2 additions & 0 deletions src/PatternLab/PatternData/Rules/PatternRule.php
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@ public function run($depth, $ext, $path, $pathName, $name) {
$patternTypeDash = PatternData::getPatternTypeDash();
$dirSep = PatternData::getDirSep();
$frontMeta = PatternData::getFrontMeta();
$patternOrder = PatternData::getPatternOrder();

// should this pattern get rendered?
$hidden = ($name[0] == "_");
@@ -83,6 +84,7 @@ public function run($depth, $ext, $path, $pathName, $name) {
"noviewall" => $noviewall,
"depth" => $depth,
"ext" => $ext,
"order" => $patternOrder, // default pattern order
"path" => $path,
"pathName" => $patternPath,
"pathDash" => $patternPathDash,
4 changes: 3 additions & 1 deletion src/PatternLab/PatternData/Rules/PatternSubtypeRule.php
Original file line number Diff line number Diff line change
@@ -37,7 +37,8 @@ public function run($depth, $ext, $path, $pathName, $name) {
$patternTypeDash = PatternData::getPatternTypeDash();
$patternTypeClean = PatternData::getPatternTypeClean();
$dirSep = PatternData::getDirSep();

$patternOrder = PatternData::getPatternOrder();

// set-up the names
$patternSubtype = $name; // 02-blocks
$patternSubtypeDash = $this->getPatternName($name,false); // blocks
@@ -61,6 +62,7 @@ public function run($depth, $ext, $path, $pathName, $name) {
"breadcrumb" => array("patternType" => $patternTypeClean),
"depth" => $depth,
"ext" => $ext,
"order" => $patternOrder,
"path" => $path,
"pathName" => $patternSubtypePath,
"pathDash" => $patternSubtypePathDash,
2 changes: 2 additions & 0 deletions src/PatternLab/PatternData/Rules/PatternTypeRule.php
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ public function run($depth, $ext, $path, $pathName, $name) {

// load default vars
$dirSep = PatternData::getDirSep();
$patternOrder = PatternData::getPatternOrder();

// set-up the names
$patternType = $name; // 00-atoms
@@ -53,6 +54,7 @@ public function run($depth, $ext, $path, $pathName, $name) {
"nameClean" => $patternTypeClean,
"depth" => $depth,
"ext" => $ext,
"order" => $patternOrder,
"path" => $path,
"pathName" => $patternTypePath,
"pathDash" => $patternTypePathDash,
3 changes: 3 additions & 0 deletions src/PatternLab/PatternData/Rules/PseudoPatternRule.php
Original file line number Diff line number Diff line change
@@ -86,6 +86,8 @@ public function run($depth, $ext, $path, $pathName, $name) {
$patternPathOrigDash = str_replace($dirSep,"-",$patternPathOrig); // 04-pages-00-homepage
}

// Use the original pattern's order value by default if we can find it
$patternOrder = !empty(PatternData::getPatternOption($patternBaseOrig,"order")) ? PatternData::getPatternOption($patternBaseOrig,"order") : PatternData::getPatternOrder();
// create a key for the data store
$patternStoreKey = $patternPartial;

@@ -103,6 +105,7 @@ public function run($depth, $ext, $path, $pathName, $name) {
"noviewall" => $noviewall,
"depth" => $depth,
"ext" => $ext,
"order" => $patternOrder,
"path" => $path,
"pathName" => $patternPath,
"pathDash" => $patternPathDash,