ZF-12069: ZendX_JQuery_View_Helper_JQuery_Container - addStylesheet() - ordering

Description

Sometimes one wants to keep control over the order in which css files are included. This is e.g. the case when you are using Zend_Layout with layout.phtml (which includes some standard files that are needed on all pages) and want to add a css file needed in a certain view. If additionally you overwrite a certain css class in your standard css file, that is used in the class you only add in your special view (e.g. a 3rd party file you don't want to change) the 3rd party css file must be included before the standard css file. I dind't find a way to keep control about the ordering of included css files (there is a getter but no setter). So this patch is what I came up with and which works fine for me. It lets you include a css file right in front of another css file if needed.

Index: ZendX/JQuery/View/Helper/JQuery/Container.php
===================================================================
--- ZendX/JQuery/View/Helper/JQuery/Container.php   (Revision 1856)
+++ ZendX/JQuery/View/Helper/JQuery/Container.php   (Arbeitskopie)
@@ -300,7 +300,7 @@
 
     /**
      * Set jQuery UI version used.
-     * 
+     *
      * @param  string $version
      * @return ZendX_JQuery_View_Helper_JQuery_Container
      */
@@ -551,14 +551,31 @@
      * Add a stylesheet
      *
      * @param  string $path
+     * @param  string $insertBeforePath
      * @return ZendX_JQuery_View_Helper_JQuery_Container
      */
-    public function addStylesheet($path)
+    public function addStylesheet($path, $insertBeforePath = null)
     {
         $path = (string) $path;
-        if (!in_array($path, $this->_stylesheets)) {
-            $this->_stylesheets[] = (string) $path;
+
+        if (in_array($path, $this->_stylesheets)) {
+            return $this;
         }
+
+        if ($insertBeforePath !== null && in_array($insertBeforePath, $this->_stylesheets)) {
+            $styles = $this->_stylesheets;
+            $this->_stylesheets = array();
+            foreach ($styles as $style) {
+                if ($style == $insertBeforePath) {
+                    $this->_stylesheets[] = $path;
+                }
+                $this->_stylesheets[] = $style;
+            }
+            return $this;
+        }
+
+        $this->_stylesheets[] = (string) $path;
+
         return $this;
     }

Comments

No comments to display