From cdcab5593311b4bdec2b54c070d9f6133b95aaa7 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Fri, 13 May 2016 21:50:30 +0200 Subject: Multiple updates --- async.php | 6 +++--- include/classes/BigPipe/BigPipe.php | 22 ++++++++++++---------- include/classes/BigPipe/Pagelet.php | 30 +++++++++++++++++++++--------- include/functions.php | 10 +++------- index.php | 6 +++--- static/bigpipe.js | 20 +++++++++++++++----- 6 files changed, 57 insertions(+), 37 deletions(-) diff --git a/async.php b/async.php index 9093c87..3431705 100755 --- a/async.php +++ b/async.php @@ -37,7 +37,7 @@ if(isset($_GET['bigpipe']) AND (int) $_GET['bigpipe'] === 0) { $PageletRed = new BigPipe\DemoPagelet(); $PageletRed->addHTML('
['.time().'] I AM A PAGELET WITH RED BACKGROUND
'); $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('
['.time().'] I AM A PAGELET WITH BLUE BACKGROUND
'); $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('
['.time().'] I AM A PAGELET WITH GREEN BACKGROUND
'); $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';"); ?> 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 "getID()}\">\n"; - echo "\n\n"; + echo "\n\n"; } #=============================================================================== @@ -71,15 +73,15 @@ class BigPipe { foreach($pagelets as $Pagelet) { if(!self::isEnabled()) { foreach($Pagelet->getCSSFiles() as $CSSFile) { - echo ''."\n"; + echo "\n"; } foreach($Pagelet->getJSFiles() as $JSFile) { - echo ''."\n"; + echo "\n"; } - if($Pagelet->getJSCode()) { - echo ''."\n"; + foreach($Pagelet->getJSCode() as $JSCode) { + echo "\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; } #=============================================================================== @@ -112,10 +113,21 @@ class Pagelet { return $this->phaseDoneJS; } + #=============================================================================== + # Set custom placeholder tagname + #=============================================================================== + public function setTagname($tagname) { + return $this->tagname = $tagname; + } + #=============================================================================== # Magic method: __toString() #=============================================================================== public function __toString() { - return '
'.((!BigPipe::isEnabled()) ? $this->getHTML() : NULL).'
'; + $pageletHTML = "<{$this->tagname} id=\"{$this->getID()}\">"; + $pageletHTML .= !BigPipe::isEnabled() ? $this->getHTML() : NULL; + $pageletHTML .= "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 @@ \ No newline at end of file diff --git a/index.php b/index.php index 1eae51e..8dadd1c 100755 --- a/index.php +++ b/index.php @@ -26,7 +26,7 @@ if(isset($_GET['bigpipe']) AND (int) $_GET['bigpipe'] === 0) { $PageletRed = new BigPipe\DemoPagelet(); $PageletRed->addHTML('
I AM A PAGELET WITH RED BACKGROUND
'); $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('
I AM A PAGELET WITH BLUE BACKGROUND
'); $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('
I AM A PAGELET WITH GREEN BACKGROUND
'); $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';"); ?> 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 -- cgit v1.2.3