<?php if ( ! defined('EXT')) exit('No direct script access allowed');

/**
 * Template Editor Tab Extension
 *
 * @author        Pascal Kriete
 * @package        PK Template Tab
 * @copyright    Copyright (c) 2008, Pascal Kriete
 * @license     http://www.opensource.org/licenses/mit-license.php
 */
class PK_Template_tab
{
    var 
$settings        = array();
    var 
$name            'PK Template Tab';
    var 
$version         '1.0.0';
    var 
$description     'Hijacks the template editor textfield to allow tab input (\t).';
    var 
$settings_exist  'n';
    var 
$docs_url        'http://projects.pascalkriete.com/template_tab';
    
    
/**
     * Constructor
     *
     * @access    public
     */
    
function PK_Template_tab($settings '')
    {
        
$this->settings $settings;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Activate the Extension
     *
     * @access    public
     */
    
function activate_extension()
    {
        global 
$DB;

        
$info = array(
                    
'extension_id'    => '',
                    
'class'            => __CLASS__,
                    
'method'        => 'hijack_editor',
                    
'hook'            => 'show_full_control_panel_end',
                    
'settings'        => '',
                    
'priority'        => 10,    // ladys first
                    
'version'        => $this->version,
                    
'enabled'        => 'y'
                    
        
);
        
        
$DB->query$DB->insert_string('exp_extensions'$info) );
    }

    
// --------------------------------------------------------------------

    /**
     * Update the Extension
     *
     * @access    public
     * @param    string    installed version
     */
    
function update_extension($current '')
    {
        
// Nothing yet, but the function is required.
        
return FALSE;
    }

    
// --------------------------------------------------------------------
    
    /**
     * Disable the Extension
     *
     * @access    public
     */
    
function disable_extension()
    {
        global 
$DB;
        
        
$DB->query("DELETE FROM exp_extensions WHERE class = '".__CLASS__."'");
    }

    
// --------------------------------------------------------------------
    
    /**
     * Add the editor to the html
     *
     * @access    public
     * @param    string    cp html
     * @return    string    cp html + script
     */
    
function hijack_editor($out)
    {
        global 
$IN;
        
        
$page $IN->GBL('M''GET');
        
        
// Only include it on the relevant pages
        
if ($page == 'edit_template' OR $page == 'update_template')
        {
            
$out .= $this->_get_script();
        }
        
        return 
$out;
    }

    
// --------------------------------------------------------------------
    
    /**
     * The javascript text
     *
     * @access    private
     * @return    string    script text
     */
    
function _get_script()
    {
        
$script = <<<EOT
<script type="text/javascript">
<!--
(function()
{

    /* Configuration
    ----------------------------------------- */

    var _textarea = null;    // the textarea to hijack
    var _sel_start = 0;        // current selection beginning
    var _sel_end = 0;        // current selection end

    var _tab = "
\t";        // want spaces? go for it


    /* General functions
    ----------------------------------------- */

    // Source: Scott Andrew
    function addEvent(elm, evType, fn, useCapture) 
{
        if (elm.addEventListener) 
{
            elm.addEventListener(evType, fn, useCapture);
            return true;
        
}
        else if (elm.attachEvent) 
{
            var r = elm.attachEvent('on' + evType, fn);
            return r;
        
}
        else 
{
            elm
['on' + evType] = fn;
        
}
    
}

    /* Doing the actual work
    ----------------------------------------- */

    /*
     * Keydown event handler
     */
    function _observer(e) 
{
        if (e.keyCode && e.keyCode == 9) 
{
            _insert_tab();
            if (e.preventDefault) 
{
                 e.preventDefault(); 
            
}
        
}
    
}

    /*
     * Add the tab
     */
    function _insert_tab() 
{
        var selected, new_pos;

        /* IE has the worst model for getting selections
         * You can get the range which has these great values:
         * http://msdn.microsoft.com/en-us/library/ms535872(VS.85).aspx
         *
         * Useless pixels. I wanted block indents (select the block and move it),
         * but IE was a real 
[expletive deleted] so I dropped it.
         */

        // ---------------------------------------------------------

        /*
         * In the case of updating text at the current location IE is painless,
         * because we can replace the 'selection', which the W3C model doesn't
         * provide for.  Thank you Microsoft.
         */
        if (document.selection) 
{
            _textarea.focus();
            selected = document.selection.createRange();
            selected.text = myValue;
        
}
        else 
{
            // Grab the selection
            _sel_start = _textarea.selectionStart;
            _sel_end = _textarea.selectionEnd;

            // Plop a tag between the two cursors
            _textarea.value = _textarea.value.substr(0, _sel_start) + _tab + _textarea.value.substr(_sel_end);

            // Then move them both to the end of that tab (length for space support)
            new_pos = _sel_start + _tab.length;

            // Update the cursor position
            _textarea.selectionStart = new_pos;
            _textarea.selectionEnd = new_pos;
        
}

        // Some browsers don't allow preventDefault for tabs
        _textarea.focus();
    
}

    /*
     * Called on load
     */
    function init() 
{
        _textarea = document.getElementById('template_data');
        addEvent(_textarea, 'keydown', _observer);
    
}

    addEvent(window,'load', init, false);

})();
//-->
</script>
EOT;
        return 
$script;
    }
}
// End PK_Template_tab class

/* End of file ext.pk_template_tab.txt */
/* Location: ./system/extensions/ext.pk_template_tab.txt */