Changeset 2865

Show
Ignore:
Timestamp:
11/24/08 05:24:59 (7 weeks ago)
Author:
ringmaster
Message:

Stacks now support the $after parameter. Setting the $after parameter lets you name one or more named stack entries after which a new stack entry will appear. So if you add a new stack with Stack::add('admin_header_javascript', $output, 'podcast', array('jquery', 'ui.tabs')); then the 'podcast' entry will be inserted after both the 'jquery' and the 'ui.tabs' items.

Also, Stack::scripts() and Stack::styles() have been added which allow Stack::add() to be called to add both URL-referenced and inline styles. For example, you can now call this to add an inline script after jquery is loaded: Stack::add('admin_header_javascript', '$(function(){alert("on load");});', 'my alert', 'jquery');

Referenced URLs added to these stacks must be absolute URLs, starting with 'http://'

This should eliminate the need for the 'admin_header_after' hook.

Location:
trunk/htdocs/system
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/htdocs/system/admin/header.php

    r2861 r2865  
    2020    <?php 
    2121        Plugins::act( 'admin_header', $this ); 
    22         Stack::out( 'admin_header_javascript', '<script src="%s" type="text/javascript"></script>'."\r\n" ); 
    23         Stack::out( 'admin_stylesheet', '<link rel="stylesheet" type="text/css" href="%s" media="%s">'."\r\n" ); 
     22        Stack::out( 'admin_header_javascript', array('Stack', 'scripts') ); 
     23        Stack::out( 'admin_stylesheet', array('Stack', 'styles') ); 
    2424    ?> 
    2525    <!--[if IE 7]> 
     
    6969 
    7070<?php Plugins::act('admin_info', $theme, $page); ?> 
    71  
  • trunk/htdocs/system/classes/adminhandler.php

    r2863 r2865  
    22022202        echo json_encode($output); 
    22032203    } 
    2204      
     2204 
    22052205    public function ajax_update_groups($handler_vars) 
    22062206    { 
     
    23992399        $users= Users::get_all(); 
    24002400        $members= $group->members; 
    2401         foreach($users as $user) {           
     2401        foreach($users as $user) { 
    24022402            if(in_array($user->id, $members)) { 
    24032403                $user->membership= TRUE; 
     
    27162716     */ 
    27172717    public static function setup_stacks() { 
    2718         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/jquery.js" ); 
    2719         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.core.js" ); 
    2720         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.slider.js" ); 
    2721         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.tabs.js" ); 
    2722         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.sortable.js" ); 
    2723         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.resizable.js" ); 
    2724         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/jquery.spinner.js" ); 
    2725         Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/jquery.color.js" ); 
    2726         Stack::add( 'admin_header_javascript', Site::get_url('habari') . "/3rdparty/humanmsg/humanmsg.js" ); 
    2727         Stack::add( 'admin_header_javascript', Site::get_url('habari') . "/3rdparty/hotkeys/jquery.hotkeys.js" ); 
    2728         Stack::add( 'admin_header_javascript', Site::get_url('admin_theme') . "/js/media.js" ); 
    2729         Stack::add( 'admin_header_javascript', Site::get_url('admin_theme') . "/js/admin.js" ); 
     2718        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/jquery.js", 'jquery' ); 
     2719        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.core.js", 'ui.core' ); 
     2720        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.slider.js", 'ui.slider' ); 
     2721        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.tabs.js", 'ui.tabs' ); 
     2722        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.sortable.js", 'ui.sortable' ); 
     2723        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/ui.resizable.js", 'ui.resizable' ); 
     2724        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/jquery.spinner.js", 'jquery.spinner' ); 
     2725        Stack::add( 'admin_header_javascript', Site::get_url('scripts') . "/jquery.color.js", 'jquery.color' ); 
     2726        Stack::add( 'admin_header_javascript', Site::get_url('habari') . "/3rdparty/humanmsg/humanmsg.js", 'humanmsg' ); 
     2727        Stack::add( 'admin_header_javascript', Site::get_url('habari') . "/3rdparty/hotkeys/jquery.hotkeys.js", 'jquery.hotkeys' ); 
     2728        Stack::add( 'admin_header_javascript', Site::get_url('admin_theme') . "/js/media.js", 'media' ); 
     2729        Stack::add( 'admin_header_javascript', Site::get_url('admin_theme') . "/js/admin.js", 'admin' ); 
    27302730    } 
    27312731} 
  • trunk/htdocs/system/classes/stack.php

    r2592 r2865  
    77 * This is useful for collecting a set of unique javascript references to output 
    88 * and then insert them at a specific point on the page. 
    9  *  
     9 * 
    1010 * <code> 
    11  * // Add jquery to the javascript stack:  
     11 * // Add jquery to the javascript stack: 
    1212 * Stack::add( 'template_header_javascript', Site::get_url('scripts') . '/jquery.js', 'jquery' ); 
    1313 * 
    1414 * // Add stylesheet to theme_stylesheet stack with media type 
    1515 * Stack::add( 'template_stylesheet', array( Site::get_url('theme') . '/style.css', 'screen' ), 'style' ); 
    16  *   
     16 * 
    1717 * // Output the javascript stack: 
    1818 * Stack::out( 'template_header_javascript', '<script src="%s" type="text/javascript"></script>' ); 
     
    2020 * // Output the theme_stylesheet stack: 
    2121 * Stack::out( 'template_stylesheet', '<link rel="stylesheet" type="text/css"  href="%s" media="%s">' ); 
    22  * </code>    
     22 * </code> 
    2323 * 
    2424 * @package Habari 
     
    2828{ 
    2929    private static $stacks = array(); 
    30      
     30 
    3131    /** 
    3232     * Private constructor for Stack. 
    33      * Stack objects should only be created using the static  
     33     * Stack objects should only be created using the static 
    3434     * method Static::create_stack(), or one of the methods that 
    35      * adds a value directly to a stack.  This prevents multiple Stack  
     35     * adds a value directly to a stack.  This prevents multiple Stack 
    3636     * objects from being created with the same name. 
    37      *  
     37     * 
    3838     * @param mixed $input An array or ArrayObject to create the stack from. 
    3939     * @return array The created stack 
     
    4848     * @param string $stack_name The name of the stack to return 
    4949     * @return Stack The requested stack 
    50      **/                 
     50     **/ 
    5151    public static function get_named_stack( $stack_name ) 
    5252    { 
     
    5858        } 
    5959    } 
    60      
     60 
    6161    /** 
    6262     * Check for the existence of a given stack item. 
     
    6767     */ 
    6868    public static function has ( $stack_name, $value_name ) { 
    69          
     69 
    7070        // get the stack 
    7171        $stack = self::get_named_stack( $stack_name ); 
    72          
     72 
    7373        if ( isset( $stack[ $value_name ] ) ) { 
    7474            return true; 
     
    7777            return false; 
    7878        } 
    79          
    80     } 
    81      
     79 
     80    } 
     81 
    8282    /** 
    8383     * Get a single item from a given stack. 
     
    8989     */ 
    9090    public static function get_item ( $stack_name, $value_name, $default_value = null ) { 
    91          
     91 
    9292        // get the stack 
    9393        $stack = self::get_named_stack( $stack_name ); 
    94          
     94 
    9595        if ( isset( $stack[ $value_name ] ) ) { 
    9696            return $stack[ $value_name ]; 
     
    9999            return $default_value; 
    100100        } 
    101          
    102     } 
    103      
     101 
     102    } 
     103 
    104104    /** 
    105105     * Creates and retreives a named stack instance 
    106106     * @param string $stack_name The name of the stack to create and return 
    107107     * @return array The created stack 
    108      **/                 
     108     **/ 
    109109    public static function create_stack( $stack_name ) 
    110110    { 
     
    115115        return self::$stacks[$stack_name]; 
    116116    } 
    117      
     117 
    118118    /** 
    119119     * Add a value to a stack 
     
    121121     * @param mixed $value The value to add 
    122122     * @param string $value_name The name of the value to add 
    123      * @return array The stack that was added to      
    124      **/      
    125     public static function add( $stack_name, $value, $value_name = null ) 
     123     * @return array The stack that was added to 
     124     **/ 
     125    public static function add( $stack_name, $value, $value_name = null, $after = null ) 
    126126    { 
    127127        $stack = self::get_named_stack( $stack_name ); 
    128128        $value_name = $value_name ? $value_name : md5( serialize( $value ) ); 
    129         $stack[$value_name]= $value; 
     129        if(!is_null($after)) { 
     130            if(!is_array($after)) { 
     131                $after = array($after); 
     132            } 
     133            $newstack = array(); 
     134            foreach($stack as $k => $v) { 
     135                $newstack[$k] = $v; 
     136                $inserted = false; 
     137                if(in_array($k, $after)) { 
     138                    unset($after[array_search($k, $after)]); 
     139                    if(count($after) == 0) { 
     140                        $newstack[$value_name] = $value; 
     141                        $inserted = true; 
     142                    } 
     143                } 
     144            } 
     145            if(!$inserted) { 
     146                $newstack[$value_name] = $value; 
     147            } 
     148            $stack = $newstack; 
     149        } 
     150        else { 
     151            $stack[$value_name]= $value; 
     152        } 
    130153        self::$stacks[$stack_name]= $stack; 
    131154        return $stack; 
    132155    } 
    133      
     156 
    134157    /** 
    135158     * Remove a value to a stack 
    136159     * @param string $stack_name The name of the stack 
    137160     * @param string $value_name The name of the value to remove 
    138      * @return array The rest of the stack, post-remove   
    139      **/      
     161     * @return array The rest of the stack, post-remove 
     162     **/ 
    140163    public static function remove( $stack_name, $value_name ) 
    141164    { 
     
    147170        return $stack; 
    148171    } 
    149      
     172 
    150173    /** 
    151174     * Returns all of the values of the stack 
    152175     * @param string $stack_name The name of the stack to output 
    153176     * @param mixed $format A printf-style formatting string or callback used to output each stack element 
    154      **/          
     177     **/ 
    155178    public static function get( $stack_name, $format = null) 
    156179    { 
     
    160183        foreach( $stack as $element ) { 
    161184            if ( is_callable($format) ) { 
    162                 $out.= call_user_func_array( $format, (array) $element );  
     185                $out.= call_user_func_array( $format, (array) $element ); 
    163186            } 
    164187            elseif ( is_string( $format ) ) { 
     
    171194        return $out; 
    172195    } 
    173      
     196 
    174197    /** 
    175198     * Outputs all of the values of the stack 
    176199     * @param string $stack_name The name of the stack to output 
    177200     * @param mixed $format A printf-style formatting string or callback used to output each stack element 
    178      **/          
     201     **/ 
    179202    public static function out( $stack_name, $format = null) 
    180203    { 
    181204        echo self::get( $stack_name, $format ); 
    182205    } 
     206 
     207    public static function scripts( $element ) 
     208    { 
     209        if(strpos($element, 'http://') === 0 && strpos($element, "\n") === FALSE) { 
     210            $output = sprintf( '<script src="%s" type="text/javascript"></script>'."\r\n", $element); 
     211        } 
     212        else { 
     213            $output = sprintf( '<script type="text/javascript">%s</script>'."\r\n", $element); 
     214        } 
     215        return $output; 
     216    } 
     217 
     218    public static function styles( $element, $typename ) 
     219    { 
     220        if(strpos($element, 'http://') === 0 && strpos($element, "\n") === FALSE) { 
     221            $output = sprintf( '<link rel="stylesheet" type="text/css" href="%s" media="%s">'."\r\n", $element, $typename); 
     222        } 
     223        else { 
     224            $output = sprintf( '<style type="text/stylesheet" media="%s">%s</style>'."\r\n", $typename, $element); 
     225        } 
     226        return $output; 
     227    } 
    183228} 
    184229