Skip to content

Instantly share code, notes, and snippets.

@u7ter
Created March 15, 2017 11:14
Show Gist options
  • Select an option

  • Save u7ter/d722fdacc1fe65ae70119a1f3c76dfb5 to your computer and use it in GitHub Desktop.

Select an option

Save u7ter/d722fdacc1fe65ae70119a1f3c76dfb5 to your computer and use it in GitHub Desktop.
ajax_load_bx.php
<div id="ajaxpages_reviewsuser" class="user-msg-holder">
<?
global $APPLICATION, $JSON;
if($_REQUEST['ajaxpages']=='Y' && $_REQUEST['ajaxpagesid']=='ajaxpages_reviewsuser')
{
$IS_AJAXPAGES = 'Y';
$JSON['TYPE'] = 'OK';
}?>
<?$APPLICATION->IncludeComponent(
"bitrix:news.list",
"",
array(
// ajaxpages
'AJAXPAGESID' => 'ajaxpages_reviewsuser',
'IS_AJAXPAGES' => $IS_AJAXPAGES,
'IS_SORTERCHANGE' => $IS_SORTERCHANGE,
),
false
);?>
<?
if($IS_AJAXPAGES=='Y')
{
$APPLICATION->RestartBuffer();
header('Content-Type: application/json');
if(SITE_CHARSET!='utf-8')
{
$data = $APPLICATION->ConvertCharsetArray($JSON, SITE_CHARSET, 'utf-8');
$json_str_utf = json_encode($data);
$json_str = $APPLICATION->ConvertCharset($json_str_utf, 'utf-8', SITE_CHARSET);
echo $json_str;
} else {
echo json_encode($JSON);
}
die();
}
?>
</div>
//template.php
if($arParams['IS_AJAXPAGES']=="Y") {
$this->SetViewTarget("reviewsuser");
}
?>
<?foreach($arResult["ITEMS"] as $arItem):?>
<?
$this->AddEditAction($arItem['ID'], $arItem['EDIT_LINK'], CIBlock::GetArrayByID($arItem["IBLOCK_ID"], "ELEMENT_EDIT"));
$this->AddDeleteAction($arItem['ID'], $arItem['DELETE_LINK'], CIBlock::GetArrayByID($arItem["IBLOCK_ID"], "ELEMENT_DELETE"), array("CONFIRM" => GetMessage('CT_BNL_ELEMENT_DELETE_CONFIRM')));
?>
<?endforeach;?>
<?
if($arParams['IS_AJAXPAGES']=='Y')
{
$this->EndViewTarget();
$templateData['reviewsuser'] = $APPLICATION->GetViewContent('reviewsuser');
}
if($arParams['IS_AJAXPAGES']=='Y')
{
}
$this->SetViewTarget('ajax_btn');
if(IntVal($arResult['NAV_RESULT']->NavPageNomer)<IntVal($arResult['NAV_RESULT']->NavPageCount))
{
?><a class="load-more__btn btn btn_green ajaxpages<?if($arParams['USE_AUTO_AJAXPAGES']=='Y'):?> auto<?endif;?>" rel="nofollow" href="#" <?
?>data-ajaxurl="<?=$arResult['AJAXPAGE_URL']?>" <?
?>data-ajaxpagesid="<?=$arParams['AJAXPAGESID']?>" <?
?>data-navpagenomer="<?=($arResult['NAV_RESULT']->NavPageNomer)?>" <?
?>data-navpagecount="<?=($arResult['NAV_RESULT']->NavPageCount)?>" <?
?>data-navnum="<?=($arResult['NAV_RESULT']->NavNum)?>"<?
?>><span class="icon-round-arr">показать еще 10</span></a><?
}
$this->EndViewTarget();
if($arParams['IS_AJAXPAGES']=='Y')
{
$this->EndViewTarget();
$templateData['ajaxpagesid'] =$arParams['AJAXPAGESID'];
$templateData['ajax_btn'] = $APPLICATION->GetViewContent('ajax_btn');
}
/*component_epilog.php*/
if($arParams['IS_AJAXPAGES']=='Y') {
global $JSON;
$JSON['IDENTIFIER'] = $arParams['AJAXPAGESID'];
$JSON['HTML'] = $templateData;
}
/*modifier.php*/
// ADD AJAX URL
$arResult['AJAXPAGE_URL'] = $APPLICATION->GetCurPageParam('',array('ajaxpages', 'ajaxpagesid', 'get', 'AJAX_CALL', 'PAGEN_'.($arResult['NAV_RESULT']->NavNum)));
?>
<script>
function PutJSon(json,$linkObj,ajaxpagesid)
{
console.log(json);
if(json.TYPE=='OK')
{
if(ajaxpagesid && ajaxpagesid==json.IDENTIFIER)
{
if(json.HTML.reviewsuser && json.IDENTIFIER=='ajaxpages_reviewsuser')
{
$('.load-more.text_center').html( json.HTML.ajax_btn );
$('#'+ajaxpagesid).append( json.HTML.reviewsuser );
toggleBlockOpen({
btn: '.short-text__btn',
parent: '.short-text',
toggleBtnClass: 'short-text__btn_open',
toggleBlock: '.short-text__toggle-block',
toggleBlockClass: 'short-text__toggle-block_open'
});
}
console.log(json);
if(json.HTML.ajaxpages_list && json.IDENTIFIER=='ajaxpages_list')
{
$('.load-more.text_center').html( json.HTML.ajax_btn );
$("#ajaxpages_list section").last().find('.portal-list__wrapper.wrapper').append( json.HTML.ajaxpages_list );
}
} else {
console.warn( 'PutJSon -> no ajaxpages' );
}
if(json.HTMLBYID)
{
for(var key in json.HTMLBYID)
{
if( $('#'+key) )
{
$('#'+key).html( json.HTMLBYID[key] );
}
}
}
} else {
console.warn( 'PutJSon -> request return error' );
}
}
// AjaxPages
function AjaxPages(linkObj)
{
}
// AJAXPAGES
function AJAXPAGESAuto()
{
$('.ajaxpages.auto').each(function(i){
var porog = 200,
$ajaxpObj = $(this);
var ajaxpOffsetTop = $ajaxpObj.offset().top,
window_height = $(window).height();
console.log("window_height " + window_height);
if( porog>(ajaxpOffsetTop-window.pageYOffset-window_height) && !AJAXPAGES_processing && !$ajaxpObj.hasClass('') )
{
console.log("click a");
console.log($ajaxpObj.find('a'));
$ajaxpObj.find('a').trigger('click');
}
});
}
$(document).ready(function(){
// AJAXPAGES
$(document).on('click','a.ajaxpages',function(){
var $linkObj = $(this);
var ajaxurl = $linkObj.data('ajaxurl');
var ajaxpagesid = $linkObj.data('ajaxpagesid');
var navpagenomer = parseInt($linkObj.data('navpagenomer'));
var navpagecount = parseInt($linkObj.data('navpagecount'));
var navnum = $linkObj.data('navnum');
var nextpagenomer = parseInt(navpagenomer) + 1;
var url = "";
if( $('#'+ajaxpagesid).length>0 && navpagenomer<navpagecount && parseInt(navnum)>0 && ajaxurl!="" )
{
console.log("navnum ---", navnum);
console.log("nextpagenomer ---", nextpagenomer);
AJAXPAGES_processing = true;
AjaxPages( $linkObj );
if(ajaxurl.indexOf("?")<1)
{
url = ajaxurl + '?ajaxpages=Y&ajaxpagesid=' + ajaxpagesid + '&PAGEN_'+navnum+'='+nextpagenomer;
} else {
url = ajaxurl + '&ajaxpages=Y&ajaxpagesid=' + ajaxpagesid + '&PAGEN_'+navnum+'='+nextpagenomer;
}
$.getJSON(url, {}, function(json){
PutJSon( json,$linkObj,ajaxpagesid );
}).fail(function(json){
console.warn( 'ajaxpages - error responsed' );
}).always(function(){
setTimeout(function(){ // fix for slow shit
AJAXPAGES_processing = false;
AjaxPages( $linkObj );
},50)
});
} else {
if( !($('#'+ajaxpagesid).length>0) )
{
console.warn( 'AJAXPAGES: ajaxpages -> empty DOM element' );
}
if( !(navpagenomer<navpagecount) )
{
console.warn( 'AJAXPAGES: ajaxpages -> navpagenomer !< navpagecount' );
}
if( !(parseInt(navnum)>0) )
{
console.warn( 'AJAXPAGES: ajaxpages -> parseInt(navnum)!>0' );
}
if( !(ajaxurl!="") )
{
console.warn( 'AJAXPAGES: ajaxpages -> ajaxurl is empty' );
}
}
return false;
});
});
</script>
<?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment