diff --git a/src/PatternLab/PatternData.php b/src/PatternLab/PatternData.php index 3e7ced4e..74fe4066 100644 --- a/src/PatternLab/PatternData.php +++ b/src/PatternLab/PatternData.php @@ -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 diff --git a/src/PatternLab/PatternData/Exporters/NavItemsExporter.php b/src/PatternLab/PatternData/Exporters/NavItemsExporter.php index 43db5694..9e90bcee 100644 --- a/src/PatternLab/PatternData/Exporters/NavItemsExporter.php +++ b/src/PatternLab/PatternData/Exporters/NavItemsExporter.php @@ -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,7 +59,7 @@ 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 @@ -54,6 +67,7 @@ public function run() { "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; - } - } diff --git a/src/PatternLab/PatternData/Rules/DocumentationRule.php b/src/PatternLab/PatternData/Rules/DocumentationRule.php index 528925fb..609bbb00 100644 --- a/src/PatternLab/PatternData/Rules/DocumentationRule.php +++ b/src/PatternLab/PatternData/Rules/DocumentationRule.php @@ -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,10 +56,19 @@ 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) { @@ -68,12 +76,22 @@ public function run($depth, $ext, $path, $pathName, $name) { 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); diff --git a/src/PatternLab/PatternData/Rules/PatternRule.php b/src/PatternLab/PatternData/Rules/PatternRule.php index 84a90bc0..cff06211 100644 --- a/src/PatternLab/PatternData/Rules/PatternRule.php +++ b/src/PatternLab/PatternData/Rules/PatternRule.php @@ -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, diff --git a/src/PatternLab/PatternData/Rules/PatternSubtypeRule.php b/src/PatternLab/PatternData/Rules/PatternSubtypeRule.php index 1f26ff64..dcd6c6a3 100644 --- a/src/PatternLab/PatternData/Rules/PatternSubtypeRule.php +++ b/src/PatternLab/PatternData/Rules/PatternSubtypeRule.php @@ -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, diff --git a/src/PatternLab/PatternData/Rules/PatternTypeRule.php b/src/PatternLab/PatternData/Rules/PatternTypeRule.php index 3ce60e1e..2665e52c 100644 --- a/src/PatternLab/PatternData/Rules/PatternTypeRule.php +++ b/src/PatternLab/PatternData/Rules/PatternTypeRule.php @@ -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, diff --git a/src/PatternLab/PatternData/Rules/PseudoPatternRule.php b/src/PatternLab/PatternData/Rules/PseudoPatternRule.php index 8575790c..85172b96 100644 --- a/src/PatternLab/PatternData/Rules/PseudoPatternRule.php +++ b/src/PatternLab/PatternData/Rules/PseudoPatternRule.php @@ -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,