diff options
-rwxr-xr-x | async.php | 6 | ||||
-rwxr-xr-x | include/classes/BigPipe/BigPipe.php | 22 | ||||
-rwxr-xr-x | include/classes/BigPipe/Pagelet.php | 30 | ||||
-rwxr-xr-x | include/functions.php | 10 | ||||
-rwxr-xr-x | index.php | 6 | ||||
-rwxr-xr-x | static/bigpipe.js | 20 |
6 files changed, 57 insertions, 37 deletions
@@ -37,7 +37,7 @@ if(isset($_GET['bigpipe']) AND (int) $_GET['bigpipe'] === 0) { $PageletRed = new BigPipe\DemoPagelet();
$PageletRed->addHTML('<section id="red" class="text">['.time().'] I AM A PAGELET WITH RED BACKGROUND</section>');
$PageletRed->addCSS('static/red.php');
-$PageletRed->addJS('static/delayJS.php');
+$PageletRed->addJS('static/delayJS.php?red');
$PageletRed->addJSCode("document.getElementById('red').innerHTML += ' [JS executed]';document.getElementById('red').style.borderRadius = '30px';");
#===============================================================================
@@ -46,7 +46,7 @@ $PageletRed->addJSCode("document.getElementById('red').innerHTML += ' [JS execut $PageletBlue = new BigPipe\DemoPagelet('customPageletID', BigPipe\Pagelet::PRIORITY_HIGH);
$PageletBlue->addHTML('<section id="blue" class="text">['.time().'] I AM A PAGELET WITH BLUE BACKGROUND</section>');
$PageletBlue->addCSS('static/blue.php');
-$PageletBlue->addJS('static/delayJS.php');
+$PageletBlue->addJS('static/delayJS.php?blue');
$PageletBlue->addJSCode("document.getElementById('blue').innerHTML += ' [JS executed]';document.getElementById('blue').style.borderRadius = '30px';");
#===============================================================================
@@ -55,7 +55,7 @@ $PageletBlue->addJSCode("document.getElementById('blue').innerHTML += ' [JS exec $PageletGreen = new BigPipe\DemoPagelet();
$PageletGreen->addHTML('<section id="green" class="text">['.time().'] I AM A PAGELET WITH GREEN BACKGROUND</section>');
$PageletGreen->addCSS('static/green.php');
-$PageletGreen->addJS('static/delayJS.php');
+$PageletGreen->addJS('static/delayJS.php?green');
$PageletGreen->addJSCode("document.getElementById('green').innerHTML += ' [JS executed]';document.getElementById('green').style.borderRadius = '30px';");
?>
<!DOCTYPE html>
diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php index 39a812d..8cdda0b 100755 --- a/include/classes/BigPipe/BigPipe.php +++ b/include/classes/BigPipe/BigPipe.php @@ -33,21 +33,23 @@ class BigPipe { # Prints a single pagelet response #=============================================================================== private static function singleResponse(Pagelet $Pagelet, $last = FALSE) { - $data = [ + $pageletJSON = [ 'ID' => $Pagelet->getID(), 'RESOURCES' => ['CSS' => $Pagelet->getCSSFiles(), 'JS' => $Pagelet->getJSFiles(), 'JS_CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode())], - 'PHASES' => (object) $Pagelet->getPhaseDoneJS(), + 'PHASES' => (object) $Pagelet->getPhaseDoneJS() ]; if($last) { - $data['IS_LAST'] = true; + $pageletJSON['IS_LAST'] = true; } - $pageletHTML = str_replace('--', '--', removeLineBreaksAndTabs($Pagelet->getHTML())); - $pageletJSON = json_encode($data, (self::$debug ? JSON_PRETTY_PRINT : FALSE)); + $pageletHTML = removeLineBreaksAndTabs($Pagelet->getHTML()); + $pageletHTML = str_replace('--', '--', $pageletHTML); + + $pageletJSON = json_encode($pageletJSON, (self::$debug ? JSON_PRETTY_PRINT : NULL)); echo "<code class=\"hidden\" id=\"_{$Pagelet->getID()}\"><!-- {$pageletHTML} --></code>\n"; - echo "<script>BigPipe.onPageletArrive({$pageletJSON}, (document.getElementById(\"_{$Pagelet->getID()}\")));</script>\n\n"; + echo "<script>BigPipe.onPageletArrive({$pageletJSON}, document.getElementById(\"_{$Pagelet->getID()}\"));</script>\n\n"; } #=============================================================================== @@ -71,15 +73,15 @@ class BigPipe { foreach($pagelets as $Pagelet) { if(!self::isEnabled()) { foreach($Pagelet->getCSSFiles() as $CSSFile) { - echo '<link href="'.$CSSFile.'" rel="stylesheet" />'."\n"; + echo "<link href=\"{$CSSFile}\" rel=\"stylesheet\" />\n"; } foreach($Pagelet->getJSFiles() as $JSFile) { - echo '<script src="'.$JSFile.'"></script>'."\n"; + echo "<script src=\"{$JSFile}\"></script>\n"; } - if($Pagelet->getJSCode()) { - echo '<script>'.$Pagelet->getJSCode().'</script>'."\n"; + foreach($Pagelet->getJSCode() as $JSCode) { + echo "<script>{$JSCode}</script>\n"; } } diff --git a/include/classes/BigPipe/Pagelet.php b/include/classes/BigPipe/Pagelet.php index 36b13ea..22aadce 100755 --- a/include/classes/BigPipe/Pagelet.php +++ b/include/classes/BigPipe/Pagelet.php @@ -4,10 +4,11 @@ namespace BigPipe; class Pagelet { private $ID = NULL; private $HTML = ''; - private $JSCode = ''; + private $JSCode = []; private $JSFiles = []; private $CSSFiles = []; private $phaseDoneJS = []; + private $tagname = 'div'; private static $count = 0; #=============================================================================== @@ -29,7 +30,7 @@ class Pagelet { const PHASE_EXECJS = 4; # After the static JS code has been executed public function __construct($customID = NULL, $priority = self::PRIORITY_NORMAL) { - $this->phaseDoneJS = array_pad([], 5, []); + $this->phaseDoneJS = array_pad($this->phaseDoneJS, 5, []); $this->ID = is_string($customID) ? $customID : 'P'.++self::$count; BigPipe::addPagelet($this, $priority); @@ -80,22 +81,22 @@ class Pagelet { #=============================================================================== # Attach a CSS resource #=============================================================================== - public function addCSS($file) { - return $this->CSSFiles[] = $file; + public function addCSS($href) { + return $this->CSSFiles[] = $href; } #=============================================================================== # Attach a JS resource #=============================================================================== - public function addJS($file) { - return $this->JSFiles[] = $file; + public function addJS($href) { + return $this->JSFiles[] = $href; } #=============================================================================== - # Add JS code or attach more + # Attach a main JS code part #=============================================================================== public function addJSCode($code) { - return $this->JSCode .= $code; + return $this->JSCode[] = $code; } #=============================================================================== @@ -113,9 +114,20 @@ class Pagelet { } #=============================================================================== + # Set custom placeholder tagname + #=============================================================================== + public function setTagname($tagname) { + return $this->tagname = $tagname; + } + + #=============================================================================== # Magic method: __toString() #=============================================================================== public function __toString() { - return '<div id="'.$this->getID().'">'.((!BigPipe::isEnabled()) ? $this->getHTML() : NULL).'</div>'; + $pageletHTML = "<{$this->tagname} id=\"{$this->getID()}\">"; + $pageletHTML .= !BigPipe::isEnabled() ? $this->getHTML() : NULL; + $pageletHTML .= "</{$this->tagname}>"; + + return $pageletHTML; } }
\ No newline at end of file diff --git a/include/functions.php b/include/functions.php index d06823d..fd46c63 100755 --- a/include/functions.php +++ b/include/functions.php @@ -1,12 +1,8 @@ <?php
#===============================================================================
-# FUNCTION: Removes all line breaks and tabs from a string or an array with strings
+# Remove line breaks and tabs from a string
#===============================================================================
-function removeLineBreaksAndTabs($mixed, $replace = NULL) {
- if(is_array($mixed)) {
- return array_map(__FUNCTION__, $mixed);
- }
-
- return is_string($mixed) ? str_replace(["\r\n", "\r", "\n", "\t"], $replace, $mixed) : $mixed;
+function removeLineBreaksAndTabs($string) {
+ return str_replace(["\r\n", "\r", "\n", "\t"], NULL, $string);
}
?>
\ No newline at end of file @@ -26,7 +26,7 @@ if(isset($_GET['bigpipe']) AND (int) $_GET['bigpipe'] === 0) { $PageletRed = new BigPipe\DemoPagelet();
$PageletRed->addHTML('<section id="red" class="text">I AM A PAGELET WITH RED BACKGROUND</section>');
$PageletRed->addCSS('static/red.php');
-$PageletRed->addJS('static/delayJS.php');
+$PageletRed->addJS('static/delayJS.php?red');
$PageletRed->addJSCode("document.getElementById('red').innerHTML += ' [JS executed]';document.getElementById('red').style.borderRadius = '30px';");
#===============================================================================
@@ -35,7 +35,7 @@ $PageletRed->addJSCode("document.getElementById('red').innerHTML += ' [JS execut $PageletBlue = new BigPipe\DemoPagelet('customPageletID', BigPipe\Pagelet::PRIORITY_HIGH);
$PageletBlue->addHTML('<section id="blue" class="text">I AM A PAGELET WITH BLUE BACKGROUND</section>');
$PageletBlue->addCSS('static/blue.php');
-$PageletBlue->addJS('static/delayJS.php');
+$PageletBlue->addJS('static/delayJS.php?blue');
$PageletBlue->addJSCode("document.getElementById('blue').innerHTML += ' [JS executed]';document.getElementById('blue').style.borderRadius = '30px';");
#===============================================================================
@@ -44,7 +44,7 @@ $PageletBlue->addJSCode("document.getElementById('blue').innerHTML += ' [JS exec $PageletGreen = new BigPipe\DemoPagelet();
$PageletGreen->addHTML('<section id="green" class="text">I AM A PAGELET WITH GREEN BACKGROUND</section>');
$PageletGreen->addCSS('static/green.php');
-$PageletGreen->addJS('static/delayJS.php');
+$PageletGreen->addJS('static/delayJS.php?green');
$PageletGreen->addJSCode("document.getElementById('green').innerHTML += ' [JS executed]';document.getElementById('green').style.borderRadius = '30px';");
?>
<!DOCTYPE html>
diff --git a/static/bigpipe.js b/static/bigpipe.js index fed6e39..30d1f55 100755 --- a/static/bigpipe.js +++ b/static/bigpipe.js @@ -28,6 +28,8 @@ var BigPipe = (function() { element.async = true;
}
+ element.setAttribute('class', 'bigpipe');
+
document.head.appendChild(element);
element.onload = function() {
@@ -143,11 +145,13 @@ var BigPipe = (function() { // Pagelet: Executes the main JS code of the pagelet
//==============================================================================
Pagelet.prototype.executeJSCode = function() {
- try {
- globalExecution(this.JSCode);
- } catch(e) {
- console.error(this.pageletID + ": " + e);
- }
+ this.JSCode.forEach(function(code) {
+ try {
+ globalExecution(code);
+ } catch(e) {
+ console.error(this.pageletID + ": " + e);
+ }
+ });
};
//==============================================================================
@@ -268,6 +272,12 @@ var BigPipe = (function() { BigPipe.pagelets = [];
BigPipe.offset = 0;
BigPipe.phase = 0;
+
+ var resources = document.head.getElementsByClassName('bigpipe');
+
+ while(resources[0]) {
+ resources[0].parentNode.removeChild(resources[0]);
+ }
}
};
})();
\ No newline at end of file |