Skip to content

Commit 6afc386

Browse files
committed
Merge pull request #20 from tristanlins/feature/colorify
Add colors to identify start and end elements in backend listing.
2 parents d2d328d + 54e7384 commit 6afc386

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

system/modules/semantic_html5/SemanticHTML5Content.php

+109
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ static public function initDataContainer()
2222
$GLOBALS['TL_DCA']['tl_content']['list']['sorting']['child_record_callback'] = array('SemanticHTML5Content', 'addCteType');
2323
}
2424

25+
private static $rotatingColor = .2;
26+
27+
private static $elementColors = [];
28+
2529
protected static $arrContentElements = null;
2630

2731
/**
@@ -105,6 +109,7 @@ public function addCteType($arrRow)
105109

106110
if ($arrRow['type'] == 'semantic_html5')
107111
{
112+
$strReturn = $this->colorize($strReturn, $arrRow);
108113
$strReturn = str_replace('limit_height', '', $strReturn);
109114
$strReturn = str_replace('h64', '', $strReturn);
110115
}
@@ -132,6 +137,7 @@ public function addCteType($arrRow)
132137

133138
if ($arrRow['type'] == 'semantic_html5')
134139
{
140+
$strReturn = $this->colorize($strReturn, $arrRow);
135141
$strReturn = str_replace('limit_height', '', $strReturn);
136142
$strReturn = str_replace('h64', '', $strReturn);
137143
}
@@ -148,6 +154,7 @@ public function addCteType($arrRow)
148154

149155
if ($arrRow['type'] == 'semantic_html5')
150156
{
157+
$strReturn = $this->colorize($strReturn, $arrRow);
151158
$strReturn = str_replace('limit_height', '', $strReturn);
152159
$strReturn = str_replace('h64', '', $strReturn);
153160
}
@@ -345,6 +352,108 @@ protected function insertUndo($strSourceSQL, $strSaveSQL, $strTable)
345352
->execute($this->User->id, time(), $strTable, $strPrefix . $strSourceSQL, count($arrSave[$strTable]), serialize($arrSave));
346353
}
347354

355+
private function colorize($strReturn, $arrRow)
356+
{
357+
if ('end' === $arrRow['sh5_tag']) {
358+
$pid = $arrRow['sh5_pid'];
359+
$color = static::$elementColors[$pid];
360+
} else {
361+
$id = $arrRow['id'];
362+
$color = $this->rotateColor();
363+
364+
static::$elementColors[$id] = $color;
365+
}
366+
367+
$script = <<<'SCRIPT'
368+
<script>
369+
(function(){
370+
var element = document.getElementById('cte_%s');
371+
element = element.parentNode;
372+
while (element) {
373+
var classes = element.getAttribute('class');
374+
if (classes && -1 != classes.indexOf('tl_content')) {
375+
element.setAttribute(
376+
'style',
377+
'border-left: 3px solid %s; padding-left: 3px;'
378+
);
379+
return;
380+
}
381+
element = element.parentNode;
382+
}
383+
})();
384+
</script>
385+
SCRIPT;
386+
387+
$strReturn = str_replace(
388+
'class="cte_type',
389+
sprintf('id="cte_%s" class="cte_type', $arrRow['id']),
390+
$strReturn
391+
);
392+
$strReturn .= sprintf(
393+
$script,
394+
$arrRow['id'],
395+
$color
396+
);
397+
398+
return $strReturn;
399+
}
400+
401+
private function currentColor()
402+
{
403+
return $this->HSVtoRGB(static::$rotatingColor, 1, .8);
404+
}
405+
406+
private function rotateColor()
407+
{
408+
$color = $this->currentColor();
409+
410+
static::$rotatingColor += .7;
411+
412+
if (static::$rotatingColor > 1) {
413+
static::$rotatingColor -= 1;
414+
}
415+
416+
return $color;
417+
}
418+
419+
/**
420+
* @see http://stackoverflow.com/a/3597447
421+
*/
422+
private function HSVtoRGB($hue, $saturation, $value)
423+
{
424+
//1
425+
$hue *= 6;
426+
//2
427+
$I = floor($hue);
428+
$F = $hue - $I;
429+
//3
430+
$M = $value * (1 - $saturation);
431+
$N = $value * (1 - $saturation * $F);
432+
$K = $value * (1 - $saturation * (1 - $F));
433+
//4
434+
switch ($I) {
435+
case 0:
436+
list($red, $green, $blue) = array($value, $K, $M);
437+
break;
438+
case 1:
439+
list($red, $green, $blue) = array($N, $value, $M);
440+
break;
441+
case 2:
442+
list($red, $green, $blue) = array($M, $value, $K);
443+
break;
444+
case 3:
445+
list($red, $green, $blue) = array($M, $N, $value);
446+
break;
447+
case 4:
448+
list($red, $green, $blue) = array($K, $M, $value);
449+
break;
450+
case 5:
451+
case 6: //for when $H=1 is given
452+
list($red, $green, $blue) = array($value, $M, $N);
453+
break;
454+
}
455+
return sprintf('#%02x%02x%02x', $red * 255, $green * 255, $blue * 255);
456+
}
348457
}
349458

350459
?>

0 commit comments

Comments
 (0)