1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-07-09 11:04:01 +02:00

Better names in the dataset selection box (fixed #26)

This commit is contained in:
maximebf
2013-09-19 22:35:17 -04:00
parent 6330bca1c7
commit f74037a29d
4 changed files with 87 additions and 27 deletions

View File

@ -662,11 +662,12 @@ class JavascriptRenderer
if ($renderStackedData && $this->debugBar->hasStackedData()) { if ($renderStackedData && $this->debugBar->hasStackedData()) {
foreach ($this->debugBar->getStackedData() as $id => $data) { foreach ($this->debugBar->getStackedData() as $id => $data) {
$js .= $this->getAddDatasetCode($id, $data); $js .= $this->getAddDatasetCode($id, $data, '(stacked)');
} }
} }
$js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData()); $suffix = !$initialize ? '(ajax)' : null;
$js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData(), $suffix);
return "<script type=\"text/javascript\">\n$js\n</script>\n"; return "<script type=\"text/javascript\">\n$js\n</script>\n";
} }
@ -782,12 +783,14 @@ class JavascriptRenderer
* @param array $data * @param array $data
* @return string * @return string
*/ */
protected function getAddDatasetCode($requestId, $data) protected function getAddDatasetCode($requestId, $data, $suffix = null)
{ {
$js = sprintf("%s.addDataSet(%s, \"%s\");\n", $js = sprintf("%s.addDataSet(%s, \"%s\"%s);\n",
$this->variableName, $this->variableName,
json_encode($data), json_encode($data),
$requestId); $requestId,
$suffix ? ", " . json_encode($suffix) : ''
);
return $js; return $js;
} }
} }

View File

@ -105,6 +105,7 @@ select.phpdebugbar-datasets-switcher {
float: right; float: right;
display: none; display: none;
margin: 2px 0 0 7px; margin: 2px 0 0 7px;
max-width: 200px;
} }
/* -------------------------------------- */ /* -------------------------------------- */

View File

@ -34,6 +34,25 @@ if (typeof(PhpDebugBar) == 'undefined') {
return d; return d;
} }
/**
* Counts the number of properties in an object
*
* @param {Object} obj
* @return {Integer}
*/
function getObjectSize(obj) {
if (Object.keys) {
return Object.keys(obj).length;
}
var count = 0;
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
count++;
}
}
return count;
}
// ------------------------------------------------------------------ // ------------------------------------------------------------------
/** /**
@ -274,6 +293,48 @@ if (typeof(PhpDebugBar) == 'undefined') {
// ------------------------------------------------------------------ // ------------------------------------------------------------------
/**
* Dataset title formater
*
* Formats the title of a dataset for the select box
*/
var DatasetTitleFormater = PhpDebugBar.DatasetTitleFormater = function(debugbar) {
this.debugbar = debugbar;
};
$.extend(DatasetTitleFormater.prototype, {
/**
* Formats the title of a dataset
*
* @this {DatasetTitleFormater}
* @param {String} id
* @param {Object} data
* @param {String} suffix
* @return {String}
*/
format: function(id, data, suffix) {
if (suffix) {
suffix = ' ' + suffix;
} else {
suffix = '';
}
var nb = getObjectSize(this.debugbar.datasets) + 1;
if (typeof(data['__meta']) === 'undefined') {
return "Request #" + nb + suffix;
}
var filename = data['__meta']['uri'].substr(data['__meta']['uri'].lastIndexOf('/') + 1);
var label = "#" + nb + " " + filename + suffix + ' (' + data['__meta']['datetime'] + ')';
return label;
}
});
// ------------------------------------------------------------------
/** /**
* DebugBar * DebugBar
@ -299,6 +360,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
this.datasets = {}; this.datasets = {};
this.firstTabName = null; this.firstTabName = null;
this.activePanelName = null; this.activePanelName = null;
this.datesetTitleFormater = new DatasetTitleFormater(this);
}, },
/** /**
@ -344,7 +406,8 @@ if (typeof(PhpDebugBar) == 'undefined') {
this.$openbtn = $('<a class="phpdebugbar-open-btn" href="javascript:"><i class="icon-folder-open"></i></a>').appendTo(this.$header).hide(); this.$openbtn = $('<a class="phpdebugbar-open-btn" href="javascript:"><i class="icon-folder-open"></i></a>').appendTo(this.$header).hide();
this.$openbtn.click(function() { this.$openbtn.click(function() {
self.openHandler.show(function(id, dataset) { self.openHandler.show(function(id, dataset) {
self.addDataSet(dataset, id, id + " (opened)"); self.addDataSet(dataset, id, "(opened)");
self.showTab();
}); });
}); });
@ -352,6 +415,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
this.$datasets = $('<select class="phpdebugbar-datasets-switcher" />').appendTo(this.$header); this.$datasets = $('<select class="phpdebugbar-datasets-switcher" />').appendTo(this.$header);
this.$datasets.change(function() { this.$datasets.change(function() {
self.dataChangeHandler(self.datasets[this.value]); self.dataChangeHandler(self.datasets[this.value]);
self.showTab();
}); });
}, },
@ -659,28 +723,16 @@ if (typeof(PhpDebugBar) == 'undefined') {
* @this {DebugBar} * @this {DebugBar}
* @param {Object} data * @param {Object} data
* @param {String} id The name of this set, optional * @param {String} id The name of this set, optional
* @param {String} label * @param {String} suffix
* @return {String} Dataset's id * @return {String} Dataset's id
*/ */
addDataSet: function(data, id, label) { addDataSet: function(data, id, suffix) {
var count = 0; var label = this.datesetTitleFormater.format(id, data, suffix);
if (!Object.keys) { id = id || (getObjectSize(this.datasets) + 1);
for (var k in this.datasets) {
if (this.datasets.hasOwnProperty(k)) {
count++;
}
}
} else {
count = Object.keys(this.datasets).length;
}
id = id || ("Request #" + (count + 1));
label = label || id;
this.datasets[id] = data; this.datasets[id] = data;
count++;
this.$datasets.append($('<option value="' + id + '">' + label + '</option>')); this.$datasets.append($('<option value="' + id + '">' + label + '</option>'));
if (count > 1) { if (this.$datasets.children().length > 1) {
this.$datasets.show(); this.$datasets.show();
} }
@ -786,7 +838,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
} }
var data = this.parseHeaders(raw); var data = this.parseHeaders(raw);
this.debugbar.addDataSet(data.data, data.id); this.debugbar.addDataSet(data.data, data.id, "(ajax)");
}, },
/** /**
@ -824,10 +876,14 @@ if (typeof(PhpDebugBar) == 'undefined') {
/** /**
* Attaches an event listener to jQuery.ajaxComplete() * Attaches an event listener to jQuery.ajaxComplete()
*
* @this {AjaxHandler}
* @param {jQuery} jq Optional
*/ */
bindToJquery: function() { bindToJquery: function(jq) {
var self = this; var self = this;
$(document).ajaxComplete(function(e, xhr, settings) { var jq = jq || $;
jq(document).ajaxComplete(function(e, xhr, settings) {
self.handle(xhr); self.handle(xhr);
}); });
} }

View File

@ -172,7 +172,7 @@ div.phpdebugbar-widgets-exceptions li.list-item {
font-weight: bold; font-weight: bold;
} }
div.phpdebugbar-widgets-exceptions li.list-item span.file { div.phpdebugbar-widgets-exceptions li.list-item div.file {
display: none; display: none;
margin: 10px; margin: 10px;
padding: 5px; padding: 5px;