Skip to content

Commit

Permalink
Merge pull request #43 from scottchiefbaker/fix_countable
Browse files Browse the repository at this point in the history
Fix countable errors on PHP 7.2+
  • Loading branch information
scottchiefbaker authored Feb 6, 2019
2 parents d5a8d2c + 27eeaf7 commit 0e9cb47
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 102 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.8
0.6.0
34 changes: 20 additions & 14 deletions class.krumo.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,12 @@ private static function _css()
// the JS
print "<script type=\"text/javascript\">\n";

$js_file = KRUMO_DIR . "/js/krumo.min.js";
if (is_readable($js_file)) {
$js_min_file = KRUMO_DIR . "/js/krumo.min.js";
$js_file = KRUMO_DIR . "/js/krumo.js";

if (is_readable($js_min_file)) {
$js_text = file_get_contents($js_min_file);
} elseif (is_readable($js_file)) {
$js_text = file_get_contents($js_file);
} else {
$js_text = "// Missing JS file krumo.min.js\n";
Expand Down Expand Up @@ -1074,8 +1078,13 @@ private static function _vars(&$data)
// stain it
static::_hive($data);

$count = 0;
if (is_countable($data)) {
$count = count($data);
}

// render it
$collapsed = static::_isCollapsed(static::$_level, count($data)-1);
$collapsed = static::_isCollapsed(static::$_level, $count - 1);
if ($collapsed) {
$collapse_style = 'style="display: none;"';
} else {
Expand Down Expand Up @@ -1221,11 +1230,6 @@ private static function _array($data, $name)
print "<li class=\"krumo-child\">";
print "<div class=\"krumo-element $elementClasses\"";

// If there is more than one, make a dropdown
if (count($data) > 0) {
print "onClick=\"krumo.toggle(this);\"";
}

print "onMouseOver=\"krumo.over(this);\" onMouseOut=\"krumo.out(this);\">";
print "<a class=\"krumo-name\">$name</a> <em class=\"krumo-type\">Array(<strong class=\"krumo-array-length\">";
print count($data) . "</strong>)</em>";
Expand Down Expand Up @@ -1290,9 +1294,6 @@ private static function _object(&$data, $name)
}

print "<li class=\"krumo-child\"> <div class=\"krumo-element $elementClasses\"";
if ($data && count($data) > 0) {
print 'onClick="krumo.toggle(this);"';
}
print 'onMouseOver="krumo.over(this);" onMouseOut="krumo.out(this);">';

$empty_str = '';
Expand Down Expand Up @@ -1557,9 +1558,6 @@ private static function _string($data, $name)

print "<li class=\"krumo-child\">";
print "<div class=\"krumo-element $expand_class\" ";
if ($_extra) {
print " onClick=\"krumo.toggle(this);\" ";
}
print "onMouseOver=\"krumo.over(this);\" onMouseOut=\"krumo.out(this);\">\n";

print "<a class=\"krumo-name\">$name</a> ";
Expand Down Expand Up @@ -1675,4 +1673,12 @@ function kd()
exit();
}
}

// Polyfill for is_countable() from https://secure.php.net/manual/en/function.is-countable.php
if (!function_exists('is_countable')) {
function is_countable($var) {
return (is_array($var) || $var instanceof Countable);
}
}

// vim: tabstop=4 shiftwidth=4 expandtab autoindent
172 changes: 86 additions & 86 deletions js/krumo.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Command to generate minified JS:
// curl -X POST -s --data-urlencode 'input@krumo.js' https://javascript-minifier.com/raw

/**
* JavaScript routines for Krumo
*
* @version $Id: krumo.js 22 2007-12-02 07:38:18Z Mrasnika $
* @link http://sourceforge.net/projects/krumo
*/

/////////////////////////////////////////////////////////////////////////////

/**
* Krumo JS Class
*/
function krumo() {
}
function krumo() { }

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Expand All @@ -25,8 +26,8 @@ function krumo() {
krumo.reclass = function(el, className) {
if (el.className.indexOf(className) < 0) {
el.className += (' ' + className);
}
}
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Expand All @@ -40,8 +41,8 @@ krumo.reclass = function(el, className) {
krumo.unclass = function(el, className) {
if (el.className.indexOf(className) > -1) {
el.className = el.className.replace(className, '');
}
}
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Expand All @@ -51,25 +52,75 @@ krumo.unclass = function(el, className) {
* @param HtmlElement el
* @return void
*/
krumo.toggle = function(el) {
var ul = el.parentNode.getElementsByTagName('ul');
for (var i=0; i<ul.length; i++) {
if (ul[i].parentNode.parentNode == el.parentNode) {
ul[i].parentNode.style.display = (ul[i].parentNode.style.display == 'none')
? 'block'
: 'none';
}
krumo.toggle = function(event) {
var elem = event.target.closest(".krumo-expand");
var ctrl = event.ctrlKey;
var is_expanded = !elem.classList.contains("krumo-opened");

// Adding a control to you click does an expand/collapse all
if (ctrl) {
if (is_expanded) {
krumo.expand_all(elem);
} else {
krumo.collapse_all(elem);
}

// toggle class
//
if (ul[0].parentNode.style.display == 'block') {
krumo.reclass(el, 'krumo-opened');
} else {
if (is_expanded) {
krumo.expand(elem);
} else {
krumo.unclass(el, 'krumo-opened');
krumo.collapse(elem);
}
}

event.stopPropagation();
}

krumo.expand = function(el) {
paren = el.parentNode;
nest = paren.querySelector(".krumo-nest");
exp = paren.querySelector(".krumo-expand");

nest.style.display = 'block';
krumo.reclass(exp,"krumo-opened");
}

krumo.collapse = function(el) {
paren = el.parentNode;
nest = paren.querySelector(".krumo-nest");
exp = paren.querySelector(".krumo-expand");

nest.style.display = 'none';
krumo.unclass(exp,"krumo-opened");
}

krumo.expand_all = function(el) {
paren = el.parentNode;
nest = paren.querySelectorAll(".krumo-nest");
exp = paren.querySelectorAll(".krumo-expand");

nest.forEach(function(item, i) {
item.style.display = 'block';
});

exp.forEach(function(item, i) {
krumo.reclass(item,"krumo-opened");
});
}

krumo.collapse_all = function(el) {
paren = el.parentNode;
nest = paren.querySelectorAll(".krumo-nest");
exp = paren.querySelectorAll(".krumo-expand");

nest.forEach(function(item, i) {
item.style.display = 'none';
});

exp.forEach(function(item, i) {
krumo.unclass(item,"krumo-opened");
});
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

/**
Expand All @@ -80,7 +131,7 @@ krumo.toggle = function(el) {
*/
krumo.over = function(el) {
krumo.reclass(el, 'krumo-hover');
}
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Expand All @@ -93,78 +144,27 @@ krumo.over = function(el) {

krumo.out = function(el) {
krumo.unclass(el, 'krumo-hover');
}
}

/////////////////////////////////////////////////////////////////////////////

// Get element by ID
krumo.get_id = function(str) {
var ret = document.getElementById(str);
function qsa(search) {
var elems = document.querySelectorAll(search);

return ret;
return elems;
}

// Get element by Class
krumo.get_class = function(str) {
var elems = document.getElementsByClassName(str);
var ret = new Array();

// Just get the objects (not the extra stuff)
for (var i in elems) {
var elem = elems[i];
if (typeof(elem) === 'object') {
ret.push(elem);
}
}
function init_click() {
var elems = qsa('.krumo-expand');

return ret;
// Add a click item to everything that's expandable
elems.forEach(function(el) {
el.parentNode.addEventListener("click", krumo.toggle);
});
}

// This is a poor mans querySelectorAll().
// querySelectorAll() isn't supported 100% until
// IE9, so we have to use this work around until
// we can stop supporting IE8
krumo.find = function(str) {
if (!str) { return false; }

var first = str.substr(0,1);
var remain = str.substr(1);

if (first === ".") {
return krumo.get_class(remain);
} else if (first === "#") {
return krumo.get_id(remain);
} else {
return false;
}
}

function toggle_expand_all() {
// Find all the expandable items
var elems = krumo.find('.krumo-expand');
if (elems.length === 0) { return false; }

// Find the first expandable element and see what state it is in currently
var action = 'expand';
if (elems[0].nextSibling.style.display === 'block' || elems[0].nextSibling.style.display === '') {
action = 'collapse';
}

// Expand each item
for (var i in elems) {
var item = elems[i];

// The sibling is the hidden object
var sib = item.nextSibling;

if (action === 'expand') {
sib.style.display = 'block';
// Give the clicked item the krumo-opened class
krumo.reclass(item, 'krumo-opened');
} else {
sib.style.display = 'none';
// Remove the krumo-opened class
krumo.unclass(item, 'krumo-opened');
}
}
}
// Equivalent to $(document).ready() in JQUery
// https://stackoverflow.com/questions/2304941/what-is-the-non-jquery-equivalent-of-document-ready
document.addEventListener("DOMContentLoaded", function() {
init_click();
});
2 changes: 1 addition & 1 deletion js/krumo.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e9cb47

Please sign in to comment.