From 00b283329b3f1cf23e1cb45da1e78d440a5adc1f Mon Sep 17 00:00:00 2001 From: surunzi Date: Mon, 15 Aug 2016 13:27:33 +0800 Subject: [PATCH] Add: Elements parent references --- src/Elements/BottomBar.hbs | 3 --- src/Elements/Elements.es6 | 48 +++++++++++++++++++++++++++++++------- src/Elements/Elements.hbs | 12 +++++++++- src/Elements/Elements.scss | 23 +++++++++++++++++- test/index.html | 4 ++-- 5 files changed, 74 insertions(+), 16 deletions(-) diff --git a/src/Elements/BottomBar.hbs b/src/Elements/BottomBar.hbs index c55d6f4..7a2b309 100644 --- a/src/Elements/BottomBar.hbs +++ b/src/Elements/BottomBar.hbs @@ -1,7 +1,4 @@
-
- -
diff --git a/src/Elements/Elements.es6 b/src/Elements/Elements.es6 index 85f7c5a..d3f4512 100644 --- a/src/Elements/Elements.es6 +++ b/src/Elements/Elements.es6 @@ -86,7 +86,6 @@ export default class Elements extends Tool parent = this._parent, select = this._select; - this._$el.on('click', '.eruda-child', function () { var idx = util.$(this).data('idx'), @@ -139,12 +138,20 @@ export default class Elements extends Tool sources.set('json', data); parent.showTool('sources'); } + }).on('click', '.eruda-parent', function () + { + let idx = util.$(this).data('idx'), + curEl = self._curEl, + el = curEl.parentNode; + + while (idx-- && el.parentNode) el = el.parentNode; + + !isElExist(el) ? self._render() : self._setElAndRender(el); }).on('click', '.toggle-all-computed-style', () => this._toggleAllComputedStyle()); var $bottomBar = this._$el.find('.eruda-bottom-bar'); - $bottomBar.on('click', '.eruda-back', () => this._back()) - .on('click', '.eruda-refresh', () => this._render()) + $bottomBar.on('click', '.eruda-refresh', () => this._render()) .on('click', '.eruda-highlight', () => this._toggleHighlight()) .on('click', '.eruda-select', () => this._toggleSelect()) .on('click', '.eruda-reset', () => this._setElAndRender(this._htmlEl)); @@ -215,6 +222,7 @@ export default class Elements extends Tool var {className, id, attributes, tagName} = el; + ret.parents = getParents(el); ret.children = formatChildNodes(el.childNodes); ret.attributes = formatAttr(attributes); ret.name = formatElName({tagName, id, className, attributes}); @@ -288,7 +296,7 @@ function processStyleRule(val) const isElExist = val => util.isEl(val) && val.parentNode; -function formatElName(data) +function formatElName(data, {noAttr = false} = {}) { var {id, className, attributes} = data; @@ -305,12 +313,15 @@ function formatElName(data) }); } - util.each(attributes, (attr) => + if (!noAttr) { - var name = attr.name; - if (name === 'id' || name === 'class' || name === 'style') return; - ret += ` ${name}="${attr.value}"`; - }); + util.each(attributes, (attr) => + { + var name = attr.name; + if (name === 'id' || name === 'class' || name === 'style') return; + ret += ` ${name}="${attr.value}"`; + }); + } return ret; } @@ -364,6 +375,25 @@ function formatChildNodes(nodes) return ret; } +function getParents(el) +{ + var ret = [], + i = 0, + parent = el.parentNode; + + while (parent && parent.nodeType === 1) + { + ret.push({ + text: formatElName(parent, {noAttr: true}), + idx: i++ + }); + + parent = parent.parentNode; + } + + return ret.reverse(); +} + function getInlineStyle(style) { var ret = { diff --git a/src/Elements/Elements.hbs b/src/Elements/Elements.hbs index 2638540..4a7d624 100644 --- a/src/Elements/Elements.hbs +++ b/src/Elements/Elements.hbs @@ -1,4 +1,14 @@ -
+{{#if parents}} +
    + {{#each parents}} +
  • +
    {{{text}}}
    + +
  • + {{/each}} +
+{{/if}} +
{{{name}}}
{{#if children}} diff --git a/src/Elements/Elements.scss b/src/Elements/Elements.scss index cfae5a7..f9d84a6 100644 --- a/src/Elements/Elements.scss +++ b/src/Elements/Elements.scss @@ -9,6 +9,27 @@ @include overflow-auto(y); height: 100%; } + .parents { + @include overflow-auto(x); + background: #fff; + padding: $padding; + white-space: nowrap; + border-bottom: 1px solid $gray-light; + cursor: pointer; + font-size: $font-size-s; + li { + display: inline-block; + .parent { + display: inline-block; + } + &:last-child { + margin-right: 0; + } + } + .icon-chevron-right { + font-size: 8px; + } + } .breadcrumb { @include breadcrumb(); cursor: pointer; @@ -180,7 +201,7 @@ color: $gray; font-size: 14px; line-height: 40px; - width: 20%; + width: 25%; display: inline-block; transition: background $anim-duration, color $anim-duration; &:active { diff --git a/test/index.html b/test/index.html index 97def04..4bea67f 100644 --- a/test/index.html +++ b/test/index.html @@ -9,7 +9,7 @@ -
+
One
Two
@@ -78,7 +78,7 @@ setTimeout(function () { eruda.init({container: el}); init(); - eruda.get('console').log('shit'); + eruda.get('console').log('test'); }, 1000);