четверг, 11 апреля 2013 г.

создаем ajax в битрикс

первое и основное ajax должен всегда быть не навязчевым. а теперь как это реализовать
создадим пустую тему в битрекс у меня вот здесь
/bitrix/templates/AJAX_HTML
в данной теме создадим папку sub_themes (/bitrix/templates/AJAX_HTML/sub_themes)
делаем следующий header.php

 
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
IncludeTemplateLangFile(__FILE__);


/**
 * благодоря данному подходу можно создавать не только пустой шаблон но и обертывать код например наример различным кодом окон
 * Class get_sub_themes_singleton
 */
class get_sub_themes_singleton {
    protected static $instance;  // object instance

    private $list_theme;
    private $sub_theme;
    private $dir_sub_theme;
    private $params;

    private function __construct(){}
    private function __clone(){}
    private function __wakeup(){}

    public static function getInstance() {
        if (is_null(self::$instance)) {
            self::$instance = new get_sub_themes_singleton;
        }
        return self::$instance;
    }

    public function set_sub_theme($sub_theme){
        //поставить при повышенных требованиях к безопасности
        if (!empty($this->list_theme) && !empty($this->list_theme[$sub_theme])){
            $this->sub_theme = $this->list_theme[$sub_theme];
        }
        return self::$instance;
    }
    public function set_dir_sub_theme($dir_sub_theme){
        if (is_dir($dir_sub_theme)){
            $this->dir_sub_theme = $dir_sub_theme;
        }else{
            die("not found sub themes");
        }
        return self::$instance;
    }

    public function set_list_theme($list_theme){
        $this->list_theme = $list_theme;
        return self::$instance;
    }
    public function get_header(){
        //include("");
        //echo $this->sub_theme;
        if (file_exists($this->dir_sub_theme.$this->sub_theme."/header.php")){
            include_once($this->dir_sub_theme.$this->sub_theme."/header.php");
        }
    }
    public function get_footer(){
        if (file_exists($this->dir_sub_theme.$this->sub_theme."/footer.php")){
            include($this->dir_sub_theme.$this->sub_theme."/footer.php");
        }
    }


    public function set_params($key, $value){
        $this->params[$key]=$value;
        return self::$instance;
    }
    public function get_params($key){
        return $this->params[$key];
    }
    public function theme_exists($theme){
        if (!empty($this->list_theme[$theme])){
            return true;
        }else{
            return false;
        }
    }


}

///////////////////////////////////////////////////////////////////////////

get_sub_themes_singleton::getInstance()
    ->set_dir_sub_theme(dirname(__FILE__)."/sub_themes/")
    ->set_list_theme(array(
                    "full_html"     =>"full_html",
                    "work_arena"    =>"work_arena",
                    "simple_windows"=>"simple_windows",
                    ))->set_sub_theme("full_html");

//подключаем тему из url
//хотя можно поставить и в куках
if (!empty($_REQUEST['theme']) &&  get_sub_themes_singleton::getInstance()->theme_exists($_REQUEST['theme'])){
     get_sub_themes_singleton::getInstance()->set_sub_theme($_REQUEST['theme']);
}

get_sub_themes_singleton::getInstance()->get_header();

И такой footer,php
 
get_sub_themes_singleton::getInstance()->get_footer();

в папке /bitrix/templates/AJAX_HTML/sub_themes создаем подтемы с оформлением окон и не забываем указывать темы в через метод set_list_theme (сами темы состоят из header.php и footer.php)
данную тему настраиваем в настройках сайта и указываем что она должна применяться при наличии параметра ajax=Y в url
в футере основной темы создаем базовую парковочную зону (хотя не обязательно так как вы сами будете определять куда загружать контент)
 
<div class="parking_zone"
     closeajax=".parking_zone"
     closebind="click"
    >
</div>
</body>

в header основной темы вставляем следующий jquery скрипт
 
       <script type="text/javascript">
            /**
             * превращаем обычные ссылки в ajax
             */
            $(function(){
                $(".ajax_link").live("click",function(){
                    var parking_zone=$(this).attr("parking_zone");
                    var close_element_control=$(parking_zone).attr("closeajax");
                    var action_close=$(parking_zone).attr("closebind");
                    $(close_element_control).bind(action_close, function(){
                        $(parking_zone).hide();
                    });
                    $(parking_zone).load($(".ajax_link").attr("ajaxhref"), function() {
                        $(parking_zone).show();//настройка анимации окон
                    });
                    return false;
                });

            });
        </script>
и на конец создаем обработчик например на странице
 
 <span class="region-select-a">
<a id = "testid"
      class="ajax_link"
      href="/vibor_regiona/"
      ajaxhref="/vibor_regiona/?ajax=Y&theme=simple_windows&<?=(empty($_REQUEST['параметр'])?"":'region_geo_location='.$_REQUEST['параметр']);?>"
      parking_zone=".parking_zone"
      >blabla</a></span>
вот вроде все

Комментариев нет:

Отправить комментарий

PHP: The Right Way блог програмышки
Яндекс.Метрика