source: extensions/rv_tscroller/main.inc.php @ 18904

Last change on this file since 18904 was 18904, checked in by rvelices, 11 years ago

rv_tscroller hide navigation bar earlier (instead of async script loading) to avoid visible reflows on slow browsers

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1<?php /*
2Plugin Name: RV Thumb Scroller
3Version: 2.4.a
4Plugin URI: http://piwigo.org/ext/extension_view.php?eid=493
5Description: Infinite scroll - loads thumbnails on index page as you scroll down the page
6Author: rvelices
7Author URI: http://www.modusoptimus.com
8*/
9define('RVTS_VERSION', '24a');
10
11class RVTS
12{
13static function on_end_section_init()
14{
15        global $page;
16        $page['nb_image_page'] *= pwg_get_session_var('rvts_mult', 1);
17        if (count($page['items'])<$page['nb_image_page']+3)
18        {
19                if (!@$page['start'] || script_basename()=='picture')
20                        $page['nb_image_page'] = max($page['nb_image_page'], count($page['items']));
21        }
22        add_event_handler('loc_begin_index', array('RVTS','on_index_begin'), EVENT_HANDLER_PRIORITY_NEUTRAL+10);
23}
24
25static function on_index_begin()
26{
27        global $page;
28        $is_ajax = isset($_GET['rvts']);
29        if (!$is_ajax)
30        {
31                if (empty($page['items']))
32                        add_event_handler('loc_end_index', array('RVTS','on_end_index'));
33                else
34                        add_event_handler('loc_end_index_thumbnails', array('RVTS','on_index_thumbnails'), EVENT_HANDLER_PRIORITY_NEUTRAL, 1);
35        }
36        else
37        {
38                $adj = (int)@$_GET['adj'];
39                if ($adj)
40                {
41                        $mult = pwg_get_session_var('rvts_mult', 1);
42                        if ($adj>0 && $mult<5)
43                                pwg_set_session_var('rvts_mult', ++$mult);
44                        if ($adj<0 && $mult>1)
45                                pwg_set_session_var('rvts_mult', --$mult);
46                }
47                $page['nb_image_page']=(int)$_GET['rvts'];
48                add_event_handler('loc_end_index_thumbnails', array('RVTS','on_index_thumbnails_ajax'), EVENT_HANDLER_PRIORITY_NEUTRAL+5, 1);
49                $page['root_path'] = get_absolute_root_url(false);
50                global $user, $template, $conf;
51                include(PHPWG_ROOT_PATH.'include/category_default.inc.php');
52        }
53}
54
55static function on_index_thumbnails($thumbs)
56{
57        global $page, $template;
58        $total = count($page['items']);
59        if (count($thumbs) >= $total)
60        {
61                add_event_handler('loc_end_index', array('RVTS','on_end_index'));
62                return $thumbs;
63        }
64        $url_model = str_replace('123456789', '%start%', duplicate_index_url( array('start'=>123456789) ) );
65        $ajax_url_model = add_url_params($url_model, array( 'rvts'=>'%per%' ) );
66
67        $url_model = str_replace('&amp;', '&', $url_model);
68        $ajax_url_model = str_replace('&amp;', '&', $ajax_url_model);
69       
70        $my_base_name = basename(dirname(__FILE__));
71        $ajax_loader_image = get_root_url()."plugins/$my_base_name/ajax-loader.gif";
72        $template->func_combine_script( array(
73                        'id'=> 'jquery',
74                        'load'=> 'footer',
75                        'path'=> 'themes/default/js/jquery.min.js',
76                ));
77        $template->func_combine_script( array(
78                        'id'=> $my_base_name,
79                        'load'=> 'async',
80                        'path'=> 'plugins/'.$my_base_name.'/rv_tscroller.min.js',
81                        'require' => 'jquery',
82                        'version' => RVTS_VERSION,
83                ));
84  $start = (int)$page['start'];
85        $per_page = $page['nb_image_page'];
86        $moreMsg = 'See the remaining %d photos';
87        if ('en' != $GLOBALS['lang_info']['code'])
88        {
89                load_language('lang', dirname(__FILE__).'/');
90                $moreMsg = l10n($moreMsg);
91        }
92
93        // the String.fromCharCode comes from google bot which somehow manage to get these urls
94        $template->block_footer_script(null,
95                "var RVTS = {
96ajaxUrlModel: String.fromCharCode(".ord($ajax_url_model[0]).")+'".substr($ajax_url_model,1)."',
97start: $start,
98perPage: $per_page,
99next: ".($start+$per_page).",
100total: $total,
101moreUrlModel: String.fromCharCode(".ord($url_model[0]).")+'".substr($url_model,1)."',
102moreMsg: '$moreMsg',
103prevMsg: '".l10n("Previous")."',
104ajaxLoaderImage: '$ajax_loader_image'
105};
106jQuery('.navigationBar').hide();");
107        return $thumbs;
108}
109
110static function on_index_thumbnails_ajax($thumbs)
111{
112        global $template;
113        $template->assign('thumbnails', $thumbs);
114        header('Content-Type: text/html; charset='.get_pwg_charset());
115        $template->pparse('index_thumbnails');
116        exit;
117}
118
119static function on_end_index()
120{
121        global $template;
122        $req = null;
123        foreach($template->scriptLoader->get_all() as $script)
124        {
125                if($script->load_mode==2 && !$script->is_remote())
126                        $req = $script->id;
127        }
128        if($req!=null)
129        {
130                $my_base_name = basename(dirname(__FILE__));
131                $template->func_combine_script( array(
132                        'id'=> $my_base_name,
133                        'load'=> 'async',
134                        'path'=> 'plugins/'.$my_base_name.'/rv_tscroller.min.js',
135                        'require' => $req,
136                        'version' => RVTS_VERSION,
137                ), $template->smarty);
138        }
139}
140
141}
142
143add_event_handler('loc_end_section_init', array('RVTS','on_end_section_init'));
144?>
Note: See TracBrowser for help on using the repository browser.