]> git.mxchange.org Git - friendica.git/commitdiff
infinite_scroll: move js from index.php to main.js
authorrabuzarus <>
Tue, 10 May 2016 23:52:05 +0000 (01:52 +0200)
committerrabuzarus <>
Tue, 10 May 2016 23:52:05 +0000 (01:52 +0200)
boot.php
index.php
js/main.js
view/templates/head.tpl

index 13cc2aaf5bdf7a612e47a0d6ee11bcbadc879a63..891d2a61a1defb074f05741bcdb08cb6e680191c 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -862,6 +862,9 @@ class App {
                if ($touch_icon == "")
                        $touch_icon = "images/friendica-128.png";
 
+               // get data wich is needed for infinite scroll on the network page
+               $invinite_scroll = infinite_scroll_data($this->module);
+
                $tpl = get_markup_template('head.tpl');
                $this->page['htmlhead'] = replace_macros($tpl,array(
                        '$baseurl' => $this->get_baseurl(), // FIXME for z_path!!!!
@@ -874,7 +877,8 @@ class App {
                        '$update_interval' => $interval,
                        '$shortcut_icon' => $shortcut_icon,
                        '$touch_icon' => $touch_icon,
-                       '$stylesheet' => $stylesheet
+                       '$stylesheet' => $stylesheet,
+                       '$infinite_scroll' => $invinite_scroll,
                )) . $this->page['htmlhead'];
        }
 
@@ -2201,3 +2205,43 @@ function argv($x) {
 
        return '';
 }
+
+/**
+ * @brief Get the data which is needed for infinite scroll
+ * 
+ * For invinite scroll we need the page number of the actual page
+ * and the the URI where the content of the next page comes from.
+ * This data is needed for the js part in main.js.
+ * Note: infinite scroll does only work for the network page (module)
+ * 
+ * @param string $module The name of the module (e.g. "network")
+ * @return array Of infinite scroll data
+ *     'pageno' => $pageno The number of the actual page
+ *     'reload_uri' => $reload_uri The URI of the content we have to load
+ */
+function infinite_scroll_data($module) {
+
+       if (get_pconfig(local_user(),'system','infinite_scroll')
+               AND ($module == "network") AND ($_GET["mode"] != "minimal")) {
+
+               // get the page number
+               if (is_string($_GET["page"]))
+                       $pageno = $_GET["page"];
+               else
+                       $pageno = 1;
+
+               $reload_uri = "";
+
+               // try to get the uri from which we load the content
+               foreach ($_GET AS $param => $value)
+                       if (($param != "page") AND ($param != "q"))
+                               $reload_uri .= "&".$param."=".urlencode($value);
+
+               if (($a->page_offset != "") AND !strstr($reload_uri, "&offset="))
+                       $reload_uri .= "&offset=".urlencode($a->page_offset);
+
+               $arr = array("pageno" => $pageno, "reload_uri" => $reload_uri);
+
+               return $arr;
+       }
+}
index c6a9a88fe1013d299d6586758f27b4bfebf421f6..73f46cfbef2367570be09c6985e9605568c0801e 100644 (file)
--- a/index.php
+++ b/index.php
@@ -491,70 +491,6 @@ if (isset($_GET["mode"]) AND ($_GET["mode"] == "raw")) {
        session_write_close();
        exit;
 
-} elseif (get_pconfig(local_user(),'system','infinite_scroll')
-          AND ($a->module == "network") AND ($_GET["mode"] != "minimal")) {
-       if (is_string($_GET["page"]))
-               $pageno = $_GET["page"];
-       else
-               $pageno = 1;
-
-       $reload_uri = "";
-
-       foreach ($_GET AS $param => $value)
-               if (($param != "page") AND ($param != "q"))
-                       $reload_uri .= "&".$param."=".urlencode($value);
-
-       if (($a->page_offset != "") AND !strstr($reload_uri, "&offset="))
-               $reload_uri .= "&offset=".urlencode($a->page_offset);
-
-
-$a->page['htmlhead'] .= <<< EOT
-<script type="text/javascript">
-
-$(document).ready(function() {
-    num = $pageno;
-});
-
-function loadcontent() {
-       if (lockLoadContent) return;
-       lockLoadContent = true;
-
-       $("#scroll-loader").fadeIn('normal');
-
-       num+=1;
-
-       console.log('Loading page ' + num);
-
-       $.get('/network?mode=raw$reload_uri&page=' + num, function(data) {
-               $("#scroll-loader").hide();
-               if ($(data).length > 0) {
-                       $(data).insertBefore('#conversation-end');
-                       lockLoadContent = false;
-               } else {
-                       $("#scroll-end").fadeIn('normal');
-               }
-       });
-}
-
-var num = $pageno;
-var lockLoadContent = false;
-
-$(window).scroll(function(e){
-
-       if ($(document).height() != $(window).height()) {
-               // First method that is expected to work - but has problems with Chrome
-               if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
-                       loadcontent();
-       } else {
-               // This method works with Chrome - but seems to be much slower in Firefox
-               if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5))
-                       loadcontent();
-       }
-});
-</script>
-
-EOT;
-
 }
 
 $page    = $a->page;
index 7e1c22ecfc61642ed1191452153421899a3647c3..9914c3801f8e2486e00b0e169e8c04de41ac2fe9 100644 (file)
@@ -51,6 +51,7 @@
        var commentBusy = false;
        var last_popup_menu = null;
        var last_popup_button = null;
+       var lockLoadContent = false;
 
        $(function() {
                $.ajaxSetup({cache: false});
                        }
                });
 
+               // Set an event listener for infinite scroll
+               if(typeof infinite_scroll !== 'undefined') {
+                       $(window).scroll(function(e){
+                               if ($(document).height() != $(window).height()) {
+                                       // First method that is expected to work - but has problems with Chrome
+                                       if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
+                                               loadScrollContent();
+                               } else {
+                                       // This method works with Chrome - but seems to be much slower in Firefox
+                                       if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5))
+                                               loadScrollContent();
+                               }
+                       });
+               }
+
 
        });
 
                $('#pause').html('');
        }
 
+       // load more network content (used for infinite scroll)
+       function loadScrollContent() {
+               if (lockLoadContent) return;
+               lockLoadContent = true;
+
+               $("#scroll-loader").fadeIn('normal');
+
+               // the page number to load is one higher than the actual
+               // page number
+               infinite_scroll.pageno+=1;
+
+               console.log('Loading page ' + infinite_scroll.pageno);
+
+               // get the raw content from the next page and insert this content
+               // right before "#conversation-end"
+               $.get('network?mode=raw' + infinite_scroll.reload_uri + '&page=' + infinite_scroll.pageno, function(data) {
+                       $("#scroll-loader").hide();
+                       if ($(data).length > 0) {
+                               $(data).insertBefore('#conversation-end');
+                               lockLoadContent = false;
+                       } else {
+                               $("#scroll-end").fadeIn('normal');
+                       }
+               });
+       }
 
     function bin2hex(s){
         // Converts the binary representation of data to hex
index 412323f32959243c4e83d5c155dc58f21901921f..b4f82aca483698f1763255c246b8b893a836be2f 100644 (file)
        var updateInterval = {{$update_interval}};
        var localUser = {{if $local_user}}{{$local_user}}{{else}}false{{/if}};
 
+       {{* Create an object with the data which is needed for infinite scroll.
+       For the relevant js part look at function loadContent() in main.js. *}}
+       {{if $infinite_scroll}}
+       var infinite_scroll = {
+                               'pageno'        : {{$infinite_scroll.pageno}},
+                               'reload_uri'    : "{{$infinite_scroll.reload_uri}}"
+                               }
+       {{/if}}
+
        function confirmDelete() { return confirm("{{$delitem}}"); }
        function commentExpand(id) {
                $("#comment-edit-text-" + id).value = '';