Browse files

ENHANCEMENT Render page tree icons as stylesheets rather than $_TREE_…

…ICONS JS definitions. Removed file/folder swapping of SiteTree::$icon, use relative file path including extension instead.
  • Loading branch information...
1 parent 023ac99 commit ac799340776c0f76d7a68775fb67b3f6d8bf0fca phalkunz committed with chillu Apr 12, 2011
Showing with 63 additions and 48 deletions.
  1. +50 −31 code/controllers/CMSMain.php
  2. +0 −2 code/model/ErrorPage.php
  3. +1 −1 code/model/RedirectorPage.php
  4. +11 −13 code/model/SiteTree.php
  5. +1 −1 code/model/VirtualPage.php
View
81 code/controllers/CMSMain.php
@@ -137,13 +137,23 @@ public function PreviewLink() {
* Return the entire site tree as a nested set of ULs
*/
public function SiteTreeAsUL() {
- $this->generateTreeStylingJS();
+ $html = '';
+
+ // Include custom CSS for tree icons inline, as the tree might be loaded
+ // via Ajax, in which case we can't inject it into the HTML header easily through the HTTP response.
+ $css = $this->generateTreeStylingCSS();
+ if($this->isAjax()) {
+ $html .= "<style type=\"text/css\">\n" . $css . "</style>\n";
+ } else {
+ Requirements::customCSS($css);
+ }
// Pre-cache sitetree version numbers for querying efficiency
Versioned::prepopulate_versionnumber_cache("SiteTree", "Stage");
Versioned::prepopulate_versionnumber_cache("SiteTree", "Live");
+ $html .= $this->getSiteTreeFor($this->stat('tree_class'));
- return $this->getSiteTreeFor($this->stat('tree_class'));
+ return $html;
}
function SearchForm() {
@@ -257,43 +267,52 @@ public function SiteTreeHints() {
return Convert::raw2xml(Convert::raw2json($def));
}
-
- public function generateTreeStylingJS() {
- $classes = ClassInfo::subclassesFor($this->stat('tree_class'));
+
+ /**
+ * Include CSS for page icons. We're not using the JSTree 'types' option
+ * because it causes too much performance overhead just to add some icons.
+ *
+ * @return String CSS
+ */
+ function generateTreeStylingCSS() {
+ $css = '';
+
+ $classes = ClassInfo::subclassesFor('SiteTree');
foreach($classes as $class) {
- $obj = singleton($class);
- if($obj instanceof HiddenClass) continue;
- if($icon = $obj->stat('icon')) $iconInfo[$class] = $icon;
- }
- $iconInfo['BrokenLink'] = 'cms/images/treeicons/brokenlink';
-
-
- $js = "var _TREE_ICONS = [];\n";
+ $obj = singleton($class);
+ $iconSpec = $obj->stat('icon');
+ if(!$iconSpec) continue;
- foreach($iconInfo as $class => $icon) {
- // SiteTree::$icon can be set to array($icon, $option)
- // $option can be "file" or "folder" to force the icon to always be the file or the folder form
- $option = null;
- if(is_array($icon)) list($icon, $option) = $icon;
+ // Legacy support: We no longer need separate icon definitions for folders etc.
+ $iconFile = (is_array($iconSpec)) ? $iconSpec[0] : $iconSpec;
- $fileImage = ($option == "folder") ? $icon . '-openfolder.gif' : $icon . '-file.gif';
- $openFolderImage = $icon . '-openfolder.gif';
- if(!Director::fileExists($openFolderImage) || $option == "file") $openFolderImage = $fileImage;
- $closedFolderImage = $icon . '-closedfolder.gif';
- if(!Director::fileExists($closedFolderImage) || $option == "file") $closedFolderImage = $fileImage;
+ // Legacy support: Add file extension if none exists
+ if(!pathinfo($iconFile, PATHINFO_EXTENSION)) $iconFile .= '-file.gif';
- $js .= <<<JS
- _TREE_ICONS['$class'] = {
- fileIcon: '$fileImage',
- openFolderIcon: '$openFolderImage',
- closedFolderIcon: '$closedFolderImage'
- };
+ $iconPathInfo = pathinfo($iconFile);
+
+ // Base filename
+ $baseFilename = $iconPathInfo['dirname'] . '/' . $iconPathInfo['filename'];
+ $fileExtension = $iconPathInfo['extension'];
+
+ if(Director::fileExists($iconFile)) {
+ $css .= sprintf(
+ "li.class-%s > a .jstree-pageicon { background: transparent url('%s') 0 0 no-repeat; }\n",
+ $class, $iconFile
+ );
+ } else {
+ // Support for more sophisticated rules, e.g. sprited icons
+ $css .= sprintf(
+ "li.class-%s > a .jstree-pageicon { %s }\n",
+ $class, $iconFile
+ );
+ }
+
-JS;
}
- Requirements::customScript($js);
+ return $css;
}
/**
View
2 code/model/ErrorPage.php
@@ -22,8 +22,6 @@ class ErrorPage extends Page {
"ShowInSearch" => 0
);
- static $icon = array("sapphire/javascript/tree/images/page", "file");
-
static $description = 'Custom content for different error cases (e.g. "Page not found")';
protected static $static_filepath = ASSETS_PATH;
View
2 code/model/RedirectorPage.php
@@ -7,7 +7,7 @@
*/
class RedirectorPage extends Page {
- static $icon = array("cms/images/treeicons/page-shortcut","file");
+ static $icon = "cms/images/treeicons/page-shortcut-file.gif";
static $description = 'Redirects to a different internal page';
View
24 code/model/SiteTree.php
@@ -128,29 +128,27 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
*/
static $can_create = true;
+ /**
+ * @see CMSMain::generateTreeStylingCSS()
+ */
+ static $page_states = array('readonly');
/**
- * Icon to use in the CMS
- *
- * This should be the base filename. The suffixes -file.gif,
- * -openfolder.gif and -closedfolder.gif will be appended to the base name
- * that you provide there.
- * If you prefer, you can pass an array:
- * array("sapphire\javascript\tree\images\page", $option).
- * $option can be either "file" or "folder" to force the icon to always
- * be a file or folder, regardless of whether the page has children or not
- *
- * @var string|array
+ * Icon to use in the CMS page tree. This should be the full filename, relative to the webroot.
+ * Also supports custom CSS rule contents (applied to the correct selector for the tree UI implementation).
+ *
+ * @see CMSMain::generateTreeStylingCSS()
+ *
+ * @var string
*/
- static $icon = array("sapphire/javascript/tree/images/page", "file");
+ static $icon = null;
/**
* @var String Description of the class functionality, typically shown to a user
* when selecting which page type to create. Translated through {@link provideI18nEntities()}.
*/
static $description = 'Generic content page';
-
static $extensions = array(
"Hierarchy",
"Versioned('Stage', 'Live')",
View
2 code/model/VirtualPage.php
@@ -7,7 +7,7 @@
*/
class VirtualPage extends Page {
- static $icon = array("cms/images/treeicons/page-shortcut-gold","file");
+ static $icon = "cms/images/treeicons/page-shortcut-gold-file.gif";
static $description = 'Displays the content of another page';

0 comments on commit ac79934

Please sign in to comment.